home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / egltk22.zip / wave.c < prev    next >
Text File  |  1998-02-21  |  18KB  |  519 lines

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Sample Escape GL Custom Screen Saver
  4. //    Copyright (c) 1997, Snow Storm Software
  5. //    All Rights Reserved
  6. //
  7. // This sample is based on the WAVE.C sample provided by Silicon Graphics Inc.
  8. /*
  9.  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  10.  * ALL RIGHTS RESERVED
  11.  * Permission to use, copy, modify, and distribute this software for
  12.  * any purpose and without fee is hereby granted, provided that the above
  13.  * copyright notice appear in all copies and that both the copyright notice
  14.  * and this permission notice appear in supporting documentation, and that
  15.  * the name of Silicon Graphics, Inc. not be used in advertising
  16.  * or publicity pertaining to distribution of the software without specific,
  17.  * written prior permission.
  18.  *
  19.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  20.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  21.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  22.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  23.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  24.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  25.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  26.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  27.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  28.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  29.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  30.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  31.  *
  32.  * US Government Users Restricted Rights
  33.  * Use, duplication, or disclosure by the Government is subject to
  34.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  35.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  36.  * clause at DFARS 252.227-7013 and/or in similar or successor
  37.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  38.  * Unpublished-- rights reserved under the copyright laws of the
  39.  * United States.  Contractor/manufacturer is Silicon Graphics,
  40.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  41.  *
  42.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  43.  */
  44. //
  45. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46.  
  47. #include "custom.h"
  48.  
  49. #include <stdio.h>
  50. #include <string.h>
  51. #include <stdlib.h>
  52. #include <math.h>
  53. #include "tk.h"
  54.  
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. //
  57. // general instructions:
  58. //   - copy this file to another name eg. cool.c
  59. //   - modify all functions examples to provide the processing you desire
  60. //   - add your project to the 'TARGETS =' part of 'MAKEFILE.' eg. 'TARGETS = sample.egl wave.egl cool.egl'
  61. //   - make sure the 'TOP =' part of 'MAKEFILE' correctly identifies the location of your opengl base directory
  62. //      eg. 'TOP = \opengl'
  63. //   - build your project using 'nmake'
  64. //   - copy your new screen saver module 'cool.egl' to any Escape GL sub-directory
  65. //   - reload Escape GL
  66. //
  67. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68.  
  69. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  70. //
  71. // global variables
  72. //   define any variables you require
  73. //
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75.  
  76. #define PI 3.14159265358979323846
  77.  
  78. #define GETCOORD(frame, x, y) (&(theMesh.coords[frame*theMesh.numCoords+(x)+(y)*(theMesh.widthX+1)]))
  79. #define GETFACET(frame, x, y) (&(theMesh.facets[frame*theMesh.numFacets+(x)+(y)*theMesh.widthX]))
  80.  
  81. GLenum rgb, doubleBuffer, directRender;
  82.  
  83. GLint colorIndexes1[3];
  84. GLint colorIndexes2[3];
  85. GLenum clearMask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
  86.  
  87. GLenum smooth = GL_FALSE;
  88. GLenum lighting = GL_TRUE;
  89. GLenum depth = GL_TRUE;
  90. GLenum stepMode = GL_FALSE;
  91. GLenum spinMode = GL_FALSE;
  92. GLint contouring = 0;
  93.  
  94. GLint widthX, widthY;
  95. GLint checkerSize;
  96. float height;
  97.  
  98. //GLint frames, curFrame = 0, nextFrame = 0;
  99. GLint frames, nextFrame = 0;
  100. GLfloat curFrame = 0;
  101.  
  102. struct facet {
  103.     float color[3];
  104.     float normal[3];
  105. };
  106. struct coord {
  107.     float vertex[3];
  108.     float normal[3];
  109. };
  110. static struct mesh {
  111.     GLint widthX, widthY;
  112.     GLint numFacets;
  113.     GLint numCoords;
  114.     GLint frames;
  115.     struct coord *coords;
  116.     struct facet *facets;
  117. } theMesh;
  118.  
  119. GLubyte contourTexture1[] = {
  120.     255, 255, 255, 255,
  121.     255, 255, 255, 255,
  122.     255, 255, 255, 255,
  123.     127, 127, 127, 127,
  124. };
  125. GLubyte contourTexture2[] = {
  126.     255, 255, 255, 255,
  127.     255, 127, 127, 127,
  128.     255, 127, 127, 127,
  129.     255, 127, 127, 127,
  130. };
  131.  
  132. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. //
  134. // eglDesc
  135. //   defines the name of the module as it will appear in the drop down module list
  136. //   this name must be unique and not match any existing modules, nor custom modules
  137. //
  138. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  139.  
  140. char * eglDesc()
  141. {
  142.    return( "Wave Sample" );
  143. }
  144.  
  145. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  146. //
  147. // eglIdle
  148. //   animates and displays the module
  149. //   the parameter 'step' simply defines the time in seconds between time steps, use this to control the animation speed
  150. //    the size of the step is based on the speed of the target system and the complexity of the module, slower modules
  151. //    will take larger steps
  152. //
  153. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  154.  
  155. void eglIdle( GLfloat s )
  156. {
  157.    struct coord *coord;
  158.    struct facet *facet;
  159.    float *lastColor;
  160.    float *thisColor;
  161.    GLint i, j, curFrameIdx;
  162.  
  163.    glClear(clearMask);
  164.  
  165.    if (nextFrame || !stepMode) {
  166. //      curFrame++;
  167.       curFrame += s * 15.;
  168.    }
  169. //   if (curFrame >= theMesh.frames) {
  170. //      curFrame = 0;
  171. //   }
  172.  
  173.    curFrameIdx = curFrame;
  174.    curFrameIdx %= theMesh.frames;
  175.  
  176.    if ((nextFrame || !stepMode) && spinMode) {
  177. //      glRotatef(5.0, 0.0, 0.0, 1.0);
  178.       glRotatef( s * 180., 0.0, 0.0, 1.0);
  179.    }
  180.    nextFrame = 0;
  181.  
  182.    for (i = 0; i < theMesh.widthX; i++) {
  183.       glBegin(GL_QUAD_STRIP);
  184.       lastColor = NULL;
  185.       for (j = 0; j < theMesh.widthY; j++) {
  186.          facet = GETFACET(curFrameIdx, i, j);
  187.          if (!smooth && lighting) {
  188.             glNormal3fv(facet->normal);
  189.          }
  190.          if (lighting) {
  191.             if (rgb) {
  192.                thisColor = facet->color;
  193.                glColor3fv(facet->color);
  194.             } else {
  195.                thisColor = facet->color;
  196.                glMaterialfv(GL_FRONT_AND_BACK, GL_COLOR_INDEXES,
  197.                facet->color);
  198.             }
  199.          } else {
  200.             if (rgb) {
  201.                thisColor = facet->color;
  202.                glColor3fv(facet->color);
  203.             } else {
  204.                thisColor = facet->color;
  205.                glIndexf(facet->color[1]);
  206.             }
  207.          }
  208.  
  209.          if (!lastColor || (thisColor[0] != lastColor[0] && smooth)) {
  210.             if (lastColor) {
  211.                glEnd();
  212.                glBegin(GL_QUAD_STRIP);
  213.             }
  214.             coord = GETCOORD(curFrameIdx, i, j);
  215.             if (smooth && lighting) {
  216.                glNormal3fv(coord->normal);
  217.             }
  218.             glVertex3fv(coord->vertex);
  219.  
  220.             coord = GETCOORD(curFrameIdx, i+1, j);
  221.             if (smooth && lighting) {
  222.                glNormal3fv(coord->normal);
  223.             }
  224.             glVertex3fv(coord->vertex);
  225.          }
  226.  
  227.          coord = GETCOORD(curFrameIdx, i, j+1);
  228.          if (smooth && lighting) {
  229.            glNormal3fv(coord->normal);
  230.          }
  231.          glVertex3fv(coord->vertex);
  232.  
  233.          coord = GETCOORD(curFrameIdx, i+1, j+1);
  234.          if (smooth && lighting) {
  235.            glNormal3fv(coord->normal);
  236.          }
  237.          glVertex3fv(coord->vertex);
  238.  
  239.          lastColor = thisColor;
  240.       }
  241.       glEnd();
  242.    }
  243. }
  244.  
  245. static void SetColorMap(void)
  246. {
  247.    static float green[3] = {0.2, 1.0, 0.2};
  248.    static float red[3] = {1.0, 0.2, 0.2};
  249.    float *color, percent;
  250.    GLint *indexes, entries, i, j;
  251.    long buf[4];
  252.  
  253.    entries = tkGetColorMapSize();
  254.    entries = 256;
  255.  
  256.    colorIndexes1[0] = 1;
  257.    colorIndexes1[1] = 1 + (GLint)((entries - 1) * 0.3);
  258.    colorIndexes1[2] = (GLint)((entries - 1) * 0.5);
  259.    colorIndexes2[0] = 1 + (GLint)((entries - 1) * 0.5);
  260.    colorIndexes2[1] = 1 + (GLint)((entries - 1) * 0.8);
  261.    colorIndexes2[2] = entries - 1;
  262.  
  263.    for (i = 0; i < 2; i++) {
  264.       switch (i) {
  265.          case 0:
  266.             color = green;
  267.             indexes = colorIndexes1;
  268.             break;
  269.          case 1:
  270.             color = red;
  271.             indexes = colorIndexes2;
  272.             break;
  273.       }
  274.  
  275.       for (j = indexes[0]; j < indexes[1]; j++) {
  276.          percent = 0.2 + 0.8 * (j - indexes[0]) /
  277.             (float)(indexes[1] - indexes[0]);
  278.          tkSetOneColor(j, percent*color[0], percent*color[1],
  279.             percent*color[2]);
  280.       }
  281.       for (j=indexes[1]; j<=indexes[2]; j++) {
  282.          percent = (j - indexes[1]) / (float)(indexes[2] - indexes[1]);
  283.          tkSetOneColor(j, percent*(1-color[0])+color[0],
  284.            percent*(1-color[1])+color[1],
  285.            percent*(1-color[2])+color[2]);
  286.       }
  287.    }
  288. }
  289.  
  290. static void InitMesh(void)
  291. {
  292.    struct coord *coord;
  293.    struct facet *facet;
  294.    float dp1[3], dp2[3];
  295.    float *pt1, *pt2, *pt3;
  296.    float angle, d, x, y;
  297.    GLint numFacets, numCoords, frameNum, i, j;
  298.  
  299.    theMesh.widthX = widthX;
  300.    theMesh.widthY = widthY;
  301.    theMesh.frames = frames;
  302.  
  303.    numFacets = widthX * widthY;
  304.    numCoords = (widthX + 1) * (widthY + 1);
  305.  
  306.    theMesh.numCoords = numCoords;
  307.    theMesh.numFacets = numFacets;
  308.  
  309.    theMesh.coords = (struct coord *)malloc(frames*numCoords*
  310.                  sizeof(struct coord));
  311.    theMesh.facets = (struct facet *)malloc(frames*numFacets*
  312.                   sizeof(struct facet));
  313.    if (theMesh.coords == NULL || theMesh.facets == NULL) {
  314.       printf("Out of memory.\n");
  315.       return;
  316.    }
  317.  
  318.    for (frameNum = 0; frameNum < frames; frameNum++) {
  319.       for (i = 0; i <= widthX; i++) {
  320.          x = i / (float)widthX;
  321.          for (j = 0; j <= widthY; j++) {
  322.             y = j / (float)widthY;
  323.  
  324.             d = sqrt(x*x+y*y);
  325.             if (d == 0.0) {
  326.                d = 0.0001;
  327.             }
  328.             angle = 2 * PI * d + (2 * PI / frames * frameNum);
  329.  
  330.             coord = GETCOORD(frameNum, i, j);
  331.  
  332.             coord->vertex[0] = x - 0.5;
  333.             coord->vertex[1] = y - 0.5;
  334.             coord->vertex[2] = (height - height * d) * cos(angle);
  335.  
  336.             coord->normal[0] = -(height / d) * x * ((1 - d) * 2 * PI *
  337.               sin(angle) + cos(angle));
  338.             coord->normal[1] = -(height / d) * y * ((1 - d) * 2 * PI *
  339.               sin(angle) + cos(angle));
  340.             coord->normal[2] = -1;
  341.  
  342.             d = 1.0 / sqrt(coord->normal[0]*coord->normal[0]+
  343.                coord->normal[1]*coord->normal[1]+1);
  344.             coord->normal[0] *= d;
  345.             coord->normal[1] *= d;
  346.             coord->normal[2] *= d;
  347.          }
  348.       }
  349.       for (i = 0; i < widthX; i++) {
  350.          for (j = 0; j < widthY; j++) {
  351.             facet = GETFACET(frameNum, i, j);
  352.             if (((i/checkerSize)%2)^(j/checkerSize)%2) {
  353.                if (rgb) {
  354.                   facet->color[0] = 1.0;
  355.                   facet->color[1] = 0.2;
  356.                   facet->color[2] = 0.2;
  357.                } else {
  358.                   facet->color[0] = colorIndexes1[0];
  359.                   facet->color[1] = colorIndexes1[1];
  360.                   facet->color[2] = colorIndexes1[2];
  361.                }
  362.             } else {
  363.                if (rgb) {
  364.                   facet->color[0] = 0.2;
  365.                   facet->color[1] = 1.0;
  366.                   facet->color[2] = 0.2;
  367.                } else {
  368.                   facet->color[0] = colorIndexes2[0];
  369.                   facet->color[1] = colorIndexes2[1];
  370.                   facet->color[2] = colorIndexes2[2];
  371.                }
  372.             }
  373.             pt1 = GETCOORD(frameNum, i, j)->vertex;
  374.             pt2 = GETCOORD(frameNum, i, j+1)->vertex;
  375.             pt3 = GETCOORD(frameNum, i+1, j+1)->vertex;
  376.  
  377.             dp1[0] = pt2[0] - pt1[0];
  378.             dp1[1] = pt2[1] - pt1[1];
  379.             dp1[2] = pt2[2] - pt1[2];
  380.  
  381.             dp2[0] = pt3[0] - pt2[0];
  382.             dp2[1] = pt3[1] - pt2[1];
  383.             dp2[2] = pt3[2] - pt2[2];
  384.  
  385.             facet->normal[0] = dp1[1] * dp2[2] - dp1[2] * dp2[1];
  386.             facet->normal[1] = dp1[2] * dp2[0] - dp1[0] * dp2[2];
  387.             facet->normal[2] = dp1[0] * dp2[1] - dp1[1] * dp2[0];
  388.  
  389.             d = 1.0 / sqrt(facet->normal[0]*facet->normal[0]+
  390.                facet->normal[1]*facet->normal[1]+
  391.                facet->normal[2]*facet->normal[2]);
  392.  
  393.             facet->normal[0] *= d;
  394.             facet->normal[1] *= d;
  395.             facet->normal[2] *= d;
  396.          }
  397.       }
  398.    }
  399. }
  400.  
  401. static void InitMaterials(void)
  402. {
  403.    static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  404.    static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
  405.    static float position[] = {90.0, 90.0, 300.0, 0.0};
  406.    static float front_mat_shininess[] = {60.0};
  407.    static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
  408.    static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
  409.    static float back_mat_shininess[] = {60.0};
  410.    static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
  411.    static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  412.    static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  413.    static float lmodel_twoside[] = {GL_TRUE};
  414.  
  415.    glMatrixMode(GL_PROJECTION);
  416.    gluPerspective(450., 1.0, 0.5, 10.0);
  417.  
  418.    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  419.    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  420.    glLightfv(GL_LIGHT0, GL_POSITION, position);
  421.    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  422.    glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  423.    glEnable(GL_LIGHTING);
  424.    glEnable(GL_LIGHT0);
  425.  
  426.    glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
  427.    glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
  428.    glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  429.    glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
  430.    glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  431.    glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  432.    if (rgb) {
  433.       glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  434.    }
  435.  
  436.    if (rgb) {
  437.       glEnable(GL_COLOR_MATERIAL);
  438.    } else {
  439.       SetColorMap();
  440.    }
  441. }
  442.  
  443. static void InitTexture(void)
  444. {
  445.    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  446.    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  447.    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  448.    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  449.    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  450. }
  451.  
  452. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  453. //
  454. // eglInit
  455. //   initializes the opengl module you wish to display and is called once before the model is animated
  456. //   this function is called once to setup the module
  457. //
  458. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  459.  
  460. void eglInit( GLint quality )
  461. {
  462.    rgb = GL_TRUE;
  463.    frames = 10;
  464.    widthX = 10;
  465.    widthY = 10;
  466.    checkerSize = 2;
  467.    height = 0.2;
  468.    spinMode = GL_TRUE;
  469.  
  470.    glClearColor(0.0, 0.0, 0.0, 0.0);
  471.  
  472.    glShadeModel(GL_SMOOTH);
  473.    smooth = GL_TRUE;
  474.  
  475.    glFrontFace(GL_CW);
  476.  
  477.    glEnable(GL_DEPTH_TEST);
  478.  
  479.    InitMaterials();
  480.    InitTexture();
  481.    InitMesh();
  482.  
  483.    glMatrixMode(GL_MODELVIEW);
  484.    glTranslatef(0.0, 0.4, -1.8);
  485.    glScalef(2.0, 2.0, 2.0);
  486.    glRotatef(-35.0, 1.0, 0.0, 0.0);
  487.    glRotatef(35.0, 0.0, 0.0, 1.0);
  488. }
  489.  
  490. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  491. //
  492. // eglReshape
  493. //   resizes the window
  494. //   use this function to define the viewport and projection transformations based on the window size
  495. //
  496. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  497.  
  498. void eglReshape( GLint width, GLint height )
  499. {
  500.     glViewport(0, 0, (GLint)width, (GLint)height);
  501. }
  502.  
  503. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  504. //
  505. // eglDestroy
  506. //   destroy display lists, objects etc. when the module has ended
  507. //
  508. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  509.  
  510. void eglDestroy()
  511. {
  512.    if ( theMesh.coords ) free( theMesh.coords );
  513.    if ( theMesh.facets ) free( theMesh.facets );
  514.    theMesh.coords = 0;
  515.    theMesh.facets = 0;
  516. }
  517.  
  518.  
  519.