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