Anonymous user
OpenGL pixel shader: Difference between revisions
m
→{{header|C}}: less flaky noise function (still pretty flaky)
(add JavaScript/WebGL (based on OpenGL example)) |
m (→{{header|C}}: less flaky noise function (still pretty flaky)) |
||
Line 11:
Getting a true (pseudo) random number is surprisingly tricky. The following code makes something noisy, but not at all random:
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>
float angle = 0;
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glUniform1f(r_mod, rand() / (float)RAND_MAX);
glLoadIdentity();
glRotatef(angle, angle * .
glBegin(GL_TRIANGLES);
glVertex3f(-
glVertex3f(0,
glVertex3f(
glEnd();
angle += .
glutSwapBuffers();
}
Line 32 ⟶ 35:
void set_shader()
{
▲ GLuint ps, vs, prog;
const char *f =
"varying float x, y, z;"
"uniform float r_mod;"
"
"void main() {"
" gl_FragColor = vec4(rand(gl_FragCoord.x + x), rand(gl_FragCoord.y + y), rand(gl_FragCoord.z + z), 1);"
"}";
const char *v =
"varying float x, y, z;"
Line 42 ⟶ 47:
" gl_Position = ftransform();"
" x = gl_Position.x; y = gl_Position.y; z = gl_Position.z;"
" x
"}";
Line 59 ⟶ 64:
glLinkProgram(prog);
glUseProgram(prog);
r_mod = glGetUniformLocation(prog, "r_mod");
}
Line 65 ⟶ 71:
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(200, 200);
glutCreateWindow("Stuff");
Line 73 ⟶ 78:
if (!glewIsSupported("GL_VERSION_2_0")) {
fprintf(stderr, "GL 2.0 unsupported\n");
}
|