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