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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * "Brick" shader demo. Uses the example shaders from chapter 6 of
  3. * the OpenGL Shading Language "orange" book.
  4. * 10 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/gl.h>
  12. #include <GL/glut.h>
  13. #include <GL/glext.h>
  14. #include "extfuncs.h"
  15. static char *FragProgFile = "CH06-brick.frag.txt";
  16. static char *VertProgFile = "CH06-brick.vert.txt";
  17. /* program/shader objects */
  18. static GLuint fragShader;
  19. static GLuint vertShader;
  20. static GLuint program;
  21. struct uniform_info {
  22. const char *name;
  23. GLuint size;
  24. GLint location;
  25. GLfloat value[4];
  26. };
  27. static struct uniform_info Uniforms[] = {
  28. /* vert */
  29. { "LightPosition", 3, -1, { 0.1, 0.1, 9.0, 0} },
  30. /* frag */
  31. { "BrickColor", 3, -1, { 0.8, 0.2, 0.2, 0 } },
  32. { "MortarColor", 3, -1, { 0.6, 0.6, 0.6, 0 } },
  33. { "BrickSize", 2, -1, { 1.0, 0.3, 0, 0 } },
  34. { "BrickPct", 2, -1, { 0.9, 0.8, 0, 0 } },
  35. { NULL, 0, 0, { 0, 0, 0, 0 } }
  36. };
  37. static GLint win = 0;
  38. static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
  39. static void
  40. Redisplay(void)
  41. {
  42. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  43. glPushMatrix();
  44. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  45. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  46. glRotatef(zRot, 0.0f, 0.0f, 1.0f);
  47. glBegin(GL_POLYGON);
  48. glTexCoord2f(0, 0); glVertex2f(-2, -2);
  49. glTexCoord2f(1, 0); glVertex2f( 2, -2);
  50. glTexCoord2f(1, 1); glVertex2f( 2, 2);
  51. glTexCoord2f(0, 1); glVertex2f(-2, 2);
  52. glEnd();
  53. glPopMatrix();
  54. glutSwapBuffers();
  55. }
  56. static void
  57. Reshape(int width, int height)
  58. {
  59. glViewport(0, 0, width, height);
  60. glMatrixMode(GL_PROJECTION);
  61. glLoadIdentity();
  62. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  63. glMatrixMode(GL_MODELVIEW);
  64. glLoadIdentity();
  65. glTranslatef(0.0f, 0.0f, -15.0f);
  66. }
  67. static void
  68. CleanUp(void)
  69. {
  70. glDeleteShader_func(fragShader);
  71. glDeleteShader_func(vertShader);
  72. glDeleteProgram_func(program);
  73. glutDestroyWindow(win);
  74. }
  75. static void
  76. Key(unsigned char key, int x, int y)
  77. {
  78. (void) x;
  79. (void) y;
  80. switch(key) {
  81. case 'z':
  82. zRot -= 1.0;
  83. break;
  84. case 'Z':
  85. zRot += 1.0;
  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. LoadAndCompileShader(GLuint shader, const char *text)
  118. {
  119. GLint stat;
  120. glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
  121. glCompileShader_func(shader);
  122. glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
  123. if (!stat) {
  124. GLchar log[1000];
  125. GLsizei len;
  126. glGetShaderInfoLog_func(shader, 1000, &len, log);
  127. fprintf(stderr, "brick: problem compiling shader: %s\n", log);
  128. exit(1);
  129. }
  130. else {
  131. printf("Shader compiled OK\n");
  132. }
  133. }
  134. /**
  135. * Read a shader from a file.
  136. */
  137. static void
  138. ReadShader(GLuint shader, const char *filename)
  139. {
  140. const int max = 100*1000;
  141. int n;
  142. char *buffer = (char*) malloc(max);
  143. FILE *f = fopen(filename, "r");
  144. if (!f) {
  145. fprintf(stderr, "brick: Unable to open shader file %s\n", filename);
  146. exit(1);
  147. }
  148. n = fread(buffer, 1, max, f);
  149. printf("brick: read %d bytes from shader file %s\n", n, filename);
  150. if (n > 0) {
  151. buffer[n] = 0;
  152. LoadAndCompileShader(shader, buffer);
  153. }
  154. fclose(f);
  155. free(buffer);
  156. }
  157. static void
  158. CheckLink(GLuint prog)
  159. {
  160. GLint stat;
  161. glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
  162. if (!stat) {
  163. GLchar log[1000];
  164. GLsizei len;
  165. glGetProgramInfoLog_func(prog, 1000, &len, log);
  166. fprintf(stderr, "Linker error:\n%s\n", log);
  167. }
  168. else {
  169. fprintf(stderr, "Link success!\n");
  170. }
  171. }
  172. static void
  173. Init(void)
  174. {
  175. const char *version;
  176. GLint i;
  177. version = (const char *) glGetString(GL_VERSION);
  178. if (version[0] != '2' || version[1] != '.') {
  179. printf("Warning: this program expects OpenGL 2.0\n");
  180. /*exit(1);*/
  181. }
  182. GetExtensionFuncs();
  183. vertShader = glCreateShader_func(GL_VERTEX_SHADER);
  184. ReadShader(vertShader, VertProgFile);
  185. fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
  186. ReadShader(fragShader, FragProgFile);
  187. program = glCreateProgram_func();
  188. glAttachShader_func(program, fragShader);
  189. glAttachShader_func(program, vertShader);
  190. glLinkProgram_func(program);
  191. CheckLink(program);
  192. glUseProgram_func(program);
  193. for (i = 0; Uniforms[i].name; i++) {
  194. Uniforms[i].location
  195. = glGetUniformLocation_func(program, Uniforms[i].name);
  196. printf("Uniform %s location: %d\n", Uniforms[i].name,
  197. Uniforms[i].location);
  198. switch (Uniforms[i].size) {
  199. case 1:
  200. glUniform1fv_func(Uniforms[i].location, 1, Uniforms[i].value);
  201. break;
  202. case 2:
  203. glUniform2fv_func(Uniforms[i].location, 1, Uniforms[i].value);
  204. break;
  205. case 3:
  206. glUniform3fv_func(Uniforms[i].location, 1, Uniforms[i].value);
  207. break;
  208. case 4:
  209. glUniform4fv_func(Uniforms[i].location, 1, Uniforms[i].value);
  210. break;
  211. default:
  212. abort();
  213. }
  214. }
  215. assert(glGetError() == 0);
  216. glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
  217. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  218. assert(glIsProgram_func(program));
  219. assert(glIsShader_func(fragShader));
  220. assert(glIsShader_func(vertShader));
  221. glColor3f(1, 0, 0);
  222. }
  223. static void
  224. ParseOptions(int argc, char *argv[])
  225. {
  226. int i;
  227. for (i = 1; i < argc; i++) {
  228. if (strcmp(argv[i], "-fs") == 0) {
  229. FragProgFile = argv[i+1];
  230. }
  231. else if (strcmp(argv[i], "-vs") == 0) {
  232. VertProgFile = argv[i+1];
  233. }
  234. }
  235. }
  236. int
  237. main(int argc, char *argv[])
  238. {
  239. glutInit(&argc, argv);
  240. glutInitWindowPosition( 0, 0);
  241. glutInitWindowSize(400, 400);
  242. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  243. win = glutCreateWindow(argv[0]);
  244. glutReshapeFunc(Reshape);
  245. glutKeyboardFunc(Key);
  246. glutSpecialFunc(SpecialKey);
  247. glutDisplayFunc(Redisplay);
  248. ParseOptions(argc, argv);
  249. Init();
  250. glutMainLoop();
  251. return 0;
  252. }