blob: a26a8059449506b20d271ba7a400f49efec41677 [file] [log] [blame]
Brianf94e4f22007-01-28 19:01:04 -07001/**
2 * Test noise() functions.
3 * 28 Jan 2007
4 */
5
6#include <assert.h>
7#include <string.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <math.h>
11#include <GL/gl.h>
12#include <GL/glut.h>
13#include <GL/glext.h>
14#include "extfuncs.h"
15
16
17static const char *VertShaderText =
18 "void main() {\n"
19 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
Brian21625d72007-02-25 12:46:56 -070020 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
Brianf94e4f22007-01-28 19:01:04 -070021 "}\n";
22
23static const char *FragShaderText =
24 "uniform vec4 Scale, Bias;\n"
25 "uniform float Slice;\n"
26 "void main()\n"
27 "{\n"
28 " vec4 scale = vec4(5.0);\n"
29 " vec4 p;\n"
30 " p.xy = gl_TexCoord[0].xy;\n"
31 " p.z = Slice;\n"
32 " vec4 n = noise4(p * scale);\n"
33 " gl_FragColor = n * Scale + Bias;\n"
34 "}\n";
35
36
37struct uniform_info {
38 const char *name;
39 GLuint size;
40 GLint location;
41 GLfloat value[4];
42};
43
44static struct uniform_info Uniforms[] = {
45 { "Scale", 4, -1, { 0.5, 0.4, 0.0, 0} },
46 { "Bias", 4, -1, { 0.5, 0.3, 0.0, 0} },
47 { "Slice", 1, -1, { 0.5, 0, 0, 0} },
48 { NULL, 0, 0, { 0, 0, 0, 0 } }
49};
50
51/* program/shader objects */
52static GLuint fragShader;
53static GLuint vertShader;
54static GLuint program;
55
56static GLint win = 0;
57static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
58static GLfloat Slice = 0.0;
59static GLboolean Anim = GL_FALSE;
60
61
62static void
63Idle(void)
64{
65 Slice += 0.01;
66 glutPostRedisplay();
67}
68
69
70static void
71Redisplay(void)
72{
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74
75 glUniform1fv_func(Uniforms[2].location, 1, &Slice);
76
77 glPushMatrix();
78 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
79 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
80 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
81
82 glBegin(GL_POLYGON);
83 glTexCoord2f(0, 0); glVertex2f(-2, -2);
84 glTexCoord2f(1, 0); glVertex2f( 2, -2);
85 glTexCoord2f(1, 1); glVertex2f( 2, 2);
86 glTexCoord2f(0, 1); glVertex2f(-2, 2);
87 glEnd();
88
89 glPopMatrix();
90
91 glutSwapBuffers();
92}
93
94
95static void
96Reshape(int width, int height)
97{
98 glViewport(0, 0, width, height);
99 glMatrixMode(GL_PROJECTION);
100 glLoadIdentity();
101 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
102 glMatrixMode(GL_MODELVIEW);
103 glLoadIdentity();
104 glTranslatef(0.0f, 0.0f, -15.0f);
105}
106
107
108static void
109CleanUp(void)
110{
111 glDeleteShader_func(fragShader);
112 glDeleteShader_func(vertShader);
113 glDeleteProgram_func(program);
114 glutDestroyWindow(win);
115}
116
117
118static void
119Key(unsigned char key, int x, int y)
120{
121 const GLfloat step = 0.01;
122 (void) x;
123 (void) y;
124
125 switch(key) {
126 case 'a':
127 Anim = !Anim;
128 glutIdleFunc(Anim ? Idle : NULL);
129 case 's':
130 Slice -= step;
131 break;
132 case 'S':
133 Slice += step;
134 break;
135 case 'z':
136 zRot -= 1.0;
137 break;
138 case 'Z':
139 zRot += 1.0;
140 break;
141 case 27:
142 CleanUp();
143 exit(0);
144 break;
145 }
146 glutPostRedisplay();
147}
148
149
150static void
151SpecialKey(int key, int x, int y)
152{
153 const GLfloat step = 3.0f;
154
155 (void) x;
156 (void) y;
157
158 switch(key) {
159 case GLUT_KEY_UP:
160 xRot -= step;
161 break;
162 case GLUT_KEY_DOWN:
163 xRot += step;
164 break;
165 case GLUT_KEY_LEFT:
166 yRot -= step;
167 break;
168 case GLUT_KEY_RIGHT:
169 yRot += step;
170 break;
171 }
172 glutPostRedisplay();
173}
174
175
176
177static void
178LoadAndCompileShader(GLuint shader, const char *text)
179{
180 GLint stat;
181
182 glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
183
184 glCompileShader_func(shader);
185
186 glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
187 if (!stat) {
188 GLchar log[1000];
189 GLsizei len;
190 glGetShaderInfoLog_func(shader, 1000, &len, log);
191 fprintf(stderr, "brick: problem compiling shader: %s\n", log);
192 exit(1);
193 }
194 else {
195 printf("Shader compiled OK\n");
196 }
197}
198
199
200static void
201CheckLink(GLuint prog)
202{
203 GLint stat;
204 glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
205 if (!stat) {
206 GLchar log[1000];
207 GLsizei len;
208 glGetProgramInfoLog_func(prog, 1000, &len, log);
209 fprintf(stderr, "Linker error:\n%s\n", log);
210 }
211 else {
212 fprintf(stderr, "Link success!\n");
213 }
214}
215
216
217static void
218Init(void)
219{
220 const char *version;
221 GLint i;
222
223 version = (const char *) glGetString(GL_VERSION);
224 if (version[0] != '2' || version[1] != '.') {
225 printf("Warning: this program expects OpenGL 2.0\n");
226 /*exit(1);*/
227 }
228
229 GetExtensionFuncs();
230
231 vertShader = glCreateShader_func(GL_VERTEX_SHADER);
232 LoadAndCompileShader(vertShader, VertShaderText);
233
234 fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
235 LoadAndCompileShader(fragShader, FragShaderText);
236
237 program = glCreateProgram_func();
238 glAttachShader_func(program, fragShader);
239 glAttachShader_func(program, vertShader);
240 glLinkProgram_func(program);
241 CheckLink(program);
242 glUseProgram_func(program);
243
244 for (i = 0; Uniforms[i].name; i++) {
245 Uniforms[i].location
246 = glGetUniformLocation_func(program, Uniforms[i].name);
247 printf("Uniform %s location: %d\n", Uniforms[i].name,
248 Uniforms[i].location);
249 switch (Uniforms[i].size) {
250 case 1:
251 glUniform1fv_func(Uniforms[i].location, 1, Uniforms[i].value);
252 break;
253 case 2:
254 glUniform2fv_func(Uniforms[i].location, 1, Uniforms[i].value);
255 break;
256 case 3:
257 glUniform3fv_func(Uniforms[i].location, 1, Uniforms[i].value);
258 break;
259 case 4:
260 glUniform4fv_func(Uniforms[i].location, 1, Uniforms[i].value);
261 break;
262 default:
263 abort();
264 }
265 }
266
267 assert(glGetError() == 0);
268
269 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
270
271 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
272
273 assert(glIsProgram_func(program));
274 assert(glIsShader_func(fragShader));
275 assert(glIsShader_func(vertShader));
276
277 glColor3f(1, 0, 0);
278}
279
280
281int
282main(int argc, char *argv[])
283{
284 glutInit(&argc, argv);
285 glutInitWindowPosition( 0, 0);
286 glutInitWindowSize(400, 400);
287 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
288 win = glutCreateWindow(argv[0]);
289 glutReshapeFunc(Reshape);
290 glutKeyboardFunc(Key);
291 glutSpecialFunc(SpecialKey);
292 glutDisplayFunc(Redisplay);
293 Init();
294 glutMainLoop();
295 return 0;
296}
297