Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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/gl.h>
  15. #include <GL/glut.h>
  16. #include <GL/glext.h>
  17. #include "extfuncs.h"
  18. static char *FragProgFile = NULL;
  19. static char *VertProgFile = NULL;
  20. static GLuint fragShader;
  21. static GLuint vertShader;
  22. static GLuint program;
  23. static GLuint SphereList, RectList, CurList;
  24. static GLint win = 0;
  25. static GLboolean anim = GL_TRUE;
  26. static GLfloat xRot = 0.0f, yRot = 0.0f;
  27. static void
  28. Redisplay(void)
  29. {
  30. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  31. glPushMatrix();
  32. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  33. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  34. glCallList(CurList);
  35. glPopMatrix();
  36. glutSwapBuffers();
  37. }
  38. static void
  39. Idle(void)
  40. {
  41. yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
  42. glutPostRedisplay();
  43. }
  44. static void
  45. Reshape(int width, int height)
  46. {
  47. glViewport(0, 0, width, height);
  48. glMatrixMode(GL_PROJECTION);
  49. glLoadIdentity();
  50. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  51. glMatrixMode(GL_MODELVIEW);
  52. glLoadIdentity();
  53. glTranslatef(0.0f, 0.0f, -15.0f);
  54. }
  55. static void
  56. CleanUp(void)
  57. {
  58. glDeleteShader_func(fragShader);
  59. glDeleteShader_func(vertShader);
  60. glDeleteProgram_func(program);
  61. glutDestroyWindow(win);
  62. }
  63. static void
  64. Key(unsigned char key, int x, int y)
  65. {
  66. (void) x;
  67. (void) y;
  68. switch(key) {
  69. case ' ':
  70. case 'a':
  71. anim = !anim;
  72. if (anim)
  73. glutIdleFunc(Idle);
  74. else
  75. glutIdleFunc(NULL);
  76. break;
  77. case 'o':
  78. if (CurList == SphereList)
  79. CurList = RectList;
  80. else
  81. CurList = SphereList;
  82. break;
  83. case 27:
  84. CleanUp();
  85. exit(0);
  86. break;
  87. }
  88. glutPostRedisplay();
  89. }
  90. static void
  91. SpecialKey(int key, int x, int y)
  92. {
  93. const GLfloat step = 3.0f;
  94. (void) x;
  95. (void) y;
  96. switch(key) {
  97. case GLUT_KEY_UP:
  98. xRot -= step;
  99. break;
  100. case GLUT_KEY_DOWN:
  101. xRot += step;
  102. break;
  103. case GLUT_KEY_LEFT:
  104. yRot -= step;
  105. break;
  106. case GLUT_KEY_RIGHT:
  107. yRot += step;
  108. break;
  109. }
  110. glutPostRedisplay();
  111. }
  112. static void
  113. MakeSphere(void)
  114. {
  115. GLUquadricObj *obj = gluNewQuadric();
  116. SphereList = glGenLists(1);
  117. gluQuadricTexture(obj, GL_TRUE);
  118. glNewList(SphereList, GL_COMPILE);
  119. gluSphere(obj, 2.0f, 30, 15);
  120. glEndList();
  121. }
  122. static void
  123. MakeRect(void)
  124. {
  125. RectList = glGenLists(1);
  126. glNewList(RectList, GL_COMPILE);
  127. glBegin(GL_POLYGON);
  128. glTexCoord2f(0, 0); glVertex2f(-2, -2);
  129. glTexCoord2f(1, 0); glVertex2f( 2, -2);
  130. glTexCoord2f(1, 1); glVertex2f( 2, 2);
  131. glTexCoord2f(0, 1); glVertex2f(-2, 2);
  132. glEnd();
  133. glEndList();
  134. }
  135. static void
  136. LoadAndCompileShader(GLuint shader, const char *text)
  137. {
  138. GLint stat;
  139. glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
  140. glCompileShader_func(shader);
  141. glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
  142. if (!stat) {
  143. GLchar log[1000];
  144. GLsizei len;
  145. glGetShaderInfoLog_func(shader, 1000, &len, log);
  146. fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log);
  147. exit(1);
  148. }
  149. }
  150. /**
  151. * Read a shader from a file.
  152. */
  153. static void
  154. ReadShader(GLuint shader, const char *filename)
  155. {
  156. const int max = 100*1000;
  157. int n;
  158. char *buffer = (char*) malloc(max);
  159. FILE *f = fopen(filename, "r");
  160. if (!f) {
  161. fprintf(stderr, "fslight: Unable to open shader file %s\n", filename);
  162. exit(1);
  163. }
  164. n = fread(buffer, 1, max, f);
  165. printf("fslight: read %d bytes from shader file %s\n", n, filename);
  166. if (n > 0) {
  167. buffer[n] = 0;
  168. LoadAndCompileShader(shader, buffer);
  169. }
  170. fclose(f);
  171. free(buffer);
  172. }
  173. static void
  174. CheckLink(GLuint prog)
  175. {
  176. GLint stat;
  177. glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
  178. if (!stat) {
  179. GLchar log[1000];
  180. GLsizei len;
  181. glGetProgramInfoLog_func(prog, 1000, &len, log);
  182. fprintf(stderr, "Linker error:\n%s\n", log);
  183. }
  184. }
  185. static void
  186. Init(void)
  187. {
  188. static const char *fragShaderText =
  189. "void main() {\n"
  190. " gl_FragColor = abs(dFdy(gl_TexCoord[0])) * 50.0;\n"
  191. " // gl_FragColor = gl_TexCoord[0];\n"
  192. "}\n";
  193. static const char *vertShaderText =
  194. "void main() {\n"
  195. " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
  196. " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
  197. "}\n";
  198. const char *version;
  199. version = (const char *) glGetString(GL_VERSION);
  200. if (version[0] != '2' || version[1] != '.') {
  201. printf("This program requires OpenGL 2.x, found %s\n", version);
  202. exit(1);
  203. }
  204. GetExtensionFuncs();
  205. fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
  206. if (FragProgFile)
  207. ReadShader(fragShader, FragProgFile);
  208. else
  209. LoadAndCompileShader(fragShader, fragShaderText);
  210. vertShader = glCreateShader_func(GL_VERTEX_SHADER);
  211. if (VertProgFile)
  212. ReadShader(vertShader, VertProgFile);
  213. else
  214. LoadAndCompileShader(vertShader, vertShaderText);
  215. program = glCreateProgram_func();
  216. glAttachShader_func(program, fragShader);
  217. glAttachShader_func(program, vertShader);
  218. glLinkProgram_func(program);
  219. CheckLink(program);
  220. glUseProgram_func(program);
  221. /*assert(glGetError() == 0);*/
  222. glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  223. glEnable(GL_DEPTH_TEST);
  224. MakeSphere();
  225. MakeRect();
  226. CurList = SphereList;
  227. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  228. assert(glIsProgram_func(program));
  229. assert(glIsShader_func(fragShader));
  230. assert(glIsShader_func(vertShader));
  231. glColor3f(1, 0, 0);
  232. }
  233. static void
  234. ParseOptions(int argc, char *argv[])
  235. {
  236. int i;
  237. for (i = 1; i < argc; i++) {
  238. if (strcmp(argv[i], "-fs") == 0) {
  239. FragProgFile = argv[i+1];
  240. }
  241. else if (strcmp(argv[i], "-vs") == 0) {
  242. VertProgFile = argv[i+1];
  243. }
  244. }
  245. }
  246. int
  247. main(int argc, char *argv[])
  248. {
  249. glutInit(&argc, argv);
  250. glutInitWindowPosition( 0, 0);
  251. glutInitWindowSize(200, 200);
  252. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  253. win = glutCreateWindow(argv[0]);
  254. glutReshapeFunc(Reshape);
  255. glutKeyboardFunc(Key);
  256. glutSpecialFunc(SpecialKey);
  257. glutDisplayFunc(Redisplay);
  258. if (anim)
  259. glutIdleFunc(Idle);
  260. ParseOptions(argc, argv);
  261. Init();
  262. glutMainLoop();
  263. return 0;
  264. }