home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / samples / point.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  5KB  |  222 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_RED TK_RED
  32. #define CI_ANTI_ALIAS_GREEN 16
  33. #define CI_ANTI_ALIAS_YELLOW 32
  34. #define CI_ANTI_ALIAS_RED 48
  35.  
  36.  
  37. GLenum rgb, doubleBuffer, directRender, windType;
  38. GLint windW, windH;
  39.  
  40. GLenum mode;
  41. GLint size;
  42. float point[3] = {
  43.     1.0, 1.0, 0.0
  44. };
  45.  
  46.  
  47. static void Init(void)
  48. {
  49.     GLint i;
  50.  
  51.     glClearColor(0.0, 0.0, 0.0, 0.0);
  52.  
  53.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  54.  
  55.     if (!rgb) {
  56.     for (i = 0; i < 16; i++) {
  57.         tkSetOneColor(i+CI_ANTI_ALIAS_RED, i/15.0, 0.0, 0.0);
  58.         tkSetOneColor(i+CI_ANTI_ALIAS_YELLOW, i/15.0, i/15.0, 0.0);
  59.         tkSetOneColor(i+CI_ANTI_ALIAS_GREEN, 0.0, i/15.0, 0.0);
  60.     }
  61.     }
  62.  
  63.     mode = GL_FALSE;
  64.     size = 1;
  65. }
  66.  
  67. static void Reshape(int width, int height)
  68. {
  69.  
  70.     windW = (GLint)width;
  71.     windH = (GLint)height;
  72.  
  73.     glViewport(0, 0, width, height);
  74.  
  75.     glMatrixMode(GL_PROJECTION);
  76.     glLoadIdentity();
  77.     gluOrtho2D(-windW/2, windW/2, -windH/2, windH/2);
  78.     glMatrixMode(GL_MODELVIEW);
  79. }
  80.  
  81. static GLenum Key(int key, GLenum mask)
  82. {
  83.  
  84.     switch (key) {
  85.       case TK_ESCAPE:
  86.     tkQuit();
  87.       case TK_1:
  88.     mode = !mode;
  89.     break;
  90.       case TK_W:
  91.     size++;
  92.     break;
  93.       case TK_w:
  94.     size--;
  95.     if (size < 1) {
  96.         size = 1;
  97.     }
  98.     break;
  99.       case TK_LEFT:
  100.     point[0] -= 0.25;
  101.     break;
  102.       case TK_RIGHT:
  103.     point[0] += 0.25;
  104.     break;
  105.       case TK_UP:
  106.     point[1] += 0.25;
  107.     break;
  108.       case TK_DOWN:
  109.     point[1] -= 0.25;
  110.     break;
  111.       default:
  112.     return GL_FALSE;
  113.     }
  114.     return GL_TRUE;
  115. }
  116.  
  117. static void Draw(void)
  118. {
  119.  
  120.     glClear(GL_COLOR_BUFFER_BIT);
  121.  
  122.     TK_SETCOLOR(windType, TK_YELLOW);
  123.     glBegin(GL_LINE_STRIP);
  124.     glVertex2f(-windW/2, 0);
  125.     glVertex2f(windW/2, 0);
  126.     glEnd();
  127.     glBegin(GL_LINE_STRIP);
  128.     glVertex2f(0, -windH/2);
  129.     glVertex2f(0, windH/2);
  130.     glEnd();
  131.  
  132.     if (mode) {
  133.     glEnable(GL_BLEND);
  134.     glEnable(GL_POINT_SMOOTH);
  135.     } else {
  136.     glDisable(GL_BLEND);
  137.     glDisable(GL_POINT_SMOOTH);
  138.     }
  139.  
  140.     glPointSize(size);
  141.     if (mode) {
  142.     (rgb) ? glColor3f(1.0, 0.0, 0.0) : glIndexf(CI_ANTI_ALIAS_RED);
  143.     } else {
  144.     (rgb) ? glColor3f(1.0, 0.0, 0.0) : glIndexf(CI_RED);
  145.     }
  146.     glBegin(GL_POINTS);
  147.     glVertex3fv(point);
  148.     glEnd();
  149.  
  150.     glDisable(GL_POINT_SMOOTH);
  151.  
  152.     glPointSize(1);
  153.     TK_SETCOLOR(windType, TK_GREEN);
  154.     glBegin(GL_POINTS);
  155.     glVertex3fv(point);
  156.     glEnd();
  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.     windW = 300;
  202.     windH = 300;
  203.     tkInitPosition(0, 0, windW, windH);
  204.  
  205.     windType = (rgb) ? TK_RGB : TK_INDEX;
  206.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  207.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  208.     tkInitDisplayMode(windType);
  209.  
  210.     if (tkInitWindow("Point Test") == GL_FALSE) {
  211.     tkQuit();
  212.     }
  213.  
  214.     Init();
  215.  
  216.     tkExposeFunc(Reshape);
  217.     tkReshapeFunc(Reshape);
  218.     tkKeyDownFunc(Key);
  219.     tkDisplayFunc(Draw);
  220.     tkExec();
  221. }
  222.