Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

arbvpwarpmesh.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Warp a triangle mesh with a 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 = -60.0, Yrot = 0.0, Zrot = 0.0;
  12. static GLboolean Anim = GL_TRUE;
  13. static GLfloat Phi = 0.0;
  14. static void Idle( void )
  15. {
  16. Phi += 0.01;
  17. glutPostRedisplay();
  18. }
  19. static void DrawMesh( int rows, int cols )
  20. {
  21. static const GLfloat colorA[3] = { 0, 1, 0 };
  22. static const GLfloat colorB[3] = { 0, 0, 1 };
  23. const float dx = 2.0 / (cols - 1);
  24. const float dy = 2.0 / (rows - 1);
  25. float x, y;
  26. int i, j;
  27. #if 1
  28. #define COLOR3FV(c) glVertexAttrib3fvARB(3, c)
  29. #define VERTEX2F(x, y) glVertexAttrib2fARB(0, x, y)
  30. #else
  31. #define COLOR3FV(c) glColor3fv(c)
  32. #define VERTEX2F(x, y) glVertex2f(x, y)
  33. #endif
  34. y = -1.0;
  35. for (i = 0; i < rows - 1; i++) {
  36. glBegin(GL_QUAD_STRIP);
  37. x = -1.0;
  38. for (j = 0; j < cols; j++) {
  39. if ((i + j) & 1)
  40. COLOR3FV(colorA);
  41. else
  42. COLOR3FV(colorB);
  43. VERTEX2F(x, y);
  44. VERTEX2F(x, y + dy);
  45. x += dx;
  46. }
  47. glEnd();
  48. y += dy;
  49. }
  50. }
  51. static void Display( void )
  52. {
  53. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  54. glPushMatrix();
  55. glRotatef(Xrot, 1, 0, 0);
  56. glRotatef(Yrot, 0, 1, 0);
  57. glRotatef(Zrot, 0, 0, 1);
  58. /* Position the gravity source */
  59. {
  60. GLfloat x, y, z, r = 0.5;
  61. x = r * cos(Phi);
  62. y = r * sin(Phi);
  63. z = 1.0;
  64. glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 30, x, y, z, 1);
  65. glDisable(GL_VERTEX_PROGRAM_ARB);
  66. glBegin(GL_POINTS);
  67. glColor3f(1,1,1);
  68. glVertex3f(x, y, z);
  69. glEnd();
  70. }
  71. glEnable(GL_VERTEX_PROGRAM_ARB);
  72. DrawMesh(8, 8);
  73. glPopMatrix();
  74. glutSwapBuffers();
  75. }
  76. static void Reshape( int width, int height )
  77. {
  78. float ar = (float) width / (float) height;
  79. glViewport( 0, 0, width, height );
  80. glMatrixMode( GL_PROJECTION );
  81. glLoadIdentity();
  82. glFrustum( -1.0 * ar, 1.0 * ar, -1.0, 1.0, 5.0, 25.0 );
  83. glMatrixMode( GL_MODELVIEW );
  84. glLoadIdentity();
  85. glTranslatef( 0.0, 0.0, -12.0 );
  86. glScalef(2, 2, 2);
  87. }
  88. static void Key( unsigned char key, int x, int y )
  89. {
  90. (void) x;
  91. (void) y;
  92. switch (key) {
  93. case 'a':
  94. Anim = !Anim;
  95. if (Anim)
  96. glutIdleFunc(Idle);
  97. else
  98. glutIdleFunc(NULL);
  99. break;
  100. case 'p':
  101. Phi += 0.2;
  102. break;
  103. case 'z':
  104. Zrot -= 5.0;
  105. break;
  106. case 'Z':
  107. Zrot += 5.0;
  108. break;
  109. case 27:
  110. exit(0);
  111. break;
  112. }
  113. glutPostRedisplay();
  114. }
  115. static void SpecialKey( int key, int x, int y )
  116. {
  117. const GLfloat step = 3.0;
  118. (void) x;
  119. (void) y;
  120. switch (key) {
  121. case GLUT_KEY_UP:
  122. Xrot -= step;
  123. break;
  124. case GLUT_KEY_DOWN:
  125. Xrot += step;
  126. break;
  127. case GLUT_KEY_LEFT:
  128. Yrot -= step;
  129. break;
  130. case GLUT_KEY_RIGHT:
  131. Yrot += step;
  132. break;
  133. }
  134. glutPostRedisplay();
  135. }
  136. static void Init( void )
  137. {
  138. GLuint prognum;
  139. GLint errno;
  140. /*
  141. * c[0..3] = modelview matrix
  142. * c[4..7] = inverse modelview matrix
  143. * c[30] = gravity source location
  144. * c[31] = gravity source strength
  145. * c[32] = light pos
  146. * c[35] = diffuse color
  147. */
  148. static const char prog[] =
  149. "!!ARBvp1.0\n"
  150. "TEMP R1, R2, R3; "
  151. "# Compute distance from vertex to gravity source\n"
  152. "ADD R1, program.local[30], -vertex.position; # vector from vertex to gravity\n"
  153. "DP3 R2, R1, R1; # dot product\n"
  154. "RSQ R2, R2.x; # square root = distance\n"
  155. "MUL R2, R2, program.local[31].xxxx; # scale by the gravity factor\n"
  156. "# Displace vertex by gravity factor along R1 vector\n"
  157. "MAD R3, R1, R2, vertex.position;\n"
  158. "# Continue with typical modelview/projection\n"
  159. "DP4 result.position.x, state.matrix.mvp.row[0], R3 ; # object x MVP -> clip\n"
  160. "DP4 result.position.y, state.matrix.mvp.row[1], R3 ;\n"
  161. "DP4 result.position.z, state.matrix.mvp.row[2], R3 ;\n"
  162. "DP4 result.position.w, state.matrix.mvp.row[3], R3 ;\n"
  163. "MOV result.color, vertex.color;\n # copy input color to output color\n"
  164. "END";
  165. if (!glutExtensionSupported("GL_ARB_vertex_program")) {
  166. printf("Sorry, this program requires GL_ARB_vertex_program\n");
  167. exit(1);
  168. }
  169. glGenProgramsARB(1, &prognum);
  170. glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
  171. glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  172. strlen(prog), (const GLubyte *)prog);
  173. errno = glGetError();
  174. printf("glGetError = %d\n", errno);
  175. if (errno != GL_NO_ERROR)
  176. {
  177. GLint errorpos;
  178. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
  179. printf("errorpos: %d\n", errorpos);
  180. printf("%s\n", glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  181. }
  182. /* Light position */
  183. glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 32, 2, 2, 4, 1);
  184. /* Diffuse material color */
  185. glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 35, 0.25, 0, 0.25, 1);
  186. /* Gravity strength */
  187. glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 31, .5, 0, 0, 0);
  188. glEnable(GL_DEPTH_TEST);
  189. glClearColor(0.3, 0.3, 0.3, 1);
  190. glShadeModel(GL_FLAT);
  191. glPointSize(3);
  192. }
  193. int main( int argc, char *argv[] )
  194. {
  195. glutInit( &argc, argv );
  196. glutInitWindowPosition( 0, 0 );
  197. glutInitWindowSize( 250, 250 );
  198. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  199. glutCreateWindow(argv[0]);
  200. glutReshapeFunc( Reshape );
  201. glutKeyboardFunc( Key );
  202. glutSpecialFunc( SpecialKey );
  203. glutDisplayFunc( Display );
  204. if (Anim)
  205. glutIdleFunc(Idle);
  206. Init();
  207. glutMainLoop();
  208. return 0;
  209. }