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.

vert-or-frag-only.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Draw two quads, one using only a vertex shader, the other only with a
  3. * fragment shader. They should appear the same.
  4. * 17 Dec 2008
  5. * Brian Paul
  6. */
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glew.h>
  13. #include <GL/glut.h>
  14. #include "shaderutil.h"
  15. static char *FragProgFile = NULL;
  16. static char *VertProgFile = NULL;
  17. static GLuint FragShader;
  18. static GLuint VertShader;
  19. static GLuint VertProgram; /* w/out vertex shader */
  20. static GLuint FragProgram; /* w/out fragment shader */
  21. static GLint Win = 0;
  22. static void
  23. DrawQuadColor(void)
  24. {
  25. glBegin(GL_QUADS);
  26. glColor3f(1, 0, 0); glVertex2f(-1, -1);
  27. glColor3f(0, 1, 0); glVertex2f( 1, -1);
  28. glColor3f(0, 0, 1); glVertex2f( 1, 1);
  29. glColor3f(1, 0, 1); glVertex2f(-1, 1);
  30. glEnd();
  31. }
  32. /** as above, but specify color via texcoords */
  33. static void
  34. DrawQuadTex(void)
  35. {
  36. glBegin(GL_QUADS);
  37. glTexCoord3f(1, 0, 0); glVertex2f(-1, -1);
  38. glTexCoord3f(0, 1, 0); glVertex2f( 1, -1);
  39. glTexCoord3f(0, 0, 1); glVertex2f( 1, 1);
  40. glTexCoord3f(1, 0, 1); glVertex2f(-1, 1);
  41. glEnd();
  42. }
  43. static void
  44. Redisplay(void)
  45. {
  46. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  47. /* render with vertex shader only */
  48. glUseProgram(VertProgram);
  49. glPushMatrix();
  50. glTranslatef(-1.5, 0, 0);
  51. DrawQuadTex();
  52. glPopMatrix();
  53. /* render with fragment shader only */
  54. glUseProgram(FragProgram);
  55. glPushMatrix();
  56. glTranslatef(+1.5, 0, 0);
  57. DrawQuadColor();
  58. glPopMatrix();
  59. glutSwapBuffers();
  60. }
  61. static void
  62. Reshape(int width, int height)
  63. {
  64. glViewport(0, 0, width, height);
  65. glMatrixMode(GL_PROJECTION);
  66. glLoadIdentity();
  67. glOrtho(-4, 4, -2, 2, -1, 1);
  68. glMatrixMode(GL_MODELVIEW);
  69. glLoadIdentity();
  70. }
  71. static void
  72. CleanUp(void)
  73. {
  74. glDeleteShader(FragShader);
  75. glDeleteShader(VertShader);
  76. glDeleteProgram(VertProgram);
  77. glDeleteProgram(FragProgram);
  78. glutDestroyWindow(Win);
  79. }
  80. static void
  81. Key(unsigned char key, int x, int y)
  82. {
  83. (void) x;
  84. (void) y;
  85. switch(key) {
  86. case 27:
  87. CleanUp();
  88. exit(0);
  89. break;
  90. }
  91. glutPostRedisplay();
  92. }
  93. static void
  94. Init(void)
  95. {
  96. static const char *fragShaderText =
  97. "void main() {\n"
  98. " gl_FragColor = gl_Color;\n"
  99. "}\n";
  100. static const char *vertShaderText =
  101. "void main() {\n"
  102. " gl_Position = ftransform();\n"
  103. " gl_FrontColor = gl_MultiTexCoord0;\n" /* see DrawQuadTex() */
  104. "}\n";
  105. if (!ShadersSupported())
  106. exit(1);
  107. if (FragProgFile)
  108. FragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
  109. else
  110. FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
  111. if (VertProgFile)
  112. VertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
  113. else
  114. VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
  115. VertProgram = LinkShaders(VertShader, 0);
  116. FragProgram = LinkShaders(0, FragShader);
  117. glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
  118. glEnable(GL_DEPTH_TEST);
  119. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  120. assert(glIsProgram(VertProgram));
  121. assert(glIsProgram(FragProgram));
  122. assert(glIsShader(FragShader));
  123. assert(glIsShader(VertShader));
  124. glColor3f(1, 0, 0);
  125. }
  126. static void
  127. ParseOptions(int argc, char *argv[])
  128. {
  129. int i;
  130. for (i = 1; i < argc; i++) {
  131. if (strcmp(argv[i], "-fs") == 0) {
  132. FragProgFile = argv[i+1];
  133. }
  134. else if (strcmp(argv[i], "-vs") == 0) {
  135. VertProgFile = argv[i+1];
  136. }
  137. }
  138. }
  139. int
  140. main(int argc, char *argv[])
  141. {
  142. glutInit(&argc, argv);
  143. glutInitWindowSize(400, 200);
  144. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  145. Win = glutCreateWindow(argv[0]);
  146. glewInit();
  147. glutReshapeFunc(Reshape);
  148. glutKeyboardFunc(Key);
  149. glutDisplayFunc(Redisplay);
  150. ParseOptions(argc, argv);
  151. Init();
  152. glutMainLoop();
  153. return 0;
  154. }