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>
 
GLuint ps, vs, prog, r_mod;
float angle = 0;
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glUniform1f(r_mod, rand() / (float)RAND_MAX);
 
glLoadIdentity();
glRotatef(angle, angle * .31, 1, 0);
glBegin(GL_TRIANGLES);
glVertex3f(-.51, -.5, 0);
glVertex3f(0, .51, 0);
glVertex3f(.51, 0, 0);
glEnd();
angle += .0102;
glutSwapBuffers();
}
Line 32 ⟶ 35:
void set_shader()
{
GLuint ps, vs, prog;
const char *f =
"varying float x, y, z;"
"uniform float r_mod;"
"float rand(float s) { return mod(abs(s) * 7654321 + 1, 1); }"
"voidfloat mainrand(float s) { gl_FragColorreturn = vec4mod(randpow(x), randabs(ys * 7.654321) + 1, rand(z1 + r_mod), 1); }";
"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 *+= zy; y *-= x; z += (x +- y);"
"}";
 
Line 59 ⟶ 64:
glLinkProgram(prog);
glUseProgram(prog);
r_mod = glGetUniformLocation(prog, "r_mod");
}
 
Line 65 ⟶ 71:
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(200, 200);
glutCreateWindow("Stuff");
Line 73 ⟶ 78:
if (!glewIsSupported("GL_VERSION_2_0")) {
fprintf(stderr, "GL 2.0 unsupported\n");
exit(return 1);
}
 
Anonymous user