home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / contrib / text3d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  13.9 KB  |  508 lines

  1. /* Text3d by Robert J. Doyle, Jr., Naval Research Laboratory, Washington, DC. */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <unistd.h>
  7. #include <GL/glut.h>
  8.  
  9. typedef enum {RESERVED, M_SIDE, M_EDGE, M_WHOLE, O_SIDE, O_EDGE, O_WHOLE, 
  10. T_SIDE, T_EDGE, T_WHOLE, H_SIDE, H_EDGE, H_WHOLE,REPEAT_SIDE, REPEAT_EDGE, REPEAT1, 
  11. REPEAT2_SIDE, REPEAT2_EDGE,REPEAT2, REPEAT3_SIDE, REPEAT3_EDGE,REPEAT3, 
  12. REPEAT4_SIDE, REPEAT4_EDGE,REPEAT4} displayLists;
  13.  
  14. GLfloat sideColor[] = {0.0, 0.0, 0.5, 1.0};
  15. GLfloat edgeColor[] = {0.7, 0.7, 0.0, 1.0};
  16. GLfloat shininess[] = {128.0};
  17. GLfloat mat_specular[] = {0.7, 0.7, 0.7, 1.0};
  18.  
  19. GLfloat width = 0.0;
  20. GLfloat width2 = 2.0;
  21.  
  22. GLfloat letterM[][3] = 
  23.     {-3.125000, 0.000000, 0.000000},
  24.     {-3.125000, 6.208000, 0.000000},
  25.     {-1.233000, 6.208000, 0.000000},
  26.     {0.003000, 1.484000, 0.000000},
  27.     {1.223000, 6.208000, 0.000000},
  28.     {3.123000, 6.208000, 0.000000},
  29.     {3.123000, 0.000000, 0.000000},
  30.     {1.923000, 0.000000, 0.000000},
  31.     {1.923000, 5.010000, 0.000000},
  32.     {0.659000, 0.000000, 0.000000},
  33.     {-0.649000, 0.000000, 0.000000},
  34.     {-1.925000, 5.010000, 0.000000},
  35.     {-1.925000, 0.000000, 0.000000}
  36.  
  37. };
  38.  
  39. GLfloat letterO[][3] = 
  40.     {-3.038000, 3.102000, 0.000000},
  41.     {-2.974000, 3.874000, 0.000000},
  42.     {-2.827000, 4.440000, 0.000000},
  43.     {-2.802000, 4.508000, 0.000000},
  44.     {-2.544000, 5.042000, 0.000000},
  45.     {-2.502000, 5.110000, 0.000000},
  46.     {-2.223000, 5.479000, 0.000000},
  47.     {-2.132000, 5.576000, 0.000000},
  48.     {-1.784000, 5.869000, 0.000000},
  49.     {-1.678000, 5.940000, 0.000000},
  50.     {-1.260000, 6.155000, 0.000000},
  51.     {-1.148000, 6.198000, 0.000000},
  52.     {-0.677000, 6.321000, 0.000000},
  53.     {-0.638000, 6.328000, 0.000000},
  54.     {-0.002000, 6.378000, 0.000000},
  55.     {0.634000, 6.328000, 0.000000},
  56.     {1.107000, 6.210000, 0.000000},
  57.     {1.144000, 6.198000, 0.000000},
  58.     {1.570000, 6.002000, 0.000000},
  59.     {1.674000, 5.940000, 0.000000},
  60.     {2.038000, 5.661000, 0.000000},
  61.     {2.128000, 5.576000, 0.000000},
  62.     {2.428000, 5.217000, 0.000000},
  63.     {2.504000, 5.104000, 0.000000},
  64.     {2.762000, 4.598000, 0.000000},
  65.     {2.798000, 4.508000, 0.000000},
  66.     {2.960000, 3.913000, 0.000000},
  67.     {2.970000, 3.862000, 0.000000},
  68.     {3.034000, 3.102000, 0.000000},
  69.     {2.970000, 2.342000, 0.000000},
  70.     {2.815000, 1.745000, 0.000000},
  71.     {2.798000, 1.696000, 0.000000},
  72.     {2.554000, 1.182000, 0.000000},
  73.     {2.504000, 1.100000, 0.000000},
  74.     {2.221000, 0.726000, 0.000000},
  75.     {2.128000, 0.628000, 0.000000},
  76.     {1.776000, 0.332000, 0.000000},
  77.     {1.674000, 0.264000, 0.000000},
  78.     {1.256000, 0.049000, 0.000000},
  79.     {1.144000, 0.006000, 0.000000},
  80.     {0.672000, -0.117000, 0.000000},
  81.     {0.634000, -0.124000, 0.000000},
  82.     {-0.002000, -0.174000, 0.000000},
  83.     {-0.638000, -0.124000, 0.000000},
  84.     {-1.112000, -0.006000, 0.000000},
  85.     {-1.148000, 0.006000, 0.000000},
  86.     {-1.576000, 0.202000, 0.000000},
  87.     {-1.678000, 0.264000, 0.000000},
  88.     {-2.041000, 0.540000, 0.000000},
  89.     {-2.132000, 0.628000, 0.000000},
  90.     {-2.430000, 0.983000, 0.000000},
  91.     {-2.502000, 1.094000, 0.000000},
  92.     {-2.773000, 1.622000, 0.000000},
  93.     {-2.802000, 1.696000, 0.000000},
  94.     {-2.962000, 2.258000, 0.000000},
  95.     {-2.974000, 2.330000, 0.000000},
  96.     {-1.736000, 3.102000, 10000.0},
  97.     {-1.710000, 3.578000, 0.000000},
  98.     {-1.644000, 3.934000, 0.000000},
  99.     {-1.503000, 4.328000, 0.000000},
  100.     {-1.494000, 4.346000, 0.000000},
  101.     {-1.352000, 4.593000, 0.000000},
  102.     {-1.306000, 4.656000, 0.000000},
  103.     {-1.120000, 4.857000, 0.000000},
  104.     {-1.040000, 4.926000, 0.000000},
  105.     {-0.825000, 5.067000, 0.000000},
  106.     {-0.726000, 5.116000, 0.000000},
  107.     {-0.480000, 5.200000, 0.000000},
  108.     {-0.402000, 5.218000, 0.000000},
  109.     {-0.041000, 5.257000, 0.000000},
  110.     {-0.002000, 5.258000, 0.000000},
  111.     {0.361000, 5.227000, 0.000000},
  112.     {0.400000, 5.220000, 0.000000},
  113.     {0.650000, 5.147000, 0.000000},
  114.     {0.726000, 5.116000, 0.000000},
  115.     {0.950000, 4.990000, 0.000000},
  116.     {1.038000, 4.926000, 0.000000},
  117.     {1.239000, 4.736000, 0.000000},
  118.     {1.306000, 4.656000, 0.000000},
  119.     {1.462000, 4.413000, 0.000000},
  120.     {1.498000, 4.342000, 0.000000},
  121.     {1.635000, 3.964000, 0.000000},
  122.     {1.644000, 3.934000, 0.000000},
  123.     {1.710000, 3.568000, 0.000000},
  124.     {1.736000, 3.102000, 0.000000},
  125.     {1.710000, 2.636000, 0.000000},
  126.     {1.642000, 2.268000, 0.000000},
  127.     {1.508000, 1.886000, 0.000000},
  128.     {1.496000, 1.860000, 0.000000},
  129.     {1.351000, 1.610000, 0.000000},
  130.     {1.304000, 1.546000, 0.000000},
  131.     {1.115000, 1.343000, 0.000000},
  132.     {1.036000, 1.276000, 0.000000},
  133.     {0.823000, 1.135000, 0.000000},
  134.     {0.724000, 1.086000, 0.000000},
  135.     {0.480000, 1.001000, 0.000000},
  136.     {0.400000, 0.984000, 0.000000},
  137.     {0.035000, 0.946000, 0.000000},
  138.     {-0.002000, 0.946000, 0.000000},
  139.     {-0.368000, 0.979000, 0.000000},
  140.     {-0.402000, 0.986000, 0.000000},
  141.     {-0.653000, 1.057000, 0.000000},
  142.     {-0.726000, 1.088000, 0.000000},
  143.     {-0.952000, 1.213000, 0.000000},
  144.     {-1.040000, 1.278000, 0.000000},
  145.     {-1.240000, 1.467000, 0.000000},
  146.     {-1.306000, 1.548000, 0.000000},
  147.     {-1.460000, 1.788000, 0.000000},
  148.     {-1.494000, 1.858000, 0.000000},
  149.     {-1.639000, 2.251000, 0.000000},
  150.     {-1.644000, 2.270000, 0.000000},
  151.     {-1.710000, 2.626000, 0.000000}
  152. };
  153.  
  154. GLfloat letterT[][3] = 
  155. {
  156.     {-0.640000, 0.000000, 0.000000},
  157.     {-0.640000, 5.104000, 0.000000},
  158.     {-2.476000, 5.104000, 0.000000},
  159.     {-2.476000, 6.208000, 0.000000},
  160.     {2.476000, 6.208000, 0.000000},
  161.     {2.476000, 5.104000, 0.000000},
  162.     {0.640000, 5.104000, 0.000000},
  163.     {0.640000, 0.000000, 0.000000}
  164. };
  165.  
  166. GLfloat letterH[][3] = 
  167. {
  168.     {-2.570000, 0.000000, 0.000000},
  169.     {-2.570000, 6.208000, 0.000000},
  170.     {-1.282000, 6.208000, 0.000000},
  171.     {-1.282000, 3.900000, 0.000000},
  172.     {1.280000, 3.900000, 0.000000},
  173.     {1.280000, 6.208000, 0.000000},
  174.     {2.568000, 6.208000, 0.000000},
  175.     {2.568000, 0.000000, 0.000000},
  176.     {1.280000, 0.000000, 0.000000},
  177.     {1.280000, 2.760000, 0.000000},
  178.     {-1.282000, 2.760000, 0.000000},
  179.     {-1.282000, 0.000000, 0.000000}
  180. };
  181.  
  182. /*  Initialize light source and lighting.
  183.  */
  184.  
  185.  
  186. static void checkErrors(void)
  187. {
  188.   GLenum error;
  189.   while ((error = glGetError()) != GL_NO_ERROR) {
  190.     fprintf(stderr, "Error: %s\n", (char *) gluErrorString(error));
  191.   }
  192. }
  193.  
  194. void myinit(void)
  195. {
  196.     int count1 =  sizeof(letterM) / (3 * sizeof(GLfloat)); 
  197.     int count2 =  sizeof(letterO) / (3 * sizeof(GLfloat));
  198.     int count3 =  sizeof(letterT) / (3 * sizeof(GLfloat)); 
  199.     int count4 =  sizeof(letterH) / (3 * sizeof(GLfloat));
  200.  
  201.     int i;
  202.     
  203.     GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
  204.     GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  205.     GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  206. /*    light_position is NOT default value    */
  207.     GLfloat light_position[] = { -1.0, -1.0, 1.0, 0.0 };
  208.  
  209.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  210.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  211.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  212.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  213.     
  214.     glEnable(GL_LIGHTING);
  215.     glEnable(GL_LIGHT0);
  216.     glDepthFunc(GL_LESS);
  217.     glEnable(GL_DEPTH_TEST);
  218.  
  219.     glDrawBuffer(GL_FRONT_AND_BACK);
  220.     glClearColor(0.0, 0.0, 0.0, 1.0);
  221.     glClear(GL_COLOR_BUFFER_BIT);
  222.     glClear(GL_ACCUM_BUFFER_BIT);
  223.     glDrawBuffer(GL_BACK);    
  224.  
  225. /* Zero position of text */
  226.  
  227.             for(i = 0; i < count1; i++) {
  228.                 letterM[i][1] = letterM[i][1] - 3.175;
  229.             }
  230.             for(i = 0; i < count2; i++) {
  231.                 letterO[i][1] = letterO[i][1] - 3.175;
  232.             }
  233.             for(i = 0; i < count3; i++) {
  234.                 letterT[i][1] = letterT[i][1] - 3.175;
  235.             } 
  236.             for(i = 0; i < count4; i++) {
  237.                 letterH[i][1] = letterH[i][1] - 3.175;
  238.             } 
  239. }
  240.  
  241.  
  242. /* Mark Kilgard's tessellation code from the "dino" demos. */
  243. void extrudeSolidFromPolygon(GLfloat data[][3], unsigned int dataSize,
  244.         GLdouble thickness, GLuint side, GLuint edge, GLuint whole)
  245. {
  246.     GLdouble vertex[3], dx, dy, len;
  247.     int i, k;
  248.     int flag = 0;
  249.     int count = dataSize / (3 * sizeof(GLfloat));
  250.     static GLUtriangulatorObj *tobj = NULL;
  251.  
  252.     if (tobj == NULL) {
  253.         tobj = gluNewTess();
  254.         
  255.         gluTessCallback(tobj, GLU_BEGIN, glBegin);
  256.         gluTessCallback(tobj, GLU_VERTEX, glVertex3fv);
  257.         gluTessCallback(tobj, GLU_END, glEnd);
  258.     }
  259.     glNewList(side, GL_COMPILE);
  260.         glShadeModel(GL_SMOOTH);
  261.         gluBeginPolygon(tobj);
  262.             for(i = 0; i < count; i++) {
  263.                 /* This detects a new contour from a large number placed in
  264.                 the unused z coordinate of the vertex where the new contour 
  265.                 starts. See the coordinates for letterO, above. The coordinate 
  266.                 must be reset below for additional calls. */
  267.  
  268.                 if (data[i][2] > 1000.0) {
  269.                     data[i][2] = 0.0;
  270.                     flag = 1; k = i;
  271.                     gluNextContour(tobj, GLU_INTERIOR); 
  272.                 }
  273.                 
  274.                 vertex[0] = data[i][0];
  275.                 vertex[1] = data[i][1];
  276.                 vertex[2] = 0.0;
  277.                 gluTessVertex(tobj, vertex, data[i]);
  278.             }
  279.         gluEndPolygon(tobj);
  280.     glEndList();
  281.     
  282.                 /* Reset coordinate for new calls. */
  283.                 if (flag == 1) {
  284.                 data[k][2] = 10000.0;
  285.                 flag = 0;
  286.                 }
  287.     glNewList(edge, GL_COMPILE);
  288.         glBegin(GL_QUAD_STRIP);
  289.         for(i = 0; i <= count; i++) {
  290.             glVertex3f(data[i % count][0], data[i % count][1], 0.0);
  291.             glVertex3f(data[i % count][0], data[i % count][1], thickness);
  292.             /* Normals */
  293.             dx = data[(i+ 1) % count][1] - data[i % count][1];
  294.             dy = data[i % count][0] - data[(i + 1) % count][0];
  295.             len = sqrt(dx * dx + dy * dy);
  296.             glNormal3f(dx / len, dy / len, 0.0);
  297.         }
  298.         glEnd();
  299.     glEndList();
  300.     
  301.     glNewList(whole, GL_COMPILE);
  302.         glFrontFace(GL_CW);
  303.  
  304.         glMaterialfv(GL_FRONT, GL_DIFFUSE, edgeColor);
  305.         glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
  306.         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);                
  307.  
  308.         glCallList(edge);
  309.         glNormal3f(0.0, 0.0, -1.0); 
  310.         glCallList(side);
  311.         glPushMatrix();
  312.             glTranslatef(0.0, 0.0, thickness);
  313.             glFrontFace(GL_CCW);
  314.             glNormal3f(0.0, 0.0, 1.0);
  315.  
  316.         glMaterialfv(GL_FRONT, GL_DIFFUSE, sideColor);
  317.         glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
  318.         glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);        
  319.         
  320.             glCallList(side);
  321.         glPopMatrix();
  322.     glEndList();
  323. }
  324.  
  325. void repeat(int j)
  326. {
  327.     if(j == 2){            
  328.         glPushMatrix();
  329.             glTranslatef((31 * -0.34) , 9.3, -9.6);
  330.             glCallList(REPEAT1);
  331.         glPopMatrix();                
  332.     }
  333.     if(j == 3){            
  334.         glPushMatrix();
  335.             glTranslatef(31 * -0.34, 9.3, -9.6);
  336.             glCallList(REPEAT1);
  337.         glPopMatrix();
  338.         glPushMatrix();
  339.             glTranslatef(31 * -.09, 9.3, -9.6);
  340.             glCallList(REPEAT2);
  341.         glPopMatrix();    
  342.     }
  343.     if(j == 4){            
  344.         glPushMatrix();
  345.             glTranslatef(31 * -0.34, 9.3, -9.6);
  346.             glCallList(REPEAT1);
  347.         glPopMatrix();
  348.         glPushMatrix();
  349.             glTranslatef(31 * -.09, 9.3, -9.6);
  350.             glCallList(REPEAT2);
  351.         glPopMatrix();    
  352.         glPushMatrix();
  353.             glTranslatef(31 * 0.12, 9.3, -9.6);
  354.             glCallList(REPEAT3);
  355.         glPopMatrix();
  356.     }    
  357. }
  358.  
  359. void display(void)
  360. {
  361.     int i, j;
  362.     GLfloat xPos = -0.34;
  363.     glLoadIdentity();
  364.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  365.         
  366.     glTranslatef(0.0, 0.0, -10.0);
  367.  
  368.     extrudeSolidFromPolygon(letterM, sizeof(letterM), width2, REPEAT_SIDE, 
  369.         REPEAT_EDGE, REPEAT1);
  370.     extrudeSolidFromPolygon(letterO, sizeof(letterO), width2, REPEAT2_SIDE, 
  371.         REPEAT2_EDGE, REPEAT2);
  372.     extrudeSolidFromPolygon(letterT, sizeof(letterT), width2, REPEAT3_SIDE, 
  373.         REPEAT3_EDGE, REPEAT3);
  374.     extrudeSolidFromPolygon(letterH, sizeof(letterH), width2, REPEAT4_SIDE, 
  375.         REPEAT4_EDGE, REPEAT4);            
  376.  
  377.     for(j = 1; j < 5; j++){ 
  378.         width = 0.0;
  379. checkErrors();
  380.         for(i = 0; i < 10; i++){ 
  381.  
  382.             glPushMatrix();
  383.                 repeat(j);
  384.             glPopMatrix();
  385.             
  386.             glPushMatrix();
  387.                 glRotatef(90.0, 0.0, 1.0, 0.0); 
  388.                 if(j == 1){
  389.                     extrudeSolidFromPolygon(letterM, sizeof(letterM), width, M_SIDE, 
  390.                         M_EDGE, M_WHOLE);
  391.                     glCallList(M_WHOLE);                    
  392.                 }
  393.                 if(j == 2){
  394.                     extrudeSolidFromPolygon(letterO, sizeof(letterO), width, O_SIDE, 
  395.                         O_EDGE, O_WHOLE);
  396.                     glCallList(O_WHOLE);
  397.                 }                
  398.                 if(j == 3){
  399.                     extrudeSolidFromPolygon(letterT, sizeof(letterT), width, T_SIDE, 
  400.                         T_EDGE, T_WHOLE);
  401.                     glCallList(T_WHOLE);
  402.                 }
  403.                 if(j == 4){
  404.                     extrudeSolidFromPolygon(letterH, sizeof(letterH), width, H_SIDE, 
  405.                         H_EDGE, H_WHOLE);
  406.                     glCallList(H_WHOLE);
  407.                 }                
  408.                 glutSwapBuffers();
  409.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  410.                 width = width + 0.2;
  411.             glPopMatrix();
  412.         }
  413.         for(i = 0; i < 45 ; i++){
  414.                 
  415.             glPushMatrix();
  416.                 repeat(j);
  417.             glPopMatrix();                    
  418.             
  419.             glPushMatrix();                    
  420.                 glRotatef(90.0 - (2.0 * i), 0.0, 1.0, 0.0);
  421.                 if(j == 1){
  422.                 glCallList(M_WHOLE);
  423.                 }
  424.                 if(j == 2){
  425.                 glCallList(O_WHOLE);
  426.                 }
  427.                 if(j == 3){
  428.                 glCallList(T_WHOLE);
  429.                 }
  430.                 if(j == 4){
  431.                 glCallList(H_WHOLE);
  432.                 }            
  433.                 glutSwapBuffers();
  434.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        
  435.             glPopMatrix();
  436.         } 
  437.         for(i = 1; i < 32 ; i++){
  438.             
  439.             glPushMatrix();
  440.                 repeat(j);
  441.             glPopMatrix();
  442.             
  443.             glPushMatrix();
  444.                 glTranslatef(i * xPos, i * 0.3, i * -0.3);
  445.                 if(j == 1){
  446.                 glCallList(M_WHOLE);
  447.                 }
  448.                 if(j == 2){
  449.                 glCallList(O_WHOLE);
  450.                 }
  451.                 if(j == 3){
  452.                 glCallList(T_WHOLE);
  453.                 }
  454.                 if(j == 4){
  455.                 glCallList(H_WHOLE);
  456.                 }        
  457.                 glutSwapBuffers();
  458.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                
  459.             glPopMatrix();
  460.         }                
  461.  
  462.         if(j == 1){
  463.             xPos = xPos + 0.25;
  464.         }
  465.         else{
  466.             xPos = xPos + 0.21;
  467.         }
  468.     }
  469.     glFlush();
  470. }
  471.  
  472. void myReshape(int w, int h)
  473. {
  474.     glViewport(0, 0, w, h);
  475.     glMatrixMode(GL_PROJECTION);
  476.     glLoadIdentity();
  477.   glFrustum(-7.0, 7.0, -7.0, 7.0, 6.0, 20.0);
  478. /*    if (w <= h) 
  479.     glOrtho (-7.0, 7.0, -7.0*(GLfloat)h/(GLfloat)w, 
  480.         7.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
  481.     else 
  482.     glOrtho (-7.0*(GLfloat)w/(GLfloat)h, 
  483.         7.0*(GLfloat)w/(GLfloat)h, -7.0, 7.0, -10.0, 10.0); */
  484.     glMatrixMode(GL_MODELVIEW);
  485. }
  486.  
  487. /*  Main Loop
  488.  *  Open window with initial window size, title bar, 
  489.  *  RGBA display mode, and handle input events.
  490.  */
  491. int main(int argc, char** argv)
  492. {
  493.     glutInit(&argc, argv);
  494.     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  495.     glutCreateWindow ("text3d");
  496.  
  497. /*glCullFace(GL_FRONT);*/
  498. /*glEnable(GL_CULL_FACE);*/
  499.  
  500.     myinit();
  501.     glutReshapeFunc (myReshape);
  502.     glutDisplayFunc (display);
  503.     glutMainLoop();
  504.     return 0;             /* ANSI C requires main to return int. */
  505. }
  506.