Guest User

NVidia Optimus Driver - TEST

a guest
Dec 30th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.90 KB | None | 0 0
  1. /* Optimus driver test */
  2.  
  3. //http://developer.download.nvidia.com/devzone/devcenter/gamegraphics/files/OptimusRenderingPolicies.pdf
  4. typedef unsigned long DWORD;
  5.  
  6. extern "C" {
  7.     _declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
  8. }
  9.  
  10. #include "stdafx.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <math.h>
  15. #include <glut.h>
  16.  
  17. static int useRGB = 1;
  18. static int useLighting = 1;
  19. static int useFog = 0;
  20. static int useDB = 1;
  21. static int useLogo = 0;
  22. static int useQuads = 1;
  23.  
  24. static int tick = -1;
  25. static int moving = 1;
  26.  
  27. #define GREY    0
  28. #define RED 1
  29. #define GREEN   2
  30. #define BLUE    3
  31. #define CYAN    4
  32. #define MAGENTA 5
  33. #define YELLOW  6
  34. #define BLACK   7
  35.  
  36. static float materialColor[8][4] =
  37. {
  38.     { 0.8, 0.8, 0.8, 1.0 },
  39.     { 0.8, 0.0, 0.0, 1.0 },
  40.     { 0.0, 0.8, 0.0, 1.0 },
  41.     { 0.0, 0.0, 0.8, 1.0 },
  42.     { 0.0, 0.8, 0.8, 1.0 },
  43.     { 0.8, 0.0, 0.8, 1.0 },
  44.     { 0.8, 0.8, 0.0, 1.0 },
  45.     { 0.0, 0.0, 0.0, 0.6 },
  46. };
  47.  
  48. static float lightPos[4] =
  49. { 2.0, 4.0, 2.0, 1.0 };
  50. #if 0
  51. static float lightDir[4] =
  52. { -2.0, -4.0, -2.0, 1.0 };
  53. #endif
  54. static float lightAmb[4] =
  55. { 0.2, 0.2, 0.2, 1.0 };
  56. static float lightDiff[4] =
  57. { 0.8, 0.8, 0.8, 1.0 };
  58. static float lightSpec[4] =
  59. { 0.4, 0.4, 0.4, 1.0 };
  60.  
  61. static float groundPlane[4] =
  62. { 0.0, 1.0, 0.0, 1.499 };
  63. static float backPlane[4] =
  64. { 0.0, 0.0, 1.0, 0.899 };
  65.  
  66. static float fogColor[4] =
  67. { 0.0, 0.0, 0.0, 0.0 };
  68. static float fogIndex[1] =
  69. { 0.0 };
  70.  
  71. static unsigned char shadowPattern[128] =
  72. {
  73.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,  /* 50% Grey */
  74.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  75.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  76.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  77.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  78.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  79.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  80.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  81.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  82.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  83.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  84.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  85.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  86.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  87.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55,
  88.     0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55
  89. };
  90.  
  91. static unsigned char sgiPattern[128] =
  92. {
  93.     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,  /* SGI Logo */
  94.     0xff, 0xbd, 0xff, 0x83, 0xff, 0x5a, 0xff, 0xef,
  95.     0xfe, 0xdb, 0x7f, 0xef, 0xfd, 0xdb, 0xbf, 0xef,
  96.     0xfb, 0xdb, 0xdf, 0xef, 0xf7, 0xdb, 0xef, 0xef,
  97.     0xfb, 0xdb, 0xdf, 0xef, 0xfd, 0xdb, 0xbf, 0x83,
  98.     0xce, 0xdb, 0x73, 0xff, 0xb7, 0x5a, 0xed, 0xff,
  99.     0xbb, 0xdb, 0xdd, 0xc7, 0xbd, 0xdb, 0xbd, 0xbb,
  100.     0xbe, 0xbd, 0x7d, 0xbb, 0xbf, 0x7e, 0xfd, 0xb3,
  101.     0xbe, 0xe7, 0x7d, 0xbf, 0xbd, 0xdb, 0xbd, 0xbf,
  102.     0xbb, 0xbd, 0xdd, 0xbb, 0xb7, 0x7e, 0xed, 0xc7,
  103.     0xce, 0xdb, 0x73, 0xff, 0xfd, 0xdb, 0xbf, 0xff,
  104.     0xfb, 0xdb, 0xdf, 0x87, 0xf7, 0xdb, 0xef, 0xfb,
  105.     0xf7, 0xdb, 0xef, 0xfb, 0xfb, 0xdb, 0xdf, 0xfb,
  106.     0xfd, 0xdb, 0xbf, 0xc7, 0xfe, 0xdb, 0x7f, 0xbf,
  107.     0xff, 0x5a, 0xff, 0xbf, 0xff, 0xbd, 0xff, 0xc3,
  108.     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  109. };
  110.  
  111. static float cube_vertexes[6][4][4] =
  112. {
  113.     {
  114.         { -1.0, -1.0, -1.0, 1.0 },
  115.         { -1.0, -1.0, 1.0, 1.0 },
  116.         { -1.0, 1.0, 1.0, 1.0 },
  117.         { -1.0, 1.0, -1.0, 1.0 } },
  118.  
  119.         {
  120.             { 1.0, 1.0, 1.0, 1.0 },
  121.             { 1.0, -1.0, 1.0, 1.0 },
  122.             { 1.0, -1.0, -1.0, 1.0 },
  123.             { 1.0, 1.0, -1.0, 1.0 } },
  124.  
  125.             {
  126.                 { -1.0, -1.0, -1.0, 1.0 },
  127.                 { 1.0, -1.0, -1.0, 1.0 },
  128.                 { 1.0, -1.0, 1.0, 1.0 },
  129.                 { -1.0, -1.0, 1.0, 1.0 } },
  130.  
  131.                 {
  132.                     { 1.0, 1.0, 1.0, 1.0 },
  133.                     { 1.0, 1.0, -1.0, 1.0 },
  134.                     { -1.0, 1.0, -1.0, 1.0 },
  135.                     { -1.0, 1.0, 1.0, 1.0 } },
  136.  
  137.                     {
  138.                         { -1.0, -1.0, -1.0, 1.0 },
  139.                         { -1.0, 1.0, -1.0, 1.0 },
  140.                         { 1.0, 1.0, -1.0, 1.0 },
  141.                         { 1.0, -1.0, -1.0, 1.0 } },
  142.  
  143.                         {
  144.                             { 1.0, 1.0, 1.0, 1.0 },
  145.                             { -1.0, 1.0, 1.0, 1.0 },
  146.                             { -1.0, -1.0, 1.0, 1.0 },
  147.                             { 1.0, -1.0, 1.0, 1.0 } }
  148. };
  149.  
  150. static float cube_normals[6][4] =
  151. {
  152.     { -1.0, 0.0, 0.0, 0.0 },
  153.     { 1.0, 0.0, 0.0, 0.0 },
  154.     { 0.0, -1.0, 0.0, 0.0 },
  155.     { 0.0, 1.0, 0.0, 0.0 },
  156.     { 0.0, 0.0, -1.0, 0.0 },
  157.     { 0.0, 0.0, 1.0, 0.0 }
  158. };
  159.  
  160.  
  161. int frame = 0, time, timebase = 0;
  162.  
  163. static void
  164. usage(void)
  165. {
  166.     printf("\n");
  167.     printf("usage: scube [options]\n");
  168.     printf("\n");
  169.     printf("    display a spinning cube and its shadow\n");
  170.     printf("\n");
  171.     printf("  Options:\n");
  172.     printf("    -geometry  window size and location\n");
  173.     printf("    -c         toggle color index mode\n");
  174.     printf("    -l         toggle lighting\n");
  175.     printf("    -f         toggle fog\n");
  176.     printf("    -db        toggle double buffering\n");
  177.     printf("    -logo      toggle sgi logo for the shadow pattern\n");
  178.     printf("    -quads     toggle use of GL_QUADS to draw the checkerboard\n");
  179.     printf("\n");
  180. #ifndef EXIT_FAILURE    /* should be defined by ANSI C
  181.     <stdlib.h> */
  182. #define EXIT_FAILURE 1
  183. #endif
  184.     exit(EXIT_FAILURE);
  185. }
  186.  
  187.  
  188.  
  189. void
  190. buildColormap(void)
  191. {
  192.     if (useRGB) {
  193.         return;
  194.     }
  195.     else {
  196.         int mapSize = 1 << glutGet(GLUT_WINDOW_BUFFER_SIZE);
  197.         int rampSize = mapSize / 8;
  198.         int entry;
  199.         int i;
  200.  
  201.         for (entry = 0; entry < mapSize; ++entry) {
  202.             int hue = entry / rampSize;
  203.             GLfloat val = (entry % rampSize) * (1.0 / (rampSize - 1));
  204.             GLfloat red, green, blue;
  205.  
  206.             red = (hue == 0 || hue == 1 || hue == 5 || hue == 6) ? val : 0;
  207.             green = (hue == 0 || hue == 2 || hue == 4 || hue == 6) ? val : 0;
  208.             blue = (hue == 0 || hue == 3 || hue == 4 || hue == 5) ? val : 0;
  209.  
  210.             glutSetColor(entry, red, green, blue);
  211.         }
  212.  
  213.         for (i = 0; i < 8; ++i) {
  214.             materialColor[i][0] = i * rampSize + 0.2 * (rampSize - 1);
  215.             materialColor[i][1] = i * rampSize + 0.8 * (rampSize - 1);
  216.             materialColor[i][2] = i * rampSize + 1.0 * (rampSize - 1);
  217.             materialColor[i][3] = 0.0;
  218.         }
  219.  
  220.         fogIndex[0] = -0.2 * (rampSize - 1);
  221.     }
  222. }
  223.  
  224. static void
  225. setColor(int c)
  226. {
  227.     if (useLighting) {
  228.         if (useRGB) {
  229.             glMaterialfv(GL_FRONT_AND_BACK,
  230.                 GL_AMBIENT_AND_DIFFUSE, &materialColor[c][0]);
  231.         }
  232.         else {
  233.             glMaterialfv(GL_FRONT_AND_BACK,
  234.                 GL_COLOR_INDEXES, &materialColor[c][0]);
  235.         }
  236.     }
  237.     else {
  238.         if (useRGB) {
  239.             glColor4fv(&materialColor[c][0]);
  240.         }
  241.         else {
  242.             glIndexf(materialColor[c][1]);
  243.         }
  244.     }
  245. }
  246.  
  247. static void
  248. drawCube(int color)
  249. {
  250.     int i;
  251.  
  252.     setColor(color);
  253.  
  254.     for (i = 0; i < 6; ++i) {
  255.         glNormal3fv(&cube_normals[i][0]);
  256.         glBegin(GL_POLYGON);
  257.         glVertex4fv(&cube_vertexes[i][0][0]);
  258.         glVertex4fv(&cube_vertexes[i][1][0]);
  259.         glVertex4fv(&cube_vertexes[i][2][0]);
  260.         glVertex4fv(&cube_vertexes[i][3][0]);
  261.         glEnd();
  262.     }
  263. }
  264.  
  265. static void
  266. drawCheck(int w, int h, int evenColor, int oddColor)
  267. {
  268.     static int initialized = 0;
  269.     static int usedLighting = 0;
  270.     static GLuint checklist = 0;
  271.  
  272.     if (!initialized || (usedLighting != useLighting)) {
  273.         static float square_normal[4] =
  274.         { 0.0, 0.0, 1.0, 0.0 };
  275.         static float square[4][4];
  276.         int i, j;
  277.  
  278.         if (!checklist) {
  279.             checklist = glGenLists(1);
  280.         }
  281.         glNewList(checklist, GL_COMPILE_AND_EXECUTE);
  282.  
  283.         if (useQuads) {
  284.             glNormal3fv(square_normal);
  285.             glBegin(GL_QUADS);
  286.         }
  287.         for (j = 0; j < h; ++j) {
  288.             for (i = 0; i < w; ++i) {
  289.                 square[0][0] = -1.0 + 2.0 / w * i;
  290.                 square[0][1] = -1.0 + 2.0 / h * (j + 1);
  291.                 square[0][2] = 0.0;
  292.                 square[0][3] = 1.0;
  293.  
  294.                 square[1][0] = -1.0 + 2.0 / w * i;
  295.                 square[1][1] = -1.0 + 2.0 / h * j;
  296.                 square[1][2] = 0.0;
  297.                 square[1][3] = 1.0;
  298.  
  299.                 square[2][0] = -1.0 + 2.0 / w * (i + 1);
  300.                 square[2][1] = -1.0 + 2.0 / h * j;
  301.                 square[2][2] = 0.0;
  302.                 square[2][3] = 1.0;
  303.  
  304.                 square[3][0] = -1.0 + 2.0 / w * (i + 1);
  305.                 square[3][1] = -1.0 + 2.0 / h * (j + 1);
  306.                 square[3][2] = 0.0;
  307.                 square[3][3] = 1.0;
  308.  
  309.                 if (i & 1 ^ j & 1) {
  310.                     setColor(oddColor);
  311.                 }
  312.                 else {
  313.                     setColor(evenColor);
  314.                 }
  315.  
  316.                 if (!useQuads) {
  317.                     glBegin(GL_POLYGON);
  318.                 }
  319.                 glVertex4fv(&square[0][0]);
  320.                 glVertex4fv(&square[1][0]);
  321.                 glVertex4fv(&square[2][0]);
  322.                 glVertex4fv(&square[3][0]);
  323.                 if (!useQuads) {
  324.                     glEnd();
  325.                 }
  326.             }
  327.         }
  328.  
  329.         if (useQuads) {
  330.             glEnd();
  331.         }
  332.         glEndList();
  333.  
  334.         initialized = 1;
  335.         usedLighting = useLighting;
  336.     }
  337.     else {
  338.         glCallList(checklist);
  339.     }
  340. }
  341.  
  342. static void
  343. myShadowMatrix(float ground[4], float light[4])
  344. {
  345.     float dot;
  346.     float shadowMat[4][4];
  347.  
  348.     dot = ground[0] * light[0] +
  349.         ground[1] * light[1] +
  350.         ground[2] * light[2] +
  351.         ground[3] * light[3];
  352.  
  353.     shadowMat[0][0] = dot - light[0] * ground[0];
  354.     shadowMat[1][0] = 0.0 - light[0] * ground[1];
  355.     shadowMat[2][0] = 0.0 - light[0] * ground[2];
  356.     shadowMat[3][0] = 0.0 - light[0] * ground[3];
  357.  
  358.     shadowMat[0][1] = 0.0 - light[1] * ground[0];
  359.     shadowMat[1][1] = dot - light[1] * ground[1];
  360.     shadowMat[2][1] = 0.0 - light[1] * ground[2];
  361.     shadowMat[3][1] = 0.0 - light[1] * ground[3];
  362.  
  363.     shadowMat[0][2] = 0.0 - light[2] * ground[0];
  364.     shadowMat[1][2] = 0.0 - light[2] * ground[1];
  365.     shadowMat[2][2] = dot - light[2] * ground[2];
  366.     shadowMat[3][2] = 0.0 - light[2] * ground[3];
  367.  
  368.     shadowMat[0][3] = 0.0 - light[3] * ground[0];
  369.     shadowMat[1][3] = 0.0 - light[3] * ground[1];
  370.     shadowMat[2][3] = 0.0 - light[3] * ground[2];
  371.     shadowMat[3][3] = dot - light[3] * ground[3];
  372.  
  373.     glMultMatrixf((const GLfloat *)shadowMat);
  374. }
  375.  
  376. static char *windowNameRGBDB = "shadow cube (OpenGL RGB DB)";
  377. static char *windowNameRGB = "shadow cube (OpenGL RGB)";
  378. static char *windowNameIndexDB = "shadow cube (OpenGL Index DB)";
  379. static char *windowNameIndex = "shadow cube (OpenGL Index)";
  380.  
  381. void
  382. idle(void)
  383. {
  384.     tick++;
  385.     if (tick >= 120) {
  386.         tick = 0;
  387.     }
  388.     glutPostRedisplay();
  389. }
  390.  
  391. /* ARGSUSED1 */
  392. void
  393. keyboard(unsigned char ch, int x, int y)
  394. {
  395.     switch (ch) {
  396.     case 27:             /* escape */
  397.         exit(0);
  398.         break;
  399.     case 'L':
  400.     case 'l':
  401.         useLighting = !useLighting;
  402.         useLighting ? glEnable(GL_LIGHTING) :
  403.             glDisable(GL_LIGHTING);
  404.         glutPostRedisplay();
  405.         break;
  406.     case 'F':
  407.     case 'f':
  408.         useFog = !useFog;
  409.         useFog ? glEnable(GL_FOG) : glDisable(GL_FOG);
  410.         glutPostRedisplay();
  411.         break;
  412.     case '1':
  413.         glFogf(GL_FOG_MODE, GL_LINEAR);
  414.         glutPostRedisplay();
  415.         break;
  416.     case '2':
  417.         glFogf(GL_FOG_MODE, GL_EXP);
  418.         glutPostRedisplay();
  419.         break;
  420.     case '3':
  421.         glFogf(GL_FOG_MODE, GL_EXP2);
  422.         glutPostRedisplay();
  423.         break;
  424.     case ' ':
  425.         if (!moving) {
  426.             idle();
  427.             glutPostRedisplay();
  428.         }
  429.     }
  430. }
  431.  
  432. void
  433. display(void)
  434. {
  435.     GLfloat cubeXform[4][4];
  436.  
  437.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  438.  
  439.     glPushMatrix();
  440.     glTranslatef(0.0, -1.5, 0.0);
  441.     glRotatef(-90.0, 1, 0, 0);
  442.     glScalef(2.0, 2.0, 2.0);
  443.  
  444.     drawCheck(6, 6, BLUE, YELLOW);  /* draw ground */
  445.     glPopMatrix();
  446.  
  447.     glPushMatrix();
  448.     glTranslatef(0.0, 0.0, -0.9);
  449.     glScalef(2.0, 2.0, 2.0);
  450.  
  451.     drawCheck(6, 6, BLUE, YELLOW);  /* draw back */
  452.     glPopMatrix();
  453.  
  454.     glPushMatrix();
  455.     glTranslatef(0.0, 0.2, 0.0);
  456.     glScalef(0.3, 0.3, 0.3);
  457.     glRotatef((360.0 / (30 * 1)) * tick, 1, 0, 0);
  458.     glRotatef((360.0 / (30 * 2)) * tick, 0, 1, 0);
  459.     glRotatef((360.0 / (30 * 4)) * tick, 0, 0, 1);
  460.     glScalef(1.0, 2.0, 1.0);
  461.     glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *)cubeXform);
  462.  
  463.     drawCube(RED);        /* draw cube */
  464.     glPopMatrix();
  465.  
  466.     glDepthMask(GL_FALSE);
  467.     if (useRGB) {
  468.         glEnable(GL_BLEND);
  469.     }
  470.     else {
  471.         glEnable(GL_POLYGON_STIPPLE);
  472.     }
  473.     if (useFog) {
  474.         glDisable(GL_FOG);
  475.     }
  476.     glPushMatrix();
  477.     myShadowMatrix(groundPlane, lightPos);
  478.     glTranslatef(0.0, 0.0, 2.0);
  479.     glMultMatrixf((const GLfloat *)cubeXform);
  480.  
  481.     drawCube(BLACK);      /* draw ground shadow */
  482.     glPopMatrix();
  483.  
  484.     glPushMatrix();
  485.     myShadowMatrix(backPlane, lightPos);
  486.     glTranslatef(0.0, 0.0, 2.0);
  487.     glMultMatrixf((const GLfloat *)cubeXform);
  488.  
  489.     drawCube(BLACK);      /* draw back shadow */
  490.     glPopMatrix();
  491.  
  492.     glDepthMask(GL_TRUE);
  493.     if (useRGB) {
  494.         glDisable(GL_BLEND);
  495.     }
  496.     else {
  497.         glDisable(GL_POLYGON_STIPPLE);
  498.     }
  499.     if (useFog) {
  500.         glEnable(GL_FOG);
  501.     }
  502.     if (useDB) {
  503.         glutSwapBuffers();
  504.     }
  505.     else {
  506.         glFlush();
  507.     }
  508.  
  509.  
  510.     frame++;
  511.     time = glutGet(GLUT_ELAPSED_TIME);
  512.  
  513.     if (time - timebase > 1000) {
  514.         float fps = frame*1000.0 / (time - timebase);
  515.         printf("fps: %5.3f\n", fps);
  516.         timebase = time;
  517.         frame = 0;
  518.     }
  519.  
  520.     //glPushMatrix();
  521.     //glLoadIdentity();
  522.     //setOrthographicProjection();
  523.     //renderBitmapString(30, 35, (void *)font, s);
  524.     //glPopMatrix();
  525.     //restorePerspectiveProjection();
  526.  
  527. }
  528.  
  529. void
  530. fog_select(int fog)
  531. {
  532.     glFogf(GL_FOG_MODE, fog);
  533.     glutPostRedisplay();
  534. }
  535.  
  536. void
  537. menu_select(int mode)
  538. {
  539.     switch (mode) {
  540.     case 1:
  541.         moving = 1;
  542.         glutIdleFunc(idle);
  543.         break;
  544.     case 2:
  545.         moving = 0;
  546.         glutIdleFunc(NULL);
  547.         break;
  548.     case 3:
  549.         useFog = !useFog;
  550.         useFog ? glEnable(GL_FOG) : glDisable(GL_FOG);
  551.         glutPostRedisplay();
  552.         break;
  553.     case 4:
  554.         useLighting = !useLighting;
  555.         useLighting ? glEnable(GL_LIGHTING) :
  556.             glDisable(GL_LIGHTING);
  557.         glutPostRedisplay();
  558.         break;
  559.     case 5:
  560.         exit(0);
  561.         break;
  562.     }
  563. }
  564.  
  565. void
  566. visible(int state)
  567. {
  568.     if (state == GLUT_VISIBLE) {
  569.         if (moving)
  570.             glutIdleFunc(idle);
  571.     }
  572.     else {
  573.         if (moving)
  574.             glutIdleFunc(NULL);
  575.     }
  576. }
  577.  
  578. int
  579. main(int argc, char **argv)
  580. {
  581.     int width = 350, height = 350;
  582.     int i;
  583.     char *name;
  584.     int fog_menu;
  585.  
  586.     glutInitWindowSize(width, height);
  587.     glutInit(&argc, argv);
  588.     /* process commmand line args */
  589.     for (i = 1; i < argc; ++i) {
  590.         if (!strcmp("-c", argv[i])) {
  591.             useRGB = !useRGB;
  592.         }
  593.         else if (!strcmp("-l", argv[i])) {
  594.             useLighting = !useLighting;
  595.         }
  596.         else if (!strcmp("-f", argv[i])) {
  597.             useFog = !useFog;
  598.         }
  599.         else if (!strcmp("-db", argv[i])) {
  600.             useDB = !useDB;
  601.         }
  602.         else if (!strcmp("-logo", argv[i])) {
  603.             useLogo = !useLogo;
  604.         }
  605.         else if (!strcmp("-quads", argv[i])) {
  606.             useQuads = !useQuads;
  607.         }
  608.         else {
  609.             usage();
  610.         }
  611.     }
  612.  
  613.     /* choose visual */
  614.     if (useRGB) {
  615.         if (useDB) {
  616.             glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  617.             name = windowNameRGBDB;
  618.         }
  619.         else {
  620.             glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  621.             name = windowNameRGB;
  622.         }
  623.     }
  624.     else {
  625.         if (useDB) {
  626.             glutInitDisplayMode(GLUT_DOUBLE | GLUT_INDEX | GLUT_DEPTH);
  627.             name = windowNameIndexDB;
  628.         }
  629.         else {
  630.             glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX | GLUT_DEPTH);
  631.             name = windowNameIndex;
  632.         }
  633.     }
  634.  
  635.     glutCreateWindow(name);
  636.  
  637.     buildColormap();
  638.  
  639.     glutKeyboardFunc(keyboard);
  640.     glutDisplayFunc(display);
  641.     glutVisibilityFunc(visible);
  642.  
  643.     fog_menu = glutCreateMenu(fog_select);
  644.     glutAddMenuEntry("Linear fog", GL_LINEAR);
  645.     glutAddMenuEntry("Exp fog", GL_EXP);
  646.     glutAddMenuEntry("Exp^2 fog", GL_EXP2);
  647.  
  648.     glutCreateMenu(menu_select);
  649.     glutAddMenuEntry("Start motion", 1);
  650.     glutAddMenuEntry("Stop motion", 2);
  651.     glutAddMenuEntry("Toggle fog", 3);
  652.     glutAddMenuEntry("Toggle lighting", 4);
  653.     glutAddSubMenu("Fog type", fog_menu);
  654.     glutAddMenuEntry("Quit", 5);
  655.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  656.  
  657.     /* setup context */
  658.     glMatrixMode(GL_PROJECTION);
  659.     glLoadIdentity();
  660.     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 3.0);
  661.  
  662.     glMatrixMode(GL_MODELVIEW);
  663.     glLoadIdentity();
  664.     glTranslatef(0.0, 0.0, -2.0);
  665.  
  666.     glEnable(GL_DEPTH_TEST);
  667.  
  668.     if (useLighting) {
  669.         glEnable(GL_LIGHTING);
  670.     }
  671.     glEnable(GL_LIGHT0);
  672.     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  673.     glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
  674.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiff);
  675.     glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpec);
  676. #if 0
  677.     glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDir);
  678.     glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 80);
  679.     glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 25);
  680. #endif
  681.  
  682.     glEnable(GL_NORMALIZE);
  683.  
  684.     if (useFog) {
  685.         glEnable(GL_FOG);
  686.     }
  687.     glFogfv(GL_FOG_COLOR, fogColor);
  688.     glFogfv(GL_FOG_INDEX, fogIndex);
  689.     glFogf(GL_FOG_MODE, GL_EXP);
  690.     glFogf(GL_FOG_DENSITY, 0.5);
  691.     glFogf(GL_FOG_START, 1.0);
  692.     glFogf(GL_FOG_END, 3.0);
  693.  
  694.     glEnable(GL_CULL_FACE);
  695.     glCullFace(GL_BACK);
  696.  
  697.     glShadeModel(GL_SMOOTH);
  698.  
  699.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  700.     if (useLogo) {
  701.         glPolygonStipple((const GLubyte *)sgiPattern);
  702.     }
  703.     else {
  704.         glPolygonStipple((const GLubyte *)shadowPattern);
  705.     }
  706.  
  707.     glClearColor(0.0, 0.0, 0.0, 1);
  708.     glClearIndex(0);
  709.     glClearDepth(1);
  710.  
  711.     glutMainLoop();
  712.     return 0;             /* ANSI C requires main to return int. */
  713. }
Advertisement
Add Comment
Please, Sign In to add comment