home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / samples / eval.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  11KB  |  467 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 <math.h>
  29. #include "gltk.h"
  30.  
  31.  
  32. #define VORDER 10
  33. #define CORDER 10
  34. #define TORDER 3
  35.  
  36. #define VMAJOR_ORDER 2
  37. #define VMINOR_ORDER 3
  38.  
  39. #define CMAJOR_ORDER 2
  40. #define CMINOR_ORDER 2
  41.  
  42. #define TMAJOR_ORDER 2
  43. #define TMINOR_ORDER 2
  44.  
  45. #define VDIM 4
  46. #define CDIM 4
  47. #define TDIM 2
  48.  
  49. #define ONE_D 1
  50. #define TWO_D 2
  51.  
  52. #define EVAL 3
  53. #define MESH 4
  54.  
  55.  
  56. GLenum doubleBuffer, directRender;
  57.  
  58. float rotX = 0.0, rotY = 0.0, translateZ = -1.0;
  59.  
  60. GLenum arrayType = ONE_D;
  61. GLenum colorType = GL_FALSE;
  62. GLenum textureType = GL_FALSE;
  63. GLenum polygonFilled = GL_FALSE;
  64. GLenum lighting = GL_FALSE;
  65. GLenum mapPoint = GL_FALSE;
  66. GLenum mapType = EVAL;
  67.  
  68. double point1[10*4] = {
  69.     -0.5, 0.0, 0.0, 1.0,
  70.     -0.4, 0.5, 0.0, 1.0,
  71.     -0.3,-0.5, 0.0, 1.0,
  72.     -0.2, 0.5, 0.0, 1.0,
  73.     -0.1,-0.5, 0.0, 1.0,
  74.     0.0, 0.5, 0.0, 1.0,
  75.     0.1,-0.5, 0.0, 1.0,
  76.     0.2, 0.5, 0.0, 1.0,
  77.     0.3,-0.5, 0.0, 1.0,
  78.     0.4, 0.0, 0.0, 1.0,
  79. };
  80. double cpoint1[10*4] = {
  81.     0.0, 0.0, 1.0, 1.0,
  82.     0.3, 0.0, 0.7, 1.0,
  83.     0.6, 0.0, 0.3, 1.0,
  84.     1.0, 0.0, 0.0, 1.0,
  85.     1.0, 0.3, 0.0, 1.0,
  86.     1.0, 0.6, 0.0, 1.0,
  87.     1.0, 1.0, 0.0, 1.0,
  88.     1.0, 1.0, 0.5, 1.0,
  89.     1.0, 1.0, 1.0, 1.0,
  90. };
  91. double tpoint1[11*4] = {
  92.     0.0, 0.0, 0.0, 1.0,
  93.     0.0, 0.1, 0.0, 1.0,
  94.     0.0, 0.2, 0.0, 1.0,
  95.     0.0, 0.3, 0.0, 1.0,
  96.     0.0, 0.4, 0.0, 1.0,
  97.     0.0, 0.5, 0.0, 1.0,
  98.     0.0, 0.6, 0.0, 1.0,
  99.     0.0, 0.7, 0.0, 1.0,
  100.     0.0, 0.8, 0.0, 1.0,
  101.     0.0, 0.9, 0.0, 1.0,
  102. };
  103. double point2[2*3*4] = {
  104.     -0.5, -0.5,  0.5, 1.0, 
  105.      0.0,  1.0,  0.5, 1.0, 
  106.      0.5, -0.5,  0.5, 1.0,
  107.     -0.5,  0.5, -0.5, 1.0, 
  108.      0.0, -1.0, -0.5, 1.0, 
  109.      0.5,  0.5, -0.5, 1.0,
  110. };
  111. double cpoint2[2*2*4] = {
  112.     0.0, 0.0, 0.0, 1.0,
  113.     0.0, 0.0, 1.0, 1.0,
  114.     0.0, 1.0, 0.0, 1.0,
  115.     1.0, 1.0, 1.0, 1.0,
  116. };
  117. double tpoint2[2*2*2] = {
  118.     0.0, 0.0, 0.0, 1.0,
  119.     1.0, 0.0, 1.0, 1.0, 
  120. };
  121. float textureImage[4*2*4] =  {
  122.     1.0, 1.0, 1.0, 1.0,
  123.     1.0, 0.0, 0.0, 1.0,
  124.     1.0, 0.0, 0.0, 1.0,
  125.     1.0, 1.0, 1.0, 1.0, 
  126.     1.0, 1.0, 1.0, 1.0,
  127.     1.0, 0.0, 0.0, 1.0,
  128.     1.0, 0.0, 0.0, 1.0,
  129.     1.0, 1.0, 1.0, 1.0, 
  130. };
  131.  
  132.  
  133. static void Init(void)
  134. {
  135.     static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  136.     static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
  137.     static float position[] = {0.0, 0.0, -150.0, 0.0};
  138.     static float front_mat_shininess[] = {30.0};
  139.     static float front_mat_specular[] = {0.7, 0.7, 0.7, 1.0};
  140.     static float front_mat_diffuse[] = {1.0, 0.2, 1.0, 1.0};
  141.     static float back_mat_shininess[] = {50.0};
  142.     static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
  143.     static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  144.     static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  145.     static float lmodel_twoside[] = {GL_TRUE};
  146.     static float decal[] = {GL_DECAL};
  147.     static float modulate[] = {GL_MODULATE};
  148.     static float repeat[] = {GL_REPEAT};
  149.     static float clamp[] = {GL_CLAMP};
  150.     static float nr[] = {GL_NEAREST};
  151.     static float ln[] = {GL_LINEAR};
  152.  
  153.     glFrontFace(GL_CCW);
  154.  
  155.     glEnable(GL_DEPTH_TEST);
  156.  
  157.     glMap1d(GL_MAP1_VERTEX_4, 0.0, 1.0, VDIM, VORDER, point1);
  158.     glMap1d(GL_MAP1_COLOR_4, 0.0, 1.0, CDIM, CORDER, cpoint1);
  159.  
  160.     glMap2d(GL_MAP2_VERTEX_4, 0.0, 1.0, VMINOR_ORDER*VDIM, VMAJOR_ORDER, 0.0,
  161.         1.0, VDIM, VMINOR_ORDER, point2);
  162.     glMap2d(GL_MAP2_COLOR_4, 0.0, 1.0, CMINOR_ORDER*CDIM, CMAJOR_ORDER, 0.0,
  163.         1.0, CDIM, CMINOR_ORDER, cpoint2);
  164.     glMap2d(GL_MAP2_TEXTURE_COORD_2, 0.0, 1.0, TMINOR_ORDER*TDIM,
  165.         TMAJOR_ORDER, 0.0, 1.0, TDIM, TMINOR_ORDER, tpoint2);
  166.  
  167.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  168.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  169.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  170.     
  171.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  172.     glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  173.  
  174.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  175.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  176.  
  177.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  178.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat);
  179.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat);
  180.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nr);
  181.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nr);
  182.     glTexImage2D(GL_TEXTURE_2D, 0, 4, 2, 4, 0, GL_RGBA, GL_FLOAT,
  183.          (GLvoid *)textureImage);
  184. }
  185.  
  186. static void DrawPoints1(void)
  187. {
  188.     GLint i;
  189.  
  190.     glColor3f(0.0, 1.0, 0.0);
  191.     glPointSize(2);
  192.     glBegin(GL_POINTS);
  193.     for (i = 0; i < VORDER; i++) {
  194.         glVertex4dv(&point1[i*4]);
  195.     }
  196.     glEnd();
  197. }
  198.  
  199. static void DrawPoints2(void)
  200. {
  201.     GLint i, j;
  202.  
  203.     glColor3f(1.0, 0.0, 1.0);
  204.     glPointSize(2);
  205.     glBegin(GL_POINTS);
  206.     for (i = 0; i < VMAJOR_ORDER; i++) {
  207.         for (j = 0; j < VMINOR_ORDER; j++) {
  208.         glVertex4dv(&point2[i*4*VMINOR_ORDER+j*4]);
  209.         }
  210.     }
  211.     glEnd();
  212. }
  213.  
  214. static void DrawMapEval1(float du)
  215. {
  216.     float u;
  217.  
  218.     glColor3f(1.0, 0.0, 0.0);
  219.     glBegin(GL_LINE_STRIP);
  220.     for (u = 0.0; u < 1.0; u += du) {
  221.         glEvalCoord1d(u);
  222.     }
  223.     glEvalCoord1d(1.0);
  224.     glEnd();
  225. }
  226.  
  227. static void DrawMapEval2(float du, float dv)
  228. {
  229.     float u, v, tmp;
  230.  
  231.     glColor3f(1.0, 0.0, 0.0);
  232.     for (v = 0.0; v < 1.0; v += dv) {
  233.     glBegin(GL_QUAD_STRIP);
  234.         for (u = 0.0; u <= 1.0; u += du) {
  235.         glEvalCoord2d(u,v);
  236.         tmp = (v + dv < 1.0) ? (v + dv) : 1.0;
  237.         glEvalCoord2d(u, tmp);
  238.         }
  239.         glEvalCoord2d(1.0, v);
  240.         glEvalCoord2d(1.0, v+dv);
  241.     glEnd();
  242.     }
  243. }
  244.  
  245. static void RenderEval(void)
  246. {
  247.  
  248.     if (colorType) {
  249.     glEnable(GL_MAP1_COLOR_4);
  250.     glEnable(GL_MAP2_COLOR_4);
  251.     } else {
  252.     glDisable(GL_MAP1_COLOR_4);
  253.     glDisable(GL_MAP2_COLOR_4);
  254.     }
  255.  
  256.     if (textureType) {
  257.     glEnable(GL_TEXTURE_2D);
  258.     glEnable(GL_MAP2_TEXTURE_COORD_2);
  259.     } else {
  260.     glDisable(GL_TEXTURE_2D);
  261.     glDisable(GL_MAP2_TEXTURE_COORD_2);
  262.     }
  263.  
  264.     if (polygonFilled) {
  265.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  266.     } else {
  267.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  268.     }
  269.  
  270.     glShadeModel(GL_SMOOTH);
  271.  
  272.     switch (mapType) {
  273.       case EVAL:
  274.     switch (arrayType) {
  275.       case ONE_D:
  276.         glDisable(GL_MAP2_VERTEX_4);
  277.         glEnable(GL_MAP1_VERTEX_4);
  278.         DrawPoints1();
  279.         DrawMapEval1(0.1/VORDER);
  280.         break;
  281.       case TWO_D:
  282.         glDisable(GL_MAP1_VERTEX_4);
  283.         glEnable(GL_MAP2_VERTEX_4);
  284.         DrawPoints2();
  285.         DrawMapEval2(0.1/VMAJOR_ORDER,0.1/VMINOR_ORDER);
  286.         break;
  287.     }
  288.     break;
  289.       case MESH:
  290.     switch (arrayType) {
  291.       case ONE_D:
  292.         DrawPoints1();
  293.         glDisable(GL_MAP2_VERTEX_4);
  294.         glEnable (GL_MAP1_VERTEX_4);
  295.         glColor3f(0.0, 0.0, 1.0);
  296.         glMapGrid1d(40, 0.0, 1.0);
  297.         if (mapPoint) {
  298.         glPointSize(2);
  299.         glEvalMesh1(GL_POINT, 0, 40);
  300.         } else {
  301.         glEvalMesh1(GL_LINE, 0, 40);
  302.         }
  303.         break;
  304.       case TWO_D:
  305.         DrawPoints2();
  306.         glDisable(GL_MAP1_VERTEX_4);
  307.         glEnable(GL_MAP2_VERTEX_4);
  308.         glColor3f(0.0, 0.0, 1.0);
  309.         glMapGrid2d(20, 0.0, 1.0, 20, 0.0, 1.0);
  310.         if (mapPoint) {
  311.         glPointSize(2);
  312.         glEvalMesh2(GL_POINT, 0, 20, 0, 20);
  313.         } else if (polygonFilled) {
  314.         glEvalMesh2(GL_FILL, 0, 20, 0, 20);
  315.         } else {
  316.         glEvalMesh2(GL_LINE, 0, 20, 0, 20);
  317.         }
  318.         break;
  319.     }
  320.     break;
  321.     }
  322. }
  323.  
  324. static void Reshape(int width, int height)
  325. {
  326.  
  327.     glViewport(0, 0, (GLint)width, (GLint)height);
  328.  
  329.     glMatrixMode(GL_PROJECTION);
  330.     glLoadIdentity();
  331.     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 10.0);
  332.     glMatrixMode(GL_MODELVIEW);
  333. }
  334.  
  335. static GLenum Key(int key, GLenum mask)
  336. {
  337.  
  338.     switch (key) {
  339.       case TK_ESCAPE:
  340.     tkQuit();
  341.       case TK_LEFT:
  342.     rotY -= 30;
  343.     break;
  344.       case TK_RIGHT:
  345.     rotY += 30;
  346.     break;
  347.       case TK_UP:
  348.     rotX -= 30;
  349.     break;
  350.       case TK_DOWN:
  351.     rotX += 30;
  352.     break;
  353.       case TK_1:
  354.     arrayType = ONE_D;
  355.     break;
  356.       case TK_2:
  357.     arrayType = TWO_D;
  358.     break;
  359.       case TK_e:
  360.     mapType = EVAL;
  361.     break;
  362.       case TK_m:
  363.     mapType = MESH;
  364.     break;
  365.       case TK_f:
  366.     polygonFilled = !polygonFilled;
  367.     break;
  368.       case TK_p:
  369.     mapPoint = !mapPoint;
  370.     break;
  371.       case TK_c:
  372.     colorType = !colorType;
  373.     break;
  374.       case TK_t:
  375.     textureType = !textureType;
  376.     break;
  377.       case TK_l:
  378.     lighting =! lighting;
  379.     if (lighting) {
  380.         glEnable(GL_LIGHTING);
  381.         glEnable(GL_LIGHT0);
  382.         glEnable(GL_AUTO_NORMAL);
  383.     } else {
  384.         glDisable(GL_LIGHTING);
  385.         glDisable(GL_LIGHT0);
  386.         glDisable(GL_AUTO_NORMAL);
  387.     }
  388.     break;
  389.       default:
  390.     return GL_FALSE;
  391.     }
  392.     return GL_TRUE;
  393. }
  394.  
  395. static void Draw(void)
  396. {
  397.  
  398.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  399.  
  400.     glPushMatrix();
  401.  
  402.     glTranslatef(0.0, 0.0 , translateZ);
  403.     glRotatef(rotX, 1, 0, 0);
  404.     glRotatef(rotY, 0, 1, 0);
  405.     RenderEval();
  406.  
  407.     glPopMatrix();
  408.  
  409.     glFlush();
  410.  
  411.     if (doubleBuffer) {
  412.     tkSwapBuffers();
  413.     }
  414. }
  415.  
  416. static GLenum Args(int argc, char **argv)
  417. {
  418.     GLint i;
  419.  
  420.     doubleBuffer = GL_FALSE;
  421.     directRender = GL_TRUE;
  422.  
  423.     for (i = 1; i < argc; i++) {
  424.     if (strcmp(argv[i], "-sb") == 0) {
  425.         doubleBuffer = GL_FALSE;
  426.     } else if (strcmp(argv[i], "-db") == 0) {
  427.         doubleBuffer = GL_TRUE;
  428.     } else if (strcmp(argv[i], "-dr") == 0) {
  429.         directRender = GL_TRUE;
  430.     } else if (strcmp(argv[i], "-ir") == 0) {
  431.         directRender = GL_FALSE;
  432.     } else {
  433.         printf("%s (Bad option).\n", argv[i]);
  434.         return GL_FALSE;
  435.     }
  436.     }
  437.     return GL_TRUE;
  438. }
  439.  
  440. void main(int argc, char **argv)
  441. {
  442.     GLenum type;
  443.  
  444.     if (Args(argc, argv) == GL_FALSE) {
  445.     tkQuit();
  446.     }
  447.  
  448.     tkInitPosition(0, 0, 300, 300);
  449.  
  450.     type = TK_RGB | TK_DEPTH;
  451.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  452.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  453.     tkInitDisplayMode(type);
  454.  
  455.     if (tkInitWindow("Evaluator Test") == GL_FALSE) {
  456.     tkQuit();
  457.     }
  458.  
  459.     Init();
  460.  
  461.     tkExposeFunc(Reshape);
  462.     tkReshapeFunc(Reshape);
  463.     tkKeyDownFunc(Key);
  464.     tkDisplayFunc(Draw);
  465.     tkExec();
  466. }
  467.