Mesa (mesa_7_2_branch): Add "multinoise" demo, to test 1/2/3/ 4 dimensional noise.

Gary Wong gary at kemper.freedesktop.org
Sat Dec 13 21:32:19 UTC 2008


Module: Mesa
Branch: mesa_7_2_branch
Commit: 112ddd776f7925a53da579689d5ab5cff2d78bad
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=112ddd776f7925a53da579689d5ab5cff2d78bad

Author: Gary Wong <gtw at gnu.org>
Date:   Sat Dec 13 14:00:37 2008 -0700

Add "multinoise" demo, to test 1/2/3/4 dimensional noise.
(cherry picked from commit d427a2910fbb130c1be49667b0133c7605eef7ed)

---

 progs/glsl/.gitignore   |    1 +
 progs/glsl/Makefile     |    1 +
 progs/glsl/multinoise.c |  277 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 279 insertions(+), 0 deletions(-)

diff --git a/progs/glsl/.gitignore b/progs/glsl/.gitignore
index 1e0aece..21c41da 100644
--- a/progs/glsl/.gitignore
+++ b/progs/glsl/.gitignore
@@ -4,6 +4,7 @@ bump
 deriv
 extfuncs.h
 mandelbrot
+multinoise
 multitex
 noise
 points
diff --git a/progs/glsl/Makefile b/progs/glsl/Makefile
index 850b6bd..384649e 100644
--- a/progs/glsl/Makefile
+++ b/progs/glsl/Makefile
@@ -16,6 +16,7 @@ PROGS = \
 	convolutions \
 	deriv \
 	mandelbrot \
+	multinoise \
 	multitex \
 	noise \
 	points \
