blob: bd8f50036bfe6d7a96e0e20cdf568fb16e20f498 [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"
Gary Wong0df3dfa2008-12-13 12:58:18 -070033 " p.w = 0;\n"
Brianf94e4f22007-01-28 19:01:04 -070034 " vec4 n = noise4(p * scale);\n"
35 " gl_FragColor = n * Scale + Bias;\n"
36 "}\n";
37
38
Brianf94e4f22007-01-28 19:01:04 -070039static struct uniform_info Uniforms[] = {
Brian2dca3372008-04-09 22:28:23 -060040 { "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
Brianf94e4f22007-01-28 19:01:04 -070044};
45
46/* program/shader objects */
47static GLuint fragShader;
48static GLuint vertShader;
49static GLuint program;
50
51static GLint win = 0;
52static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
53static GLfloat Slice = 0.0;
54static GLboolean Anim = GL_FALSE;
55
56
57static void
58Idle(void)
59{
60 Slice += 0.01;
61 glutPostRedisplay();
62}
63
64
65static void
66Redisplay(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
90static void
91Reshape(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
103static void
104CleanUp(void)
105{
106 glDeleteShader_func(fragShader);
107 glDeleteShader_func(vertShader);
108 glDeleteProgram_func(program);
109 glutDestroyWindow(win);
110}
111
112
113static void
114Key(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
145static void
146SpecialKey(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
172static void
Brianf94e4f22007-01-28 19:01:04 -0700173Init(void)
174{
Brian2dca3372008-04-09 22:28:23 -0600175 if (!ShadersSupported())
176 exit(1);
Brianf94e4f22007-01-28 19:01:04 -0700177
178 GetExtensionFuncs();
179
Brian2dca3372008-04-09 22:28:23 -0600180 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
181 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
182 program = LinkShaders(vertShader, fragShader);
Brianf94e4f22007-01-28 19:01:04 -0700183
Brianf94e4f22007-01-28 19:01:04 -0700184 glUseProgram_func(program);
185
Brian2dca3372008-04-09 22:28:23 -0600186 InitUniforms(program, Uniforms);
Brianf94e4f22007-01-28 19:01:04 -0700187
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
202int
203main(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