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.

deriv.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * Test OpenGL 2.0 dx/dy functions for texcoords.
  3. * Brian Paul
  4. * 2 May 2007
  5. *
  6. * NOTE: resize the window to observe how the partial derivatives of
  7. * the texcoords change.
  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. static char *FragProgFile = NULL;
  18. static char *VertProgFile = NULL;
  19. static GLuint fragShader;
  20. static GLuint vertShader;
  21. static GLuint program;
  22. static GLuint SphereList, RectList, CurList;
  23. static GLint win = 0;
  24. static GLboolean anim = GL_TRUE;
  25. static GLfloat xRot = 0.0f, yRot = 0.0f;
  26. static GLint WinSize[2];
  27. static GLint WinSizeUniform = -1;
  28. static void
  29. Redisplay(void)
  30. {
  31. glUniform2iv(WinSizeUniform, 1, WinSize);
  32. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  33. glPushMatrix();
  34. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  35. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  36. glCallList(CurList);
  37. glPopMatrix();
  38. glutSwapBuffers();
  39. }
  40. static void
  41. Idle(void)
  42. {
  43. yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
  44. glutPostRedisplay();
  45. }
  46. static void
  47. Reshape(int width, int height)
  48. {
  49. WinSize[0] = width;
  50. WinSize[1] = height;
  51. glViewport(0, 0, width, height);
  52. glMatrixMode(GL_PROJECTION);
  53. glLoadIdentity();
  54. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  55. glMatrixMode(GL_MODELVIEW);
  56. glLoadIdentity();
  57. glTranslatef(0.0f, 0.0f, -15.0f);
  58. }
  59. static void
  60. CleanUp(void)
  61. {
  62. glDeleteShader(fragShader);
  63. glDeleteShader(vertShader);
  64. glDeleteProgram(program);
  65. glutDestroyWindow(win);
  66. }
  67. static void
  68. Key(unsigned char key, int x, int y)
  69. {
  70. (void) x;
  71. (void) y;
  72. switch(key) {
  73. case ' ':
  74. case 'a':
  75. anim = !anim;
  76. if (anim)
  77. glutIdleFunc(Idle);
  78. else
  79. glutIdleFunc(NULL);
  80. break;
  81. case 'o':
  82. if (CurList == SphereList)
  83. CurList = RectList;
  84. else
  85. CurList = SphereList;
  86. break;
  87. case 27:
  88. CleanUp();
  89. exit(0);
  90. break;
  91. }
  92. glutPostRedisplay();
  93. }
  94. static void
  95. SpecialKey(int key, int x, int y)
  96. {
  97. const GLfloat step = 3.0f;
  98. (void) x;
  99. (void) y;
  100. switch(key) {
  101. case GLUT_KEY_UP:
  102. xRot -= step;
  103. break;
  104. case GLUT_KEY_DOWN:
  105. xRot += step;
  106. break;
  107. case GLUT_KEY_LEFT:
  108. yRot -= step;
  109. break;
  110. case GLUT_KEY_RIGHT:
  111. yRot += step;
  112. break;
  113. }
  114. glutPostRedisplay();
  115. }
  116. static void
  117. MakeSphere(void)
  118. {
  119. GLUquadricObj *obj = gluNewQuadric();
  120. SphereList = glGenLists(1);
  121. gluQuadricTexture(obj, GL_TRUE);
  122. glNewList(SphereList, GL_COMPILE);
  123. gluSphere(obj, 2.0f, 30, 15);
  124. glEndList();
  125. gluDeleteQuadric(obj);
  126. }
  127. static void
  128. MakeRect(void)
  129. {
  130. RectList = glGenLists(1);
  131. glNewList(RectList, GL_COMPILE);
  132. glBegin(GL_POLYGON);
  133. glTexCoord2f(0, 0); glVertex2f(-2, -2);
  134. glTexCoord2f(1, 0); glVertex2f( 2, -2);
  135. glTexCoord2f(1, 1); glVertex2f( 2, 2);
  136. glTexCoord2f(0, 1); glVertex2f(-2, 2);
  137. glEnd();
  138. glEndList();
  139. }
  140. static void
  141. Init(void)
  142. {
  143. static const char *fragShaderText =
  144. "uniform ivec2 WinSize; \n"
  145. "void main() {\n"
  146. " vec2 d = dFdy(gl_TexCoord[0].xy) * vec2(WinSize); \n"
  147. " gl_FragColor = vec4(d.x, d.y, 0.0, 1.0);\n"
  148. " // gl_FragColor = gl_TexCoord[0];\n"
  149. "}\n";
  150. static const char *vertShaderText =
  151. "void main() {\n"
  152. " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
  153. " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
  154. "}\n";
  155. if (!ShadersSupported())
  156. exit(1);
  157. vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
  158. fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
  159. program = LinkShaders(vertShader, fragShader);
  160. glUseProgram(program);
  161. WinSizeUniform = glGetUniformLocation(program, "WinSize");
  162. /*assert(glGetError() == 0);*/
  163. glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  164. glEnable(GL_DEPTH_TEST);
  165. MakeSphere();
  166. MakeRect();
  167. CurList = SphereList;
  168. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  169. assert(glIsProgram(program));
  170. assert(glIsShader(fragShader));
  171. assert(glIsShader(vertShader));
  172. glColor3f(1, 0, 0);
  173. }
  174. static void
  175. ParseOptions(int argc, char *argv[])
  176. {
  177. int i;
  178. for (i = 1; i < argc; i++) {
  179. if (strcmp(argv[i], "-fs") == 0) {
  180. FragProgFile = argv[i+1];
  181. }
  182. else if (strcmp(argv[i], "-vs") == 0) {
  183. VertProgFile = argv[i+1];
  184. }
  185. }
  186. }
  187. int
  188. main(int argc, char *argv[])
  189. {
  190. WinSize[0] = WinSize[1] = 200;
  191. glutInit(&argc, argv);
  192. glutInitWindowSize(WinSize[0], WinSize[1]);
  193. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  194. win = glutCreateWindow(argv[0]);
  195. glewInit();
  196. glutReshapeFunc(Reshape);
  197. glutKeyboardFunc(Key);
  198. glutSpecialFunc(SpecialKey);
  199. glutDisplayFunc(Redisplay);
  200. if (anim)
  201. glutIdleFunc(Idle);
  202. ParseOptions(argc, argv);
  203. Init();
  204. glutMainLoop();
  205. return 0;
  206. }