blob: d504ba1cc4e55a2b7fcf4996612325295fb3fc75 [file] [log] [blame]
Gary Wongd427a292008-12-13 14:00:37 -07001/**
2 * Another test for noise() functions (noise1 to noise4 tested independently).
3 * 13 Dec 2008
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>
Gary Wongd427a292008-12-13 14:00:37 -070012#include <GL/glut.h>
Gary Wongd427a292008-12-13 14:00:37 -070013
14static const char *VertShaderText =
15 "void main() {\n"
16 " gl_TexCoord[0].xyz = gl_Vertex.xyz;\n"
17 " gl_TexCoord[0].w = gl_MultiTexCoord1.x;\n"
18 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
19 "}\n";
20
21static const char *FragShaderText[ 4 ] = {
22 "void main()\n"
23 "{\n"
Gary Wonga42342c2008-12-13 20:06:21 -070024 " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].w ) * 0.5 + 0.5;\n"
Vinson Leed4dc2e32009-11-18 12:49:31 -080025 " gl_FragColor.a = 1.0;\n"
Gary Wongd427a292008-12-13 14:00:37 -070026 "}\n",
27 "void main()\n"
28 "{\n"
Gary Wonga42342c2008-12-13 20:06:21 -070029 " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xw ) * 0.5 + 0.5;\n"
Vinson Leed4dc2e32009-11-18 12:49:31 -080030 " gl_FragColor.a = 1.0;\n"
Gary Wongd427a292008-12-13 14:00:37 -070031 "}\n",
32 "void main()\n"
33 "{\n"
Gary Wonga42342c2008-12-13 20:06:21 -070034 " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyw ) * 0.5 + 0.5;\n"
Vinson Leed4dc2e32009-11-18 12:49:31 -080035 " gl_FragColor.a = 1.0;\n"
Gary Wongd427a292008-12-13 14:00:37 -070036 "}\n",
37 "void main()\n"
38 "{\n"
Gary Wonga42342c2008-12-13 20:06:21 -070039 " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyzw ) * 0.5 + 0.5;\n"
Vinson Leed4dc2e32009-11-18 12:49:31 -080040 " gl_FragColor.a = 1.0;\n"
Gary Wongd427a292008-12-13 14:00:37 -070041 "}\n"
42};
43
44struct uniform_info {
45 const char *name;
46 GLuint size;
47 GLint location;
48 GLfloat value[4];
49};
50
51/* program/shader objects */
52static GLuint fragShader[ 4 ];
53static GLuint vertShader;
54static GLuint program[ 4 ];
55
56static GLint win = 0;
57static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
58static GLfloat Slice = 0.0;
59static GLboolean Anim = GL_FALSE;
60
61
62static void
63Idle(void)
64{
65 Slice += 0.01;
66 glutPostRedisplay();
67}
68
69
70static void
71Redisplay(void)
72{
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74
75 glMultiTexCoord1f( GL_TEXTURE1, Slice );
76
77 glPushMatrix();
78 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
79 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
80 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
81
82 glutSolidTeapot( 1.0 );
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 GLint i;
107
Brian Paulee0b1bc2009-07-17 13:23:11 -0600108 glDeleteShader(vertShader);
Gary Wongd427a292008-12-13 14:00:37 -0700109 for( i = 0; i < 4; i++ ) {
Brian Paulee0b1bc2009-07-17 13:23:11 -0600110 glDeleteShader(fragShader[ i ]);
111 glDeleteProgram(program[ i ]);
Gary Wongd427a292008-12-13 14:00:37 -0700112 }
113 glutDestroyWindow(win);
114}
115
116
117static void
118Key(unsigned char key, int x, int y)
119{
120 const GLfloat step = 0.01;
121 (void) x;
122 (void) y;
123
124 switch(key) {
125 case 'a':
126 Anim = !Anim;
127 glutIdleFunc(Anim ? Idle : NULL);
Vinson Lee43080e42009-11-12 16:20:23 -0800128 break;
Gary Wongd427a292008-12-13 14:00:37 -0700129 case 's':
130 Slice -= step;
131 break;
132 case 'S':
133 Slice += step;
134 break;
135 case 'z':
136 zRot -= 1.0;
137 break;
138 case 'Z':
139 zRot += 1.0;
140 break;
141 case '1':
142 case '2':
143 case '3':
144 case '4':
Brian Paulee0b1bc2009-07-17 13:23:11 -0600145 glUseProgram(program[ key - '1' ]);
Gary Wongd427a292008-12-13 14:00:37 -0700146 break;
147 case 27:
148 CleanUp();
149 exit(0);
150 break;
151 }
152 glutPostRedisplay();
153}
154
155
156static void
157SpecialKey(int key, int x, int y)
158{
159 const GLfloat step = 3.0f;
160
161 (void) x;
162 (void) y;
163
164 switch(key) {
165 case GLUT_KEY_UP:
166 xRot -= step;
167 break;
168 case GLUT_KEY_DOWN:
169 xRot += step;
170 break;
171 case GLUT_KEY_LEFT:
172 yRot -= step;
173 break;
174 case GLUT_KEY_RIGHT:
175 yRot += step;
176 break;
177 }
178 glutPostRedisplay();
179}
180
181
182
183static void
184LoadAndCompileShader(GLuint shader, const char *text)
185{
186 GLint stat;
187
Brian Paulee0b1bc2009-07-17 13:23:11 -0600188 glShaderSource(shader, 1, (const GLchar **) &text, NULL);
Gary Wongd427a292008-12-13 14:00:37 -0700189
Brian Paulee0b1bc2009-07-17 13:23:11 -0600190 glCompileShader(shader);
Gary Wongd427a292008-12-13 14:00:37 -0700191
Brian Paulee0b1bc2009-07-17 13:23:11 -0600192 glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
Gary Wongd427a292008-12-13 14:00:37 -0700193 if (!stat) {
194 GLchar log[1000];
195 GLsizei len;
Brian Paulee0b1bc2009-07-17 13:23:11 -0600196 glGetShaderInfoLog(shader, 1000, &len, log);
Vinson Leed4dc2e32009-11-18 12:49:31 -0800197 fprintf(stderr, "multinoise: problem compiling shader: %s\n", log);
Gary Wongd427a292008-12-13 14:00:37 -0700198 exit(1);
199 }
200 else {
201 printf("Shader compiled OK\n");
202 }
203}
204
205
206static void
207CheckLink(GLuint prog)
208{
209 GLint stat;
Brian Paulee0b1bc2009-07-17 13:23:11 -0600210 glGetProgramiv(prog, GL_LINK_STATUS, &stat);
Gary Wongd427a292008-12-13 14:00:37 -0700211 if (!stat) {
212 GLchar log[1000];
213 GLsizei len;
Brian Paulee0b1bc2009-07-17 13:23:11 -0600214 glGetProgramInfoLog(prog, 1000, &len, log);
Gary Wongd427a292008-12-13 14:00:37 -0700215 fprintf(stderr, "Linker error:\n%s\n", log);
216 }
217 else {
218 fprintf(stderr, "Link success!\n");
219 }
220}
221
222
223static void
224Init(void)
225{
226 const char *version;
227 GLint i;
228
229 version = (const char *) glGetString(GL_VERSION);
230 if (version[0] != '2' || version[1] != '.') {
231 printf("Warning: this program expects OpenGL 2.0\n");
232 /*exit(1);*/
233 }
234
Brian Paulee0b1bc2009-07-17 13:23:11 -0600235 vertShader = glCreateShader(GL_VERTEX_SHADER);
Gary Wongd427a292008-12-13 14:00:37 -0700236 LoadAndCompileShader(vertShader, VertShaderText);
237
238 for( i = 0; i < 4; i++ ) {
Brian Paulee0b1bc2009-07-17 13:23:11 -0600239 fragShader[ i ] = glCreateShader(GL_FRAGMENT_SHADER);
Gary Wongd427a292008-12-13 14:00:37 -0700240 LoadAndCompileShader(fragShader[ i ], FragShaderText[ i ]);
Brian Paulee0b1bc2009-07-17 13:23:11 -0600241 program[ i ] = glCreateProgram();
242 glAttachShader(program[ i ], fragShader[ i ]);
243 glAttachShader(program[ i ], vertShader);
244 glLinkProgram(program[ i ]);
Gary Wongd427a292008-12-13 14:00:37 -0700245 CheckLink(program[ i ]);
246 }
247
Brian Paulee0b1bc2009-07-17 13:23:11 -0600248 glUseProgram(program[ 0 ]);
Gary Wongd427a292008-12-13 14:00:37 -0700249
250 assert(glGetError() == 0);
251
252 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
253
254 glColor3f(1, 0, 0);
255
256 glFrontFace( GL_CW );
257 glEnable( GL_CULL_FACE );
258 glEnable( GL_DEPTH_TEST );
259}
260
261
262int
263main(int argc, char *argv[])
264{
265 glutInit(&argc, argv);
Gary Wongd427a292008-12-13 14:00:37 -0700266 glutInitWindowSize(400, 400);
267 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
268 win = glutCreateWindow(argv[0]);
Keith Whitwellb799af92009-06-29 14:13:58 +0100269 glewInit();
Gary Wongd427a292008-12-13 14:00:37 -0700270 glutReshapeFunc(Reshape);
271 glutKeyboardFunc(Key);
272 glutSpecialFunc(SpecialKey);
273 glutDisplayFunc(Redisplay);
274 Init();
275 glutMainLoop();
276 return 0;
277}
278