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.

identity.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Test very basic glsl functionality (identity vertex and fragment shaders).
  3. * Brian Paul & Stephane Marchesin
  4. */
  5. #include <assert.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <GL/glew.h>
  11. #include <GL/glut.h>
  12. #include "shaderutil.h"
  13. static char *FragProgFile = NULL;
  14. static char *VertProgFile = NULL;
  15. static GLuint fragShader;
  16. static GLuint vertShader;
  17. static GLuint program;
  18. static GLint win = 0;
  19. static GLboolean anim = GL_FALSE;
  20. static GLfloat xRot = 0.0f, yRot = 0.0f;
  21. static int w,h;
  22. static void
  23. Redisplay(void)
  24. {
  25. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  26. glBegin(GL_TRIANGLES);
  27. glColor3f(.8,0,0);
  28. glVertex3f(-0.9, -0.9, 0.0);
  29. glColor3f(0,.9,0);
  30. glVertex3f( 0.9, -0.9, 0.0);
  31. glColor3f(0,0,.7);
  32. glVertex3f( 0.0, 0.9, 0.0);
  33. glEnd();
  34. glutSwapBuffers();
  35. }
  36. static void
  37. Idle(void)
  38. {
  39. yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
  40. glutPostRedisplay();
  41. }
  42. static void
  43. Reshape(int width, int height)
  44. {
  45. glViewport(0, 0, width, height);
  46. glMatrixMode(GL_PROJECTION);
  47. glLoadIdentity();
  48. glMatrixMode(GL_MODELVIEW);
  49. glLoadIdentity();
  50. w = width;
  51. h = height;
  52. }
  53. static void
  54. CleanUp(void)
  55. {
  56. glDeleteShader(fragShader);
  57. glDeleteShader(vertShader);
  58. glDeleteProgram(program);
  59. glutDestroyWindow(win);
  60. }
  61. static void
  62. Key(unsigned char key, int x, int y)
  63. {
  64. (void) x;
  65. (void) y;
  66. switch(key) {
  67. case ' ':
  68. case 'a':
  69. anim = !anim;
  70. if (anim)
  71. glutIdleFunc(Idle);
  72. else
  73. glutIdleFunc(NULL);
  74. break;
  75. case 27:
  76. CleanUp();
  77. exit(0);
  78. break;
  79. }
  80. glutPostRedisplay();
  81. }
  82. static void
  83. SpecialKey(int key, int x, int y)
  84. {
  85. const GLfloat step = 3.0f;
  86. (void) x;
  87. (void) y;
  88. switch(key) {
  89. case GLUT_KEY_UP:
  90. xRot -= step;
  91. break;
  92. case GLUT_KEY_DOWN:
  93. xRot += step;
  94. break;
  95. case GLUT_KEY_LEFT:
  96. yRot -= step;
  97. break;
  98. case GLUT_KEY_RIGHT:
  99. yRot += step;
  100. break;
  101. }
  102. glutPostRedisplay();
  103. }
  104. static void
  105. Init(void)
  106. {
  107. static const char *fragShaderText =
  108. "void main() {\n"
  109. " gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n"
  110. "}\n";
  111. static const char *vertShaderText =
  112. "void main() {\n"
  113. " gl_Position = gl_Vertex;\n"
  114. "}\n";
  115. if (!ShadersSupported())
  116. exit(1);
  117. if (FragProgFile)
  118. fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
  119. else
  120. fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
  121. if (VertProgFile)
  122. vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
  123. else
  124. vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
  125. program = LinkShaders(vertShader, fragShader);
  126. glUseProgram(program);
  127. /*assert(glGetError() == 0);*/
  128. glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  129. glEnable(GL_DEPTH_TEST);
  130. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  131. assert(glIsProgram(program));
  132. assert(glIsShader(fragShader));
  133. assert(glIsShader(vertShader));
  134. glColor3f(1, 0, 0);
  135. }
  136. static void
  137. ParseOptions(int argc, char *argv[])
  138. {
  139. int i;
  140. for (i = 1; i < argc; i++) {
  141. if (strcmp(argv[i], "-fs") == 0) {
  142. FragProgFile = argv[i+1];
  143. }
  144. else if (strcmp(argv[i], "-vs") == 0) {
  145. VertProgFile = argv[i+1];
  146. }
  147. }
  148. }
  149. int
  150. main(int argc, char *argv[])
  151. {
  152. glutInit(&argc, argv);
  153. glutInitWindowSize(200, 200);
  154. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  155. win = glutCreateWindow(argv[0]);
  156. glewInit();
  157. glutReshapeFunc(Reshape);
  158. glutKeyboardFunc(Key);
  159. glutSpecialFunc(SpecialKey);
  160. glutDisplayFunc(Redisplay);
  161. if (anim)
  162. glutIdleFunc(Idle);
  163. ParseOptions(argc, argv);
  164. Init();
  165. glutMainLoop();
  166. return 0;
  167. }