home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / demos / modeler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.4 KB  |  388 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*  Rendering tutorial using GLUT library
  19.  */
  20. #include <GL/gl.h>
  21. #include <GL/glu.h>
  22. #include <GL/glut.h>
  23.  
  24. /* Function Prototypes */
  25. void progInit( char * );
  26. void drawScene( void );
  27. void resize( int, int );
  28. void mouse( int, int, int, int );
  29. void motion( int, int );
  30. void colorMenu( int);
  31. void shapeMenu( int);
  32. void mainMenu( int);
  33. void drawCrossHairs( void);
  34. void markPoints(void);
  35. void renderString( const char *s);
  36. void printStatus( void);
  37.  
  38.  
  39. typedef struct {
  40.     GLfloat color[3];
  41.     GLint coords[2];
  42. } Point;
  43.  
  44. enum Color { RED = 1, GREEN, BLUE, WHITE };
  45.  
  46. static Point points[1000];
  47. static int winW, winH, mouseX, mouseY, numPts = 0;
  48. static int currColor = RED;
  49. static GLenum currMode = GL_POINTS;
  50.  
  51. void
  52. main( int argc, char *argv[] )
  53. {
  54.     glutInit( &argc, argv);
  55.     progInit( argv[0] );
  56.     glutMainLoop();
  57. }
  58.  
  59. void
  60. progInit( char *progname )
  61. {
  62.     int colorMenuID, shapeMenuID;
  63.  
  64.     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  65.     glutInitWindowSize( 800, 600);
  66.     glutCreateWindow( progname );
  67.     glutDisplayFunc( drawScene );
  68.     glutReshapeFunc( resize );
  69.     glutMouseFunc( mouse );
  70.     glutPassiveMotionFunc( motion );
  71.  
  72.     colorMenuID = glutCreateMenu(colorMenu);
  73.     glutAddMenuEntry( "Red", RED);
  74.     glutAddMenuEntry( "Green", GREEN);
  75.     glutAddMenuEntry( "Blue", BLUE);
  76.     glutAddMenuEntry( "White", WHITE);
  77.     
  78.     shapeMenuID = glutCreateMenu(shapeMenu);
  79.     glutAddMenuEntry( "Points", 1);
  80.     glutAddMenuEntry( "Lines", 2);
  81.     glutAddMenuEntry( "Line strip", 3);
  82.     glutAddMenuEntry( "Line loop", 4);
  83.     glutAddMenuEntry( "Polygon", 5);
  84.     glutAddMenuEntry( "Triangles", 6);
  85.     glutAddMenuEntry( "Quads", 7);
  86.     glutAddMenuEntry( "Triangle strip", 8);
  87.     glutAddMenuEntry( "Triangle fan", 9);
  88.     glutAddMenuEntry( "Quad strip", 10);
  89.     
  90.     glutCreateMenu(mainMenu);
  91.     glutAddSubMenu( "Set current color", colorMenuID);
  92.     glutAddSubMenu( "Generate primitive", shapeMenuID);
  93.     glutAddMenuEntry( "Start over", 1);
  94.     glutAddMenuEntry( "Undo last vertex", 2);
  95.     glutAddMenuEntry( "Use flat shading", 3);
  96.     glutAddMenuEntry( "Exit", 1000);
  97.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  98.  
  99.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  100.     glPointSize(3.0);
  101.     glLineWidth(2.0);
  102.  
  103.     /* For accurate rasterization */
  104.     glTranslatef(0.375, 0.375, 0.0);
  105. }
  106.  
  107. void resize( int w, int h )
  108. {
  109.     winW = w;
  110.     winH = h;
  111.  
  112.     glViewport(0, 0, w, h);
  113.  
  114.     glMatrixMode(GL_PROJECTION);
  115.     glLoadIdentity();
  116.     gluOrtho2D( 0, w, 0, h);
  117.     glMatrixMode(GL_MODELVIEW);
  118. }
  119.     
  120.     
  121. void mouse( int button, int state, int x, int y )
  122. {
  123.     if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
  124.         return;
  125.  
  126.     switch(currColor) {
  127.         case RED:
  128.             points[numPts].color[0] = 1.0;
  129.             points[numPts].color[1] = 0.0;
  130.             points[numPts].color[2] = 0.0;
  131.             break;
  132.         case GREEN:
  133.             points[numPts].color[0] = 0.0;
  134.             points[numPts].color[1] = 1.0;
  135.             points[numPts].color[2] = 0.0;
  136.             break;
  137.         case BLUE:
  138.             points[numPts].color[0] = 0.0;
  139.             points[numPts].color[1] = 0.0;
  140.             points[numPts].color[2] = 1.0;
  141.             break;
  142.         case WHITE:
  143.             points[numPts].color[0] = 1.0;
  144.             points[numPts].color[1] = 1.0;
  145.             points[numPts].color[2] = 1.0;
  146.             break;
  147.     }
  148.     points[numPts].coords[0] = x;
  149.     points[numPts].coords[1] = winH - y;
  150.     numPts++;
  151.  
  152.     glutPostRedisplay();
  153. }
  154.  
  155. void motion( int x, int y )
  156. {
  157.     mouseX = x;
  158.     mouseY = winH - y;
  159.  
  160.     glutPostRedisplay();
  161. }
  162.  
  163.  
  164. void colorMenu( int value)
  165. {
  166.     currColor = value;
  167.     glutPostRedisplay();
  168. }
  169.  
  170. void shapeMenu( int value)
  171. {
  172.     switch(value) {
  173.         case 1:  currMode = GL_POINTS; break;
  174.         case 2:  currMode = GL_LINES; break;
  175.         case 3:  currMode = GL_LINE_STRIP; break;
  176.         case 4:  currMode = GL_LINE_LOOP; break;
  177.         case 5:  currMode = GL_POLYGON; break;
  178.         case 6:  currMode = GL_TRIANGLES; break;
  179.         case 7:  currMode = GL_QUADS; break;
  180.         case 8:  currMode = GL_TRIANGLE_STRIP; break;
  181.         case 9:  currMode = GL_TRIANGLE_FAN; break;
  182.         case 10: currMode = GL_QUAD_STRIP; break;
  183.         default: currMode = GL_POINTS; break;
  184.     }
  185.     glutPostRedisplay();
  186. }
  187.  
  188. void mainMenu( int value)
  189. {
  190.     int numItems = glutGet(GLUT_MENU_NUM_ITEMS);
  191.  
  192.     switch(value) {
  193.         case 1:  numPts = 0; break;
  194.         case 2:  numPts--; break;
  195.         case 3:
  196.              glShadeModel(GL_FLAT);
  197.              glutChangeToMenuEntry(numItems - 1, 
  198.                     "Use smooth shading",
  199.                     4);
  200.              break;
  201.         case 4:  
  202.              glShadeModel(GL_SMOOTH);
  203.              glutChangeToMenuEntry(numItems - 1, 
  204.                     "Use flat shading",
  205.                     3);
  206.              break;
  207.         case 1000:
  208.              exit(0);
  209.              break;
  210.  
  211.         default: break;
  212.     }
  213.     glutPostRedisplay();
  214. }
  215.  
  216. #if 0
  217. void drawGrid( void)
  218. {
  219.     int i, j;
  220.     GLfloat grey[] = { 0.6, 0.6, 0.6 };
  221.  
  222.     glPushAttrib(GL_CURRENT_BIT | GL_LINE_BIT);
  223.     glColor3fv(grey);
  224.     glLineWidth(1.0);
  225.     glBegin(GL_LINES);
  226.  
  227.         for (i = 0; i < winW; i += 30) {
  228.         glVertex2i(i, 0);
  229.         glVertex2i(i, winH);
  230.         }
  231.         for (j = 0; j < winH; j += 30) {
  232.         glVertex2i(0, j);
  233.         glVertex2i(winW, j);
  234.         }
  235.     glEnd();
  236.     glPopAttrib();
  237. }
  238. #endif
  239.  
  240. void drawCrossHairs( void)
  241. {
  242.     GLfloat red[] = { 1.0, 0.0, 0.0 };
  243.  
  244.     glPushAttrib(GL_ALL_ATTRIB_BITS);
  245.  
  246.     glEnable(GL_BLEND);
  247.     glBlendEquationEXT(GL_LOGIC_OP);
  248.     glLogicOp(GL_XOR);
  249.  
  250.     glColor3fv(red);
  251.     glLineWidth(1.0);
  252.     glBegin(GL_LINES);
  253.         glVertex2i(0, mouseY);
  254.         glVertex2i(winW, mouseY);
  255.         glVertex2i(mouseX, 0);
  256.         glVertex2i(mouseX, winH);
  257.     glEnd();
  258.  
  259.     glPopAttrib();
  260. }
  261.  
  262. void markPoints( void)
  263. {
  264.     int i, x, y;
  265.     char str[5];
  266.  
  267.     glPushAttrib(GL_ALL_ATTRIB_BITS);
  268.  
  269.     glEnable(GL_BLEND);
  270.     glBlendEquationEXT(GL_LOGIC_OP);
  271.     glLogicOp(GL_XOR);
  272.  
  273.     /* Label each vertex */
  274.  
  275.     for (i = 0; i < numPts; i++) {
  276.         glColor3fv(points[i].color);            
  277.         x = points[i].coords[0];
  278.         y = points[i].coords[1];
  279.         glRasterPos2i(x - 26, y - 4);
  280.         sprintf(str, "v%d", i);
  281.         renderString(str);
  282.     }
  283.  
  284.     /* If not drawing points, put a little 'x' at each vertex */
  285.  
  286.     if (currMode != GL_POINTS) {
  287.         glLineWidth(1.5);
  288.         glBegin(GL_LINES);    
  289.             for (i = 0; i < numPts; i++) {
  290.             glColor3fv(points[i].color);            
  291.             x = points[i].coords[0];
  292.             y = points[i].coords[1];
  293.             glVertex2i(x - 5, y - 5);
  294.             glVertex2i(x + 5, y + 5);
  295.             glVertex2i(x - 5, y + 5);
  296.             glVertex2i(x + 5, y - 5);
  297.             }
  298.         glEnd();
  299.     }
  300.     glPopAttrib();
  301. }
  302.  
  303. void renderString( const char *s)
  304. {
  305.     while (*s != '\0')
  306.       glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *s++);
  307. }
  308.  
  309.  
  310. void printStatus( void)
  311. {
  312.     GLint shadeModel;
  313.  
  314.     glPushAttrib(GL_CURRENT_BIT | GL_LINE_BIT);
  315.  
  316.         glColor3f(1.0, 1.0, 0.0);
  317.         glRasterPos2f(5.0, 35.0);
  318.         renderString( "Drawing ");
  319.     switch(currMode) {
  320.         case GL_POINTS:  
  321.                 renderString("GL_POINTS"); break;
  322.         case GL_LINES: 
  323.                 renderString("GL_LINES"); break;
  324.         case GL_LINE_STRIP:  
  325.                 renderString("GL_LINE_STRIP"); break;
  326.         case GL_LINE_LOOP:  
  327.                 renderString("GL_LINE_LOOP"); break;
  328.         case GL_POLYGON:  
  329.                 renderString("GL_POLYGON"); break;
  330.         case GL_TRIANGLES:  
  331.                 renderString("GL_TRIANGLES"); break;
  332.         case GL_QUADS:  
  333.                 renderString("GL_QUADS"); break;
  334.         case GL_TRIANGLE_STRIP:  
  335.                 renderString("GL_TRIANGLE_STRIP"); break;
  336.         case GL_TRIANGLE_FAN:  
  337.                 renderString("GL_TRIANGLE_FAN"); break;
  338.         case GL_QUAD_STRIP: 
  339.                 renderString("GL_QUAD_STRIP"); break;
  340.         default: break;
  341.     }
  342.     
  343.         glRasterPos2f(5.0, 20.0);
  344.         renderString( "Current color is ");
  345.     switch(currColor) {
  346.         case RED:  renderString("red"); break;
  347.         case GREEN:  renderString("green"); break;
  348.         case BLUE:  renderString("blue"); break;
  349.         case WHITE:  renderString("white"); break;
  350.         default: break;
  351.     }
  352.         glRasterPos2f(5.0, 5.0);
  353.     glGetIntegerv(GL_SHADE_MODEL, &shadeModel);
  354.     switch(shadeModel) {
  355.         case GL_FLAT:  renderString("Flat shaded"); break;
  356.         case GL_SMOOTH:  renderString("Smooth shaded"); break;
  357.         default: break;
  358.     }
  359.     glColor3f(1.0, 1.0, 1.0);
  360.         glRasterPos2f(winW - 313, 20.0);
  361.         renderString( "Left mouse button creates vertex");
  362.         glRasterPos2f(winW - 313, 5.0);
  363.         renderString( "Right mouse button pops up menu");
  364.     glPopAttrib();
  365. }
  366.  
  367.  
  368. GLvoid
  369. drawScene( void )
  370. {
  371.     int i;
  372.  
  373.     glClear( GL_COLOR_BUFFER_BIT );
  374.  
  375.     glBegin(currMode);
  376.         for (i = 0; i < numPts; i++) {
  377.         glColor3fv(points[i].color);            
  378.         glVertex2iv(points[i].coords);            
  379.         }
  380.     glEnd();
  381.  
  382.     drawCrossHairs();
  383.     markPoints();
  384.     printStatus();
  385.  
  386.     glutSwapBuffers();
  387. }
  388.