Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

twoside.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Test two-sided lighting with shaders.
  3. * Both GL_VERTEX_PROGRAM_TWO_SIDE and gl_FrontFacing can be tested
  4. * (see keys below).
  5. *
  6. * Brian Paul
  7. * 18 Dec 2007
  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. #include "shaderutil.h"
  19. static GLint WinWidth = 300, WinHeight = 300;
  20. static char *FragProgFile = NULL;
  21. static char *VertProgFile = NULL;
  22. static GLuint fragShader;
  23. static GLuint vertShader;
  24. static GLuint program;
  25. static GLint win = 0;
  26. static GLboolean anim = 0*GL_TRUE;
  27. static GLboolean DetermineInFragProg = GL_TRUE;
  28. static GLfloat Yrot = 0.0f;
  29. static GLint u_fragface;
  30. static GLenum FrontWinding = GL_CCW;
  31. static int prevTime = 0;
  32. static const GLfloat Red[4] = {1, 0, 0, 0};
  33. static const GLfloat Green[4] = {0, 1, 0, 0};
  34. static void
  35. Redisplay(void)
  36. {
  37. float xmin = -1, xmax = 1, ymin = -1, ymax = 1;
  38. glFrontFace(FrontWinding);
  39. if (DetermineInFragProg) {
  40. glUniform1i_func(u_fragface, 1);
  41. glDisable(GL_VERTEX_PROGRAM_TWO_SIDE);
  42. }
  43. else {
  44. glUniform1i_func(u_fragface, 0);
  45. glEnable(GL_VERTEX_PROGRAM_TWO_SIDE);
  46. }
  47. glClear(GL_COLOR_BUFFER_BIT);
  48. glPushMatrix();
  49. glRotatef(Yrot, 0, 1, 0);
  50. glBegin(GL_POLYGON);
  51. glColor4fv(Red);
  52. glSecondaryColor3fv_func(Green);
  53. glVertex2f(xmin, ymin);
  54. glVertex2f(xmax, ymin);
  55. glVertex2f(xmax, ymax);
  56. glVertex2f(xmin, ymax);
  57. glEnd();
  58. glPopMatrix();
  59. glutSwapBuffers();
  60. }
  61. static void
  62. Idle(void)
  63. {
  64. int curTime = glutGet(GLUT_ELAPSED_TIME);
  65. int dt = curTime - prevTime;
  66. if (prevTime == 0) {
  67. prevTime = curTime;
  68. return;
  69. }
  70. prevTime = curTime;
  71. Yrot += dt * 0.1;
  72. glutPostRedisplay();
  73. }
  74. static void
  75. Reshape(int width, int height)
  76. {
  77. float ar = (float) width / height;
  78. glViewport(0, 0, width, height);
  79. glMatrixMode(GL_PROJECTION);
  80. glLoadIdentity();
  81. glFrustum(-ar, ar, -1, 1, 5, 15);
  82. glMatrixMode(GL_MODELVIEW);
  83. glLoadIdentity();
  84. glTranslatef(0, 0, -10);
  85. }
  86. static void
  87. CleanUp(void)
  88. {
  89. glDeleteShader_func(fragShader);
  90. glDeleteShader_func(vertShader);
  91. glDeleteProgram_func(program);
  92. glutDestroyWindow(win);
  93. }
  94. static void
  95. Key(unsigned char key, int x, int y)
  96. {
  97. (void) x;
  98. (void) y;
  99. switch(key) {
  100. case ' ':
  101. case 'a':
  102. anim = !anim;
  103. if (anim) {
  104. prevTime = glutGet(GLUT_ELAPSED_TIME);
  105. glutIdleFunc(Idle);
  106. }
  107. else
  108. glutIdleFunc(NULL);
  109. break;
  110. case 'f':
  111. printf("Using frag shader gl_FrontFacing\n");
  112. DetermineInFragProg = GL_TRUE;
  113. break;
  114. case 'v':
  115. printf("Using vert shader Two-sided lighting\n");
  116. DetermineInFragProg = GL_FALSE;
  117. break;
  118. case 'r':
  119. Yrot = 0;
  120. anim = 0;
  121. glutIdleFunc(NULL);
  122. break;
  123. case 's':
  124. Yrot += 5;
  125. break;
  126. case 'w':
  127. if (FrontWinding == GL_CCW) {
  128. FrontWinding = GL_CW;
  129. printf("FrontFace = GL_CW\n");
  130. }
  131. else {
  132. FrontWinding = GL_CCW;
  133. printf("FrontFace = GL_CCW\n");
  134. }
  135. break;
  136. case 27:
  137. CleanUp();
  138. exit(0);
  139. break;
  140. }
  141. glutPostRedisplay();
  142. }
  143. static void
  144. Init(void)
  145. {
  146. static const char *fragShaderText =
  147. "uniform bool fragface; \n"
  148. "void main() { \n"
  149. " if (!fragface || gl_FrontFacing) { \n"
  150. " gl_FragColor = gl_Color; \n"
  151. " } \n"
  152. " else { \n"
  153. " gl_FragColor = 0.8 * gl_SecondaryColor; \n"
  154. " } \n"
  155. "} \n";
  156. static const char *vertShaderText =
  157. "uniform bool fragface; \n"
  158. "void main() { \n"
  159. " gl_FrontColor = gl_Color; \n"
  160. " if (fragface) { \n"
  161. " // front/back chosen in frag prog \n"
  162. " gl_FrontSecondaryColor = gl_SecondaryColor; \n"
  163. " } \n"
  164. " else { \n"
  165. " // front/back chosen in prim setup \n"
  166. " gl_BackColor = gl_SecondaryColor; \n"
  167. " } \n"
  168. " gl_Position = ftransform(); \n"
  169. "} \n";
  170. if (!ShadersSupported())
  171. exit(1);
  172. GetExtensionFuncs();
  173. vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
  174. fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
  175. program = LinkShaders(vertShader, fragShader);
  176. glUseProgram_func(program);
  177. u_fragface = glGetUniformLocation_func(program, "fragface");
  178. printf("Uniforms: %d\n", u_fragface);
  179. /*assert(glGetError() == 0);*/
  180. glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  181. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  182. assert(glIsProgram_func(program));
  183. assert(glIsShader_func(fragShader));
  184. assert(glIsShader_func(vertShader));
  185. }
  186. static void
  187. ParseOptions(int argc, char *argv[])
  188. {
  189. int i;
  190. for (i = 1; i < argc; i++) {
  191. if (strcmp(argv[i], "-fs") == 0) {
  192. FragProgFile = argv[i+1];
  193. }
  194. else if (strcmp(argv[i], "-vs") == 0) {
  195. VertProgFile = argv[i+1];
  196. }
  197. }
  198. }
  199. static void
  200. Usage(void)
  201. {
  202. printf("Keys:\n");
  203. printf(" f - do front/back determination in fragment shader\n");
  204. printf(" v - do front/back determination in vertex shader\n");
  205. printf(" r - reset, show front\n");
  206. printf(" a - toggle animation\n");
  207. printf(" s - step rotation\n");
  208. printf(" w - toggle CW, CCW front-face winding\n");
  209. }
  210. int
  211. main(int argc, char *argv[])
  212. {
  213. glutInit(&argc, argv);
  214. glutInitWindowPosition( 0, 0);
  215. glutInitWindowSize(WinWidth, WinHeight);
  216. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  217. win = glutCreateWindow(argv[0]);
  218. glutReshapeFunc(Reshape);
  219. glutKeyboardFunc(Key);
  220. glutDisplayFunc(Redisplay);
  221. if (anim)
  222. glutIdleFunc(Idle);
  223. ParseOptions(argc, argv);
  224. Init();
  225. Usage();
  226. glutMainLoop();
  227. return 0;
  228. }