bd8f50036bfe6d7a96e0e20cdf568fb16e20f498
[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 #include "shaderutil.h"
16
17
18 static const char *VertShaderText =
19 "void main() {\n"
20 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
21 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
22 "}\n";
23
24 static const char *FragShaderText =
25 "uniform vec4 Scale, Bias;\n"
26 "uniform float Slice;\n"
27 "void main()\n"
28 "{\n"
29 " vec4 scale = vec4(5.0);\n"
30 " vec4 p;\n"
31 " p.xy = gl_TexCoord[0].xy;\n"
32 " p.z = Slice;\n"
33 " p.w = 0;\n"
34 " vec4 n = noise4(p * scale);\n"
35 " gl_FragColor = n * Scale + Bias;\n"
36 "}\n";
37
38
39 static struct uniform_info Uniforms[] = {
40 { "Scale", 4, GL_FLOAT, { 0.5, 0.4, 0.0, 0}, -1 },
41 { "Bias", 4, GL_FLOAT, { 0.5, 0.3, 0.0, 0}, -1 },
42 { "Slice", 1, GL_FLOAT, { 0.5, 0, 0, 0}, -1 },
43 END_OF_UNIFORMS
44 };
45
46 /* program/shader objects */
47 static GLuint fragShader;
48 static GLuint vertShader;
49 static GLuint program;
50
51 static GLint win = 0;
52 static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
53 static GLfloat Slice = 0.0;
54 static GLboolean Anim = GL_FALSE;
55
56
57 static void
58 Idle(void)
59 {
60 Slice += 0.01;
61 glutPostRedisplay();
62 }
63
64
65 static void
66 Redisplay(void)
67 {
68 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
69
70 glUniform1fv_func(Uniforms[2].location, 1, &Slice);
71
72 glPushMatrix();
73 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
74 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
75 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
76
77 glBegin(GL_POLYGON);
78 glTexCoord2f(0, 0); glVertex2f(-2, -2);
79 glTexCoord2f(1, 0); glVertex2f( 2, -2);
80 glTexCoord2f(1, 1); glVertex2f( 2, 2);
81 glTexCoord2f(0, 1); glVertex2f(-2, 2);
82 glEnd();
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 glDeleteShader_func(fragShader);
107 glDeleteShader_func(vertShader);
108 glDeleteProgram_func(program);
109 glutDestroyWindow(win);
110 }
111
112
113 static void
114 Key(unsigned char key, int x, int y)
115 {
116 const GLfloat step = 0.01;
117 (void) x;
118 (void) y;
119
120 switch(key) {
121 case 'a':
122 Anim = !Anim;
123 glutIdleFunc(Anim ? Idle : NULL);
124 case 's':
125 Slice -= step;
126 break;
127 case 'S':
128 Slice += step;
129 break;
130 case 'z':
131 zRot -= 1.0;
132 break;
133 case 'Z':
134 zRot += 1.0;
135 break;
136 case 27:
137 CleanUp();
138 exit(0);
139 break;
140 }
141 glutPostRedisplay();
142 }
143
144
145 static void
146 SpecialKey(int key, int x, int y)
147 {
148 const GLfloat step = 3.0f;
149
150 (void) x;
151 (void) y;
152
153 switch(key) {
154 case GLUT_KEY_UP:
155 xRot -= step;
156 break;
157 case GLUT_KEY_DOWN:
158 xRot += step;
159 break;
160 case GLUT_KEY_LEFT:
161 yRot -= step;
162 break;
163 case GLUT_KEY_RIGHT:
164 yRot += step;
165 break;
166 }
167 glutPostRedisplay();
168 }
169
170
171
172 static void
173 Init(void)
174 {
175 if (!ShadersSupported())
176 exit(1);
177
178 GetExtensionFuncs();
179
180 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
181 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
182 program = LinkShaders(vertShader, fragShader);
183
184 glUseProgram_func(program);
185
186 InitUniforms(program, Uniforms);
187
188 assert(glGetError() == 0);
189
190 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
191
192 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
193
194 assert(glIsProgram_func(program));
195 assert(glIsShader_func(fragShader));
196 assert(glIsShader_func(vertShader));
197
198 glColor3f(1, 0, 0);
199 }
200
201
202 int
203 main(int argc, char *argv[])
204 {
205 glutInit(&argc, argv);
206 glutInitWindowPosition( 0, 0);
207 glutInitWindowSize(400, 400);
208 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
209 win = glutCreateWindow(argv[0]);
210 glutReshapeFunc(Reshape);
211 glutKeyboardFunc(Key);
212 glutSpecialFunc(SpecialKey);
213 glutDisplayFunc(Redisplay);
214 Init();
215 glutMainLoop();
216 return 0;
217 }
218