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.

toyball.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * "Toy Ball" shader 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-toyball.frag";
  15. static char *VertProgFile = "CH11-toyball.vert";
  16. /* program/shader objects */
  17. static GLuint fragShader;
  18. static GLuint vertShader;
  19. static GLuint program;
  20. static struct uniform_info Uniforms[] = {
  21. { "LightDir", 1, GL_FLOAT_VEC4, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
  22. { "HVector", 1, GL_FLOAT_VEC4, { 0.32506, 0.32506, 0.88808, 0.0 }, -1 },
  23. { "BallCenter", 1, GL_FLOAT_VEC4, { 0.0, 0.0, 0.0, 1.0 }, -1 },
  24. { "SpecularColor", 1, GL_FLOAT_VEC4, { 0.4, 0.4, 0.4, 60.0 }, -1 },
  25. { "Red", 1, GL_FLOAT_VEC4, { 0.6, 0.0, 0.0, 1.0 }, -1 },
  26. { "Blue", 1, GL_FLOAT_VEC4, { 0.0, 0.3, 0.6, 1.0 }, -1 },
  27. { "Yellow", 1, GL_FLOAT_VEC4, { 0.6, 0.5, 0.0, 1.0 }, -1 },
  28. { "HalfSpace0", 1, GL_FLOAT_VEC4, { 1.0, 0.0, 0.0, 0.2 }, -1 },
  29. { "HalfSpace1", 1, GL_FLOAT_VEC4, { 0.309016994, 0.951056516, 0.0, 0.2 }, -1 },
  30. { "HalfSpace2", 1, GL_FLOAT_VEC4, { -0.809016994, 0.587785252, 0.0, 0.2 }, -1 },
  31. { "HalfSpace3", 1, GL_FLOAT_VEC4, { -0.809016994, -0.587785252, 0.0, 0.2 }, -1 },
  32. { "HalfSpace4", 1, GL_FLOAT_VEC4, { 0.309116994, -0.951056516, 0.0, 0.2 }, -1 },
  33. { "InOrOutInit", 1, GL_FLOAT, { -3.0, 0, 0, 0 }, -1 },
  34. { "StripeWidth", 1, GL_FLOAT, { 0.3, 0, 0, 0 }, -1 },
  35. { "FWidth", 1, GL_FLOAT, { 0.005, 0, 0, 0 }, -1 },
  36. END_OF_UNIFORMS
  37. };
  38. static GLint win = 0;
  39. static GLboolean Anim = GL_FALSE;
  40. static GLfloat TexRot = 0.0;
  41. static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
  42. static void
  43. Idle(void)
  44. {
  45. TexRot += 2.0;
  46. if (TexRot > 360.0)
  47. TexRot -= 360.0;
  48. glutPostRedisplay();
  49. }
  50. static void
  51. Redisplay(void)
  52. {
  53. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  54. glPushMatrix();
  55. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  56. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  57. glRotatef(zRot, 0.0f, 0.0f, 1.0f);
  58. glMatrixMode(GL_TEXTURE);
  59. glLoadIdentity();
  60. glRotatef(TexRot, 0.0f, 1.0f, 0.0f);
  61. glMatrixMode(GL_MODELVIEW);
  62. glutSolidSphere(2.0, 20, 10);
  63. glPopMatrix();
  64. glutSwapBuffers();
  65. }
  66. static void
  67. Reshape(int width, int height)
  68. {
  69. glViewport(0, 0, width, height);
  70. glMatrixMode(GL_PROJECTION);
  71. glLoadIdentity();
  72. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  73. glMatrixMode(GL_MODELVIEW);
  74. glLoadIdentity();
  75. glTranslatef(0.0f, 0.0f, -15.0f);
  76. }
  77. static void
  78. CleanUp(void)
  79. {
  80. glDeleteShader(fragShader);
  81. glDeleteShader(vertShader);
  82. glDeleteProgram(program);
  83. glutDestroyWindow(win);
  84. }
  85. static void
  86. Key(unsigned char key, int x, int y)
  87. {
  88. const GLfloat step = 2.0;
  89. (void) x;
  90. (void) y;
  91. switch(key) {
  92. case 'a':
  93. Anim = !Anim;
  94. if (Anim)
  95. glutIdleFunc(Idle);
  96. else
  97. glutIdleFunc(NULL);
  98. break;
  99. case 'z':
  100. zRot += step;
  101. break;
  102. case 'Z':
  103. zRot -= step;
  104. break;
  105. case 27:
  106. CleanUp();
  107. exit(0);
  108. break;
  109. }
  110. glutPostRedisplay();
  111. }
  112. static void
  113. SpecialKey(int key, int x, int y)
  114. {
  115. const GLfloat step = 2.0;
  116. (void) x;
  117. (void) y;
  118. switch(key) {
  119. case GLUT_KEY_UP:
  120. xRot += step;
  121. break;
  122. case GLUT_KEY_DOWN:
  123. xRot -= step;
  124. break;
  125. case GLUT_KEY_LEFT:
  126. yRot -= step;
  127. break;
  128. case GLUT_KEY_RIGHT:
  129. yRot += step;
  130. break;
  131. }
  132. glutPostRedisplay();
  133. }
  134. static void
  135. Init(void)
  136. {
  137. if (!ShadersSupported())
  138. exit(1);
  139. vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
  140. fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
  141. program = LinkShaders(vertShader, fragShader);
  142. glUseProgram(program);
  143. SetUniformValues(program, Uniforms);
  144. PrintUniforms(Uniforms);
  145. assert(glGetError() == 0);
  146. glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
  147. glEnable(GL_DEPTH_TEST);
  148. glColor3f(1, 0, 0);
  149. }
  150. static void
  151. ParseOptions(int argc, char *argv[])
  152. {
  153. int i;
  154. for (i = 1; i < argc; i++) {
  155. if (strcmp(argv[i], "-fs") == 0) {
  156. FragProgFile = argv[i+1];
  157. }
  158. else if (strcmp(argv[i], "-vs") == 0) {
  159. VertProgFile = argv[i+1];
  160. }
  161. }
  162. }
  163. int
  164. main(int argc, char *argv[])
  165. {
  166. glutInit(&argc, argv);
  167. glutInitWindowSize(400, 400);
  168. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  169. win = glutCreateWindow(argv[0]);
  170. glewInit();
  171. glutReshapeFunc(Reshape);
  172. glutKeyboardFunc(Key);
  173. glutSpecialFunc(SpecialKey);
  174. glutDisplayFunc(Redisplay);
  175. ParseOptions(argc, argv);
  176. Init();
  177. glutMainLoop();
  178. return 0;
  179. }