2 * Test noise() functions.
17 static const char *VertShaderText
=
19 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
20 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
23 static const char *FragShaderText
=
24 "uniform vec4 Scale, Bias;\n"
25 "uniform float Slice;\n"
28 " vec4 scale = vec4(5.0);\n"
30 " p.xy = gl_TexCoord[0].xy;\n"
32 " vec4 n = noise4(p * scale);\n"
33 " gl_FragColor = n * Scale + Bias;\n"
44 static struct uniform_info Uniforms
[] = {
45 { "Scale", 4, -1, { 0.5, 0.4, 0.0, 0} },
46 { "Bias", 4, -1, { 0.5, 0.3, 0.0, 0} },
47 { "Slice", 1, -1, { 0.5, 0, 0, 0} },
48 { NULL
, 0, 0, { 0, 0, 0, 0 } }
51 /* program/shader objects */
52 static GLuint fragShader
;
53 static GLuint vertShader
;
54 static GLuint program
;
57 static GLfloat xRot
= 0.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
58 static GLfloat Slice
= 0.0;
59 static GLboolean Anim
= GL_FALSE
;
73 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
75 glUniform1fv_func(Uniforms
[2].location
, 1, &Slice
);
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
);
83 glTexCoord2f(0, 0); glVertex2f(-2, -2);
84 glTexCoord2f(1, 0); glVertex2f( 2, -2);
85 glTexCoord2f(1, 1); glVertex2f( 2, 2);
86 glTexCoord2f(0, 1); glVertex2f(-2, 2);
96 Reshape(int width
, int height
)
98 glViewport(0, 0, width
, height
);
99 glMatrixMode(GL_PROJECTION
);
101 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
102 glMatrixMode(GL_MODELVIEW
);
104 glTranslatef(0.0f
, 0.0f
, -15.0f
);
111 glDeleteShader_func(fragShader
);
112 glDeleteShader_func(vertShader
);
113 glDeleteProgram_func(program
);
114 glutDestroyWindow(win
);
119 Key(unsigned char key
, int x
, int y
)
121 const GLfloat step
= 0.01;
128 glutIdleFunc(Anim
? Idle
: NULL
);
151 SpecialKey(int key
, int x
, int y
)
153 const GLfloat step
= 3.0f
;
178 LoadAndCompileShader(GLuint shader
, const char *text
)
182 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
184 glCompileShader_func(shader
);
186 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
190 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
191 fprintf(stderr
, "brick: problem compiling shader: %s\n", log
);
195 printf("Shader compiled OK\n");
201 CheckLink(GLuint prog
)
204 glGetProgramiv_func(prog
, GL_LINK_STATUS
, &stat
);
208 glGetProgramInfoLog_func(prog
, 1000, &len
, log
);
209 fprintf(stderr
, "Linker error:\n%s\n", log
);
212 fprintf(stderr
, "Link success!\n");
223 version
= (const char *) glGetString(GL_VERSION
);
224 if (version
[0] != '2' || version
[1] != '.') {
225 printf("Warning: this program expects OpenGL 2.0\n");
231 vertShader
= glCreateShader_func(GL_VERTEX_SHADER
);
232 LoadAndCompileShader(vertShader
, VertShaderText
);
234 fragShader
= glCreateShader_func(GL_FRAGMENT_SHADER
);
235 LoadAndCompileShader(fragShader
, FragShaderText
);
237 program
= glCreateProgram_func();
238 glAttachShader_func(program
, fragShader
);
239 glAttachShader_func(program
, vertShader
);
240 glLinkProgram_func(program
);
242 glUseProgram_func(program
);
244 for (i
= 0; Uniforms
[i
].name
; i
++) {
246 = glGetUniformLocation_func(program
, Uniforms
[i
].name
);
247 printf("Uniform %s location: %d\n", Uniforms
[i
].name
,
248 Uniforms
[i
].location
);
249 switch (Uniforms
[i
].size
) {
251 glUniform1fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
254 glUniform2fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
257 glUniform3fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
260 glUniform4fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
267 assert(glGetError() == 0);
269 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
271 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
273 assert(glIsProgram_func(program
));
274 assert(glIsShader_func(fragShader
));
275 assert(glIsShader_func(vertShader
));
282 main(int argc
, char *argv
[])
284 glutInit(&argc
, argv
);
285 glutInitWindowPosition( 0, 0);
286 glutInitWindowSize(400, 400);
287 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
288 win
= glutCreateWindow(argv
[0]);
289 glutReshapeFunc(Reshape
);
290 glutKeyboardFunc(Key
);
291 glutSpecialFunc(SpecialKey
);
292 glutDisplayFunc(Redisplay
);