1/**
2 * Test very basic glsl functionality (identity vertex and fragment shaders).
3 * Brian Paul & Stephane Marchesin
4 */
5
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 "glut_wrap.h"
14#include "shaderutil.h"
15
16
17static GLuint fragShader;
18static GLuint vertShader;
19static GLuint program;
20static GLint win = 0;
21
22
23static void
24Redisplay(void)
25{
26   glClear(GL_COLOR_BUFFER_BIT);
27
28   glBegin(GL_TRIANGLES);
29   glVertex3f(-0.9, -0.9, 0.0);
30   glVertex3f( 0.9, -0.9, 0.0);
31   glVertex3f( 0.0,  0.9, 0.0);
32   glEnd();
33
34   glutSwapBuffers();
35}
36
37
38static void
39Reshape(int width, int height)
40{
41   glViewport(0, 0, width, height);
42   glMatrixMode(GL_PROJECTION);
43   glLoadIdentity();
44   glMatrixMode(GL_MODELVIEW);
45   glLoadIdentity();
46}
47
48
49static void
50CleanUp(void)
51{
52   glDeleteShader(fragShader);
53   glDeleteShader(vertShader);
54   glDeleteProgram(program);
55   glutDestroyWindow(win);
56}
57
58
59static void
60Key(unsigned char key, int x, int y)
61{
62   if (key == 27) {
63      CleanUp();
64      exit(0);
65   }
66   glutPostRedisplay();
67}
68
69
70static void
71Init(void)
72{
73   static const char *fragShaderText =
74      "void main() {\n"
75      "   gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n"
76      "}\n";
77   static const char *vertShaderText =
78      "void main() {\n"
79      "   gl_Position = gl_Vertex;\n"
80      "}\n";
81
82   if (!ShadersSupported())
83      exit(1);
84
85   fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
86
87   vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
88
89   program = LinkShaders(vertShader, fragShader);
90
91   glUseProgram(program);
92
93   assert(glGetError() == 0);
94
95   glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
96
97   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
98
99   assert(glIsProgram(program));
100   assert(glIsShader(fragShader));
101   assert(glIsShader(vertShader));
102
103   glColor3f(1, 0, 0);
104}
105
106
107int
108main(int argc, char *argv[])
109{
110   glutInit(&argc, argv);
111   glutInitWindowSize(200, 200);
112   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
113   win = glutCreateWindow(argv[0]);
114   glewInit();
115   glutReshapeFunc(Reshape);
116   glutKeyboardFunc(Key);
117   glutDisplayFunc(Redisplay);
118   Init();
119   glutMainLoop();
120   return 0;
121}
122