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.0KB

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