2 * Test/demo of Ian McEwan's simplex noise function.
3 * See https://github.com/ashima/webgl-noise
15 #include "glut_wrap.h"
16 #include "shaderutil.h"
19 static const char *VertShaderText
=
21 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
22 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
25 static const char *FragShaderMainText
=
26 "uniform float Slice;\n"
29 " vec3 c = vec3(3.0 * gl_TexCoord[0].xy, Slice);\n"
30 " float r = simplexNoise3(c); \n"
31 " gl_FragColor = vec4(r, r, r, 1.0);;\n"
35 static struct uniform_info Uniforms
[] = {
36 { "pParam", 1, GL_FLOAT_VEC4
, { 17*17, 3, 2, 3 }, -1 },
37 { "Slice", 1, GL_FLOAT
, { 0.5, 0, 0, 0}, -1 },
41 /* program/shader objects */
42 static GLuint fragShader
;
43 static GLuint vertShader
;
44 static GLuint program
;
47 static GLfloat xRot
= 0.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
48 static GLfloat Slice
= 0.0;
49 static GLboolean Anim
= GL_TRUE
;
50 static GLint ParamUniform
= -1, SliceUniform
= -1;
56 Slice
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
64 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
66 glUniform1f(SliceUniform
, Slice
);
69 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
70 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
71 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
74 glTexCoord2f(0, 0); glVertex2f(-2, -2);
75 glTexCoord2f(1, 0); glVertex2f( 2, -2);
76 glTexCoord2f(1, 1); glVertex2f( 2, 2);
77 glTexCoord2f(0, 1); glVertex2f(-2, 2);
87 Reshape(int width
, int height
)
89 glViewport(0, 0, width
, height
);
90 glMatrixMode(GL_PROJECTION
);
92 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
93 glMatrixMode(GL_MODELVIEW
);
95 glTranslatef(0.0f
, 0.0f
, -15.0f
);
102 glDeleteShader(fragShader
);
103 glDeleteShader(vertShader
);
104 glDeleteProgram(program
);
105 glutDestroyWindow(win
);
110 Key(unsigned char key
, int x
, int y
)
112 const GLfloat step
= 1.0;//0.01;
119 glutIdleFunc(Anim
? Idle
: NULL
);
143 SpecialKey(int key
, int x
, int y
)
145 const GLfloat step
= 3.0f
;
172 const char *filename
= "simplex-noise.glsl";
173 char noiseText
[10000];
177 f
= fopen(filename
, "r");
179 fprintf(stderr
, "Unable to open %s\n", filename
);
183 len
= fread(noiseText
, 1, sizeof(noiseText
), f
);
186 /* append main() code onto buffer */
187 strcpy(noiseText
+ len
, FragShaderMainText
);
189 if (!ShadersSupported())
192 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText
);
193 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, noiseText
);
194 program
= LinkShaders(vertShader
, fragShader
);
196 glUseProgram(program
);
198 ParamUniform
= glGetUniformLocation(program
, "pParam");
199 SliceUniform
= glGetUniformLocation(program
, "Slice");
201 SetUniformValues(program
, Uniforms
);
202 PrintUniforms(Uniforms
);
204 assert(glGetError() == 0);
206 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
208 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
210 assert(glIsProgram(program
));
211 assert(glIsShader(fragShader
));
212 assert(glIsShader(vertShader
));
219 main(int argc
, char *argv
[])
221 glutInit(&argc
, argv
);
222 glutInitWindowSize(400, 400);
223 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
224 win
= glutCreateWindow(argv
[0]);
226 glutReshapeFunc(Reshape
);
227 glutKeyboardFunc(Key
);
228 glutSpecialFunc(SpecialKey
);
229 glutDisplayFunc(Redisplay
);
230 glutIdleFunc(Anim
? Idle
: NULL
);