2 * Test noise() functions.
17 static const char *VertShaderText
=
19 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
22 static const char *FragShaderText
=
23 "uniform vec4 Scale, Bias;\n"
24 "uniform float Slice;\n"
27 " vec4 scale = vec4(5.0);\n"
29 " p.xy = gl_TexCoord[0].xy;\n"
31 " vec4 n = noise4(p * scale);\n"
32 " gl_FragColor = n * Scale + Bias;\n"
43 static struct uniform_info Uniforms
[] = {
44 { "Scale", 4, -1, { 0.5, 0.4, 0.0, 0} },
45 { "Bias", 4, -1, { 0.5, 0.3, 0.0, 0} },
46 { "Slice", 1, -1, { 0.5, 0, 0, 0} },
47 { NULL
, 0, 0, { 0, 0, 0, 0 } }
50 /* program/shader objects */
51 static GLuint fragShader
;
52 static GLuint vertShader
;
53 static GLuint program
;
56 static GLfloat xRot
= 0.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
57 static GLfloat Slice
= 0.0;
58 static GLboolean Anim
= GL_FALSE
;
72 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
74 glUniform1fv_func(Uniforms
[2].location
, 1, &Slice
);
77 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
78 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
79 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
82 glTexCoord2f(0, 0); glVertex2f(-2, -2);
83 glTexCoord2f(1, 0); glVertex2f( 2, -2);
84 glTexCoord2f(1, 1); glVertex2f( 2, 2);
85 glTexCoord2f(0, 1); glVertex2f(-2, 2);
95 Reshape(int width
, int height
)
97 glViewport(0, 0, width
, height
);
98 glMatrixMode(GL_PROJECTION
);
100 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
101 glMatrixMode(GL_MODELVIEW
);
103 glTranslatef(0.0f
, 0.0f
, -15.0f
);
110 glDeleteShader_func(fragShader
);
111 glDeleteShader_func(vertShader
);
112 glDeleteProgram_func(program
);
113 glutDestroyWindow(win
);
118 Key(unsigned char key
, int x
, int y
)
120 const GLfloat step
= 0.01;
127 glutIdleFunc(Anim
? Idle
: NULL
);
150 SpecialKey(int key
, int x
, int y
)
152 const GLfloat step
= 3.0f
;
177 LoadAndCompileShader(GLuint shader
, const char *text
)
181 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
183 glCompileShader_func(shader
);
185 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
189 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
190 fprintf(stderr
, "brick: problem compiling shader: %s\n", log
);
194 printf("Shader compiled OK\n");
200 CheckLink(GLuint prog
)
203 glGetProgramiv_func(prog
, GL_LINK_STATUS
, &stat
);
207 glGetProgramInfoLog_func(prog
, 1000, &len
, log
);
208 fprintf(stderr
, "Linker error:\n%s\n", log
);
211 fprintf(stderr
, "Link success!\n");
222 version
= (const char *) glGetString(GL_VERSION
);
223 if (version
[0] != '2' || version
[1] != '.') {
224 printf("Warning: this program expects OpenGL 2.0\n");
230 vertShader
= glCreateShader_func(GL_VERTEX_SHADER
);
231 LoadAndCompileShader(vertShader
, VertShaderText
);
233 fragShader
= glCreateShader_func(GL_FRAGMENT_SHADER
);
234 LoadAndCompileShader(fragShader
, FragShaderText
);
236 program
= glCreateProgram_func();
237 glAttachShader_func(program
, fragShader
);
238 glAttachShader_func(program
, vertShader
);
239 glLinkProgram_func(program
);
241 glUseProgram_func(program
);
243 for (i
= 0; Uniforms
[i
].name
; i
++) {
245 = glGetUniformLocation_func(program
, Uniforms
[i
].name
);
246 printf("Uniform %s location: %d\n", Uniforms
[i
].name
,
247 Uniforms
[i
].location
);
248 switch (Uniforms
[i
].size
) {
250 glUniform1fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
253 glUniform2fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
256 glUniform3fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
259 glUniform4fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
266 assert(glGetError() == 0);
268 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
270 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
272 assert(glIsProgram_func(program
));
273 assert(glIsShader_func(fragShader
));
274 assert(glIsShader_func(vertShader
));
281 main(int argc
, char *argv
[])
283 glutInit(&argc
, argv
);
284 glutInitWindowPosition( 0, 0);
285 glutInitWindowSize(400, 400);
286 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
287 win
= glutCreateWindow(argv
[0]);
288 glutReshapeFunc(Reshape
);
289 glutKeyboardFunc(Key
);
290 glutSpecialFunc(SpecialKey
);
291 glutDisplayFunc(Redisplay
);