Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vptorus.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * A lit, rotating torus via vertex program
  3. */
  4. #include <assert.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #define GL_GLEXT_PROTOTYPES
  10. #include <GL/glut.h>
  11. static float Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
  12. static GLboolean Anim = GL_TRUE;
  13. static void Idle( void )
  14. {
  15. Xrot += .3;
  16. Yrot += .4;
  17. Zrot += .2;
  18. glutPostRedisplay();
  19. }
  20. static void Display( void )
  21. {
  22. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  23. glPushMatrix();
  24. glRotatef(Xrot, 1, 0, 0);
  25. glRotatef(Yrot, 0, 1, 0);
  26. glRotatef(Zrot, 0, 0, 1);
  27. glutSolidTorus(0.75, 2.0, 10, 20);
  28. glPopMatrix();
  29. glutSwapBuffers();
  30. }
  31. static void Reshape( int width, int height )
  32. {
  33. glViewport( 0, 0, width, height );
  34. glMatrixMode( GL_PROJECTION );
  35. glLoadIdentity();
  36. glFrustum( -2.0, 2.0, -2.0, 2.0, 5.0, 25.0 );
  37. glMatrixMode( GL_MODELVIEW );
  38. glLoadIdentity();
  39. glTranslatef( 0.0, 0.0, -12.0 );
  40. }
  41. static void Key( unsigned char key, int x, int y )
  42. {
  43. (void) x;
  44. (void) y;
  45. switch (key) {
  46. case ' ':
  47. Xrot = Yrot = Zrot = 0;
  48. break;
  49. case 'a':
  50. Anim = !Anim;
  51. if (Anim)
  52. glutIdleFunc(Idle);
  53. else
  54. glutIdleFunc(NULL);
  55. break;
  56. case 'z':
  57. Zrot -= 5.0;
  58. break;
  59. case 'Z':
  60. Zrot += 5.0;
  61. break;
  62. case 27:
  63. exit(0);
  64. break;
  65. }
  66. glutPostRedisplay();
  67. }
  68. static void SpecialKey( int key, int x, int y )
  69. {
  70. const GLfloat step = 3.0;
  71. (void) x;
  72. (void) y;
  73. switch (key) {
  74. case GLUT_KEY_UP:
  75. Xrot -= step;
  76. break;
  77. case GLUT_KEY_DOWN:
  78. Xrot += step;
  79. break;
  80. case GLUT_KEY_LEFT:
  81. Yrot -= step;
  82. break;
  83. case GLUT_KEY_RIGHT:
  84. Yrot += step;
  85. break;
  86. }
  87. glutPostRedisplay();
  88. }
  89. static void Init( void )
  90. {
  91. /* borrowed from an nvidia demo:
  92. * c[0..3] = modelview matrix
  93. * c[4..7] = inverse modelview matrix
  94. * c[32] = light pos
  95. * c[35] = diffuse color
  96. */
  97. static const char prog[] =
  98. "!!VP1.0\n"
  99. "#Simple transform and diffuse lighting\n"
  100. "\n"
  101. "DP4 o[HPOS].x, c[0], v[OPOS] ; # object x MVP -> clip\n"
  102. "DP4 o[HPOS].y, c[1], v[OPOS] ;\n"
  103. "DP4 o[HPOS].z, c[2], v[OPOS] ;\n"
  104. "DP4 o[HPOS].w, c[3], v[OPOS] ;\n"
  105. "DP3 R1.x, c[4], v[NRML] ; # normal x MV-1T -> lighting normal\n"
  106. "DP3 R1.y, c[5], v[NRML] ;\n"
  107. "DP3 R1.z, c[6], v[NRML] ;\n"
  108. "DP3 R0, c[32], R1 ; # L.N\n"
  109. "MUL o[COL0].xyz, R0, c[35] ; # col = L.N * diffuse\n"
  110. "MOV o[TEX0], v[TEX0];\n"
  111. "END";
  112. if (!glutExtensionSupported("GL_NV_vertex_program")) {
  113. printf("Sorry, this program requires GL_NV_vertex_program");
  114. exit(1);
  115. }
  116. glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 1,
  117. strlen(prog), (const GLubyte *) prog);
  118. assert(glIsProgramNV(1));
  119. glBindProgramNV(GL_VERTEX_PROGRAM_NV, 1);
  120. /* Load the program registers */
  121. glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
  122. glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 4, GL_MODELVIEW, GL_INVERSE_TRANSPOSE_NV);
  123. /* Light position */
  124. glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 32, 2, 2, 4, 1);
  125. /* Diffuse material color */
  126. glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 35, 0.25, 0, 0.25, 1);
  127. glEnable(GL_VERTEX_PROGRAM_NV);
  128. glEnable(GL_DEPTH_TEST);
  129. glClearColor(0.3, 0.3, 0.3, 1);
  130. printf("glGetError = %d\n", (int) glGetError());
  131. }
  132. int main( int argc, char *argv[] )
  133. {
  134. glutInit( &argc, argv );
  135. glutInitWindowPosition( 0, 0 );
  136. glutInitWindowSize( 250, 250 );
  137. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  138. glutCreateWindow(argv[0]);
  139. glutReshapeFunc( Reshape );
  140. glutKeyboardFunc( Key );
  141. glutSpecialFunc( SpecialKey );
  142. glutDisplayFunc( Display );
  143. if (Anim)
  144. glutIdleFunc(Idle);
  145. Init();
  146. glutMainLoop();
  147. return 0;
  148. }