added texdemo1
[mesa.git] / progs / glsl / noise.c
1 /**
2 * Test noise() functions.
3 * 28 Jan 2007
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
17 static const char *VertShaderText =
18 "void main() {\n"
19 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
20 "}\n";
21
22 static const char *FragShaderText =
23 "uniform vec4 Scale, Bias;\n"
24 "uniform float Slice;\n"
25 "void main()\n"
26 "{\n"
27 " vec4 scale = vec4(5.0);\n"
28 " vec4 p;\n"
29 " p.xy = gl_TexCoord[0].xy;\n"
30 " p.z = Slice;\n"
31 " vec4 n = noise4(p * scale);\n"
32 " gl_FragColor = n * Scale + Bias;\n"
33 "}\n";
34
35
36 struct uniform_info {
37 const char *name;
38 GLuint size;
39 GLint location;
40 GLfloat value[4];
41 };
42
43 static struct uniform_info Uniforms[] = {
44 { "Scale", 4, -1, { 0.5, 0.4, 0.0, 0} },
45 { "Bias", 4, -1, { 0.5, 0.3, 0.0, 0} },
46 { "Slice", 1, -1, { 0.5, 0, 0, 0} },
47 { NULL, 0, 0, { 0, 0, 0, 0 } }
48 };
49
50 /* program/shader objects */
51 static GLuint fragShader;
52 static GLuint vertShader;
53 static GLuint program;
54
55 static GLint win = 0;
56 static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
57 static GLfloat Slice = 0.0;
58 static GLboolean Anim = GL_FALSE;
59
60
61 static void
62 Idle(void)
63 {
64 Slice += 0.01;
65 glutPostRedisplay();
66 }
67
68
69 static void
70 Redisplay(void)
71 {
72 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
73
74 glUniform1fv_func(Uniforms[2].location, 1, &Slice);
75
76 glPushMatrix();
77 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
78 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
79 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
80
81 glBegin(GL_POLYGON);
82 glTexCoord2f(0, 0); glVertex2f(-2, -2);
83 glTexCoord2f(1, 0); glVertex2f( 2, -2);
84 glTexCoord2f(1, 1); glVertex2f( 2, 2);
85 glTexCoord2f(0, 1); glVertex2f(-2, 2);
86 glEnd();
87
88 glPopMatrix();
89
90 glutSwapBuffers();
91 }
92
93
94 static void
95 Reshape(int width, int height)
96 {
97 glViewport(0, 0, width, height);
98 glMatrixMode(GL_PROJECTION);
99 glLoadIdentity();
100 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
101 glMatrixMode(GL_MODELVIEW);
102 glLoadIdentity();
103 glTranslatef(0.0f, 0.0f, -15.0f);
104 }
105
106
107 static void
108 CleanUp(void)
109 {
110 glDeleteShader_func(fragShader);
111 glDeleteShader_func(vertShader);
112 glDeleteProgram_func(program);
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 27:
141 CleanUp();
142 exit(0);
143 break;
144 }
145 glutPostRedisplay();
146 }
147
148
149 static void
150 SpecialKey(int key, int x, int y)
151 {
152 const GLfloat step = 3.0f;
153
154 (void) x;
155 (void) y;
156
157 switch(key) {
158 case GLUT_KEY_UP:
159 xRot -= step;
160 break;
161 case GLUT_KEY_DOWN:
162 xRot += step;
163 break;
164 case GLUT_KEY_LEFT:
165 yRot -= step;
166 break;
167 case GLUT_KEY_RIGHT:
168 yRot += step;
169 break;
170 }
171 glutPostRedisplay();
172 }
173
174
175
176 static void
177 LoadAndCompileShader(GLuint shader, const char *text)
178 {
179 GLint stat;
180
181 glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
182
183 glCompileShader_func(shader);
184
185 glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
186 if (!stat) {
187 GLchar log[1000];
188 GLsizei len;
189 glGetShaderInfoLog_func(shader, 1000, &len, log);
190 fprintf(stderr, "brick: problem compiling shader: %s\n", log);
191 exit(1);
192 }
193 else {
194 printf("Shader compiled OK\n");
195 }
196 }
197
198
199 static void
200 CheckLink(GLuint prog)
201 {
202 GLint stat;
203 glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
204 if (!stat) {
205 GLchar log[1000];
206 GLsizei len;
207 glGetProgramInfoLog_func(prog, 1000, &len, log);
208 fprintf(stderr, "Linker error:\n%s\n", log);
209 }
210 else {
211 fprintf(stderr, "Link success!\n");
212 }
213 }
214
215
216 static void
217 Init(void)
218 {
219 const char *version;
220 GLint i;
221
222 version = (const char *) glGetString(GL_VERSION);
223 if (version[0] != '2' || version[1] != '.') {
224 printf("Warning: this program expects OpenGL 2.0\n");
225 /*exit(1);*/
226 }
227
228 GetExtensionFuncs();
229
230 vertShader = glCreateShader_func(GL_VERTEX_SHADER);
231 LoadAndCompileShader(vertShader, VertShaderText);
232
233 fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
234 LoadAndCompileShader(fragShader, FragShaderText);
235
236 program = glCreateProgram_func();
237 glAttachShader_func(program, fragShader);
238 glAttachShader_func(program, vertShader);
239 glLinkProgram_func(program);
240 CheckLink(program);
241 glUseProgram_func(program);
242
243 for (i = 0; Uniforms[i].name; i++) {
244 Uniforms[i].location
245 = glGetUniformLocation_func(program, Uniforms[i].name);
246 printf("Uniform %s location: %d\n", Uniforms[i].name,
247 Uniforms[i].location);
248 switch (Uniforms[i].size) {
249 case 1:
250 glUniform1fv_func(Uniforms[i].location, 1, Uniforms[i].value);
251 break;
252 case 2:
253 glUniform2fv_func(Uniforms[i].location, 1, Uniforms[i].value);
254 break;
255 case 3:
256 glUniform3fv_func(Uniforms[i].location, 1, Uniforms[i].value);
257 break;
258 case 4:
259 glUniform4fv_func(Uniforms[i].location, 1, Uniforms[i].value);
260 break;
261 default:
262 abort();
263 }
264 }
265
266 assert(glGetError() == 0);
267
268 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
269
270 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
271
272 assert(glIsProgram_func(program));
273 assert(glIsShader_func(fragShader));
274 assert(glIsShader_func(vertShader));
275
276 glColor3f(1, 0, 0);
277 }
278
279
280 int
281 main(int argc, char *argv[])
282 {
283 glutInit(&argc, argv);
284 glutInitWindowPosition( 0, 0);
285 glutInitWindowSize(400, 400);
286 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
287 win = glutCreateWindow(argv[0]);
288 glutReshapeFunc(Reshape);
289 glutKeyboardFunc(Key);
290 glutSpecialFunc(SpecialKey);
291 glutDisplayFunc(Redisplay);
292 Init();
293 glutMainLoop();
294 return 0;
295 }
296