home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / LINE.C < prev    next >
Text File  |  1995-03-04  |  4KB  |  221 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "tk.h"
  5.  
  6. float tkRGBMap[8][3] = {
  7.     {
  8.     0, 0, 0
  9.     },
  10.     {
  11.     1, 0, 0
  12.     },
  13.     {
  14.     0, 1, 0
  15.     },
  16.     {
  17.     1, 1, 0
  18.     },
  19.     {
  20.     0, 0, 1
  21.     },
  22.     {
  23.     1, 0, 1
  24.     },
  25.     {
  26.     0, 1, 1
  27.     },
  28.     {
  29.     1, 1, 1
  30.     }
  31. };
  32.  
  33.  
  34. #define CI_OFFSET 16
  35.  
  36.  
  37. GLenum rgb, doubleBuffer, directRender, windType;
  38.  
  39. GLenum mode1, mode2;
  40. GLint size;
  41. float pntA[3] = {
  42.     -160.0, 0.0, 0.0
  43. };
  44. float pntB[3] = {
  45.     -130.0, 0.0, 0.0
  46. };
  47. float pntC[3] = {
  48.     -40.0, -50.0, 0.0
  49. };
  50. float pntD[3] = {
  51.     30.0, 60.0, 0.0
  52. };
  53.  
  54.  
  55. static void Init(void)
  56. {
  57.     GLint i;
  58.  
  59.     glClearColor(0.0, 0.0, 0.0, 0.0);
  60.  
  61.     glLineStipple(1, 0xF0E0);
  62.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  63.  
  64.     if (!rgb) {
  65.     for (i = 0; i < 16; i++) {
  66.         tkSetOneColor(i+CI_OFFSET, i/15.0, i/15.0, 0.0);
  67.     }
  68.     }
  69.  
  70.     mode1 = GL_FALSE;
  71.     mode2 = GL_FALSE;
  72.     size = 1;
  73. }
  74.  
  75. static void Reshape(int width, int height)
  76. {
  77.  
  78.     glViewport(0, 0, (GLint)width, (GLint)height);
  79.  
  80.     glMatrixMode(GL_PROJECTION);
  81.     glLoadIdentity();
  82.     gluOrtho2D(-175., 175., -175., 175.);
  83.     glMatrixMode(GL_MODELVIEW);
  84. }
  85.  
  86. static GLenum Key(int key, GLenum mask)
  87. {
  88.  
  89.     switch (key) {
  90.       case TK_ESCAPE:
  91.     tkQuit();
  92.       case TK_1:
  93.     mode1 = !mode1;
  94.     break;
  95.       case TK_2:
  96.     mode2 = !mode2;
  97.     break;
  98.       case TK_W:
  99.     size++;
  100.     break;
  101.       case TK_w:
  102.     size--;
  103.     if (size < 1) {
  104.         size = 1;
  105.     }
  106.     break;
  107.       default:
  108.     return GL_FALSE;
  109.     }
  110.     return GL_TRUE;
  111. }
  112.  
  113. static void Draw(void)
  114. {
  115.     GLint ci, i;
  116.  
  117.     glClear(GL_COLOR_BUFFER_BIT);
  118.  
  119.     glLineWidth(size);
  120.  
  121.     if (mode1) {
  122.     glEnable(GL_LINE_STIPPLE);
  123.     } else {
  124.     glDisable(GL_LINE_STIPPLE);
  125.     }
  126.     
  127.     if (mode2) {
  128.     ci = CI_OFFSET;
  129.     glEnable(GL_LINE_SMOOTH);
  130.     glEnable(GL_BLEND);
  131.     } else {
  132.     ci = TK_YELLOW;
  133.     glDisable(GL_LINE_SMOOTH);
  134.     glDisable(GL_BLEND);
  135.     }
  136.  
  137.     glPushMatrix();
  138.  
  139.     for (i = 0; i < 360; i += 5) {
  140.     glRotatef(5.0, 0,0,1);
  141.  
  142.     (rgb) ? glColor3f(1.0, 1.0, 0.0) : glIndexi(ci);
  143.     glBegin(GL_LINE_STRIP);
  144.         glVertex3fv(pntA);
  145.         glVertex3fv(pntB);
  146.     glEnd();
  147.  
  148.     glPointSize(1);
  149.  
  150.     TK_SETCOLOR(windType, TK_GREEN);
  151.     glBegin(GL_POINTS);
  152.         glVertex3fv(pntA);
  153.         glVertex3fv(pntB);
  154.     glEnd();
  155.     }
  156.  
  157.     glPopMatrix();
  158.  
  159.     glFlush();
  160.  
  161.     if (doubleBuffer) {
  162.     tkSwapBuffers();
  163.     }
  164. }
  165.  
  166. static GLenum Args(int argc, char **argv)
  167. {
  168.     GLint i;
  169.  
  170.     rgb = GL_TRUE;
  171.     doubleBuffer = GL_FALSE;
  172.     directRender = GL_TRUE;
  173.  
  174.     for (i = 1; i < argc; i++) {
  175.     if (strcmp(argv[i], "-ci") == 0) {
  176.         rgb = GL_FALSE;
  177.     } else if (strcmp(argv[i], "-rgb") == 0) {
  178.         rgb = GL_TRUE;
  179.     } else if (strcmp(argv[i], "-sb") == 0) {
  180.         doubleBuffer = GL_FALSE;
  181.     } else if (strcmp(argv[i], "-db") == 0) {
  182.         doubleBuffer = GL_TRUE;
  183.     } else if (strcmp(argv[i], "-dr") == 0) {
  184.         directRender = GL_TRUE;
  185.     } else if (strcmp(argv[i], "-ir") == 0) {
  186.         directRender = GL_FALSE;
  187.     } else {
  188.         printf("%s (Bad option).\n", argv[i]);
  189.         return GL_FALSE;
  190.     }
  191.     }
  192.     return GL_TRUE;
  193. }
  194.  
  195. void main(int argc, char **argv)
  196. {
  197.  
  198.     if (Args(argc, argv) == GL_FALSE) {
  199.     tkQuit();
  200.     }
  201.  
  202.     tkInitPosition(0, 0, 300, 300);
  203.  
  204.     windType = (rgb) ? TK_RGB : TK_INDEX;
  205.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  206.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  207.     tkInitDisplayMode(windType);
  208.  
  209.     if (tkInitWindow("Line Test") == GL_FALSE) {
  210.     tkQuit();
  211.     }
  212.  
  213.     Init();
  214.  
  215.     tkExposeFunc(Reshape);
  216.     tkReshapeFunc(Reshape);
  217.     tkKeyDownFunc(Key);
  218.     tkDisplayFunc(Draw);
  219.     tkExec();
  220. }
  221.