WIP - port to Mali EGL
[mesa-demos/mali.git] / src / glsl / multinoise.c
blobb5ed14cf8bc567cf05a47b4ea8b509707c1a8818
1 /**
2 * Another test for noise() functions (noise1 to noise4 tested independently).
3 * 13 Dec 2008
4 */
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 "glut_wrap.h"
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";
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.0;\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.0;\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.0;\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.0;\n"
41 "}\n"
44 struct uniform_info {
45 const char *name;
46 GLuint size;
47 GLint location;
48 GLfloat value[4];
51 /* program/shader objects */
52 static GLuint fragShader[ 4 ];
53 static GLuint vertShader;
54 static GLuint program[ 4 ];
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;
62 static void
63 Idle(void)
65 Slice += 0.01;
66 glutPostRedisplay();
70 static void
71 Redisplay(void)
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
75 glMultiTexCoord1f( GL_TEXTURE1, Slice );
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);
82 glutSolidTeapot( 1.0 );
84 glPopMatrix();
86 glutSwapBuffers();
90 static void
91 Reshape(int width, int height)
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);
103 static void
104 CleanUp(void)
106 GLint i;
108 glDeleteShader(vertShader);
109 for( i = 0; i < 4; i++ ) {
110 glDeleteShader(fragShader[ i ]);
111 glDeleteProgram(program[ i ]);
113 glutDestroyWindow(win);
117 static void
118 Key(unsigned char key, int x, int y)
120 const GLfloat step = 0.01;
121 (void) x;
122 (void) y;
124 switch(key) {
125 case 'a':
126 Anim = !Anim;
127 glutIdleFunc(Anim ? Idle : NULL);
128 break;
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 '1':
142 case '2':
143 case '3':
144 case '4':
145 glUseProgram(program[ key - '1' ]);
146 break;
147 case 27:
148 CleanUp();
149 exit(0);
150 break;
152 glutPostRedisplay();
156 static void
157 SpecialKey(int key, int x, int y)
159 const GLfloat step = 3.0f;
161 (void) x;
162 (void) y;
164 switch(key) {
165 case GLUT_KEY_UP:
166 xRot -= step;
167 break;
168 case GLUT_KEY_DOWN:
169 xRot += step;
170 break;
171 case GLUT_KEY_LEFT:
172 yRot -= step;
173 break;
174 case GLUT_KEY_RIGHT:
175 yRot += step;
176 break;
178 glutPostRedisplay();
183 static void
184 LoadAndCompileShader(GLuint shader, const char *text)
186 GLint stat;
188 glShaderSource(shader, 1, (const GLchar **) &text, NULL);
190 glCompileShader(shader);
192 glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
193 if (!stat) {
194 GLchar log[1000];
195 GLsizei len;
196 glGetShaderInfoLog(shader, 1000, &len, log);
197 fprintf(stderr, "multinoise: problem compiling shader: %s\n", log);
198 exit(1);
200 else {
201 printf("Shader compiled OK\n");
206 static void
207 CheckLink(GLuint prog)
209 GLint stat;
210 glGetProgramiv(prog, GL_LINK_STATUS, &stat);
211 if (!stat) {
212 GLchar log[1000];
213 GLsizei len;
214 glGetProgramInfoLog(prog, 1000, &len, log);
215 fprintf(stderr, "Linker error:\n%s\n", log);
217 else {
218 fprintf(stderr, "Link success!\n");
223 static void
224 Init(void)
226 GLint i;
228 if (!GLEW_VERSION_2_0) {
229 printf("Warning: this program expects OpenGL 2.0\n");
230 /*exit(1);*/
233 vertShader = glCreateShader(GL_VERTEX_SHADER);
234 LoadAndCompileShader(vertShader, VertShaderText);
236 for( i = 0; i < 4; i++ ) {
237 fragShader[ i ] = glCreateShader(GL_FRAGMENT_SHADER);
238 LoadAndCompileShader(fragShader[ i ], FragShaderText[ i ]);
239 program[ i ] = glCreateProgram();
240 glAttachShader(program[ i ], fragShader[ i ]);
241 glAttachShader(program[ i ], vertShader);
242 glLinkProgram(program[ i ]);
243 CheckLink(program[ i ]);
246 glUseProgram(program[ 0 ]);
248 assert(glGetError() == 0);
250 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
252 glColor3f(1, 0, 0);
254 glFrontFace( GL_CW );
255 glEnable( GL_CULL_FACE );
256 glEnable( GL_DEPTH_TEST );
261 main(int argc, char *argv[])
263 glutInit(&argc, argv);
264 glutInitWindowSize(400, 400);
265 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
266 win = glutCreateWindow(argv[0]);
267 glewInit();
268 glutReshapeFunc(Reshape);
269 glutKeyboardFunc(Key);
270 glutSpecialFunc(SpecialKey);
271 glutDisplayFunc(Redisplay);
272 Init();
273 glutMainLoop();
274 return 0;