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