Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

arbvptorus.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. GLint errno;
  92. GLuint prognum;
  93. /* borrowed from an nvidia demo:
  94. * c[0..3] = modelview matrix
  95. * c[4..7] = invtrans modelview matrix
  96. * c[32] = light pos
  97. * c[35] = diffuse color
  98. */
  99. static const char prog[] =
  100. "!!ARBvp1.0\n"
  101. "OPTION ARB_position_invariant ;"
  102. "TEMP R0, R1; \n"
  103. "# normal x MV-1T -> lighting normal\n"
  104. "DP3 R1.x, state.matrix.modelview.invtrans.row[0], vertex.normal ;\n"
  105. "DP3 R1.y, state.matrix.modelview.invtrans.row[1], vertex.normal;\n"
  106. "DP3 R1.z, state.matrix.modelview.invtrans.row[2], vertex.normal;\n"
  107. "DP3 R0, program.local[32], R1; # L.N\n"
  108. #if 0
  109. "MUL result.color.xyz, R0, program.local[35] ; # col = L.N * diffuse\n"
  110. #else
  111. "MUL result.color.primary.xyz, R0, program.local[35] ; # col = L.N * diffuse\n"
  112. #endif
  113. "MOV result.texcoord, vertex.texcoord;\n"
  114. "END";
  115. if (!glutExtensionSupported("GL_ARB_vertex_program")) {
  116. printf("Sorry, this program requires GL_ARB_vertex_program");
  117. exit(1);
  118. }
  119. glGenProgramsARB(1, &prognum);
  120. glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
  121. glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  122. strlen(prog), (const GLubyte *) prog);
  123. assert(glIsProgramARB(prognum));
  124. errno = glGetError();
  125. printf("glGetError = %d\n", errno);
  126. if (errno != GL_NO_ERROR)
  127. {
  128. GLint errorpos;
  129. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
  130. printf("errorpos: %d\n", errorpos);
  131. printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  132. }
  133. /* Light position */
  134. glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 32, 2, 2, 4, 1);
  135. /* Diffuse material color */
  136. glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 35, 0.25, 0, 0.25, 1);
  137. glEnable(GL_VERTEX_PROGRAM_ARB);
  138. glEnable(GL_DEPTH_TEST);
  139. glClearColor(0.3, 0.3, 0.3, 1);
  140. }
  141. int main( int argc, char *argv[] )
  142. {
  143. glutInit( &argc, argv );
  144. glutInitWindowPosition( 0, 0 );
  145. glutInitWindowSize( 250, 250 );
  146. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  147. glutCreateWindow(argv[0]);
  148. glutReshapeFunc( Reshape );
  149. glutKeyboardFunc( Key );
  150. glutSpecialFunc( SpecialKey );
  151. glutDisplayFunc( Display );
  152. if (Anim)
  153. glutIdleFunc(Idle);
  154. Init();
  155. glutMainLoop();
  156. return 0;
  157. }