home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / sgi_extensions / multisample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  10.9 KB  |  419 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. /* multisample.c - shows how to use multisampling to antialias
  19.  *
  20.  *  Escape key              - exit the program 
  21.  *  Left Mouse Button        - change incidence and azimuth angles
  22.  *  Middle Mousebutton        - change the twist angle based on
  23.  *                  horizontal mouse movement
  24.  *  Right Mousebutton        - zoom in and out based on vertical
  25.  *                  mouse movement
  26.  *  m Key            - toggle multisampling on / off
  27.  *  p Key            - toggle polygon offset on / off
  28.  *  R key            - reset view
  29.  */
  30. #include <math.h>
  31. #include <stdio.h>
  32. #include <GL/gl.h>
  33. #include <GL/glu.h>
  34. #include <GL/glut.h>
  35.  
  36. /*  Function Prototypes  */
  37.  
  38. GLvoid  initgfx( GLvoid );
  39. GLvoid  drawScene( GLvoid );
  40. GLvoid  reshape( GLsizei, GLsizei );
  41. GLvoid  keyboard( GLubyte, GLint, GLint );
  42. GLvoid  mouse( GLint, GLint, GLint, GLint );
  43. GLvoid  motion( GLint, GLint );
  44.  
  45. void resetView( GLvoid );
  46. void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
  47. void printHelp( char * );
  48.  
  49. void polygonOffset( GLvoid );
  50. void multisampling( GLvoid );
  51.  
  52. void createTorusLists(void);
  53.  
  54. /* Global Definitions */
  55.  
  56. #define KEY_ESC    27    /* ascii value for the escape key */
  57.  
  58. /* Global Variables */
  59.  
  60. static GLint    action;
  61. static enum    actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  62.  
  63. static GLint    xStart = 0, yStart = 0;
  64. static GLfloat    near, far, distance, twistAngle, incAngle, azimAngle;
  65.  
  66. static GLfloat  offsetscale = 0.5;
  67. static GLfloat     offsetbias = 0.002;
  68.  
  69. static GLuint      surfaceList, gridList;
  70.  
  71. static GLboolean multisample_support = GL_TRUE;
  72.  
  73. void
  74. main( int argc, char *argv[] )
  75. {
  76.     GLsizei            width, height;
  77.  
  78.     glutInit( &argc, argv );
  79.  
  80.     width = glutGet(GLUT_SCREEN_WIDTH); 
  81.     height = glutGet(GLUT_SCREEN_HEIGHT);
  82.     glutInitWindowPosition( width / 4, height / 4); 
  83.     glutInitWindowSize( width / 2, height / 2 );
  84.     glutInitDisplayMode( GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH|GLUT_MULTISAMPLE );
  85.     glutCreateWindow( argv[0] );
  86.     
  87.     initgfx();
  88.  
  89.     glutMouseFunc( mouse );
  90.     glutMotionFunc( motion );
  91.     glutKeyboardFunc( keyboard );
  92.     glutReshapeFunc( reshape );
  93.     glutDisplayFunc( drawScene ); 
  94.  
  95.     printHelp( argv[0] );
  96.  
  97.      glutMainLoop();
  98. }
  99.  
  100. void
  101. printHelp( char *progname )
  102. {
  103.     fprintf(stdout, "\n%s - demonstrate polygon offset "
  104.         "extension\n\n"
  105.         "Axes: X - red, Y - green, Z - blue\n\n"
  106.         "Left Mousebutton    - move eye position\n"
  107.         "Middle Mousebutton    - change twist angle\n"
  108.         "Right Mousebutton    - move up / down to zoom in / out\n"
  109.         "Escape Key        - exit the program\n"
  110.         "m Key            - toggle multisampling on / off\n"
  111.         "p Key            - toggle polygon offset on / off\n"
  112.         "R Key            - reset view\n",
  113.         progname);
  114. }
  115.  
  116. GLvoid
  117. initgfx( GLvoid )
  118. {
  119.     GLdouble        maxObjectSize;
  120.     float             lmodel_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
  121.  
  122.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  123.     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  124.  
  125.     glEnable(GL_LIGHTING);
  126.     glEnable(GL_LIGHT0);
  127.  
  128.     glClearColor( 0.0, 0.0, 0.0, 0.0 );
  129.     glEnable( GL_DEPTH_TEST );
  130.  
  131.     if (!glutExtensionSupported("GL_SGIS_multisample")) {
  132.         fprintf(stderr,
  133.            "\nGL_SGIS_multisample not supported on this machine\n");
  134.         multisample_support = GL_FALSE;
  135.     }
  136.     if (!glutExtensionSupported("GL_EXT_polygon_offset")) {
  137.         fprintf( stderr,
  138.            "GL_EXT_polygon_offset not supported on this machine\n");
  139.     }
  140. #ifdef GL_EXT_polygon_offset
  141.     glPolygonOffsetEXT( offsetscale, offsetbias );
  142. #endif
  143.     createTorusLists();
  144.  
  145.     /* Set up near and far, and determine the viewing distance */
  146.     maxObjectSize = 4;
  147.     near = 5.0;
  148.     far = near + 2*maxObjectSize; 
  149.     resetView();
  150. }
  151.  
  152. GLvoid 
  153. keyboard( GLubyte key, GLint x, GLint y )
  154. {
  155.     switch (key) {
  156.     case 'm':
  157.         multisampling();
  158.         glutPostRedisplay();
  159.         break;
  160.     case 'p':
  161.         polygonOffset();
  162.         glutPostRedisplay();
  163.         break;
  164.     case 'R':
  165.         resetView();
  166.         glutPostRedisplay();
  167.         break;
  168.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  169.         exit(0);
  170.     }
  171. }
  172.  
  173. GLvoid 
  174. mouse( GLint button, GLint state, GLint x, GLint y )
  175. {
  176.     if (state == GLUT_DOWN) {
  177.         switch (button) {
  178.         case GLUT_LEFT_BUTTON:
  179.             action = MOVE_EYE;
  180.             break;
  181.         case GLUT_MIDDLE_BUTTON:
  182.             action = TWIST_EYE;
  183.             break;
  184.         case GLUT_RIGHT_BUTTON:
  185.             action = ZOOM;
  186.             break;
  187.         }
  188.  
  189.         /* Update the saved mouse position */
  190.         xStart = x;
  191.         yStart = y;
  192.     } else {
  193.         action = MOVE_NONE;
  194.     }
  195.  
  196. }
  197.  
  198. GLvoid
  199. motion( GLint x, GLint y )
  200. {
  201.     switch (action) {
  202.     case MOVE_EYE:
  203.         /* Adjust the eye position based on the mouse position */
  204.         azimAngle += (GLdouble) (x - xStart);
  205.         incAngle -= (GLdouble) (y - yStart);
  206.         break;
  207.     case TWIST_EYE:
  208.         /* Adjust the eye twist based on the mouse position */
  209.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  210.         break;
  211.     case ZOOM:
  212.         /* Adjust the eye distance based on the mouse position */
  213.         distance -= (GLdouble) (y - yStart)/10.0;
  214.         break;
  215.     default:
  216.         printf("unknown action %d\n", action);
  217.     }
  218.     
  219.     /* Update the stored mouse position for later use */
  220.     xStart = x;
  221.     yStart = y;
  222.  
  223.     glutPostRedisplay();
  224. }
  225.  
  226. GLvoid
  227. reshape( GLsizei width, GLsizei height )
  228. {
  229.     GLdouble            aspect;
  230.  
  231.     glViewport( 0, 0, width, height );
  232.  
  233.     glMatrixMode( GL_PROJECTION );
  234.     glLoadIdentity();
  235.     aspect = (GLdouble) width / (GLdouble) height;
  236.     gluPerspective( 45.0, aspect, near, far );
  237.     glMatrixMode( GL_MODELVIEW );
  238. }
  239.  
  240. GLvoid
  241. resetView( GLvoid )
  242. {
  243.     distance = near + (far - near) / 2.0;
  244.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  245.     incAngle = 0.0;
  246.     azimAngle = 0.0;
  247. }
  248.  
  249. void
  250. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  251.             GLfloat twist)
  252. {
  253.     glTranslatef( 0.0, 0.0, -distance);
  254.     glRotatef( -twist, 0.0, 0.0, 1.0);
  255.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  256.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  257. }
  258.  
  259. GLvoid  
  260. polygonOffset( GLvoid )
  261. {
  262.     static GLboolean   polyOffsetFlag = GL_FALSE;
  263.  
  264. #ifdef GL_EXT_polygon_offset
  265.     polyOffsetFlag = !polyOffsetFlag;
  266.  
  267.     /* Toggle polygon offset */
  268.     if ( polyOffsetFlag == GL_TRUE ) {
  269.         glEnable(GL_POLYGON_OFFSET_EXT);
  270.     } else {
  271.         glDisable(GL_POLYGON_OFFSET_EXT);
  272.     }
  273.     printf("polygon offset %s\n", 
  274.         (polyOffsetFlag?"enabled":"disabled"));
  275. #else
  276.     printf("GL_EXT_polygon_offset not supported\n"); 
  277. #endif
  278. }
  279.  
  280. GLvoid  
  281. multisampling( GLvoid )
  282. {
  283.     static GLboolean   multisampleFlag = GL_FALSE;
  284.  
  285. #ifdef    GL_SGIS_multisample
  286.     if ( multisample_support ) {
  287.         multisampleFlag = !multisampleFlag;
  288.  
  289.         /* Toggle multisampling */
  290.         if ( multisampleFlag == GL_TRUE ) {
  291.             glEnable( GL_MULTISAMPLE_SGIS );
  292.         } else {
  293.             glDisable( GL_MULTISAMPLE_SGIS );
  294.         }
  295.         printf("multisampling %s\n", 
  296.             (multisampleFlag ?"enabled":"disabled"));
  297.     } else 
  298.         printf("GL_SGIS_multisample not supported\n"); 
  299. #else
  300.     printf("GL_SGIS_multisample not supported\n"); 
  301. #endif
  302. }
  303.  
  304. GLvoid
  305. drawScene( GLvoid )
  306. {
  307.     /* Clear the depth buffer in addition to the window */
  308.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  309.  
  310.     glPushMatrix();
  311.         polarView( distance, azimAngle, incAngle, twistAngle );
  312.         XYZaxes();
  313.  
  314.         glCallList( surfaceList );
  315.         glCallList( gridList );
  316.     glPopMatrix();
  317.     glutSwapBuffers();
  318.  
  319.     checkError("drawScene");
  320. }
  321.  
  322. void gridmaterials(void)
  323. {
  324.     static float front_mat_diffuse[] = { 1.0, 1.0, 0.4, 1.0 };
  325.     static float front_mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
  326.     static float back_mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
  327.     static float back_mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
  328.  
  329.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  330.     glMaterialfv(GL_FRONT, GL_AMBIENT, front_mat_ambient);
  331.     glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  332.     glMaterialfv(GL_BACK, GL_AMBIENT, back_mat_ambient);
  333. }
  334.  
  335. void surfacematerials(void)
  336. {
  337.     static float front_mat_diffuse[] = { 0.2, 0.7, 0.7, 1.0 };
  338.     static float front_mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
  339.     static float back_mat_diffuse[] = { 1.0, 1.0, 0.2, 1.0 };
  340.     static float back_mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
  341.  
  342.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  343.     glMaterialfv(GL_FRONT, GL_AMBIENT, front_mat_ambient);
  344.     glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  345.     glMaterialfv(GL_BACK, GL_AMBIENT, back_mat_ambient);
  346. }
  347.  
  348. /*
  349.  * Control points of a torus in NURBS form.  Can be rendered using
  350.  * the GLU NURBS routines.
  351.  */
  352. static GLfloat torusnurbpts[] = {
  353.          4.0, 0.0, 0.0, 4.0, 2.0, 0.0, 1.0, 2.0, 4.0, 0.0, 1.0, 2.0,
  354.          8.0, 0.0, 0.0, 4.0, 4.0, 0.0,-1.0, 2.0, 2.0, 0.0,-1.0, 2.0,
  355.          4.0, 0.0, 0.0, 4.0, 2.0,-2.0, 0.0, 2.0, 1.0,-1.0, 0.5, 1.0,
  356.          2.0,-2.0, 0.5, 1.0, 4.0,-4.0, 0.0, 2.0, 2.0,-2.0,-0.5, 1.0,
  357.          1.0,-1.0,-0.5, 1.0, 2.0,-2.0, 0.0, 2.0,-2.0,-2.0, 0.0, 2.0,
  358.         -1.0,-1.0, 0.5, 1.0,-2.0,-2.0, 0.5, 1.0,-4.0,-4.0, 0.0, 2.0,
  359.         -2.0,-2.0,-0.5, 1.0,-1.0,-1.0,-0.5, 1.0,-2.0,-2.0, 0.0, 2.0,
  360.         -4.0, 0.0, 0.0, 4.0,-2.0, 0.0, 1.0, 2.0,-4.0, 0.0, 1.0, 2.0,
  361.         -8.0, 0.0, 0.0, 4.0,-4.0, 0.0,-1.0, 2.0,-2.0, 0.0,-1.0, 2.0,
  362.         -4.0, 0.0, 0.0, 4.0,-2.0, 2.0, 0.0, 2.0,-1.0, 1.0, 0.5, 1.0,
  363.         -2.0, 2.0, 0.5, 1.0,-4.0, 4.0, 0.0, 2.0,-2.0, 2.0,-0.5, 1.0,
  364.         -1.0, 1.0,-0.5, 1.0,-2.0, 2.0, 0.0, 2.0, 2.0, 2.0, 0.0, 2.0,
  365.          1.0, 1.0, 0.5, 1.0, 2.0, 2.0, 0.5, 1.0, 4.0, 4.0, 0.0, 2.0,
  366.          2.0, 2.0,-0.5, 1.0, 1.0, 1.0,-0.5, 1.0, 2.0, 2.0, 0.0, 2.0,
  367.          4.0, 0.0, 0.0, 4.0, 2.0, 0.0, 1.0, 2.0, 4.0, 0.0, 1.0, 2.0,
  368.          8.0, 0.0, 0.0, 4.0, 4.0, 0.0,-1.0, 2.0, 2.0, 0.0,-1.0, 2.0,
  369.          4.0, 0.0, 0.0, 4.0,
  370. };
  371.  
  372. float circleknots[] = { 0.0, 0.0, 0.0, 0.25, 0.50, 0.50, 0.75, 1.0, 1.0, 1.0 };
  373.  
  374. void 
  375. createTorusLists(void)
  376. {
  377.     GLUnurbsObj *nobj;
  378.     int usegments=4;
  379.     int vsegments=4;
  380.  
  381.     GLfloat modelView[16], projection[16]; 
  382.     GLint   viewport[4];
  383.  
  384.     glEnable(GL_AUTO_NORMAL);
  385.  
  386.     surfaceList = glGenLists(1);
  387.     gridList = glGenLists(1);
  388.  
  389.     nobj = gluNewNurbsRenderer();
  390.  
  391.     gluNurbsProperty(nobj, GLU_SAMPLING_METHOD, GLU_DOMAIN_DISTANCE );
  392.     gluNurbsProperty(nobj, GLU_U_STEP, (usegments-1)*4 );
  393.     gluNurbsProperty(nobj, GLU_V_STEP, (vsegments-1)*4 );
  394.     gluNurbsProperty(nobj, GLU_AUTO_LOAD_MATRIX, GL_FALSE);
  395.  
  396.     glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
  397.     glGetFloatv(GL_PROJECTION_MATRIX, projection);
  398.     glGetIntegerv(GL_VIEWPORT, viewport);
  399.     gluLoadSamplingMatrices(nobj, modelView, projection, viewport);
  400.  
  401.     gluNurbsProperty(nobj, GLU_DISPLAY_MODE, GLU_FILL);
  402.     glNewList(surfaceList, GL_COMPILE);
  403.         surfacematerials();
  404.         gluBeginSurface(nobj);
  405.             gluNurbsSurface(nobj, 10, circleknots, 10, circleknots,
  406.                 4, 28, torusnurbpts, 3, 3, GL_MAP2_VERTEX_4);
  407.         gluEndSurface(nobj);
  408.     glEndList();
  409.  
  410.     gluNurbsProperty(nobj, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
  411.     glNewList(gridList, GL_COMPILE);
  412.         gridmaterials();
  413.         gluBeginSurface(nobj);
  414.         gluNurbsSurface(nobj, 10, circleknots, 10, circleknots,
  415.                 4, 28, torusnurbpts, 3, 3, GL_MAP2_VERTEX_4);
  416.         gluEndSurface(nobj);
  417.     glEndList();
  418. }
  419.