mesa-demos: NOTE! Default branch is now main
[mesa-demos.git] / src / glsl / noise2.c
blob8daa620c82e1a453050650f29eb9b2ea3bb25a21
1 /*
2 * GLSL noise demo.
4 * Michal Krol
5 * 20 February 2006
7 * Based on the original demo by:
8 * Stefan Gustavson (stegu@itn.liu.se) 2004, 2005
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <glad/glad.h>
14 #include "glut_wrap.h"
17 static GLhandleARB fragShader;
18 static GLhandleARB vertShader;
19 static GLhandleARB program;
21 static GLint uTime;
23 static GLint t0 = 0;
24 static GLint frames = 0;
26 static GLfloat u_time = 0.0f;
28 static void
29 Redisplay(void)
31 GLint t;
33 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
35 glUniform1fARB(uTime, 0.5f * u_time);
37 glPushMatrix();
38 glutSolidSphere(2.0, 20, 10);
39 glPopMatrix();
41 glutSwapBuffers();
42 frames++;
44 t = glutGet(GLUT_ELAPSED_TIME);
45 if (t - t0 >= 5000) {
46 GLfloat seconds = (GLfloat) (t - t0) / 1000.0f;
47 GLfloat fps = frames / seconds;
48 printf("%d frames in %6.3f seconds = %6.3f FPS\n", frames, seconds,
49 fps);
50 fflush(stdout);
51 t0 = t;
52 frames = 0;
56 static void
57 Idle(void)
59 u_time += 0.1f;
60 glutPostRedisplay();
63 static void
64 Reshape(int width, int height)
66 glViewport(0, 0, width, height);
67 glMatrixMode(GL_PROJECTION);
68 glLoadIdentity();
69 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
70 glMatrixMode(GL_MODELVIEW);
71 glLoadIdentity();
72 glTranslatef(0.0f, 0.0f, -15.0f);
75 static void
76 Key(unsigned char key, int x, int y)
78 (void) x;
79 (void) y;
81 switch (key) {
82 case 27:
83 exit(0);
84 break;
86 glutPostRedisplay();
89 static void
90 Init(void)
92 static const char *fragShaderText =
93 "uniform float time;\n"
94 "varying vec3 position;\n"
95 "void main () {\n"
96 " gl_FragColor = vec4 (vec3 (0.5 + 0.5 * noise1 (vec4 (position, time))), 1.0);\n"
97 "}\n";
98 static const char *vertShaderText =
99 "varying vec3 position;\n"
100 "void main () {\n"
101 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
102 " position = 4.0 * gl_Vertex.xyz;\n" "}\n";
104 if (!glutExtensionSupported("GL_ARB_fragment_shader")) {
105 printf("Sorry, this demo requires GL_ARB_fragment_shader\n");
106 exit(1);
108 if (!glutExtensionSupported("GL_ARB_shader_objects")) {
109 printf("Sorry, this demo requires GL_ARB_shader_objects\n");
110 exit(1);
112 if (!glutExtensionSupported("GL_ARB_shading_language_100")) {
113 printf("Sorry, this demo requires GL_ARB_shading_language_100\n");
114 exit(1);
116 if (!glutExtensionSupported("GL_ARB_vertex_shader")) {
117 printf("Sorry, this demo requires GL_ARB_vertex_shader\n");
118 exit(1);
121 fragShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
122 glShaderSourceARB(fragShader, 1, &fragShaderText, NULL);
123 glCompileShaderARB(fragShader);
125 vertShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
126 glShaderSourceARB(vertShader, 1, &vertShaderText, NULL);
127 glCompileShaderARB(vertShader);
129 program = glCreateProgramObjectARB();
130 glAttachObjectARB(program, fragShader);
131 glAttachObjectARB(program, vertShader);
132 glLinkProgramARB(program);
133 glUseProgramObjectARB(program);
135 uTime = glGetUniformLocationARB(program, "time");
137 glClearColor(0.0f, 0.1f, 0.3f, 1.0f);
138 glEnable(GL_CULL_FACE);
139 glEnable(GL_DEPTH_TEST);
141 printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
145 main(int argc, char *argv[])
147 glutInit(&argc, argv);
148 glutInitWindowSize(200, 200);
149 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
150 glutCreateWindow(argv[0]);
151 glutReshapeFunc(Reshape);
152 glutKeyboardFunc(Key);
153 glutDisplayFunc(Redisplay);
154 glutIdleFunc(Idle);
155 gladLoadGL();
156 Init();
157 glutMainLoop();
158 return 0;