blob: 83e4696fc99ae40774425bed8f87f57a372a028d [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>
Keith Whitwellb799af92009-06-29 14:13:58 +010011#include <GL/glew.h>
Brianf94e4f22007-01-28 19:01:04 -070012#include <GL/gl.h>
13#include <GL/glut.h>
14#include <GL/glext.h>
15#include "extfuncs.h"
Brian2dca3372008-04-09 22:28:23 -060016#include "shaderutil.h"
Brianf94e4f22007-01-28 19:01:04 -070017
18
19static const char *VertShaderText =
20 "void main() {\n"
21 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
Brian21625d72007-02-25 12:46:56 -070022 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
Brianf94e4f22007-01-28 19:01:04 -070023 "}\n";
24
25static const char *FragShaderText =
26 "uniform vec4 Scale, Bias;\n"
27 "uniform float Slice;\n"
28 "void main()\n"
29 "{\n"
30 " vec4 scale = vec4(5.0);\n"
31 " vec4 p;\n"
32 " p.xy = gl_TexCoord[0].xy;\n"
33 " p.z = Slice;\n"
Gary Wong0df3dfa2008-12-13 12:58:18 -070034 " p.w = 0;\n"
Brianf94e4f22007-01-28 19:01:04 -070035 " vec4 n = noise4(p * scale);\n"
36 " gl_FragColor = n * Scale + Bias;\n"
37 "}\n";
38
39
Brianf94e4f22007-01-28 19:01:04 -070040static struct uniform_info Uniforms[] = {
Brian2dca3372008-04-09 22:28:23 -060041 { "Scale", 4, GL_FLOAT, { 0.5, 0.4, 0.0, 0}, -1 },
42 { "Bias", 4, GL_FLOAT, { 0.5, 0.3, 0.0, 0}, -1 },
43 { "Slice", 1, GL_FLOAT, { 0.5, 0, 0, 0}, -1 },
44 END_OF_UNIFORMS
Brianf94e4f22007-01-28 19:01:04 -070045};
46
47/* program/shader objects */
48static GLuint fragShader;
49static GLuint vertShader;
50static GLuint program;
51
52static GLint win = 0;
53static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
54static GLfloat Slice = 0.0;
55static GLboolean Anim = GL_FALSE;
56
57
58static void
59Idle(void)
60{
61 Slice += 0.01;
62 glutPostRedisplay();
63}
64
65
66static void
67Redisplay(void)
68{
69 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
70
71 glUniform1fv_func(Uniforms[2].location, 1, &Slice);
72
73 glPushMatrix();
74 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
75 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
76 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
77
78 glBegin(GL_POLYGON);
79 glTexCoord2f(0, 0); glVertex2f(-2, -2);
80 glTexCoord2f(1, 0); glVertex2f( 2, -2);
81 glTexCoord2f(1, 1); glVertex2f( 2, 2);
82 glTexCoord2f(0, 1); glVertex2f(-2, 2);
83 glEnd();
84
85 glPopMatrix();
86
87 glutSwapBuffers();
88}
89
90
91static void
92Reshape(int width, int height)
93{
94 glViewport(0, 0, width, height);
95 glMatrixMode(GL_PROJECTION);
96 glLoadIdentity();
97 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
98 glMatrixMode(GL_MODELVIEW);
99 glLoadIdentity();
100 glTranslatef(0.0f, 0.0f, -15.0f);
101}
102
103
104static void
105CleanUp(void)
106{
107 glDeleteShader_func(fragShader);
108 glDeleteShader_func(vertShader);
109 glDeleteProgram_func(program);
110 glutDestroyWindow(win);
111}
112
113
114static void
115Key(unsigned char key, int x, int y)
116{
117 const GLfloat step = 0.01;
118 (void) x;
119 (void) y;
120
121 switch(key) {
122 case 'a':
123 Anim = !Anim;
124 glutIdleFunc(Anim ? Idle : NULL);
125 case 's':
126 Slice -= step;
127 break;
128 case 'S':
129 Slice += step;
130 break;
131 case 'z':
132 zRot -= 1.0;
133 break;
134 case 'Z':
135 zRot += 1.0;
136 break;
137 case 27:
138 CleanUp();
139 exit(0);
140 break;
141 }
142 glutPostRedisplay();
143}
144
145
146static void
147SpecialKey(int key, int x, int y)
148{
149 const GLfloat step = 3.0f;
150
151 (void) x;
152 (void) y;
153
154 switch(key) {
155 case GLUT_KEY_UP:
156 xRot -= step;
157 break;
158 case GLUT_KEY_DOWN:
159 xRot += step;
160 break;
161 case GLUT_KEY_LEFT:
162 yRot -= step;
163 break;
164 case GLUT_KEY_RIGHT:
165 yRot += step;
166 break;
167 }
168 glutPostRedisplay();
169}
170
171
172
173static void
Brianf94e4f22007-01-28 19:01:04 -0700174Init(void)
175{
Brian2dca3372008-04-09 22:28:23 -0600176 if (!ShadersSupported())
177 exit(1);
Brianf94e4f22007-01-28 19:01:04 -0700178
179 GetExtensionFuncs();
180
Brian2dca3372008-04-09 22:28:23 -0600181 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
182 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
183 program = LinkShaders(vertShader, fragShader);
Brianf94e4f22007-01-28 19:01:04 -0700184
Brianf94e4f22007-01-28 19:01:04 -0700185 glUseProgram_func(program);
186
Brian2dca3372008-04-09 22:28:23 -0600187 InitUniforms(program, Uniforms);
Brianf94e4f22007-01-28 19:01:04 -0700188
189 assert(glGetError() == 0);
190
191 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
192
193 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
194
195 assert(glIsProgram_func(program));
196 assert(glIsShader_func(fragShader));
197 assert(glIsShader_func(vertShader));
198
199 glColor3f(1, 0, 0);
200}
201
202
203int
204main(int argc, char *argv[])
205{
206 glutInit(&argc, argv);
207 glutInitWindowPosition( 0, 0);
208 glutInitWindowSize(400, 400);
209 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
210 win = glutCreateWindow(argv[0]);
Keith Whitwellb799af92009-06-29 14:13:58 +0100211 glewInit();
Brianf94e4f22007-01-28 19:01:04 -0700212 glutReshapeFunc(Reshape);
213 glutKeyboardFunc(Key);
214 glutSpecialFunc(SpecialKey);
215 glutDisplayFunc(Redisplay);
216 Init();
217 glutMainLoop();
218 return 0;
219}
220