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.

vpwarpmesh.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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) glVertexAttrib3fvNV(3, c)
  29. #define VERTEX2F(x, y) glVertexAttrib2fNV(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. glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 30, x, y, z, 1);
  65. glDisable(GL_VERTEX_PROGRAM_NV);
  66. glBegin(GL_POINTS);
  67. glColor3f(1,1,1);
  68. glVertex3f(x, y, z);
  69. glEnd();
  70. }
  71. glEnable(GL_VERTEX_PROGRAM_NV);
  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. /*
  139. * c[0..3] = modelview matrix
  140. * c[4..7] = inverse modelview matrix
  141. * c[30] = gravity source location
  142. * c[31] = gravity source strength
  143. * c[32] = light pos
  144. * c[35] = diffuse color
  145. */
  146. static const char prog[] =
  147. "!!VP1.0\n"
  148. "# Compute distance from vertex to gravity source\n"
  149. "ADD R1, c[30], -v[OPOS]; # vector from vertex to gravity\n"
  150. "DP3 R2, R1, R1; # dot product\n"
  151. "RSQ R2, R2.x; # square root = distance\n"
  152. "MUL R2, R2, c[31].xxxx; # scale by the gravity factor\n"
  153. "# Displace vertex by gravity factor along R1 vector\n"
  154. "MAD R3, R1, R2, v[OPOS];\n"
  155. "# Continue with typical modelview/projection\n"
  156. "DP4 o[HPOS].x, c[0], R3 ; # object x MVP -> clip\n"
  157. "DP4 o[HPOS].y, c[1], R3 ;\n"
  158. "DP4 o[HPOS].z, c[2], R3 ;\n"
  159. "DP4 o[HPOS].w, c[3], R3 ;\n"
  160. "MOV o[COL0], v[COL0];\n # copy input color to output color\n"
  161. "END";
  162. if (!glutExtensionSupported("GL_NV_vertex_program")) {
  163. printf("Sorry, this program requires GL_NV_vertex_program\n");
  164. exit(1);
  165. }
  166. glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 1,
  167. strlen(prog), (const GLubyte *) prog);
  168. assert(glIsProgramNV(1));
  169. glBindProgramNV(GL_VERTEX_PROGRAM_NV, 1);
  170. /* Load the program registers */
  171. glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
  172. glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 4, GL_MODELVIEW, GL_INVERSE_TRANSPOSE_NV);
  173. /* Light position */
  174. glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 32, 2, 2, 4, 1);
  175. /* Diffuse material color */
  176. glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 35, 0.25, 0, 0.25, 1);
  177. /* Gravity strength */
  178. glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 31, .5, 0, 0, 0);
  179. glEnable(GL_DEPTH_TEST);
  180. glClearColor(0.3, 0.3, 0.3, 1);
  181. glShadeModel(GL_FLAT);
  182. glPointSize(3);
  183. printf("glGetError = %d\n", (int) glGetError());
  184. }
  185. int main( int argc, char *argv[] )
  186. {
  187. glutInit( &argc, argv );
  188. glutInitWindowPosition( 0, 0 );
  189. glutInitWindowSize( 250, 250 );
  190. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  191. glutCreateWindow(argv[0]);
  192. glutReshapeFunc( Reshape );
  193. glutKeyboardFunc( Key );
  194. glutSpecialFunc( SpecialKey );
  195. glutDisplayFunc( Display );
  196. if (Anim)
  197. glutIdleFunc(Idle);
  198. Init();
  199. glutMainLoop();
  200. return 0;
  201. }