blob: 9da71ac775eaa140da9818ab559c2dfa57b3fae5 [file] [log] [blame]
Brianf94e4f22007-01-28 19:01:04 -07001/**
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"
Brian2dca3372008-04-09 22:28:23 -060015#include "shaderutil.h"
Brianf94e4f22007-01-28 19:01:04 -070016
17
18static const char *VertShaderText =
19 "void main() {\n"
20 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
Brian21625d72007-02-25 12:46:56 -070021 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
Brianf94e4f22007-01-28 19:01:04 -070022 "}\n";
23
24static 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
Brianf94e4f22007-01-28 19:01:04 -070038static struct uniform_info Uniforms[] = {
Brian2dca3372008-04-09 22:28:23 -060039 { "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
Brianf94e4f22007-01-28 19:01:04 -070043};
44
45/* program/shader objects */
46static GLuint fragShader;
47static GLuint vertShader;
48static GLuint program;
49
50static GLint win = 0;
51static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
52static GLfloat Slice = 0.0;
53static GLboolean Anim = GL_FALSE;
54
55
56static void
57Idle(void)
58{
59 Slice += 0.01;
60 glutPostRedisplay();
61}
62
63
64static void
65Redisplay(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
89static void
90Reshape(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
102static void
103CleanUp(void)
104{
105 glDeleteShader_func(fragShader);
106 glDeleteShader_func(vertShader);
107 glDeleteProgram_func(program);
108 glutDestroyWindow(win);
109}
110
111
112static void
113Key(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
144static void
145SpecialKey(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
171static void
Brianf94e4f22007-01-28 19:01:04 -0700172Init(void)
173{
Brian2dca3372008-04-09 22:28:23 -0600174 if (!ShadersSupported())
175 exit(1);
Brianf94e4f22007-01-28 19:01:04 -0700176
177 GetExtensionFuncs();
178
Brian2dca3372008-04-09 22:28:23 -0600179 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
180 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
181 program = LinkShaders(vertShader, fragShader);
Brianf94e4f22007-01-28 19:01:04 -0700182
Brianf94e4f22007-01-28 19:01:04 -0700183 glUseProgram_func(program);
184
Brian2dca3372008-04-09 22:28:23 -0600185 InitUniforms(program, Uniforms);
Brianf94e4f22007-01-28 19:01:04 -0700186
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
201int
202main(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