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