Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fs-tri.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Test fragment shader */
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <GL/gl.h>
  8. #include <GL/glut.h>
  9. #include <GL/glext.h>
  10. #include "extfuncs.h"
  11. static GLuint fragShader;
  12. static GLuint vertShader;
  13. static GLuint program;
  14. static GLint win = 0;
  15. static GLfloat xpos = 0, ypos = 0;
  16. static void
  17. Redisplay(void)
  18. {
  19. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  20. glPushMatrix();
  21. glTranslatef(xpos, ypos, 0);
  22. glBegin(GL_TRIANGLES);
  23. glColor3f(1, 0, 0);
  24. glVertex2f(-0.9, -0.9);
  25. glColor3f(0, 1, 0);
  26. glVertex2f( 0.9, -0.9);
  27. glColor3f(0, 0, 1);
  28. glVertex2f( 0, 0.9);
  29. glEnd();
  30. glPopMatrix();
  31. glutSwapBuffers();
  32. }
  33. static void
  34. Reshape(int width, int height)
  35. {
  36. glViewport(0, 0, width, height);
  37. glMatrixMode(GL_PROJECTION);
  38. glLoadIdentity();
  39. glOrtho(-1, 1, -1, 1, -1, 1);
  40. glMatrixMode(GL_MODELVIEW);
  41. glLoadIdentity();
  42. }
  43. static void
  44. CleanUp(void)
  45. {
  46. glDeleteShader_func(fragShader);
  47. glDeleteShader_func(vertShader);
  48. glDeleteProgram_func(program);
  49. glutDestroyWindow(win);
  50. }
  51. static void
  52. Key(unsigned char key, int x, int y)
  53. {
  54. (void) x;
  55. (void) y;
  56. switch(key) {
  57. case 27:
  58. CleanUp();
  59. exit(0);
  60. break;
  61. }
  62. glutPostRedisplay();
  63. }
  64. static void
  65. SpecialKey(int key, int x, int y)
  66. {
  67. const GLfloat step = 0.1;
  68. (void) x;
  69. (void) y;
  70. switch(key) {
  71. case GLUT_KEY_UP:
  72. ypos += step;
  73. break;
  74. case GLUT_KEY_DOWN:
  75. ypos -= step;
  76. break;
  77. case GLUT_KEY_LEFT:
  78. xpos -= step;
  79. break;
  80. case GLUT_KEY_RIGHT:
  81. xpos += step;
  82. break;
  83. }
  84. glutPostRedisplay();
  85. }
  86. static void
  87. LoadAndCompileShader(GLuint shader, const char *text)
  88. {
  89. GLint stat;
  90. glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
  91. glCompileShader_func(shader);
  92. glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
  93. if (!stat) {
  94. GLchar log[1000];
  95. GLsizei len;
  96. glGetShaderInfoLog_func(shader, 1000, &len, log);
  97. fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log);
  98. exit(1);
  99. }
  100. }
  101. static void
  102. CheckLink(GLuint prog)
  103. {
  104. GLint stat;
  105. glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
  106. if (!stat) {
  107. GLchar log[1000];
  108. GLsizei len;
  109. glGetProgramInfoLog_func(prog, 1000, &len, log);
  110. fprintf(stderr, "Linker error:\n%s\n", log);
  111. }
  112. }
  113. static void
  114. Init(void)
  115. {
  116. /* fragment color is a function of fragment position: */
  117. static const char *fragShaderText =
  118. "void main() {\n"
  119. " gl_FragColor = gl_FragCoord * vec4(0.005); \n"
  120. " //gl_FragColor = gl_Color; \n"
  121. " //gl_FragColor = vec4(1, 0, 0.5, 0); \n"
  122. "}\n";
  123. #if 0
  124. static const char *vertShaderText =
  125. "varying vec3 normal;\n"
  126. "void main() {\n"
  127. " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
  128. " normal = gl_NormalMatrix * gl_Normal;\n"
  129. "}\n";
  130. #endif
  131. const char *version;
  132. version = (const char *) glGetString(GL_VERSION);
  133. if (version[0] != '2' || version[1] != '.') {
  134. printf("This program requires OpenGL 2.x, found %s\n", version);
  135. exit(1);
  136. }
  137. GetExtensionFuncs();
  138. fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
  139. LoadAndCompileShader(fragShader, fragShaderText);
  140. #if 0
  141. vertShader = glCreateShader_func(GL_VERTEX_SHADER);
  142. LoadAndCompileShader(vertShader, vertShaderText);
  143. #endif
  144. program = glCreateProgram_func();
  145. glAttachShader_func(program, fragShader);
  146. #if 0
  147. glAttachShader_func(program, vertShader);
  148. #endif
  149. glLinkProgram_func(program);
  150. CheckLink(program);
  151. glUseProgram_func(program);
  152. assert(glGetError() == 0);
  153. glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  154. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  155. }
  156. int
  157. main(int argc, char *argv[])
  158. {
  159. glutInit(&argc, argv);
  160. glutInitWindowPosition( 0, 0);
  161. glutInitWindowSize(200, 200);
  162. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  163. win = glutCreateWindow(argv[0]);
  164. glutReshapeFunc(Reshape);
  165. glutKeyboardFunc(Key);
  166. glutSpecialFunc(SpecialKey);
  167. glutDisplayFunc(Redisplay);
  168. Init();
  169. glutMainLoop();
  170. return 0;
  171. }