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.

bump.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * Procedural Bump Mapping demo. Uses the example shaders from
  3. * chapter 11 of the OpenGL Shading Language "orange" book.
  4. * 16 Jan 2007
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <GL/glew.h>
  12. #include <GL/glut.h>
  13. #include "shaderutil.h"
  14. static char *FragProgFile = "CH11-bumpmap.frag";
  15. static char *VertProgFile = "CH11-bumpmap.vert";
  16. /* program/shader objects */
  17. static GLuint fragShader;
  18. static GLuint vertShader;
  19. static GLuint program;
  20. static struct uniform_info Uniforms[] = {
  21. { "LightPosition", 1, GL_FLOAT_VEC3, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
  22. { "SurfaceColor", 1, GL_FLOAT_VEC3, { 0.8, 0.8, 0.2, 0 }, -1 },
  23. { "BumpDensity", 1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 },
  24. { "BumpSize", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 },
  25. { "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
  26. END_OF_UNIFORMS
  27. };
  28. static GLint win = 0;
  29. static GLfloat xRot = 20.0f, yRot = 0.0f, zRot = 0.0f;
  30. static GLint tangentAttrib;
  31. static GLboolean Anim = GL_FALSE;
  32. static void
  33. CheckError(int line)
  34. {
  35. GLenum err = glGetError();
  36. if (err) {
  37. printf("GL Error %s (0x%x) at line %d\n",
  38. gluErrorString(err), (int) err, line);
  39. }
  40. }
  41. /*
  42. * Draw a square, specifying normal and tangent vectors.
  43. */
  44. static void
  45. Square(GLfloat size)
  46. {
  47. glNormal3f(0, 0, 1);
  48. glVertexAttrib3f(tangentAttrib, 1, 0, 0);
  49. glBegin(GL_POLYGON);
  50. glTexCoord2f(0, 0); glVertex2f(-size, -size);
  51. glTexCoord2f(1, 0); glVertex2f( size, -size);
  52. glTexCoord2f(1, 1); glVertex2f( size, size);
  53. glTexCoord2f(0, 1); glVertex2f(-size, size);
  54. glEnd();
  55. }
  56. static void
  57. Cube(GLfloat size)
  58. {
  59. /* +X */
  60. glPushMatrix();
  61. glRotatef(90, 0, 1, 0);
  62. glTranslatef(0, 0, size);
  63. Square(size);
  64. glPopMatrix();
  65. /* -X */
  66. glPushMatrix();
  67. glRotatef(-90, 0, 1, 0);
  68. glTranslatef(0, 0, size);
  69. Square(size);
  70. glPopMatrix();
  71. /* +Y */
  72. glPushMatrix();
  73. glRotatef(90, 1, 0, 0);
  74. glTranslatef(0, 0, size);
  75. Square(size);
  76. glPopMatrix();
  77. /* -Y */
  78. glPushMatrix();
  79. glRotatef(-90, 1, 0, 0);
  80. glTranslatef(0, 0, size);
  81. Square(size);
  82. glPopMatrix();
  83. /* +Z */
  84. glPushMatrix();
  85. glTranslatef(0, 0, size);
  86. Square(size);
  87. glPopMatrix();
  88. /* -Z */
  89. glPushMatrix();
  90. glRotatef(180, 0, 1, 0);
  91. glTranslatef(0, 0, size);
  92. Square(size);
  93. glPopMatrix();
  94. }
  95. static void
  96. Idle(void)
  97. {
  98. GLint t = glutGet(GLUT_ELAPSED_TIME);
  99. yRot = t * 0.05;
  100. glutPostRedisplay();
  101. }
  102. static void
  103. Redisplay(void)
  104. {
  105. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  106. glPushMatrix();
  107. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  108. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  109. glRotatef(zRot, 0.0f, 0.0f, 1.0f);
  110. Cube(1.5);
  111. glPopMatrix();
  112. CheckError(__LINE__);
  113. glutSwapBuffers();
  114. }
  115. static void
  116. Reshape(int width, int height)
  117. {
  118. float ar = (float) width / (float) height;
  119. glViewport(0, 0, width, height);
  120. glMatrixMode(GL_PROJECTION);
  121. glLoadIdentity();
  122. glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
  123. glMatrixMode(GL_MODELVIEW);
  124. glLoadIdentity();
  125. glTranslatef(0.0f, 0.0f, -15.0f);
  126. }
  127. static void
  128. CleanUp(void)
  129. {
  130. glDeleteShader(fragShader);
  131. glDeleteShader(vertShader);
  132. glDeleteProgram(program);
  133. glutDestroyWindow(win);
  134. }
  135. static void
  136. Key(unsigned char key, int x, int y)
  137. {
  138. const GLfloat step = 2.0;
  139. (void) x;
  140. (void) y;
  141. switch(key) {
  142. case 'a':
  143. Anim = !Anim;
  144. glutIdleFunc(Anim ? Idle : NULL);
  145. break;
  146. case 'z':
  147. zRot += step;
  148. break;
  149. case 'Z':
  150. zRot -= step;
  151. break;
  152. case 27:
  153. CleanUp();
  154. exit(0);
  155. break;
  156. }
  157. glutPostRedisplay();
  158. }
  159. static void
  160. SpecialKey(int key, int x, int y)
  161. {
  162. const GLfloat step = 2.0;
  163. (void) x;
  164. (void) y;
  165. switch(key) {
  166. case GLUT_KEY_UP:
  167. xRot += step;
  168. break;
  169. case GLUT_KEY_DOWN:
  170. xRot -= step;
  171. break;
  172. case GLUT_KEY_LEFT:
  173. yRot -= step;
  174. break;
  175. case GLUT_KEY_RIGHT:
  176. yRot += step;
  177. break;
  178. }
  179. glutPostRedisplay();
  180. }
  181. static void
  182. Init(void)
  183. {
  184. if (!ShadersSupported())
  185. exit(1);
  186. vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
  187. fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
  188. program = LinkShaders(vertShader, fragShader);
  189. glUseProgram(program);
  190. assert(glIsProgram(program));
  191. assert(glIsShader(fragShader));
  192. assert(glIsShader(vertShader));
  193. assert(glGetError() == 0);
  194. CheckError(__LINE__);
  195. SetUniformValues(program, Uniforms);
  196. PrintUniforms(Uniforms);
  197. CheckError(__LINE__);
  198. tangentAttrib = glGetAttribLocation(program, "Tangent");
  199. printf("Tangent Attrib: %d\n", tangentAttrib);
  200. assert(tangentAttrib >= 0);
  201. CheckError(__LINE__);
  202. glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
  203. glEnable(GL_DEPTH_TEST);
  204. glColor3f(1, 0, 0);
  205. }
  206. static void
  207. ParseOptions(int argc, char *argv[])
  208. {
  209. int i;
  210. for (i = 1; i < argc; i++) {
  211. if (strcmp(argv[i], "-fs") == 0) {
  212. FragProgFile = argv[i+1];
  213. }
  214. else if (strcmp(argv[i], "-vs") == 0) {
  215. VertProgFile = argv[i+1];
  216. }
  217. }
  218. }
  219. int
  220. main(int argc, char *argv[])
  221. {
  222. glutInit(&argc, argv);
  223. glutInitWindowSize(400, 400);
  224. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  225. win = glutCreateWindow(argv[0]);
  226. glewInit();
  227. glutReshapeFunc(Reshape);
  228. glutKeyboardFunc(Key);
  229. glutSpecialFunc(SpecialKey);
  230. glutDisplayFunc(Redisplay);
  231. ParseOptions(argc, argv);
  232. Init();
  233. glutMainLoop();
  234. return 0;
  235. }