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