WIP - port to Mali EGL
[mesa-demos/mali.git] / src / glsl / simplex-noise.c
blob13fdd5df243eebf23da096fb4678fe6d3c524b14
1 /**
2 * Test/demo of Ian McEwan's simplex noise function.
3 * See https://github.com/ashima/webgl-noise
5 * Brian Paul
6 * 17 March 2011
7 */
9 #include <assert.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <math.h>
14 #include <GL/glew.h>
15 #include "glut_wrap.h"
16 #include "shaderutil.h"
19 static const char *VertShaderText =
20 "void main() {\n"
21 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
22 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
23 "}\n";
25 static const char *FragShaderMainText =
26 "uniform float Slice;\n"
27 "void main()\n"
28 "{\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"
32 "}\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 },
38 END_OF_UNIFORMS
41 /* program/shader objects */
42 static GLuint fragShader;
43 static GLuint vertShader;
44 static GLuint program;
46 static GLint win = 0;
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;
53 static void
54 Idle(void)
56 Slice = glutGet(GLUT_ELAPSED_TIME) * 0.001;
57 glutPostRedisplay();
61 static void
62 Redisplay(void)
64 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
66 glUniform1f(SliceUniform, Slice);
68 glPushMatrix();
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);
73 glBegin(GL_POLYGON);
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);
78 glEnd();
80 glPopMatrix();
82 glutSwapBuffers();
86 static void
87 Reshape(int width, int height)
89 glViewport(0, 0, width, height);
90 glMatrixMode(GL_PROJECTION);
91 glLoadIdentity();
92 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
93 glMatrixMode(GL_MODELVIEW);
94 glLoadIdentity();
95 glTranslatef(0.0f, 0.0f, -15.0f);
99 static void
100 CleanUp(void)
102 glDeleteShader(fragShader);
103 glDeleteShader(vertShader);
104 glDeleteProgram(program);
105 glutDestroyWindow(win);
109 static void
110 Key(unsigned char key, int x, int y)
112 const GLfloat step = 1.0;//0.01;
113 (void) x;
114 (void) y;
116 switch(key) {
117 case 'a':
118 Anim = !Anim;
119 glutIdleFunc(Anim ? Idle : NULL);
120 break;
121 case 's':
122 Slice -= step;
123 break;
124 case 'S':
125 Slice += step;
126 break;
127 case 'z':
128 zRot -= 1.0;
129 break;
130 case 'Z':
131 zRot += 1.0;
132 break;
133 case 27:
134 CleanUp();
135 exit(0);
136 break;
138 glutPostRedisplay();
142 static void
143 SpecialKey(int key, int x, int y)
145 const GLfloat step = 3.0f;
147 (void) x;
148 (void) y;
150 switch(key) {
151 case GLUT_KEY_UP:
152 xRot -= step;
153 break;
154 case GLUT_KEY_DOWN:
155 xRot += step;
156 break;
157 case GLUT_KEY_LEFT:
158 yRot -= step;
159 break;
160 case GLUT_KEY_RIGHT:
161 yRot += step;
162 break;
164 glutPostRedisplay();
169 static void
170 Init(void)
172 const char *filename = "simplex-noise.glsl";
173 char noiseText[10000];
174 FILE *f;
175 int len;
177 f = fopen(filename, "r");
178 if (!f) {
179 fprintf(stderr, "Unable to open %s\n", filename);
180 exit(1);
183 len = fread(noiseText, 1, sizeof(noiseText), f);
184 fclose(f);
186 /* append main() code onto buffer */
187 strcpy(noiseText + len, FragShaderMainText);
189 if (!ShadersSupported())
190 exit(1);
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));
214 glColor3f(1, 0, 0);
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]);
225 glewInit();
226 glutReshapeFunc(Reshape);
227 glutKeyboardFunc(Key);
228 glutSpecialFunc(SpecialKey);
229 glutDisplayFunc(Redisplay);
230 glutIdleFunc(Anim ? Idle : NULL);
231 Init();
232 glutMainLoop();
233 return 0;