home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / samples / line.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  4KB  |  220 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "gltk.h"
  29.  
  30.  
  31. #define CI_OFFSET 16
  32.  
  33.  
  34. GLenum rgb, doubleBuffer, directRender, windType;
  35.  
  36. GLenum mode1, mode2;
  37. GLint size;
  38. float pntA[3] = {
  39.     -160.0, 0.0, 0.0
  40. };
  41. float pntB[3] = {
  42.     -130.0, 0.0, 0.0
  43. };
  44. float pntC[3] = {
  45.     -40.0, -50.0, 0.0
  46. };
  47. float pntD[3] = {
  48.     30.0, 60.0, 0.0
  49. };
  50.  
  51.  
  52. static void Init(void)
  53. {
  54.     GLint i;
  55.  
  56.     glClearColor(0.0, 0.0, 0.0, 0.0);
  57.  
  58.     glLineStipple(1, 0xF0E0);
  59.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  60.  
  61.     if (!rgb) {
  62.     for (i = 0; i < 16; i++) {
  63.         tkSetOneColor(i+CI_OFFSET, i/15.0, i/15.0, 0.0);
  64.     }
  65.     }
  66.  
  67.     mode1 = GL_FALSE;
  68.     mode2 = GL_FALSE;
  69.     size = 1;
  70. }
  71.  
  72. static void Reshape(int width, int height)
  73. {
  74.  
  75.     glViewport(0, 0, (GLint)width, (GLint)height);
  76.  
  77.     glMatrixMode(GL_PROJECTION);
  78.     glLoadIdentity();
  79.     gluOrtho2D(-175, 175, -175, 175);
  80.     glMatrixMode(GL_MODELVIEW);
  81. }
  82.  
  83. static GLenum Key(int key, GLenum mask)
  84. {
  85.  
  86.     switch (key) {
  87.       case TK_ESCAPE:
  88.     tkQuit();
  89.       case TK_1:
  90.     mode1 = !mode1;
  91.     break;
  92.       case TK_2:
  93.     mode2 = !mode2;
  94.     break;
  95.       case TK_W:
  96.     size++;
  97.     break;
  98.       case TK_w:
  99.     size--;
  100.     if (size < 1) {
  101.         size = 1;
  102.     }
  103.     break;
  104.       default:
  105.     return GL_FALSE;
  106.     }
  107.     return GL_TRUE;
  108. }
  109.  
  110. static void Draw(void)
  111. {
  112.     GLint ci, i;
  113.  
  114.     glClear(GL_COLOR_BUFFER_BIT);
  115.  
  116.     glLineWidth(size);
  117.  
  118.     if (mode1) {
  119.     glEnable(GL_LINE_STIPPLE);
  120.     } else {
  121.     glDisable(GL_LINE_STIPPLE);
  122.     }
  123.     
  124.     if (mode2) {
  125.     ci = CI_OFFSET;
  126.     glEnable(GL_LINE_SMOOTH);
  127.     glEnable(GL_BLEND);
  128.     } else {
  129.     ci = TK_YELLOW;
  130.     glDisable(GL_LINE_SMOOTH);
  131.     glDisable(GL_BLEND);
  132.     }
  133.  
  134.     glPushMatrix();
  135.  
  136.     glShadeModel( GL_FLAT );
  137.  
  138.     for (i = 0; i < 360; i += 5) {
  139.     glRotatef(5.0, 0,0,1);
  140.  
  141.     (rgb) ? glColor3f(1.0, 1.0, 0.0) : glIndexi(ci);
  142.     glBegin(GL_LINE_STRIP);
  143.         glVertex3fv(pntA);
  144.         glVertex3fv(pntB);
  145.     glEnd();
  146.  
  147.     glPointSize(1);
  148.  
  149.     TK_SETCOLOR(windType, TK_GREEN);
  150.     glBegin(GL_POINTS);
  151.         glVertex3fv(pntA);
  152.         glVertex3fv(pntB);
  153.     glEnd();
  154.     }
  155.  
  156.     glPopMatrix();
  157.  
  158.     glFlush();
  159.  
  160.     if (doubleBuffer) {
  161.     tkSwapBuffers();
  162.     }
  163. }
  164.  
  165. static GLenum Args(int argc, char **argv)
  166. {
  167.     GLint i;
  168.  
  169.     rgb = GL_TRUE;
  170.     doubleBuffer = GL_FALSE;
  171.     directRender = GL_TRUE;
  172.  
  173.     for (i = 1; i < argc; i++) {
  174.     if (strcmp(argv[i], "-ci") == 0) {
  175.         rgb = GL_FALSE;
  176.     } else if (strcmp(argv[i], "-rgb") == 0) {
  177.         rgb = GL_TRUE;
  178.     } else if (strcmp(argv[i], "-sb") == 0) {
  179.         doubleBuffer = GL_FALSE;
  180.     } else if (strcmp(argv[i], "-db") == 0) {
  181.         doubleBuffer = GL_TRUE;
  182.     } else if (strcmp(argv[i], "-dr") == 0) {
  183.         directRender = GL_TRUE;
  184.     } else if (strcmp(argv[i], "-ir") == 0) {
  185.         directRender = GL_FALSE;
  186.     } else {
  187.         printf("%s (Bad option).\n", argv[i]);
  188.         return GL_FALSE;
  189.     }
  190.     }
  191.     return GL_TRUE;
  192. }
  193.  
  194. void main(int argc, char **argv)
  195. {
  196.  
  197.     if (Args(argc, argv) == GL_FALSE) {
  198.     tkQuit();
  199.     }
  200.  
  201.     tkInitPosition(0, 0, 300, 300);
  202.  
  203.     windType = (rgb) ? TK_RGB : TK_INDEX;
  204.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  205.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  206.     tkInitDisplayMode(windType);
  207.  
  208.     if (tkInitWindow("Line Test") == GL_FALSE) {
  209.     tkQuit();
  210.     }
  211.  
  212.     Init();
  213.  
  214.     tkExposeFunc(Reshape);
  215.     tkReshapeFunc(Reshape);
  216.     tkKeyDownFunc(Key);
  217.     tkDisplayFunc(Draw);
  218.     tkExec();
  219. }
  220.