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