132001f49Smrg/** 232001f49Smrg * Test noise() functions. 332001f49Smrg * 28 Jan 2007 432001f49Smrg */ 532001f49Smrg 632001f49Smrg#include <assert.h> 732001f49Smrg#include <string.h> 832001f49Smrg#include <stdio.h> 932001f49Smrg#include <stdlib.h> 1032001f49Smrg#include <math.h> 1132001f49Smrg#include <GL/glew.h> 1232001f49Smrg#include "glut_wrap.h" 1332001f49Smrg#include "shaderutil.h" 1432001f49Smrg 1532001f49Smrg 1632001f49Smrgstatic const char *VertShaderText = 1732001f49Smrg "void main() {\n" 1832001f49Smrg " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" 1932001f49Smrg " gl_TexCoord[0] = gl_MultiTexCoord0;\n" 2032001f49Smrg "}\n"; 2132001f49Smrg 2232001f49Smrgstatic const char *FragShaderText = 2332001f49Smrg "uniform vec4 Scale, Bias;\n" 2432001f49Smrg "uniform float Slice;\n" 2532001f49Smrg "void main()\n" 2632001f49Smrg "{\n" 2732001f49Smrg " vec4 scale = vec4(5.0);\n" 2832001f49Smrg " vec4 p;\n" 2932001f49Smrg " p.xy = gl_TexCoord[0].xy;\n" 3032001f49Smrg " p.z = Slice;\n" 3132001f49Smrg " p.w = 0.0;\n" 3232001f49Smrg " vec4 n = noise4(p * scale);\n" 3332001f49Smrg " gl_FragColor = n * Scale + Bias;\n" 3432001f49Smrg "}\n"; 3532001f49Smrg 3632001f49Smrg 3732001f49Smrgstatic struct uniform_info Uniforms[] = { 3832001f49Smrg { "Scale", 1, GL_FLOAT_VEC4, { 0.5, 0.4, 0.0, 0}, -1 }, 3932001f49Smrg { "Bias", 1, GL_FLOAT_VEC4, { 0.5, 0.3, 0.0, 0}, -1 }, 4032001f49Smrg { "Slice", 1, GL_FLOAT, { 0.5, 0, 0, 0}, -1 }, 4132001f49Smrg END_OF_UNIFORMS 4232001f49Smrg}; 4332001f49Smrg 4432001f49Smrg/* program/shader objects */ 4532001f49Smrgstatic GLuint fragShader; 4632001f49Smrgstatic GLuint vertShader; 4732001f49Smrgstatic GLuint program; 4832001f49Smrg 4932001f49Smrgstatic GLint win = 0; 5032001f49Smrgstatic GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f; 5132001f49Smrgstatic GLfloat Slice = 0.0; 5232001f49Smrgstatic GLboolean Anim = GL_FALSE; 5332001f49Smrg 5432001f49Smrg 5532001f49Smrgstatic void 5632001f49SmrgIdle(void) 5732001f49Smrg{ 5832001f49Smrg Slice += 0.01; 5932001f49Smrg glutPostRedisplay(); 6032001f49Smrg} 6132001f49Smrg 6232001f49Smrg 6332001f49Smrgstatic void 6432001f49SmrgRedisplay(void) 6532001f49Smrg{ 6632001f49Smrg glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 6732001f49Smrg 6832001f49Smrg glUniform1fv(Uniforms[2].location, 1, &Slice); 6932001f49Smrg 7032001f49Smrg glPushMatrix(); 7132001f49Smrg glRotatef(xRot, 1.0f, 0.0f, 0.0f); 7232001f49Smrg glRotatef(yRot, 0.0f, 1.0f, 0.0f); 7332001f49Smrg glRotatef(zRot, 0.0f, 0.0f, 1.0f); 7432001f49Smrg 7532001f49Smrg glBegin(GL_POLYGON); 7632001f49Smrg glTexCoord2f(0, 0); glVertex2f(-2, -2); 7732001f49Smrg glTexCoord2f(1, 0); glVertex2f( 2, -2); 7832001f49Smrg glTexCoord2f(1, 1); glVertex2f( 2, 2); 7932001f49Smrg glTexCoord2f(0, 1); glVertex2f(-2, 2); 8032001f49Smrg glEnd(); 8132001f49Smrg 8232001f49Smrg glPopMatrix(); 8332001f49Smrg 8432001f49Smrg glutSwapBuffers(); 8532001f49Smrg} 8632001f49Smrg 8732001f49Smrg 8832001f49Smrgstatic void 8932001f49SmrgReshape(int width, int height) 9032001f49Smrg{ 9132001f49Smrg glViewport(0, 0, width, height); 9232001f49Smrg glMatrixMode(GL_PROJECTION); 9332001f49Smrg glLoadIdentity(); 9432001f49Smrg glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); 9532001f49Smrg glMatrixMode(GL_MODELVIEW); 9632001f49Smrg glLoadIdentity(); 9732001f49Smrg glTranslatef(0.0f, 0.0f, -15.0f); 9832001f49Smrg} 9932001f49Smrg 10032001f49Smrg 10132001f49Smrgstatic void 10232001f49SmrgCleanUp(void) 10332001f49Smrg{ 10432001f49Smrg glDeleteShader(fragShader); 10532001f49Smrg glDeleteShader(vertShader); 10632001f49Smrg glDeleteProgram(program); 10732001f49Smrg glutDestroyWindow(win); 10832001f49Smrg} 10932001f49Smrg 11032001f49Smrg 11132001f49Smrgstatic void 11232001f49SmrgKey(unsigned char key, int x, int y) 11332001f49Smrg{ 11432001f49Smrg const GLfloat step = 0.01; 11532001f49Smrg (void) x; 11632001f49Smrg (void) y; 11732001f49Smrg 11832001f49Smrg switch(key) { 11932001f49Smrg case 'a': 12032001f49Smrg Anim = !Anim; 12132001f49Smrg glutIdleFunc(Anim ? Idle : NULL); 12232001f49Smrg break; 12332001f49Smrg case 's': 12432001f49Smrg Slice -= step; 12532001f49Smrg break; 12632001f49Smrg case 'S': 12732001f49Smrg Slice += step; 12832001f49Smrg break; 12932001f49Smrg case 'z': 13032001f49Smrg zRot -= 1.0; 13132001f49Smrg break; 13232001f49Smrg case 'Z': 13332001f49Smrg zRot += 1.0; 13432001f49Smrg break; 13532001f49Smrg case 27: 13632001f49Smrg CleanUp(); 13732001f49Smrg exit(0); 13832001f49Smrg break; 13932001f49Smrg } 14032001f49Smrg glutPostRedisplay(); 14132001f49Smrg} 14232001f49Smrg 14332001f49Smrg 14432001f49Smrgstatic void 14532001f49SmrgSpecialKey(int key, int x, int y) 14632001f49Smrg{ 14732001f49Smrg const GLfloat step = 3.0f; 14832001f49Smrg 14932001f49Smrg (void) x; 15032001f49Smrg (void) y; 15132001f49Smrg 15232001f49Smrg switch(key) { 15332001f49Smrg case GLUT_KEY_UP: 15432001f49Smrg xRot -= step; 15532001f49Smrg break; 15632001f49Smrg case GLUT_KEY_DOWN: 15732001f49Smrg xRot += step; 15832001f49Smrg break; 15932001f49Smrg case GLUT_KEY_LEFT: 16032001f49Smrg yRot -= step; 16132001f49Smrg break; 16232001f49Smrg case GLUT_KEY_RIGHT: 16332001f49Smrg yRot += step; 16432001f49Smrg break; 16532001f49Smrg } 16632001f49Smrg glutPostRedisplay(); 16732001f49Smrg} 16832001f49Smrg 16932001f49Smrg 17032001f49Smrg 17132001f49Smrgstatic void 17232001f49SmrgInit(void) 17332001f49Smrg{ 17432001f49Smrg if (!ShadersSupported()) 17532001f49Smrg exit(1); 17632001f49Smrg 17732001f49Smrg vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText); 17832001f49Smrg fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText); 17932001f49Smrg program = LinkShaders(vertShader, fragShader); 18032001f49Smrg 18132001f49Smrg glUseProgram(program); 18232001f49Smrg 18332001f49Smrg SetUniformValues(program, Uniforms); 18432001f49Smrg PrintUniforms(Uniforms); 18532001f49Smrg 18632001f49Smrg assert(glGetError() == 0); 18732001f49Smrg 18832001f49Smrg glClearColor(0.4f, 0.4f, 0.8f, 0.0f); 18932001f49Smrg 19032001f49Smrg printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); 19132001f49Smrg 19232001f49Smrg assert(glIsProgram(program)); 19332001f49Smrg assert(glIsShader(fragShader)); 19432001f49Smrg assert(glIsShader(vertShader)); 19532001f49Smrg 19632001f49Smrg glColor3f(1, 0, 0); 19732001f49Smrg} 19832001f49Smrg 19932001f49Smrg 20032001f49Smrgint 20132001f49Smrgmain(int argc, char *argv[]) 20232001f49Smrg{ 20332001f49Smrg glutInit(&argc, argv); 20432001f49Smrg glutInitWindowSize(400, 400); 20532001f49Smrg glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 20632001f49Smrg win = glutCreateWindow(argv[0]); 20732001f49Smrg glewInit(); 20832001f49Smrg glutReshapeFunc(Reshape); 20932001f49Smrg glutKeyboardFunc(Key); 21032001f49Smrg glutSpecialFunc(SpecialKey); 21132001f49Smrg glutDisplayFunc(Redisplay); 21232001f49Smrg Init(); 21332001f49Smrg glutMainLoop(); 21432001f49Smrg return 0; 21532001f49Smrg} 21632001f49Smrg 217