2009-05-17 14:26:36 -06:00
|
|
|
/**
|
|
|
|
* 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>
|
2010-05-22 14:05:42 -06:00
|
|
|
#include <GL/glew.h>
|
2009-05-17 14:26:36 -06:00
|
|
|
#include <GL/glut.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.rgb = noise3( gl_TexCoord[ 0 ].w ) * 0.5 + 0.5;\n"
|
2010-05-22 14:05:42 -06:00
|
|
|
" gl_FragColor.a = 1.0;\n"
|
2009-05-17 14:26:36 -06:00
|
|
|
"}\n",
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xw ) * 0.5 + 0.5;\n"
|
2010-05-22 14:05:42 -06:00
|
|
|
" gl_FragColor.a = 1.0;\n"
|
2009-05-17 14:26:36 -06:00
|
|
|
"}\n",
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyw ) * 0.5 + 0.5;\n"
|
2010-05-22 14:05:42 -06:00
|
|
|
" gl_FragColor.a = 1.0;\n"
|
2009-05-17 14:26:36 -06:00
|
|
|
"}\n",
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyzw ) * 0.5 + 0.5;\n"
|
2010-05-22 14:05:42 -06:00
|
|
|
" gl_FragColor.a = 1.0;\n"
|
2009-05-17 14:26:36 -06:00
|
|
|
"}\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;
|
|
|
|
|
2010-05-22 14:05:42 -06:00
|
|
|
glDeleteShader(vertShader);
|
2009-05-17 14:26:36 -06:00
|
|
|
for( i = 0; i < 4; i++ ) {
|
2010-05-22 14:05:42 -06:00
|
|
|
glDeleteShader(fragShader[ i ]);
|
|
|
|
glDeleteProgram(program[ i ]);
|
2009-05-17 14:26:36 -06:00
|
|
|
}
|
|
|
|
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);
|
2010-05-22 14:05:42 -06:00
|
|
|
break;
|
2009-05-17 14:26:36 -06:00
|
|
|
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':
|
2010-05-22 14:05:42 -06:00
|
|
|
glUseProgram(program[ key - '1' ]);
|
2009-05-17 14:26:36 -06:00
|
|
|
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;
|
|
|
|
|
2010-05-22 14:05:42 -06:00
|
|
|
glShaderSource(shader, 1, (const GLchar **) &text, NULL);
|
2009-05-17 14:26:36 -06:00
|
|
|
|
2010-05-22 14:05:42 -06:00
|
|
|
glCompileShader(shader);
|
2009-05-17 14:26:36 -06:00
|
|
|
|
2010-05-22 14:05:42 -06:00
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
|
2009-05-17 14:26:36 -06:00
|
|
|
if (!stat) {
|
|
|
|
GLchar log[1000];
|
|
|
|
GLsizei len;
|
2010-05-22 14:05:42 -06:00
|
|
|
glGetShaderInfoLog(shader, 1000, &len, log);
|
|
|
|
fprintf(stderr, "multinoise: problem compiling shader: %s\n", log);
|
2009-05-17 14:26:36 -06:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("Shader compiled OK\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
CheckLink(GLuint prog)
|
|
|
|
{
|
|
|
|
GLint stat;
|
2010-05-22 14:05:42 -06:00
|
|
|
glGetProgramiv(prog, GL_LINK_STATUS, &stat);
|
2009-05-17 14:26:36 -06:00
|
|
|
if (!stat) {
|
|
|
|
GLchar log[1000];
|
|
|
|
GLsizei len;
|
2010-05-22 14:05:42 -06:00
|
|
|
glGetProgramInfoLog(prog, 1000, &len, log);
|
2009-05-17 14:26:36 -06:00
|
|
|
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);*/
|
|
|
|
}
|
|
|
|
|
2010-05-22 14:05:42 -06:00
|
|
|
vertShader = glCreateShader(GL_VERTEX_SHADER);
|
2009-05-17 14:26:36 -06:00
|
|
|
LoadAndCompileShader(vertShader, VertShaderText);
|
|
|
|
|
|
|
|
for( i = 0; i < 4; i++ ) {
|
2010-05-22 14:05:42 -06:00
|
|
|
fragShader[ i ] = glCreateShader(GL_FRAGMENT_SHADER);
|
2009-05-17 14:26:36 -06:00
|
|
|
LoadAndCompileShader(fragShader[ i ], FragShaderText[ i ]);
|
2010-05-22 14:05:42 -06:00
|
|
|
program[ i ] = glCreateProgram();
|
|
|
|
glAttachShader(program[ i ], fragShader[ i ]);
|
|
|
|
glAttachShader(program[ i ], vertShader);
|
|
|
|
glLinkProgram(program[ i ]);
|
2009-05-17 14:26:36 -06:00
|
|
|
CheckLink(program[ i ]);
|
|
|
|
}
|
|
|
|
|
2010-05-22 14:05:42 -06:00
|
|
|
glUseProgram(program[ 0 ]);
|
2009-05-17 14:26:36 -06:00
|
|
|
|
|
|
|
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);
|
|
|
|
glutInitWindowSize(400, 400);
|
|
|
|
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
|
|
|
|
win = glutCreateWindow(argv[0]);
|
2010-05-22 14:05:42 -06:00
|
|
|
glewInit();
|
2009-05-17 14:26:36 -06:00
|
|
|
glutReshapeFunc(Reshape);
|
|
|
|
glutKeyboardFunc(Key);
|
|
|
|
glutSpecialFunc(SpecialKey);
|
|
|
|
glutDisplayFunc(Redisplay);
|
|
|
|
Init();
|
|
|
|
glutMainLoop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|