blob: 59f594e78bf3050da7e4292a2f5d73066c4c9283 [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/glut.h>
Brian2dca3372008-04-09 22:28:23 -060013#include "shaderutil.h"
Brianf94e4f22007-01-28 19:01:04 -070014
15
16static const char *VertShaderText =
17 "void main() {\n"
18 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
Brian21625d72007-02-25 12:46:56 -070019 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
Brianf94e4f22007-01-28 19:01:04 -070020 "}\n";
21
22static const char *FragShaderText =
23 "uniform vec4 Scale, Bias;\n"
24 "uniform float Slice;\n"
25 "void main()\n"
26 "{\n"
27 " vec4 scale = vec4(5.0);\n"
28 " vec4 p;\n"
29 " p.xy = gl_TexCoord[0].xy;\n"
30 " p.z = Slice;\n"
Gary Wong0df3dfa2008-12-13 12:58:18 -070031 " p.w = 0;\n"
Brianf94e4f22007-01-28 19:01:04 -070032 " vec4 n = noise4(p * scale);\n"
33 " gl_FragColor = n * Scale + Bias;\n"
34 "}\n";
35
36
Brianf94e4f22007-01-28 19:01:04 -070037static struct uniform_info Uniforms[] = {
Brian2dca3372008-04-09 22:28:23 -060038 { "Scale", 4, GL_FLOAT, { 0.5, 0.4, 0.0, 0}, -1 },
39 { "Bias", 4, GL_FLOAT, { 0.5, 0.3, 0.0, 0}, -1 },
40 { "Slice", 1, GL_FLOAT, { 0.5, 0, 0, 0}, -1 },
41 END_OF_UNIFORMS
Brianf94e4f22007-01-28 19:01:04 -070042};
43
44/* program/shader objects */
45static GLuint fragShader;
46static GLuint vertShader;
47static GLuint program;
48
49static GLint win = 0;
50static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
51static GLfloat Slice = 0.0;
52static GLboolean Anim = GL_FALSE;
53
54
55static void
56Idle(void)
57{
58 Slice += 0.01;
59 glutPostRedisplay();
60}
61
62
63static void
64Redisplay(void)
65{
66 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
67
Brian Paulee0b1bc2009-07-17 13:23:11 -060068 glUniform1fv(Uniforms[2].location, 1, &Slice);
Brianf94e4f22007-01-28 19:01:04 -070069
70 glPushMatrix();
71 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
72 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
73 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
74
75 glBegin(GL_POLYGON);
76 glTexCoord2f(0, 0); glVertex2f(-2, -2);
77 glTexCoord2f(1, 0); glVertex2f( 2, -2);
78 glTexCoord2f(1, 1); glVertex2f( 2, 2);
79 glTexCoord2f(0, 1); glVertex2f(-2, 2);
80 glEnd();
81
82 glPopMatrix();
83
84 glutSwapBuffers();
85}
86
87
88static void
89Reshape(int width, int height)
90{
91 glViewport(0, 0, width, height);
92 glMatrixMode(GL_PROJECTION);
93 glLoadIdentity();
94 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
95 glMatrixMode(GL_MODELVIEW);
96 glLoadIdentity();
97 glTranslatef(0.0f, 0.0f, -15.0f);
98}
99
100
101static void
102CleanUp(void)
103{
Brian Paulee0b1bc2009-07-17 13:23:11 -0600104 glDeleteShader(fragShader);
105 glDeleteShader(vertShader);
106 glDeleteProgram(program);
Brianf94e4f22007-01-28 19:01:04 -0700107 glutDestroyWindow(win);
108}
109
110
111static void
112Key(unsigned char key, int x, int y)
113{
114 const GLfloat step = 0.01;
115 (void) x;
116 (void) y;
117
118 switch(key) {
119 case 'a':
120 Anim = !Anim;
121 glutIdleFunc(Anim ? Idle : NULL);
122 case 's':
123 Slice -= step;
124 break;
125 case 'S':
126 Slice += step;
127 break;
128 case 'z':
129 zRot -= 1.0;
130 break;
131 case 'Z':
132 zRot += 1.0;
133 break;
134 case 27:
135 CleanUp();
136 exit(0);
137 break;
138 }
139 glutPostRedisplay();
140}
141
142
143static void
144SpecialKey(int key, int x, int y)
145{
146 const GLfloat step = 3.0f;
147
148 (void) x;
149 (void) y;
150
151 switch(key) {
152 case GLUT_KEY_UP:
153 xRot -= step;
154 break;
155 case GLUT_KEY_DOWN:
156 xRot += step;
157 break;
158 case GLUT_KEY_LEFT:
159 yRot -= step;
160 break;
161 case GLUT_KEY_RIGHT:
162 yRot += step;
163 break;
164 }
165 glutPostRedisplay();
166}
167
168
169
170static void
Brianf94e4f22007-01-28 19:01:04 -0700171Init(void)
172{
Brian2dca3372008-04-09 22:28:23 -0600173 if (!ShadersSupported())
174 exit(1);
Brianf94e4f22007-01-28 19:01:04 -0700175
Brian2dca3372008-04-09 22:28:23 -0600176 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
177 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
178 program = LinkShaders(vertShader, fragShader);
Brianf94e4f22007-01-28 19:01:04 -0700179
Brian Paulee0b1bc2009-07-17 13:23:11 -0600180 glUseProgram(program);
Brianf94e4f22007-01-28 19:01:04 -0700181
Brian2dca3372008-04-09 22:28:23 -0600182 InitUniforms(program, Uniforms);
Brianf94e4f22007-01-28 19:01:04 -0700183
184 assert(glGetError() == 0);
185
186 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
187
188 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
189
Brian Paulee0b1bc2009-07-17 13:23:11 -0600190 assert(glIsProgram(program));
191 assert(glIsShader(fragShader));
192 assert(glIsShader(vertShader));
Brianf94e4f22007-01-28 19:01:04 -0700193
194 glColor3f(1, 0, 0);
195}
196
197
198int
199main(int argc, char *argv[])
200{
201 glutInit(&argc, argv);
202 glutInitWindowPosition( 0, 0);
203 glutInitWindowSize(400, 400);
204 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
205 win = glutCreateWindow(argv[0]);
Keith Whitwellb799af92009-06-29 14:13:58 +0100206 glewInit();
Brianf94e4f22007-01-28 19:01:04 -0700207 glutReshapeFunc(Reshape);
208 glutKeyboardFunc(Key);
209 glutSpecialFunc(SpecialKey);
210 glutDisplayFunc(Redisplay);
211 Init();
212 glutMainLoop();
213 return 0;
214}
215