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