Clone of mesa.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * Demonstration of doing triangle rasterization with a fragment program.
  3. * Basic idea:
  4. * 1. Draw screen-aligned quad / bounding box around the triangle verts.
  5. * 2. For each pixel in the quad, determine if pixel is inside/outside
  6. * the triangle edges.
  7. *
  8. * Brian Paul
  9. * 1 Aug 2007
  10. */
  11. #include <assert.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <GL/gl.h>
  17. #include <GL/glut.h>
  18. #include <GL/glext.h>
  19. #include "extfuncs.h"
  20. #include "shaderutil.h"
  21. static GLint WinWidth = 300, WinHeight = 300;
  22. static char *FragProgFile = NULL;
  23. static char *VertProgFile = NULL;
  24. static GLuint fragShader;
  25. static GLuint vertShader;
  26. static GLuint program;
  27. static GLint win = 0;
  28. static GLboolean anim = GL_TRUE;
  29. static GLfloat Zrot = 0.0f;
  30. static GLint uv0, uv1, uv2;
  31. static const GLfloat TriVerts[3][2] = {
  32. { 50, 50 },
  33. { 250, 50 },
  34. { 150, 250 }
  35. };
  36. static void
  37. RotateVerts(GLfloat a,
  38. GLuint n, const GLfloat vertsIn[][2], GLfloat vertsOut[][2])
  39. {
  40. GLuint i;
  41. GLfloat cx = WinWidth / 2, cy = WinHeight / 2;
  42. for (i = 0; i < n; i++) {
  43. float x = vertsIn[i][0] - cx;
  44. float y = vertsIn[i][1] - cy;
  45. vertsOut[i][0] = x * cos(a) + y * sin(a) + cx;
  46. vertsOut[i][1] = -x * sin(a) + y * cos(a) + cy;
  47. }
  48. }
  49. static void
  50. ComputeBounds(GLuint n, GLfloat vertsIn[][2],
  51. GLfloat *xmin, GLfloat *ymin,
  52. GLfloat *xmax, GLfloat *ymax)
  53. {
  54. GLuint i;
  55. *xmin = *xmax = vertsIn[0][0];
  56. *ymin = *ymax = vertsIn[0][1];
  57. for (i = 1; i < n; i++) {
  58. if (vertsIn[i][0] < *xmin)
  59. *xmin = vertsIn[i][0];
  60. else if (vertsIn[i][0] > *xmax)
  61. *xmax = vertsIn[i][0];
  62. if (vertsIn[i][1] < *ymin)
  63. *ymin = vertsIn[i][1];
  64. else if (vertsIn[i][1] > *ymax)
  65. *ymax = vertsIn[i][1];
  66. }
  67. }
  68. static void
  69. Redisplay(void)
  70. {
  71. GLfloat v[3][2], xmin, ymin, xmax, ymax;
  72. RotateVerts(Zrot, 3, TriVerts, v);
  73. ComputeBounds(3, v, &xmin, &ymin, &xmax, &ymax);
  74. glUniform2fv_func(uv0, 1, v[0]);
  75. glUniform2fv_func(uv1, 1, v[1]);
  76. glUniform2fv_func(uv2, 1, v[2]);
  77. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  78. glPushMatrix();
  79. glBegin(GL_POLYGON);
  80. glVertex2f(xmin, ymin);
  81. glVertex2f(xmax, ymin);
  82. glVertex2f(xmax, ymax);
  83. glVertex2f(xmin, ymax);
  84. glEnd();
  85. glPopMatrix();
  86. glutSwapBuffers();
  87. }
  88. static void
  89. Idle(void)
  90. {
  91. if (anim) {
  92. Zrot = glutGet(GLUT_ELAPSED_TIME) * 0.0005;
  93. glutPostRedisplay();
  94. }
  95. else
  96. abort();
  97. }
  98. static void
  99. Reshape(int width, int height)
  100. {
  101. glViewport(0, 0, width, height);
  102. glMatrixMode(GL_PROJECTION);
  103. glLoadIdentity();
  104. glOrtho(0, width, 0, height, -1, 1);
  105. glMatrixMode(GL_MODELVIEW);
  106. glLoadIdentity();
  107. }
  108. static void
  109. CleanUp(void)
  110. {
  111. glDeleteShader_func(fragShader);
  112. glDeleteShader_func(vertShader);
  113. glDeleteProgram_func(program);
  114. glutDestroyWindow(win);
  115. }
  116. static void
  117. Key(unsigned char key, int x, int y)
  118. {
  119. (void) x;
  120. (void) y;
  121. switch(key) {
  122. case ' ':
  123. case 'a':
  124. anim = !anim;
  125. if (anim)
  126. glutIdleFunc(Idle);
  127. else
  128. glutIdleFunc(NULL);
  129. break;
  130. case 'z':
  131. Zrot = 0;
  132. break;
  133. case 's':
  134. Zrot += 0.05;
  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 vec2 v0, v1, v2; \n"
  148. "float crs(const vec2 u, const vec2 v) \n"
  149. "{ \n"
  150. " return u.x * v.y - u.y * v.x; \n"
  151. "} \n"
  152. "\n"
  153. "void main() {\n"
  154. " vec2 p = gl_FragCoord.xy; \n"
  155. " if (crs(v1 - v0, p - v0) >= 0 && \n"
  156. " crs(v2 - v1, p - v1) >= 0 && \n"
  157. " crs(v0 - v2, p - v2) >= 0) \n"
  158. " gl_FragColor = vec4(1.0); \n"
  159. " else \n"
  160. " gl_FragColor = vec4(0.5); \n"
  161. "}\n";
  162. static const char *vertShaderText =
  163. "void main() {\n"
  164. " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
  165. "}\n";
  166. if (!ShadersSupported())
  167. exit(1);
  168. GetExtensionFuncs();
  169. vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
  170. fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
  171. program = LinkShaders(vertShader, fragShader);
  172. glUseProgram_func(program);
  173. uv0 = glGetUniformLocation_func(program, "v0");
  174. uv1 = glGetUniformLocation_func(program, "v1");
  175. uv2 = glGetUniformLocation_func(program, "v2");
  176. printf("Uniforms: %d %d %d\n", uv0, uv1, uv2);
  177. /*assert(glGetError() == 0);*/
  178. glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  179. glEnable(GL_DEPTH_TEST);
  180. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  181. assert(glIsProgram_func(program));
  182. assert(glIsShader_func(fragShader));
  183. assert(glIsShader_func(vertShader));
  184. glColor3f(1, 0, 0);
  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. int
  200. main(int argc, char *argv[])
  201. {
  202. glutInit(&argc, argv);
  203. glutInitWindowPosition( 0, 0);
  204. glutInitWindowSize(WinWidth, WinHeight);
  205. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  206. win = glutCreateWindow(argv[0]);
  207. glutReshapeFunc(Reshape);
  208. glutKeyboardFunc(Key);
  209. glutDisplayFunc(Redisplay);
  210. if (anim)
  211. glutIdleFunc(Idle);
  212. ParseOptions(argc, argv);
  213. Init();
  214. glutMainLoop();
  215. return 0;
  216. }