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.

skinning.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * Vertex "skinning" example.
  3. * The idea is there are multiple modeling matrices applied to every
  4. * vertex. Weighting values in [0,1] control the influence of each
  5. * matrix on each vertex.
  6. *
  7. * 4 Nov 2008
  8. */
  9. #include <assert.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <math.h>
  14. #include <GL/glew.h>
  15. #include <GL/glut.h>
  16. #include "shaderutil.h"
  17. #ifndef M_PI
  18. #define M_PI 3.1415926535
  19. #endif
  20. static char *FragProgFile = "skinning.frag";
  21. static char *VertProgFile = "skinning.vert";
  22. /* program/shader objects */
  23. static GLuint fragShader;
  24. static GLuint vertShader;
  25. static GLuint program;
  26. static GLint win = 0;
  27. static GLboolean Anim = GL_TRUE;
  28. static GLboolean WireFrame = GL_TRUE;
  29. static GLfloat xRot = 0.0f, yRot = 90.0f, zRot = 0.0f;
  30. #define NUM_MATS 2
  31. static GLfloat Matrices[NUM_MATS][16];
  32. static GLint uMat0, uMat1;
  33. static GLint WeightAttr;
  34. static void
  35. Idle(void)
  36. {
  37. yRot = 90 + glutGet(GLUT_ELAPSED_TIME) * 0.005;
  38. glutPostRedisplay();
  39. }
  40. static void
  41. Cylinder(GLfloat length, GLfloat radius, GLint slices, GLint stacks)
  42. {
  43. float dw = 1.0 / (stacks - 1);
  44. float dz = length / stacks;
  45. int i, j;
  46. for (j = 0; j < stacks; j++) {
  47. float w0 = j * dw;
  48. float z0 = j * dz;
  49. glBegin(GL_TRIANGLE_STRIP);
  50. for (i = 0; i < slices; i++) {
  51. float a = (float) i / (slices - 1) * M_PI * 2.0;
  52. float x = radius * cos(a);
  53. float y = radius * sin(a);
  54. glVertexAttrib1f(WeightAttr, w0);
  55. glNormal3f(x, y, 0.0);
  56. glVertex3f(x, y, z0);
  57. glVertexAttrib1f(WeightAttr, w0 + dw);
  58. glNormal3f(x, y, 0.0);
  59. glVertex3f(x, y, z0 + dz);
  60. }
  61. glEnd();
  62. }
  63. }
  64. /**
  65. * Update/animate the two matrices. One rotates, the other scales.
  66. */
  67. static void
  68. UpdateMatrices(void)
  69. {
  70. GLfloat t = glutGet(GLUT_ELAPSED_TIME) * 0.0025;
  71. GLfloat scale = 0.5 * (1.1 + sin(0.5 * t));
  72. GLfloat rot = cos(t) * 90.0;
  73. glPushMatrix();
  74. glLoadIdentity();
  75. glScalef(1.0, scale, 1.0);
  76. glGetFloatv(GL_MODELVIEW_MATRIX, Matrices[0]);
  77. glPopMatrix();
  78. glPushMatrix();
  79. glLoadIdentity();
  80. glRotatef(rot, 0, 0, 1);
  81. glGetFloatv(GL_MODELVIEW_MATRIX, Matrices[1]);
  82. glPopMatrix();
  83. }
  84. static void
  85. Redisplay(void)
  86. {
  87. UpdateMatrices();
  88. glUniformMatrix4fv(uMat0, 1, GL_FALSE, Matrices[0]);
  89. glUniformMatrix4fv(uMat1, 1, GL_FALSE, Matrices[1]);
  90. if (WireFrame)
  91. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  92. else
  93. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  94. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  95. glPushMatrix();
  96. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  97. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  98. glRotatef(zRot, 0.0f, 0.0f, 1.0f);
  99. glPushMatrix();
  100. glTranslatef(0, 0, -2.5);
  101. Cylinder(5.0, 1.0, 10, 20);
  102. glPopMatrix();
  103. glPopMatrix();
  104. glutSwapBuffers();
  105. }
  106. static void
  107. Reshape(int width, int height)
  108. {
  109. glViewport(0, 0, width, height);
  110. glMatrixMode(GL_PROJECTION);
  111. glLoadIdentity();
  112. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  113. glMatrixMode(GL_MODELVIEW);
  114. glLoadIdentity();
  115. glTranslatef(0.0f, 0.0f, -15.0f);
  116. }
  117. static void
  118. CleanUp(void)
  119. {
  120. glDeleteShader(fragShader);
  121. glDeleteShader(vertShader);
  122. glDeleteProgram(program);
  123. glutDestroyWindow(win);
  124. }
  125. static void
  126. Key(unsigned char key, int x, int y)
  127. {
  128. const GLfloat step = 2.0;
  129. (void) x;
  130. (void) y;
  131. switch(key) {
  132. case 'a':
  133. Anim = !Anim;
  134. if (Anim)
  135. glutIdleFunc(Idle);
  136. else
  137. glutIdleFunc(NULL);
  138. break;
  139. case 'w':
  140. WireFrame = !WireFrame;
  141. break;
  142. case 'z':
  143. zRot += step;
  144. break;
  145. case 'Z':
  146. zRot -= step;
  147. break;
  148. case 27:
  149. CleanUp();
  150. exit(0);
  151. break;
  152. }
  153. glutPostRedisplay();
  154. }
  155. static void
  156. SpecialKey(int key, int x, int y)
  157. {
  158. const GLfloat step = 2.0;
  159. (void) x;
  160. (void) y;
  161. switch(key) {
  162. case GLUT_KEY_UP:
  163. xRot += step;
  164. break;
  165. case GLUT_KEY_DOWN:
  166. xRot -= step;
  167. break;
  168. case GLUT_KEY_LEFT:
  169. yRot -= step;
  170. break;
  171. case GLUT_KEY_RIGHT:
  172. yRot += step;
  173. break;
  174. }
  175. glutPostRedisplay();
  176. }
  177. static void
  178. Init(void)
  179. {
  180. if (!ShadersSupported())
  181. exit(1);
  182. vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
  183. fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
  184. program = LinkShaders(vertShader, fragShader);
  185. glUseProgram(program);
  186. uMat0 = glGetUniformLocation(program, "mat0");
  187. uMat1 = glGetUniformLocation(program, "mat1");
  188. WeightAttr = glGetAttribLocation(program, "weight");
  189. assert(glGetError() == 0);
  190. glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
  191. glEnable(GL_DEPTH_TEST);
  192. glColor3f(1, 0, 0);
  193. }
  194. static void
  195. ParseOptions(int argc, char *argv[])
  196. {
  197. int i;
  198. for (i = 1; i < argc; i++) {
  199. if (strcmp(argv[i], "-fs") == 0) {
  200. FragProgFile = argv[i+1];
  201. }
  202. else if (strcmp(argv[i], "-vs") == 0) {
  203. VertProgFile = argv[i+1];
  204. }
  205. }
  206. }
  207. int
  208. main(int argc, char *argv[])
  209. {
  210. glutInit(&argc, argv);
  211. glutInitWindowSize(500, 500);
  212. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  213. win = glutCreateWindow(argv[0]);
  214. glewInit();
  215. glutReshapeFunc(Reshape);
  216. glutKeyboardFunc(Key);
  217. glutSpecialFunc(SpecialKey);
  218. glutDisplayFunc(Redisplay);
  219. ParseOptions(argc, argv);
  220. Init();
  221. if (Anim)
  222. glutIdleFunc(Idle);
  223. glutMainLoop();
  224. return 0;
  225. }