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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include "tk.h"
  6.  
  7.  
  8. float tkRGBMap[8][3] = {
  9.     {
  10.     0, 0, 0
  11.     },
  12.     {
  13.     1, 0, 0
  14.     },
  15.     {
  16.     0, 1, 0
  17.     },
  18.     {
  19.     1, 1, 0
  20.     },
  21.     {
  22.     0, 0, 1
  23.     },
  24.     {
  25.     1, 0, 1
  26.     },
  27.     {
  28.     0, 1, 1
  29.     },
  30.     {
  31.     1, 1, 1
  32.     }
  33. };
  34.  
  35. #define FALSE 0
  36. #define TRUE  1
  37. #define PI    3.14159265358979323846
  38.  
  39.  
  40. GLenum doubleBuffer, directRender;
  41. int W = 400, H = 400;
  42.  
  43. char *imageFileName = 0;
  44. TK_RGBImageRec *image;
  45.  
  46. int numComponents;
  47.  
  48. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  49. float decal[] = {GL_DECAL};
  50. float modulate[] = {GL_MODULATE};
  51. float repeat[] = {GL_REPEAT};
  52. float clamp[] = {GL_CLAMP};
  53. float nearest[] = {GL_NEAREST};
  54. float linear[] = {GL_LINEAR};
  55. float nearest_mipmap_nearest[] = {GL_NEAREST_MIPMAP_NEAREST};
  56. float nearest_mipmap_linear[] = {GL_NEAREST_MIPMAP_LINEAR};
  57. float linear_mipmap_nearest[] = {GL_LINEAR_MIPMAP_NEAREST};
  58. float linear_mipmap_linear[] = {GL_LINEAR_MIPMAP_LINEAR};
  59. GLint sphereMap[] = {GL_SPHERE_MAP};
  60.  
  61. float xRotation = 0.0, yRotation = 0.0;
  62. float zTranslate = -4.0;
  63. GLenum autoRotate = TRUE;
  64. GLenum deepestColor = TK_GREEN;
  65. GLenum isLit = TRUE;
  66. GLenum isFogged = FALSE;
  67. float *textureEnvironment = modulate;
  68.  
  69. struct MipMap {
  70.     int width, height;
  71.     unsigned char *data;
  72. };
  73.  
  74. int cube, cage, cylinder, torus, genericObject;
  75.  
  76. float c[6][4][4][3] = {
  77.     {
  78.     {
  79.         {
  80.         1.0, 1.0, -1.0
  81.         }, 
  82.         {
  83.         0.0, 1.0, -1.0
  84.         },
  85.         {
  86.         0.0, 0.0, -1.0
  87.         },
  88.         {
  89.         1.0, 0.0, -1.0
  90.         },
  91.     },
  92.     {
  93.         {
  94.         0.0, 1.0, -1.0
  95.         },
  96.         {
  97.         -1.0, 1.0, -1.0
  98.         }, 
  99.         {
  100.         -1.0, 0.0, -1.0
  101.         }, 
  102.         {
  103.         0.0, 0.0, -1.0
  104.         },
  105.     },
  106.     {
  107.         {
  108.         0.0,  0.0, -1.0
  109.         },
  110.         {
  111.         -1.0, 0.0, -1.0
  112.         },
  113.         {
  114.         -1.0, -1.0, -1.0
  115.         },
  116.         {
  117.         0.0, -1.0, -1.0
  118.         },
  119.     },
  120.     {
  121.         {
  122.         1.0, 0.0, -1.0
  123.         },
  124.         {
  125.         0.0, 0.0, -1.0
  126.         },
  127.         {
  128.         0.0, -1.0, -1.0
  129.         },
  130.         {
  131.         1.0, -1.0, -1.0
  132.         },
  133.     },
  134.     },
  135.     {
  136.     {
  137.         {
  138.         1.0, 1.0, 1.0
  139.         },
  140.         {
  141.         1.0, 1.0, 0.0
  142.         },
  143.         {
  144.         1.0, 0.0, 0.0
  145.         },
  146.         {
  147.         1.0, 0.0, 1.0
  148.         },
  149.     },
  150.     {
  151.         {
  152.         1.0, 1.0, 0.0
  153.         },
  154.         {
  155.         1.0, 1.0, -1.0
  156.         },
  157.         {
  158.         1.0, 0.0, -1.0
  159.         },
  160.         {
  161.         1.0, 0.0, 0.0
  162.         },
  163.     },
  164.     {
  165.         {
  166.         1.0, 0.0, -1.0
  167.         },
  168.         {
  169.         1.0, -1.0, -1.0
  170.         },
  171.         {
  172.         1.0, -1.0, 0.0
  173.         },
  174.         {
  175.         1.0, 0.0, 0.0
  176.         },
  177.     },
  178.     {
  179.         {
  180.         1.0, 0.0, 0.0
  181.         },
  182.         {
  183.         1.0, -1.0, 0.0
  184.         },
  185.         {
  186.         1.0, -1.0, 1.0
  187.         },
  188.         {
  189.         1.0, 0.0, 1.0
  190.         },
  191.     },
  192.     },
  193.     {
  194.     {
  195.         {
  196.         -1.0, 1.0, 1.0
  197.         },
  198.         {
  199.         0.0, 1.0, 1.0
  200.         },
  201.         {
  202.         0.0, 0.0, 1.0
  203.         },
  204.         {
  205.         -1.0, 0.0, 1.0
  206.         },
  207.     },
  208.     {
  209.         {
  210.         0.0, 1.0, 1.0
  211.         },
  212.         {
  213.         1.0, 1.0, 1.0
  214.         },
  215.         {
  216.         1.0, 0.0, 1.0
  217.         },
  218.         {
  219.         0.0, 0.0, 1.0
  220.         },
  221.     },
  222.     {
  223.         {
  224.         1.0, 0.0, 1.0
  225.         },
  226.         {
  227.         1.0, -1.0, 1.0
  228.         },
  229.         {
  230.         0.0, -1.0, 1.0
  231.         },
  232.         {
  233.         0.0, 0.0, 1.0
  234.         },
  235.     },
  236.     {
  237.         {
  238.         0.0, -1.0, 1.0
  239.         },
  240.         {
  241.         -1.0, -1.0, 1.0
  242.         },
  243.         {
  244.         -1.0, 0.0, 1.0
  245.         },
  246.         {
  247.         0.0, 0.0, 1.0
  248.         },
  249.     },
  250.     },
  251.     {
  252.     {
  253.         {
  254.         -1.0, 1.0, -1.0
  255.         },
  256.         {
  257.         -1.0, 1.0, 0.0
  258.         },
  259.         {
  260.         -1.0, 0.0, 0.0
  261.         },
  262.         {
  263.         -1.0, 0.0, -1.0
  264.         },
  265.     }, 
  266.     {
  267.         {
  268.         -1.0, 1.0, 0.0
  269.         },
  270.         {
  271.         -1.0, 1.0, 1.0
  272.         },
  273.         {
  274.         -1.0, 0.0, 1.0
  275.         },
  276.         {
  277.         -1.0, 0.0, 0.0
  278.         },
  279.     }, 
  280.     {
  281.         {
  282.         -1.0, 0.0, 1.0
  283.         },
  284.         {
  285.         -1.0, -1.0, 1.0
  286.         },
  287.         {
  288.         -1.0, -1.0, 0.0
  289.         },
  290.         {
  291.         -1.0, 0.0, 0.0
  292.         },
  293.     }, 
  294.     {
  295.         {
  296.         -1.0, -1.0, 0.0
  297.         },
  298.         {
  299.         -1.0, -1.0, -1.0
  300.         },
  301.         {
  302.         -1.0, 0.0, -1.0
  303.         },
  304.         {
  305.         -1.0, 0.0, 0.0
  306.         },
  307.     }, 
  308.     },
  309.     {
  310.     {
  311.         {
  312.         -1.0, 1.0, 1.0
  313.         },
  314.         {
  315.         -1.0, 1.0, 0.0
  316.         },
  317.         {
  318.         0.0, 1.0, 0.0
  319.         },
  320.         {
  321.         0.0, 1.0, 1.0
  322.         },
  323.     },
  324.     {
  325.         {
  326.         -1.0, 1.0, 0.0
  327.         },
  328.         {
  329.         -1.0, 1.0, -1.0
  330.         },
  331.         {
  332.         0.0, 1.0, -1.0
  333.         },
  334.         {
  335.         0.0, 1.0, 0.0
  336.         },
  337.     },
  338.     {
  339.         {
  340.         0.0, 1.0, -1.0
  341.         },
  342.         {
  343.         1.0, 1.0, -1.0
  344.         },
  345.         {
  346.         1.0, 1.0, 0.0
  347.         },
  348.         {
  349.         0.0, 1.0, 0.0
  350.         },
  351.     },
  352.     {
  353.         {
  354.         1.0, 1.0, 0.0
  355.         },
  356.         {
  357.         1.0, 1.0, 1.0
  358.         },
  359.         {
  360.         0.0, 1.0, 1.0
  361.         },
  362.         {
  363.         0.0, 1.0, 0.0
  364.         },
  365.     },
  366.     },
  367.     {
  368.     {
  369.         {
  370.         -1.0, -1.0, -1.0
  371.         },
  372.         {
  373.         -1.0, -1.0, 0.0
  374.         },
  375.         {
  376.         0.0, -1.0, 0.0
  377.         },
  378.         {
  379.         0.0, -1.0, -1.0
  380.         },
  381.     },
  382.     {
  383.         {
  384.         -1.0, -1.0, 0.0
  385.         },
  386.         {
  387.         -1.0, -1.0, 1.0
  388.         },
  389.         {
  390.         0.0, -1.0, 1.0
  391.         },
  392.         {
  393.         0.0, -1.0, 0.0
  394.         },
  395.     },
  396.     {
  397.         {
  398.         0.0, -1.0, 1.0
  399.         },
  400.         {
  401.         1.0, -1.0, 1.0
  402.         },
  403.         {
  404.         1.0, -1.0, 0.0
  405.         },
  406.         {
  407.         0.0, -1.0, 0.0
  408.         },
  409.     },
  410.     {
  411.         {
  412.         1.0, -1.0, 0.0
  413.         },
  414.         {
  415.         1.0, -1.0, -1.0
  416.         },
  417.         {
  418.         0.0, -1.0, -1.0
  419.         },
  420.         {
  421.         0.0, -1.0, 0.0
  422.         },
  423.     },
  424.     }
  425. };
  426.  
  427. float n[6][3] = {
  428.     {
  429.     0.0, 0.0, -1.0
  430.     },
  431.     {
  432.     1.0, 0.0, 0.0
  433.     },
  434.     {
  435.     0.0, 0.0, 1.0
  436.     },
  437.     {
  438.     -1.0, 0.0, 0.0
  439.     },
  440.     {
  441.     0.0, 1.0, 0.0
  442.     },
  443.     {
  444.     0.0, -1.0, 0.0
  445.     }
  446. };
  447.  
  448. GLfloat identity[16] = {
  449.     1, 0, 0, 0,
  450.     0, 1, 0, 0,
  451.     0, 0, 1, 0,
  452.     0, 0, 0, 1,
  453. };
  454.  
  455.  
  456. void BuildCylinder(int numEdges)
  457. {
  458.     int i, top = 1.0, bottom = -1.0;
  459.     float x[100], y[100], angle; 
  460.     
  461.     for (i = 0; i <= numEdges; i++) {
  462.     angle = i * 2.0 * PI / numEdges;
  463.     x[i] =  (float) cos(angle);
  464.     y[i] =  (float) sin(angle);
  465.     }
  466.  
  467.     glNewList(cylinder, GL_COMPILE);
  468.     glBegin(GL_TRIANGLE_STRIP);
  469.     for (i = 0; i <= numEdges; i++) {
  470.         glNormal3f(x[i], y[i], 0.0);
  471.         glVertex3f(x[i], y[i], bottom);
  472.         glVertex3f(x[i], y[i], top);
  473.     }
  474.     glEnd();
  475.     glBegin(GL_TRIANGLE_FAN);
  476.     glNormal3f(0.0, 0.0, 1.0);
  477.     glVertex3f(0.0, 0.0, top);
  478.     for (i = 0; i <= numEdges; i++) {
  479.         glVertex3f(x[i], -y[i], top);
  480.     }
  481.     glEnd();
  482.     glBegin(GL_TRIANGLE_FAN);
  483.     glNormal3f(0.0, 0.0, -1.0);
  484.     glVertex3f(0.0, 0.0, bottom);
  485.     for (i = 0; i <= numEdges; i++) {
  486.         glVertex3f(x[i], y[i], bottom);
  487.     }
  488.     glEnd();
  489.     glEndList();
  490. }
  491.  
  492. void BuildTorus(float rc, int numc, float rt, int numt)
  493. {
  494.     int i, j, k;
  495.     GLdouble s, t;
  496.     GLdouble x, y, z;
  497.     GLdouble pi, twopi;
  498.  
  499.     pi = 3.14159265358979323846;
  500.     twopi = 2.0 * pi;
  501.  
  502.     glNewList(torus, GL_COMPILE);
  503.     for (i = 0; i < numc; i++) {
  504.     glBegin(GL_QUAD_STRIP);
  505.         for (j = 0; j <= numt; j++) {
  506.         for (k = 0; k <= 1; k++) {
  507.         s = (i + k) % numc + 0.5;
  508.         t = j % numt;
  509.  
  510.         x = cos(t*twopi/numt) * cos(s*twopi/numc);
  511.         y = sin(t*twopi/numt) * cos(s*twopi/numc);
  512.         z = sin(s*twopi/numc);
  513.         glNormal3f(x, y, z);
  514.  
  515.         x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  516.         y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  517.         z = rc * sin(s*twopi/numc);
  518.         glVertex3f(x, y, z);
  519.         }
  520.         }
  521.     glEnd();
  522.     }
  523.     glEndList();
  524. }
  525.  
  526. void BuildCage(void)
  527. {
  528.     int i, j;
  529.     float inc;
  530.     float right, left, top, bottom, front, back;
  531.  
  532.     front  = 0.0;
  533.     back   = -8.0;
  534.  
  535.     left   = -4.0;
  536.     bottom = -4.0;
  537.     right  = 4.0;
  538.     top    = 4.0; 
  539.  
  540.     inc = 2.0 * 4.0 * 0.1;
  541.  
  542.     glNewList(cage, GL_COMPILE);
  543.     for (i = 0; i < 10; i++) {
  544.  
  545.     /*
  546.     ** Back
  547.     */
  548.     glBegin(GL_LINES);
  549.         glVertex3f(left+i*inc, top,    back);
  550.         glVertex3f(left+i*inc, bottom, back);
  551.     glEnd();
  552.     glBegin(GL_LINES);
  553.         glVertex3f(right, bottom+i*inc, back);
  554.         glVertex3f(left,  bottom+i*inc, back);
  555.     glEnd();
  556.  
  557.     /*
  558.     ** Front
  559.     */
  560.     glBegin(GL_LINES);
  561.         glVertex3f(left+i*inc, top,    front);
  562.         glVertex3f(left+i*inc, bottom, front);
  563.     glEnd();
  564.     glBegin(GL_LINES);
  565.         glVertex3f(right, bottom+i*inc, front);
  566.         glVertex3f(left,  bottom+i*inc, front);
  567.     glEnd();
  568.  
  569.     /*
  570.     ** Left
  571.     */
  572.     glBegin(GL_LINES);
  573.         glVertex3f(left, bottom+i*inc, front);
  574.         glVertex3f(left, bottom+i*inc, back);
  575.     glEnd();
  576.     glBegin(GL_LINES);
  577.         glVertex3f(left, top,    back+i*inc);
  578.         glVertex3f(left, bottom, back+i*inc);
  579.     glEnd();
  580.  
  581.     /*
  582.     ** Right
  583.     */
  584.     glBegin(GL_LINES);
  585.         glVertex3f(right, top-i*inc, front);
  586.         glVertex3f(right, top-i*inc, back);
  587.     glEnd();
  588.     glBegin(GL_LINES);
  589.         glVertex3f(right, top,    back+i*inc);
  590.         glVertex3f(right, bottom, back+i*inc);
  591.     glEnd();
  592.  
  593.     /*
  594.     ** Top
  595.     */
  596.     glBegin(GL_LINES);
  597.         glVertex3f(left+i*inc, top, front);
  598.         glVertex3f(left+i*inc, top, back);
  599.     glEnd();
  600.     glBegin(GL_LINES);
  601.         glVertex3f(right, top, back+i*inc);
  602.         glVertex3f(left,  top, back+i*inc);
  603.     glEnd();
  604.  
  605.     /*
  606.     ** Bottom
  607.     */
  608.     glBegin(GL_LINES);
  609.         glVertex3f(right-i*inc, bottom, front);
  610.         glVertex3f(right-i*inc, bottom, back);
  611.     glEnd();
  612.     glBegin(GL_LINES);
  613.         glVertex3f(right, bottom, back+i*inc);
  614.         glVertex3f(left,  bottom, back+i*inc);
  615.     glEnd();
  616.     }
  617.     glEndList();
  618. }
  619.  
  620. void BuildCube(void)
  621. {
  622.     int i, j;
  623.  
  624.     glNewList(cube, GL_COMPILE);
  625.     for (i = 0; i < 6; i++) {
  626.     for (j = 0; j < 4; j++) {
  627.         glNormal3fv(n[i]); 
  628.         glBegin(GL_POLYGON);
  629.         glVertex3fv(c[i][j][0]);
  630.         glVertex3fv(c[i][j][1]);
  631.         glVertex3fv(c[i][j][2]);
  632.         glVertex3fv(c[i][j][3]);
  633.         glEnd();
  634.     }
  635.     }
  636.     glEndList();
  637. }
  638.  
  639. void BuildLists(void)
  640. {
  641.  
  642.     cube = glGenLists(1);
  643.     BuildCube();
  644.  
  645.     cage = glGenLists(2);
  646.     BuildCage();
  647.  
  648.     cylinder = glGenLists(3);
  649.     BuildCylinder(60);
  650.  
  651.     torus = glGenLists(4);
  652.     BuildTorus(0.65, 20, .85, 65);
  653.  
  654.     genericObject = torus;
  655. }
  656.  
  657. void SetDeepestColor(void)
  658. {
  659.     GLint redBits, greenBits, blueBits;
  660.  
  661.     glGetIntegerv(GL_RED_BITS, &redBits);
  662.     glGetIntegerv(GL_GREEN_BITS, &greenBits);
  663.     glGetIntegerv(GL_BLUE_BITS, &blueBits);
  664.  
  665.     deepestColor = (redBits >= greenBits) ? TK_RED : TK_GREEN;
  666.     deepestColor = (deepestColor >= blueBits) ? deepestColor : TK_BLUE; 
  667. }
  668.  
  669. void SetDefaultSettings(void)
  670. {
  671.  
  672.     magFilter = nearest;
  673.     minFilter = nearest;
  674.     sWrapMode = repeat;
  675.     tWrapMode = repeat;
  676.     textureEnvironment = modulate;
  677.     autoRotate = TRUE;
  678. }
  679.  
  680. unsigned char *AlphaPadImage(int bufSize, unsigned char *inData, int alpha)
  681. {
  682.     unsigned char *outData, *out_ptr, *in_ptr;
  683.     int i;
  684.  
  685.     outData = malloc(bufSize * 4);
  686.     out_ptr = outData;
  687.     in_ptr = inData;
  688.  
  689.     for (i = 0; i < bufSize; i++) {
  690.     *out_ptr++ = *in_ptr++;
  691.     *out_ptr++ = *in_ptr++;
  692.     *out_ptr++ = *in_ptr++;
  693.     *out_ptr++ = alpha;
  694.     }
  695.  
  696.     free (inData);
  697.     return outData;
  698. }
  699.  
  700. void Init(void)
  701. {
  702.     float ambient[] = {0.0, 0.0, 0.0, 1.0};
  703.     float diffuse[] = {0.0, 1.0, 0.0, 1.0};
  704.     float specular[] = {1.0, 1.0, 1.0, 1.0};
  705.     float position[] = {2.0, 2.0,  0.0, 1.0};
  706.     float fog_color[] = {0.0, 0.0, 0.0, 1.0};
  707.     float mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  708.     float mat_shininess[] = {90.0};
  709.     float mat_specular[] = {1.0, 1.0, 1.0, 1.0};
  710.     float mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  711.     float lmodel_ambient[] = {0.0, 0.0, 0.0, 1.0};
  712.     float lmodel_twoside[] = {GL_TRUE};
  713.     float lmodel_local_viewer[] = {GL_FALSE};
  714.  
  715.     SetDeepestColor();
  716.     SetDefaultSettings();
  717.  
  718.     if (numComponents == 4) {
  719.     image = tkRGBImageLoad(imageFileName);
  720.     image->data = AlphaPadImage(image->sizeX*image->sizeY,
  721.                                     image->data, 128);
  722.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  723.     gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  724.               image->sizeX, image->sizeY, 
  725.               GL_RGBA, GL_UNSIGNED_BYTE, image->data);
  726.     } else {
  727.     image = tkRGBImageLoad(imageFileName);
  728.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  729.     gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  730.               image->sizeX, image->sizeY, 
  731.               GL_RGB, GL_UNSIGNED_BYTE, image->data);
  732.     }
  733.     
  734.     glFogf(GL_FOG_DENSITY, 0.125);
  735.     glFogi(GL_FOG_MODE, GL_LINEAR);
  736.     glFogf(GL_FOG_START, 4.0);
  737.     glFogf(GL_FOG_END, 9.0);
  738.     glFogfv(GL_FOG_COLOR, fog_color);
  739.  
  740.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  741.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  742.     glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  743.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  744.     
  745.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  746.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  747.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  748.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  749.  
  750.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  751.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  752.     glShadeModel(GL_SMOOTH);
  753.  
  754.     glEnable(GL_LIGHTING);
  755.     glEnable(GL_LIGHT0);
  756.  
  757.     glClearColor(0.0, 0.0, 0.0, 0.0);
  758.     glViewport(0, 0, W, H);
  759.     glEnable(GL_DEPTH_TEST);
  760.  
  761.     glFrontFace(GL_CW);
  762.     glEnable(GL_CULL_FACE);
  763.     glCullFace(GL_BACK);
  764.  
  765.     glEnable(GL_TEXTURE_2D);
  766.     glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  767.     glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  768.     glEnable(GL_TEXTURE_GEN_S);
  769.     glEnable(GL_TEXTURE_GEN_T);
  770.  
  771.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  772.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  773.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  774.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  775.  
  776.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  777.  
  778.     BuildLists();
  779. }
  780.  
  781. void ReInit(void)
  782. {
  783.  
  784.     if (genericObject == torus) {
  785.     glEnable(GL_DEPTH_TEST);
  786.     } else  {
  787.     glDisable(GL_DEPTH_TEST);
  788.     }
  789.     if (isFogged) {
  790.     textureEnvironment = modulate;
  791.     }
  792.  
  793.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  794.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  795.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  796. }
  797.  
  798. void Draw(void)
  799. {
  800.  
  801.     glMatrixMode(GL_PROJECTION);
  802.     glLoadIdentity();
  803.     glFrustum(-0.2, 0.2, -0.2, 0.2, 0.15, 9.0);
  804.     glMatrixMode(GL_MODELVIEW);
  805.  
  806.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  807.     if (isFogged) {
  808.     glEnable(GL_FOG);
  809.     glColor3fv(tkRGBMap[deepestColor]);
  810.     } else {
  811.     glColor3fv(tkRGBMap[TK_WHITE]);
  812.     }
  813.     glDisable(GL_LIGHTING);
  814.     glDisable(GL_LIGHT0);
  815.     glDisable(GL_TEXTURE_2D);
  816.     glCallList(cage);
  817.  
  818.     glPushMatrix();
  819.     glTranslatef(0.0, 0.0, zTranslate);
  820.     glRotatef(xRotation, 1, 0, 0);
  821.     glRotatef(yRotation, 0, 1, 0);
  822.  
  823.     if (isLit == TRUE) {
  824.     glEnable(GL_LIGHTING);
  825.     glEnable(GL_LIGHT0);
  826.     }
  827.  
  828.     glEnable(GL_TEXTURE_2D);
  829.     if (isFogged) {
  830.     glDisable(GL_FOG);
  831.     }
  832.     glPolygonMode(GL_FRONT, GL_FILL);
  833.     glColor3fv(tkRGBMap[deepestColor]);
  834.     glCallList(genericObject);
  835.  
  836.     glPopMatrix();
  837.     glFlush();
  838.  
  839.     if (autoRotate) {
  840.     xRotation += .75;
  841.     yRotation += .375;
  842.     } 
  843.     tkSwapBuffers();
  844. }
  845.  
  846. void Reshape(int width, int height)
  847. {
  848.  
  849.     W = width;
  850.     H = height;
  851.     ReInit();
  852. }
  853.  
  854. GLenum Key(int key, GLenum mask)
  855. {
  856.  
  857.     switch (key) {
  858.       case TK_ESCAPE:
  859.     free(image->data);
  860.     tkQuit();
  861.  
  862.       case TK_LEFT:
  863.     yRotation -= 0.5;
  864.     autoRotate = FALSE;
  865.     ReInit();
  866.     break;
  867.       case TK_RIGHT:
  868.     yRotation += 0.5;
  869.     autoRotate = FALSE;
  870.     ReInit();
  871.     break;
  872.       case TK_UP:
  873.     xRotation -= 0.5;
  874.     autoRotate = FALSE;
  875.     ReInit();
  876.     break;
  877.       case TK_DOWN:
  878.     xRotation += 0.5;
  879.     autoRotate = FALSE;
  880.     ReInit();
  881.     break;
  882.       case TK_a:
  883.     autoRotate = !autoRotate;
  884.     ReInit();
  885.     break;
  886.       case TK_c:
  887.     genericObject = (genericObject == cube) ? cylinder : cube;
  888.     ReInit();
  889.     break;
  890.       case TK_d:
  891.     textureEnvironment = decal;
  892.     ReInit();
  893.     break;
  894.       case TK_m:
  895.     textureEnvironment = modulate;
  896.     ReInit();
  897.     break;
  898.       case TK_l:
  899.     isLit = !isLit;
  900.     ReInit();
  901.     break;
  902.       case TK_f:
  903.     isFogged = !isFogged;
  904.     ReInit();
  905.     break;
  906.       case TK_t:
  907.     genericObject = torus;
  908.     ReInit();
  909.     break;
  910.       case TK_0:
  911.     magFilter = nearest;
  912.     ReInit();
  913.     break;
  914.       case TK_1:
  915.     magFilter = linear;
  916.     ReInit();
  917.     break;
  918.       case TK_2:
  919.     minFilter = nearest;
  920.     ReInit();
  921.     break;
  922.       case TK_3:
  923.     minFilter = linear;
  924.     ReInit();
  925.     break;
  926.       case TK_4:
  927.     minFilter = nearest_mipmap_nearest;
  928.     ReInit();
  929.     break;
  930.       case TK_5:
  931.     minFilter = nearest_mipmap_linear;
  932.     ReInit();
  933.     break;
  934.       case TK_6:
  935.     minFilter = linear_mipmap_nearest;
  936.     ReInit();
  937.     break;
  938.       case TK_7:
  939.     minFilter = linear_mipmap_linear;
  940.     ReInit();
  941.     break;
  942.  
  943.       default:
  944.     return GL_FALSE;
  945.     }
  946.     return GL_TRUE;
  947. }
  948.  
  949. GLenum Args(int argc, char **argv)
  950. {
  951.     GLint i;
  952.  
  953.     doubleBuffer = GL_FALSE;
  954.     directRender = GL_TRUE;
  955.     numComponents = 4;
  956.  
  957.     for (i = 1; i < argc; i++) {
  958.     if (strcmp(argv[i], "-sb") == 0) {
  959.         doubleBuffer = GL_FALSE;
  960.     } else if (strcmp(argv[i], "-db") == 0) {
  961.         doubleBuffer = GL_TRUE;
  962.     } else if (strcmp(argv[i], "-dr") == 0) {
  963.         directRender = GL_TRUE;
  964.     } else if (strcmp(argv[i], "-ir") == 0) {
  965.         directRender = GL_FALSE;
  966.     } else if (strcmp(argv[i], "-f") == 0) {
  967.         if (i+1 >= argc || argv[i+1][0] == '-') {
  968.         printf("-f (No file name).\n");
  969.         return GL_FALSE;
  970.         } else {
  971.         imageFileName = argv[++i];
  972.         }
  973.     } else if (strcmp(argv[i], "-4") == 0) {
  974.         numComponents = 4;
  975.     } else if (strcmp(argv[i], "-3") == 0) {
  976.         numComponents = 3;
  977.     } else {
  978.         printf("%s (Bad option).\n", argv[i]);
  979.         return GL_FALSE;
  980.     }
  981.     }
  982.     return GL_TRUE;
  983. }
  984.  
  985. void main(int argc, char **argv)
  986. {
  987.     GLenum type;
  988.  
  989.     if (Args(argc, argv) == GL_FALSE) {
  990.     tkQuit();
  991.     }
  992.  
  993.     if (imageFileName == 0) {
  994.     printf("No image file.\n");
  995.     tkQuit();
  996.     }
  997.  
  998.     tkInitPosition(0, 0, W, H);
  999.  
  1000.     type = TK_RGB;
  1001.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  1002.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  1003.     tkInitDisplayMode(type);
  1004.  
  1005.     if (tkInitWindow("Texture Test") == GL_FALSE) {
  1006.         tkQuit();
  1007.     }
  1008.  
  1009.     Init();
  1010.  
  1011.     tkExposeFunc(Reshape);
  1012.     tkReshapeFunc(Reshape);
  1013.     tkKeyDownFunc(Key);
  1014.     tkIdleFunc(Draw);
  1015.  
  1016.     tkExec();
  1017. }
  1018.