Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

trirast.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/glew.h>
  17. #include <GL/glut.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 = GL_TRUE;
  27. static GLfloat Zrot = 0.0f;
  28. static GLint uv0, uv1, uv2;
  29. static const GLfloat TriVerts[3][2] = {
  30. { 50, 50 },
  31. { 250, 50 },
  32. { 150, 250 }
  33. };
  34. static void
  35. RotateVerts(GLfloat a,
  36. GLuint n, const GLfloat vertsIn[][2], GLfloat vertsOut[][2])
  37. {
  38. GLuint i;
  39. GLfloat cx = WinWidth / 2, cy = WinHeight / 2;
  40. for (i = 0; i < n; i++) {
  41. float x = vertsIn[i][0] - cx;
  42. float y = vertsIn[i][1] - cy;
  43. vertsOut[i][0] = x * cos(a) + y * sin(a) + cx;
  44. vertsOut[i][1] = -x * sin(a) + y * cos(a) + cy;
  45. }
  46. }
  47. static void
  48. ComputeBounds(GLuint n, GLfloat vertsIn[][2],
  49. GLfloat *xmin, GLfloat *ymin,
  50. GLfloat *xmax, GLfloat *ymax)
  51. {
  52. GLuint i;
  53. *xmin = *xmax = vertsIn[0][0];
  54. *ymin = *ymax = vertsIn[0][1];
  55. for (i = 1; i < n; i++) {
  56. if (vertsIn[i][0] < *xmin)
  57. *xmin = vertsIn[i][0];
  58. else if (vertsIn[i][0] > *xmax)
  59. *xmax = vertsIn[i][0];
  60. if (vertsIn[i][1] < *ymin)
  61. *ymin = vertsIn[i][1];
  62. else if (vertsIn[i][1] > *ymax)
  63. *ymax = vertsIn[i][1];
  64. }
  65. }
  66. static void
  67. Redisplay(void)
  68. {
  69. GLfloat v[3][2], xmin, ymin, xmax, ymax;
  70. RotateVerts(Zrot, 3, TriVerts, v);
  71. ComputeBounds(3, v, &xmin, &ymin, &xmax, &ymax);
  72. glUniform2fv(uv0, 1, v[0]);
  73. glUniform2fv(uv1, 1, v[1]);
  74. glUniform2fv(uv2, 1, v[2]);
  75. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  76. glPushMatrix();
  77. glBegin(GL_POLYGON);
  78. glVertex2f(xmin, ymin);
  79. glVertex2f(xmax, ymin);
  80. glVertex2f(xmax, ymax);
  81. glVertex2f(xmin, ymax);
  82. glEnd();
  83. glPopMatrix();
  84. glutSwapBuffers();
  85. }
  86. static void
  87. Idle(void)
  88. {
  89. if (anim) {
  90. Zrot = glutGet(GLUT_ELAPSED_TIME) * 0.0005;
  91. glutPostRedisplay();
  92. }
  93. else
  94. abort();
  95. }
  96. static void
  97. Reshape(int width, int height)
  98. {
  99. glViewport(0, 0, width, height);
  100. glMatrixMode(GL_PROJECTION);
  101. glLoadIdentity();
  102. glOrtho(0, width, 0, height, -1, 1);
  103. glMatrixMode(GL_MODELVIEW);
  104. glLoadIdentity();
  105. }
  106. static void
  107. CleanUp(void)
  108. {
  109. glDeleteShader(fragShader);
  110. glDeleteShader(vertShader);
  111. glDeleteProgram(program);
  112. glutDestroyWindow(win);
  113. }
  114. static void
  115. Key(unsigned char key, int x, int y)
  116. {
  117. (void) x;
  118. (void) y;
  119. switch(key) {
  120. case ' ':
  121. case 'a':
  122. anim = !anim;
  123. if (anim)
  124. glutIdleFunc(Idle);
  125. else
  126. glutIdleFunc(NULL);
  127. break;
  128. case 'z':
  129. Zrot = 0;
  130. break;
  131. case 's':
  132. Zrot += 0.05;
  133. break;
  134. case 27:
  135. CleanUp();
  136. exit(0);
  137. break;
  138. }
  139. glutPostRedisplay();
  140. }
  141. static void
  142. Init(void)
  143. {
  144. static const char *fragShaderText =
  145. "uniform vec2 v0, v1, v2; \n"
  146. "float crs(const vec2 u, const vec2 v) \n"
  147. "{ \n"
  148. " return u.x * v.y - u.y * v.x; \n"
  149. "} \n"
  150. "\n"
  151. "void main() {\n"
  152. " vec2 p = gl_FragCoord.xy; \n"
  153. " if (crs(v1 - v0, p - v0) >= 0.0 && \n"
  154. " crs(v2 - v1, p - v1) >= 0.0 && \n"
  155. " crs(v0 - v2, p - v2) >= 0.0) \n"
  156. " gl_FragColor = vec4(1.0); \n"
  157. " else \n"
  158. " gl_FragColor = vec4(0.5); \n"
  159. "}\n";
  160. static const char *vertShaderText =
  161. "void main() {\n"
  162. " gl_Position = ftransform(); \n"
  163. "}\n";
  164. if (!ShadersSupported())
  165. exit(1);
  166. vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
  167. fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
  168. program = LinkShaders(vertShader, fragShader);
  169. glUseProgram(program);
  170. uv0 = glGetUniformLocation(program, "v0");
  171. uv1 = glGetUniformLocation(program, "v1");
  172. uv2 = glGetUniformLocation(program, "v2");
  173. printf("Uniforms: %d %d %d\n", uv0, uv1, uv2);
  174. /*assert(glGetError() == 0);*/
  175. glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  176. glEnable(GL_DEPTH_TEST);
  177. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  178. assert(glIsProgram(program));
  179. assert(glIsShader(fragShader));
  180. assert(glIsShader(vertShader));
  181. glColor3f(1, 0, 0);
  182. }
  183. static void
  184. ParseOptions(int argc, char *argv[])
  185. {
  186. int i;
  187. for (i = 1; i < argc; i++) {
  188. if (strcmp(argv[i], "-fs") == 0) {
  189. FragProgFile = argv[i+1];
  190. }
  191. else if (strcmp(argv[i], "-vs") == 0) {
  192. VertProgFile = argv[i+1];
  193. }
  194. }
  195. }
  196. int
  197. main(int argc, char *argv[])
  198. {
  199. glutInit(&argc, argv);
  200. glutInitWindowSize(WinWidth, WinHeight);
  201. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  202. win = glutCreateWindow(argv[0]);
  203. glewInit();
  204. glutReshapeFunc(Reshape);
  205. glutKeyboardFunc(Key);
  206. glutDisplayFunc(Redisplay);
  207. if (anim)
  208. glutIdleFunc(Idle);
  209. ParseOptions(argc, argv);
  210. Init();
  211. glutMainLoop();
  212. return 0;
  213. }