diff --git a/progs/glsl/multinoise.c b/progs/glsl/multinoise.c
new file mode 100644
index 0000000..914b872
--- /dev/null
+++ b/progs/glsl/multinoise.c
@@ -0,0 +1,277 @@
+/**
+ * Another test for noise() functions (noise1 to noise4 tested independently).
+ * 13 Dec 2008
+ */
+
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <GL/gl.h>
+#include <GL/glut.h>
+#include <GL/glext.h>
+#include "extfuncs.h"
+
+static const char *VertShaderText =
+   "void main() {\n"
+   "   gl_TexCoord[0].xyz = gl_Vertex.xyz;\n"
+   "   gl_TexCoord[0].w = gl_MultiTexCoord1.x;\n"
+   "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
+   "}\n";
+
+static const char *FragShaderText[ 4 ] = {
+   "void main()\n"
+   "{\n"
+   "   gl_FragColor = noise3( gl_TexCoord[ 0 ].w ) * 0.5 + 0.5;\n"
+   "}\n",
+   "void main()\n"
+   "{\n"
+   "   gl_FragColor = noise3( gl_TexCoord[ 0 ].xw ) * 0.5 + 0.5;\n"
+   "}\n",
+   "void main()\n"
+   "{\n"
+   "   gl_FragColor = noise3( gl_TexCoord[ 0 ].xyw ) * 0.5 + 0.5;\n"
+   "}\n",
+   "void main()\n"
+   "{\n"
+   "   gl_FragColor = noise3( gl_TexCoord[ 0 ].xyzw ) * 0.5 + 0.5;\n"
+   "}\n"
+};
+    
+struct uniform_info {
+   const char *name;
+   GLuint size;
+   GLint location;
+   GLfloat value[4];
+};
+
+/* program/shader objects */
+static GLuint fragShader[ 4 ];
+static GLuint vertShader;
+static GLuint program[ 4 ];
+
+static GLint win = 0;
+static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
+static GLfloat Slice = 0.0;
+static GLboolean Anim = GL_FALSE;
+
+
+static void
+Idle(void)
+{
+   Slice += 0.01;
+   glutPostRedisplay();
+}
+
+
+static void
+Redisplay(void)
+{
+   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+   glMultiTexCoord1f( GL_TEXTURE1, Slice );
+   
+   glPushMatrix();
+   glRotatef(xRot, 1.0f, 0.0f, 0.0f);
+   glRotatef(yRot, 0.0f, 1.0f, 0.0f);
+   glRotatef(zRot, 0.0f, 0.0f, 1.0f);
+
+   glutSolidTeapot( 1.0 );
+
+   glPopMatrix();
+
+   glutSwapBuffers();
+}
+
+
+static void
+Reshape(int width, int height)
+{
+   glViewport(0, 0, width, height);
+   glMatrixMode(GL_PROJECTION);
+   glLoadIdentity();
+   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
+   glMatrixMode(GL_MODELVIEW);
+   glLoadIdentity();
+   glTranslatef(0.0f, 0.0f, -15.0f);
+}
+
+
+static void
+CleanUp(void)
+{
+   GLint i;
+
+   glDeleteShader_func(vertShader);
+   for( i = 0; i < 4; i++ ) {
+      glDeleteShader_func(fragShader[ i ]);
+      glDeleteProgram_func(program[ i ]);
+   }
+   glutDestroyWindow(win);
+}
+
+
+static void
+Key(unsigned char key, int x, int y)
+{
+   const GLfloat step = 0.01;
+  (void) x;
+  (void) y;
+
+   switch(key) {
+   case 'a':
+      Anim = !Anim;
+      glutIdleFunc(Anim ? Idle : NULL);
+   case 's':
+      Slice -= step;
+      break;
+   case 'S':
+      Slice += step;
+      break;
+   case 'z':
+      zRot -= 1.0;
+      break;
+   case 'Z':
+      zRot += 1.0;
+      break;
+   case '1':
+   case '2':
+   case '3':
+   case '4':
+      glUseProgram_func(program[ key - '1' ]);
+      break;
+   case 27:
+      CleanUp();
+      exit(0);
+      break;
+   }
+   glutPostRedisplay();
+}
+
+
+static void
+SpecialKey(int key, int x, int y)
+{
+   const GLfloat step = 3.0f;
+
+  (void) x;
+  (void) y;
+
+   switch(key) {
+   case GLUT_KEY_UP:
+      xRot -= step;
+      break;
+   case GLUT_KEY_DOWN:
+      xRot += step;
+      break;
+   case GLUT_KEY_LEFT:
+      yRot -= step;
+      break;
+   case GLUT_KEY_RIGHT:
+      yRot += step;
+      break;
+   }
+   glutPostRedisplay();
+}
+
+
+
+static void
+LoadAndCompileShader(GLuint shader, const char *text)
+{
+   GLint stat;
+
+   glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
+
+   glCompileShader_func(shader);
+
+   glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
+   if (!stat) {
+      GLchar log[1000];
+      GLsizei len;
+      glGetShaderInfoLog_func(shader, 1000, &len, log);
+      fprintf(stderr, "noise: problem compiling shader: %s\n", log);
+      exit(1);
+   }
+   else {
+      printf("Shader compiled OK\n");
+   }
+}
+
+
+static void
+CheckLink(GLuint prog)
+{
+   GLint stat;
+   glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
+   if (!stat) {
+      GLchar log[1000];
+      GLsizei len;
+      glGetProgramInfoLog_func(prog, 1000, &len, log);
+      fprintf(stderr, "Linker error:\n%s\n", log);
+   }
+   else {
+      fprintf(stderr, "Link success!\n");
+   }
+}
+
+
+static void
+Init(void)
+{
+   const char *version;
+   GLint i;
+
+   version = (const char *) glGetString(GL_VERSION);
+   if (version[0] != '2' || version[1] != '.') {
+      printf("Warning: this program expects OpenGL 2.0\n");
+      /*exit(1);*/
+   }
+
+   GetExtensionFuncs();
+
+   vertShader = glCreateShader_func(GL_VERTEX_SHADER);
+   LoadAndCompileShader(vertShader, VertShaderText);
+
+   for( i = 0; i < 4; i++ ) {
+      fragShader[ i ] = glCreateShader_func(GL_FRAGMENT_SHADER);
+      LoadAndCompileShader(fragShader[ i ], FragShaderText[ i ]);
+      program[ i ] = glCreateProgram_func();
+      glAttachShader_func(program[ i ], fragShader[ i ]);
+      glAttachShader_func(program[ i ], vertShader);
+      glLinkProgram_func(program[ i ]);
+      CheckLink(program[ i ]);
+   }
+   
+   glUseProgram_func(program[ 0 ]);
+
+   assert(glGetError() == 0);
+
+   glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
+
+   glColor3f(1, 0, 0);
+
+   glFrontFace( GL_CW );
+   glEnable( GL_CULL_FACE );
+   glEnable( GL_DEPTH_TEST );
+}
+
+
+int
+main(int argc, char *argv[])
+{
+   glutInit(&argc, argv);
+   glutInitWindowPosition( 0, 0);
+   glutInitWindowSize(400, 400);
+   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
+   win = glutCreateWindow(argv[0]);
+   glutReshapeFunc(Reshape);
+   glutKeyboardFunc(Key);
+   glutSpecialFunc(SpecialKey);
+   glutDisplayFunc(Redisplay);
+   Init();
+   glutMainLoop();
+   return 0;
+}
+




More information about the mesa-commit mailing list