home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / WAVE.C < prev   
C/C++ Source or Header  |  1995-03-04  |  15KB  |  580 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include "tk.h"
  6.  
  7.  
  8. #define PI 3.14159265358979323846
  9.  
  10. #define GETCOORD(frame, x, y) (&(theMesh.coords[frame*theMesh.numCoords+(x)+(y)*(theMesh.widthX+1)]))
  11. #define GETFACET(frame, x, y) (&(theMesh.facets[frame*theMesh.numFacets+(x)+(y)*theMesh.widthX]))
  12.  
  13.  
  14. GLenum rgb, doubleBuffer, directRender;
  15.  
  16. GLint colorIndexes1[3];
  17. GLint colorIndexes2[3];
  18. GLenum clearMask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
  19.  
  20. GLenum smooth = GL_FALSE;
  21. GLenum lighting = GL_TRUE;
  22. GLenum depth = GL_TRUE;
  23. GLenum stepMode = GL_FALSE;
  24. GLenum spinMode = GL_FALSE;
  25. GLint contouring = 0;
  26.  
  27. GLint widthX, widthY;
  28. GLint checkerSize;
  29. float height;
  30.  
  31. GLint frames, curFrame = 0, nextFrame = 0;
  32.  
  33. struct facet {
  34.     float color[3];
  35.     float normal[3];
  36. };
  37. struct coord {
  38.     float vertex[3];
  39.     float normal[3];
  40. };
  41. struct mesh {
  42.     GLint widthX, widthY;
  43.     GLint numFacets;
  44.     GLint numCoords;
  45.     GLint frames;
  46.     struct coord *coords;
  47.     struct facet *facets;
  48. } theMesh;
  49.  
  50. GLubyte contourTexture1[] = {
  51.     255, 255, 255, 255,
  52.     255, 255, 255, 255,
  53.     255, 255, 255, 255,
  54.     127, 127, 127, 127,
  55. };
  56. GLubyte contourTexture2[] = {
  57.     255, 255, 255, 255,
  58.     255, 127, 127, 127,
  59.     255, 127, 127, 127,
  60.     255, 127, 127, 127,
  61. };
  62.  
  63.  
  64. static void Animate(void)
  65. {
  66.     struct coord *coord;
  67.     struct facet *facet;
  68.     float *lastColor;
  69.     float *thisColor;
  70.     GLint i, j;
  71.  
  72.     glClear(clearMask);
  73.  
  74.     if (nextFrame || !stepMode) {
  75.     curFrame++;
  76.     }
  77.     if (curFrame >= theMesh.frames) {
  78.     curFrame = 0;
  79.     }
  80.  
  81.     if ((nextFrame || !stepMode) && spinMode) {
  82.     glRotatef(5.0, 0.0, 0.0, 1.0);
  83.     }
  84.     nextFrame = 0;
  85.  
  86.     for (i = 0; i < theMesh.widthX; i++) {
  87.     glBegin(GL_QUAD_STRIP);
  88.     lastColor = NULL;
  89.     for (j = 0; j < theMesh.widthY; j++) {
  90.         facet = GETFACET(curFrame, i, j);
  91.         if (!smooth && lighting) {
  92.         glNormal3fv(facet->normal);
  93.         }
  94.         if (lighting) {
  95.         if (rgb) {
  96.             thisColor = facet->color;
  97.             glColor3fv(facet->color);
  98.         } else {
  99.             thisColor = facet->color;
  100.             glMaterialfv(GL_FRONT_AND_BACK, GL_COLOR_INDEXES, 
  101.                  facet->color);
  102.         }
  103.         } else {
  104.         if (rgb) {
  105.             thisColor = facet->color;
  106.             glColor3fv(facet->color);
  107.         } else {
  108.             thisColor = facet->color;
  109.             glIndexf(facet->color[1]);
  110.         }
  111.         }
  112.  
  113.         if (!lastColor || (thisColor[0] != lastColor[0] && smooth)) {
  114.         if (lastColor) {
  115.             glEnd();
  116.             glBegin(GL_QUAD_STRIP);
  117.         }
  118.         coord = GETCOORD(curFrame, i, j);
  119.         if (smooth && lighting) {
  120.             glNormal3fv(coord->normal);
  121.         }
  122.         glVertex3fv(coord->vertex);
  123.  
  124.         coord = GETCOORD(curFrame, i+1, j);
  125.         if (smooth && lighting) {
  126.             glNormal3fv(coord->normal);
  127.         }
  128.         glVertex3fv(coord->vertex);
  129.         }
  130.  
  131.         coord = GETCOORD(curFrame, i, j+1);
  132.         if (smooth && lighting) {
  133.         glNormal3fv(coord->normal);
  134.         }
  135.         glVertex3fv(coord->vertex);
  136.  
  137.         coord = GETCOORD(curFrame, i+1, j+1);
  138.         if (smooth && lighting) {
  139.         glNormal3fv(coord->normal);
  140.         }
  141.         glVertex3fv(coord->vertex);
  142.  
  143.         lastColor = thisColor;
  144.     }
  145.     glEnd();
  146.     }
  147.  
  148.     glFlush();
  149.     if (doubleBuffer) {
  150.     tkSwapBuffers();
  151.     }
  152. }
  153.  
  154. static void SetColorMap(void) 
  155. {
  156.     static float green[3] = {0.2, 1.0, 0.2};
  157.     static float red[3] = {1.0, 0.2, 0.2};
  158.     float *color, percent;
  159.     GLint *indexes, entries, i, j;
  160.     long buf[4];
  161.  
  162.     entries = tkGetColorMapSize();
  163.     entries = 256;
  164.  
  165.     colorIndexes1[0] = 1;
  166.     colorIndexes1[1] = 1 + (GLint)((entries - 1) * 0.3);
  167.     colorIndexes1[2] = (GLint)((entries - 1) * 0.5);
  168.     colorIndexes2[0] = 1 + (GLint)((entries - 1) * 0.5);
  169.     colorIndexes2[1] = 1 + (GLint)((entries - 1) * 0.8);
  170.     colorIndexes2[2] = entries - 1;
  171.  
  172.     for (i = 0; i < 2; i++) {
  173.     switch (i) {
  174.       case 0:
  175.         color = green;
  176.         indexes = colorIndexes1;
  177.         break;
  178.       case 1:
  179.         color = red;
  180.         indexes = colorIndexes2;
  181.         break;
  182.     }
  183.  
  184.     for (j = indexes[0]; j < indexes[1]; j++) {
  185.         percent = 0.2 + 0.8 * (j - indexes[0]) /
  186.               (float)(indexes[1] - indexes[0]);
  187.         tkSetOneColor(j, percent*color[0], percent*color[1],
  188.                percent*color[2]);
  189.     }
  190.     for (j=indexes[1]; j<=indexes[2]; j++) {
  191.         percent = (j - indexes[1]) / (float)(indexes[2] - indexes[1]);
  192.         tkSetOneColor(j, percent*(1-color[0])+color[0],
  193.                percent*(1-color[1])+color[1],
  194.                percent*(1-color[2])+color[2]);
  195.     }
  196.     }
  197. }
  198.  
  199. static void InitMesh(void)
  200. {
  201.     struct coord *coord;
  202.     struct facet *facet;
  203.     float dp1[3], dp2[3];
  204.     float *pt1, *pt2, *pt3;
  205.     float angle, d, x, y;
  206.     GLint numFacets, numCoords, frameNum, i, j;
  207.  
  208.     theMesh.widthX = widthX;
  209.     theMesh.widthY = widthY;
  210.     theMesh.frames = frames;
  211.  
  212.     numFacets = widthX * widthY;
  213.     numCoords = (widthX + 1) * (widthY + 1);
  214.  
  215.     theMesh.numCoords = numCoords;
  216.     theMesh.numFacets = numFacets;
  217.  
  218.     theMesh.coords = (struct coord *)malloc(frames*numCoords*
  219.                         sizeof(struct coord));
  220.     theMesh.facets = (struct facet *)malloc(frames*numFacets*
  221.                         sizeof(struct facet));
  222.     if (theMesh.coords == NULL || theMesh.facets == NULL) {
  223.     printf("Out of memory.\n");
  224.     tkQuit();
  225.     }
  226.  
  227.     for (frameNum = 0; frameNum < frames; frameNum++) {
  228.     for (i = 0; i <= widthX; i++) {
  229.         x = i / (float)widthX;
  230.         for (j = 0; j <= widthY; j++) {
  231.         y = j / (float)widthY;
  232.  
  233.         d = sqrt(x*x+y*y);
  234.         if (d == 0.0) {
  235.             d = 0.0001;
  236.         }
  237.         angle = 2 * PI * d + (2 * PI / frames * frameNum);
  238.  
  239.         coord = GETCOORD(frameNum, i, j);
  240.  
  241.         coord->vertex[0] = x - 0.5;
  242.         coord->vertex[1] = y - 0.5;
  243.         coord->vertex[2] = (height - height * d) * cos(angle);
  244.  
  245.         coord->normal[0] = -(height / d) * x * ((1 - d) * 2 * PI *
  246.                    sin(angle) + cos(angle));
  247.         coord->normal[1] = -(height / d) * y * ((1 - d) * 2 * PI *
  248.                    sin(angle) + cos(angle));
  249.         coord->normal[2] = -1;
  250.  
  251.         d = 1.0 / sqrt(coord->normal[0]*coord->normal[0]+
  252.                    coord->normal[1]*coord->normal[1]+1);
  253.         coord->normal[0] *= d;
  254.         coord->normal[1] *= d;
  255.         coord->normal[2] *= d;
  256.         }
  257.     }
  258.     for (i = 0; i < widthX; i++) {
  259.         for (j = 0; j < widthY; j++) {
  260.         facet = GETFACET(frameNum, i, j);
  261.         if (((i/checkerSize)%2)^(j/checkerSize)%2) {
  262.             if (rgb) {
  263.             facet->color[0] = 1.0;
  264.             facet->color[1] = 0.2;
  265.             facet->color[2] = 0.2;
  266.             } else {
  267.             facet->color[0] = colorIndexes1[0];
  268.             facet->color[1] = colorIndexes1[1];
  269.             facet->color[2] = colorIndexes1[2];
  270.             }
  271.         } else {
  272.             if (rgb) {
  273.             facet->color[0] = 0.2;
  274.             facet->color[1] = 1.0;
  275.             facet->color[2] = 0.2;
  276.             } else {
  277.             facet->color[0] = colorIndexes2[0];
  278.             facet->color[1] = colorIndexes2[1];
  279.             facet->color[2] = colorIndexes2[2];
  280.             }
  281.         }
  282.         pt1 = GETCOORD(frameNum, i, j)->vertex;
  283.         pt2 = GETCOORD(frameNum, i, j+1)->vertex;
  284.         pt3 = GETCOORD(frameNum, i+1, j+1)->vertex;
  285.  
  286.         dp1[0] = pt2[0] - pt1[0];
  287.         dp1[1] = pt2[1] - pt1[1];
  288.         dp1[2] = pt2[2] - pt1[2];
  289.  
  290.         dp2[0] = pt3[0] - pt2[0];
  291.         dp2[1] = pt3[1] - pt2[1];
  292.         dp2[2] = pt3[2] - pt2[2];
  293.  
  294.         facet->normal[0] = dp1[1] * dp2[2] - dp1[2] * dp2[1];
  295.         facet->normal[1] = dp1[2] * dp2[0] - dp1[0] * dp2[2];
  296.         facet->normal[2] = dp1[0] * dp2[1] - dp1[1] * dp2[0];
  297.  
  298.         d = 1.0 / sqrt(facet->normal[0]*facet->normal[0]+
  299.                    facet->normal[1]*facet->normal[1]+
  300.                    facet->normal[2]*facet->normal[2]);
  301.  
  302.         facet->normal[0] *= d;
  303.         facet->normal[1] *= d;
  304.         facet->normal[2] *= d;
  305.         }
  306.     }
  307.     }
  308. }
  309.  
  310. static void InitMaterials(void)
  311. {
  312.     static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  313.     static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
  314.     static float position[] = {90.0, 90.0, 150.0, 0.0};
  315.     static float front_mat_shininess[] = {60.0};
  316.     static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
  317.     static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
  318.     static float back_mat_shininess[] = {60.0};
  319.     static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
  320.     static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  321.     static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  322.     static float lmodel_twoside[] = {GL_TRUE};
  323.  
  324.     glMatrixMode(GL_PROJECTION);
  325.     gluPerspective(450., 1.0, 0.5, 10.0);
  326.  
  327.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  328.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  329.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  330.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  331.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  332.     glEnable(GL_LIGHTING);
  333.     glEnable(GL_LIGHT0);
  334.     
  335.     glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
  336.     glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
  337.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  338.     glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
  339.     glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  340.     glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  341.     if (rgb) {
  342.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  343.     }
  344.  
  345.     if (rgb) {
  346.     glEnable(GL_COLOR_MATERIAL);
  347.     } else {
  348.     SetColorMap();
  349.     }
  350. }
  351.  
  352. static void InitTexture(void)
  353. {
  354.  
  355.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  356.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  357.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  358.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  359.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  360. }
  361.  
  362. static void Init(void)
  363. {
  364.  
  365.     glClearColor(0.0, 0.0, 0.0, 0.0);
  366.  
  367.     glShadeModel(GL_FLAT);
  368.     
  369.     glFrontFace(GL_CW);
  370.  
  371.     glEnable(GL_DEPTH_TEST);
  372.  
  373.     InitMaterials();
  374.     InitTexture();
  375.     InitMesh();
  376.  
  377.     glMatrixMode(GL_MODELVIEW);
  378.     glTranslatef(0.0, 0.4, -1.8);
  379.     glScalef(2.0, 2.0, 2.0);
  380.     glRotatef(-35.0, 1.0, 0.0, 0.0);
  381.     glRotatef(35.0, 0.0, 0.0, 1.0);
  382. }
  383.  
  384. static void Reshape(int width, int height)
  385. {
  386.  
  387.     glViewport(0, 0, (GLint)width, (GLint)height);
  388. }
  389.  
  390. static GLenum Key(int key, GLenum mask)
  391. {
  392.  
  393.     switch (key) {
  394.       case TK_B:
  395.     tkClipBoard();
  396.     break;
  397.       case TK_ESCAPE:
  398.     tkQuit();
  399.       case TK_c:
  400.     contouring++;
  401.     if (contouring == 1) {
  402.         static GLfloat map[4] = {0, 0, 20, 0};
  403.  
  404.         glTexImage2D(GL_TEXTURE_2D, 0, 3, 4, 4, 0, GL_LUMINANCE,
  405.              GL_UNSIGNED_BYTE, (GLvoid *)contourTexture1);
  406.         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  407.         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  408.         glTexGenfv(GL_S, GL_OBJECT_PLANE, map);
  409.         glTexGenfv(GL_T, GL_OBJECT_PLANE, map);
  410.         glEnable(GL_TEXTURE_2D);
  411.         glEnable(GL_TEXTURE_GEN_S);
  412.         glEnable(GL_TEXTURE_GEN_T);
  413.     } else if (contouring == 2) {
  414.         static GLfloat map[4] = {0, 0, 20, 0};
  415.  
  416.         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  417.         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  418.         glPushMatrix();
  419.         glMatrixMode(GL_MODELVIEW);
  420.         glLoadIdentity();
  421.         glTexGenfv(GL_S, GL_EYE_PLANE, map);
  422.         glTexGenfv(GL_T, GL_EYE_PLANE, map);
  423.         glPopMatrix();
  424.     } else {
  425.         contouring = 0;
  426.         glDisable(GL_TEXTURE_GEN_S);
  427.         glDisable(GL_TEXTURE_GEN_T);
  428.         glDisable(GL_TEXTURE_2D);
  429.     }
  430.     break;
  431.       case TK_s:
  432.     smooth = !smooth;
  433.     if (smooth) {
  434.         glShadeModel(GL_SMOOTH);
  435.     } else {
  436.         glShadeModel(GL_FLAT);
  437.     }
  438.     break;
  439.       case TK_l:
  440.     lighting = !lighting;
  441.     if (lighting) {
  442.         glEnable(GL_LIGHTING);
  443.         glEnable(GL_LIGHT0);
  444.         if (rgb) {
  445.         glEnable(GL_COLOR_MATERIAL);
  446.         }
  447.     } else {
  448.         glDisable(GL_LIGHTING);
  449.         glDisable(GL_LIGHT0);
  450.         if (rgb) {
  451.         glDisable(GL_COLOR_MATERIAL);
  452.         }
  453.     }
  454.     break;
  455.       case TK_d:
  456.     depth = !depth;
  457.     if (depth) {
  458.         glEnable(GL_DEPTH_TEST);
  459.         clearMask |= GL_DEPTH_BUFFER_BIT;
  460.     } else {
  461.         glDisable(GL_DEPTH_TEST);
  462.         clearMask &= ~GL_DEPTH_BUFFER_BIT;
  463.     }
  464.     break;
  465.       case TK_SPACE:
  466.     stepMode = !stepMode;
  467.     if (stepMode) {
  468.         tkIdleFunc(0);
  469.         tkDisplayFunc(Animate);
  470.     } else {
  471.         tkIdleFunc(Animate);
  472.         tkDisplayFunc(0);
  473.     }
  474.     break;
  475.       case TK_n:
  476.     if (stepMode) {
  477.         nextFrame = 1;
  478.     }
  479.     break;
  480.       case TK_a:
  481.     spinMode = !spinMode;
  482.     break;
  483.       default:
  484.     return GL_FALSE;
  485.     }
  486.     return GL_TRUE;
  487. }
  488.  
  489. static GLenum Args(int argc, char **argv)
  490. {
  491.     GLint i;
  492.  
  493.     rgb = GL_TRUE;
  494.     doubleBuffer = GL_FALSE;
  495.     directRender = GL_TRUE;
  496.     frames = 10;
  497.     widthX = 10;
  498.     widthY = 10;
  499.     checkerSize = 2;
  500.     height = 0.2;
  501.  
  502.     for (i = 1; i < argc; i++) {
  503.     if (strcmp(argv[i], "-ci") == 0) {
  504.         rgb = GL_FALSE;
  505.     } else if (strcmp(argv[i], "-rgb") == 0) {
  506.         rgb = GL_TRUE;
  507.     } else if (strcmp(argv[i], "-sb") == 0) {
  508.         doubleBuffer = GL_FALSE;
  509.     } else if (strcmp(argv[i], "-db") == 0) {
  510.         doubleBuffer = GL_TRUE;
  511.     } else if (strcmp(argv[i], "-dr") == 0) {
  512.         directRender = GL_TRUE;
  513.     } else if (strcmp(argv[i], "-ir") == 0) {
  514.         directRender = GL_FALSE;
  515.     } else if (strcmp(argv[i], "-grid") == 0) {
  516.         if (i+2 >= argc || argv[i+1][0] == '-' || argv[i+2][0] == '-') {
  517.         printf("-grid (No numbers).\n");
  518.         return GL_FALSE;
  519.         } else {
  520.         widthX = atoi(argv[++i]);
  521.         widthY = atoi(argv[++i]);
  522.         }
  523.     } else if (strcmp(argv[i], "-size") == 0) {
  524.         if (i+1 >= argc || argv[i+1][0] == '-') {
  525.         printf("-checker (No number).\n");
  526.         return GL_FALSE;
  527.         } else {
  528.         checkerSize = atoi(argv[++i]);
  529.         }
  530.     } else if (strcmp(argv[i], "-wave") == 0) {
  531.         if (i+1 >= argc || argv[i+1][0] == '-') {
  532.         printf("-wave (No number).\n");
  533.         return GL_FALSE;
  534.         } else {
  535.         height = atof(argv[++i]);
  536.         }
  537.     } else if (strcmp(argv[i], "-frames") == 0) {
  538.         if (i+1 >= argc || argv[i+1][0] == '-') {
  539.         printf("-frames (No number).\n");
  540.         return GL_FALSE;
  541.         } else {
  542.         frames = atoi(argv[++i]);
  543.         }
  544.     } else {
  545.         printf("%s (Bad option).\n", argv[i]);
  546.         return GL_FALSE;
  547.     }
  548.     }
  549.     return GL_TRUE;
  550. }
  551.  
  552. void main(int argc, char **argv)
  553. {
  554.     GLenum type;
  555.  
  556.     if (Args(argc, argv) == GL_FALSE) {
  557.     tkQuit();
  558.     }
  559.  
  560.     tkInitPosition(0, 0, 300, 300);
  561.  
  562.     type = TK_DEPTH;
  563.     type |= (rgb) ? TK_RGB : TK_INDEX;
  564.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  565.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  566.     tkInitDisplayMode(type);
  567.  
  568.     if (tkInitWindow("Wave Demo") == GL_FALSE) {
  569.     tkQuit();
  570.     }
  571.  
  572.     Init();
  573.  
  574.     tkExposeFunc(Reshape);
  575.     tkReshapeFunc(Reshape);
  576.     tkKeyDownFunc(Key);
  577.     tkIdleFunc(Animate);
  578.     tkExec();
  579. }
  580.