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