diff options
Diffstat (limited to 'progs/glsl')
-rw-r--r-- | progs/glsl/Makefile | 162 | ||||
-rw-r--r-- | progs/glsl/brick.c | 13 | ||||
-rw-r--r-- | progs/glsl/brick.shtest | 8 | ||||
-rw-r--r-- | progs/glsl/bump.c | 13 | ||||
-rw-r--r-- | progs/glsl/mandelbrot.c | 11 | ||||
-rw-r--r-- | progs/glsl/mandelbrot.shtest | 13 | ||||
-rw-r--r-- | progs/glsl/multitex.c | 13 | ||||
-rw-r--r-- | progs/glsl/multitex.shtest | 6 | ||||
-rw-r--r-- | progs/glsl/noise.c | 7 | ||||
-rw-r--r-- | progs/glsl/shtest.c | 630 | ||||
-rw-r--r-- | progs/glsl/texdemo1.c | 11 | ||||
-rw-r--r-- | progs/glsl/toyball.c | 27 | ||||
-rw-r--r-- | progs/glsl/toyball.shtest | 17 | ||||
-rw-r--r-- | progs/glsl/vert-tex.c | 2 |
14 files changed, 736 insertions, 197 deletions
diff --git a/progs/glsl/Makefile b/progs/glsl/Makefile index eedd866c95..0875bdd795 100644 --- a/progs/glsl/Makefile +++ b/progs/glsl/Makefile @@ -14,6 +14,11 @@ LIBS = -L$(TOP)/$(LIB_DIR) -l$(GLUT_LIB) -l$(GLEW_LIB) -l$(GLU_LIB) -l$(GL_LIB) INCLUDE_DIRS = -I$(TOP)/progs/util +# using : to avoid APP_CC pointing to CC loop +CC:=$(APP_CC) +CFLAGS += -I$(INCDIR) +LDLIBS=$(LIBS) + DEMO_SOURCES = \ array.c \ bitmap.c \ @@ -33,6 +38,7 @@ DEMO_SOURCES = \ points.c \ samplers.c \ shadow_sampler.c \ + shtest.c \ skinning.c \ texaaline.c \ texdemo1.c \ @@ -52,28 +58,16 @@ UTIL_SOURCES = \ readtex.c UTIL_OBJS = $(UTIL_SOURCES:.c=.o) - - +PROG_OBJS = $(DEMO_SOURCES:.c=.o) PROGS = $(DEMO_SOURCES:%.c=%) - - -##### RULES ##### - -# make .o file from .c file: -.c.o: - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) $< -o $@ - - -# make executable from .o files -.o: - $(APP_CC) $(INCLUDES) $(CFLAGS) $(LDFLAGS) $< $(UTIL_OBJS) $(LIBS) -o $@ - - ##### TARGETS ##### default: $(PROGS) +$(PROG_OBJS): $(UTIL_HEADERS) + +$(PROGS): $(UTIL_OBJS) clean: -rm -f $(PROGS) @@ -83,7 +77,6 @@ clean: -rm -f readtex.* - ##### Extra dependencies extfuncs.h: $(TOP)/progs/util/extfuncs.h @@ -101,138 +94,3 @@ shaderutil.c: $(TOP)/progs/util/shaderutil.c shaderutil.h: $(TOP)/progs/util/shaderutil.h cp $< . - - -array.o: $(UTIL_HEADERS) - -array: array.o $(UTIL_OBJS) - - -bitmap.o: $(UTIL_HEADERS) - -bitmap: bitmap.o $(UTIL_OBJS) - - -brick.o: $(UTIL_HEADERS) - -brick: brick.o $(UTIL_OBJS) - - -bump.o: $(UTIL_HEADERS) - -bump: bump.o $(UTIL_OBJS) - - -convolutions.o: $(UTIL_HEADERS) - -convolutions: convolutions.o $(UTIL_OBJS) - - -deriv.o: deriv.c $(UTIL_HEADERS) - -deriv: deriv.o $(UTIL_OBJS) - - -identity.o: $(UTIL_HEADERS) - -identity: identity.o $(UTIL_OBJS) - - -fragcoord.o: $(UTIL_HEADERS) - -fragcoord: fragcoord.o $(UTIL_OBJS) - - -linktest.o: $(UTIL_HEADERS) - -linktest: linktest.o $(UTIL_OBJS) - - -mandelbrot.o: $(UTIL_HEADERS) - -mandelbrot: mandelbrot.o $(UTIL_OBJS) - - -multinoise.o: $(UTIL_HEADERS) - -multinoise: multinoise.o $(UTIL_OBJS) - - -multitex.o: $(UTIL_HEADERS) - -multitex: multitex.o $(UTIL_OBJS) - - -noise.o: $(UTIL_HEADERS) - -noise: noise.o $(UTIL_OBJS) - - -noise2.o: $(UTIL_HEADERS) - -noise2: noise2.o $(UTIL_OBJS) - - -points.o: $(UTIL_HEADERS) - -points: points.o $(UTIL_OBJS) - - -pointcoord.o: $(UTIL_HEADERS) - -pointcoord: pointcoord.o $(UTIL_OBJS) - - -samplers.o: $(UTIL_HEADERS) - -samplers: samplers.o $(UTIL_OBJS) - - -samplers_array.o: $(UTIL_HEADERS) - -samplers_array: samplers_array.o $(UTIL_OBJS) - - -shadow_sampler.o: $(UTIL_HEADERS) - -shadow_sampler: shadow_sampler.o $(UTIL_OBJS) - - -skinning.o: $(UTIL_HEADERS) - -skinning: skinning.o $(UTIL_OBJS) - - -texaaline.o: $(UTIL_HEADERS) - -texaaline: texaaline.o $(UTIL_OBJS) - - -texdemo1.o: $(UTIL_HEADERS) - -texdemo1: texdemo1.o $(UTIL_OBJS) - - -toyball.o: $(UTIL_HEADERS) - -toyball: toyball.o $(UTIL_OBJS) - - -twoside.o: $(UTIL_HEADERS) - -twoside: twoside.o $(UTIL_OBJS) - - -trirast.o: $(UTIL_HEADERS) - -trirast: trirast.o $(UTIL_OBJS) - - -vert-or-frag-only.o: $(UTIL_HEADERS) - -vert-or-frag-only: vert-or-frag-only.o $(UTIL_OBJS) - - -vert-tex.o: $(UTIL_HEADERS) - -vert-tex: vert-tex.o $(UTIL_OBJS) diff --git a/progs/glsl/brick.c b/progs/glsl/brick.c index 1d08b231e7..0653c592e5 100644 --- a/progs/glsl/brick.c +++ b/progs/glsl/brick.c @@ -24,12 +24,12 @@ static GLuint program; static struct uniform_info Uniforms[] = { /* vert */ - { "LightPosition", 3, GL_FLOAT, { 0.1, 0.1, 9.0, 0}, -1 }, + { "LightPosition", 1, GL_FLOAT_VEC3, { 0.1, 0.1, 9.0, 0}, -1 }, /* frag */ - { "BrickColor", 3, GL_FLOAT, { 0.8, 0.2, 0.2, 0 }, -1 }, - { "MortarColor", 3, GL_FLOAT, { 0.6, 0.6, 0.6, 0 }, -1 }, - { "BrickSize", 2, GL_FLOAT, { 1.0, 0.3, 0, 0 }, -1 }, - { "BrickPct", 2, GL_FLOAT, { 0.9, 0.8, 0, 0 }, -1 }, + { "BrickColor", 1, GL_FLOAT_VEC3, { 0.8, 0.2, 0.2, 0 }, -1 }, + { "MortarColor", 1, GL_FLOAT_VEC3, { 0.6, 0.6, 0.6, 0 }, -1 }, + { "BrickSize", 1, GL_FLOAT_VEC2, { 1.0, 0.3, 0, 0 }, -1 }, + { "BrickPct", 1, GL_FLOAT_VEC2, { 0.9, 0.8, 0, 0 }, -1 }, END_OF_UNIFORMS }; @@ -148,7 +148,8 @@ Init(void) glUseProgram(program); - InitUniforms(program, Uniforms); + SetUniformValues(program, Uniforms); + PrintUniforms(Uniforms); assert(glGetError() == 0); diff --git a/progs/glsl/brick.shtest b/progs/glsl/brick.shtest new file mode 100644 index 0000000000..8a2152692e --- /dev/null +++ b/progs/glsl/brick.shtest @@ -0,0 +1,8 @@ +vs CH06-brick.vert +fs CH06-brick.frag +uniform LightPosition GL_FLOAT_VEC3 0.1 0.1 9.0 +uniform BrickColor GL_FLOAT_VEC3 0.8 0.2 0.2 +uniform MortarColor GL_FLOAT_VEC3 0.6 0.6 0.6 +uniform BrickSize GL_FLOAT_VEC2 1.0 0.3 +uniform BrickPct GL_FLOAT_VEC2 0.9 0.8 + diff --git a/progs/glsl/bump.c b/progs/glsl/bump.c index ddb986abcb..c0d39c049d 100644 --- a/progs/glsl/bump.c +++ b/progs/glsl/bump.c @@ -24,11 +24,11 @@ static GLuint program; static struct uniform_info Uniforms[] = { - { "LightPosition", 3, GL_FLOAT, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 }, - { "SurfaceColor", 3, GL_FLOAT, { 0.8, 0.8, 0.2, 0 }, -1 }, - { "BumpDensity", 1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 }, - { "BumpSize", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 }, - { "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 }, + { "LightPosition", 1, GL_FLOAT_VEC3, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 }, + { "SurfaceColor", 1, GL_FLOAT_VEC3, { 0.8, 0.8, 0.2, 0 }, -1 }, + { "BumpDensity", 1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 }, + { "BumpSize", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 }, + { "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 }, END_OF_UNIFORMS }; @@ -242,7 +242,8 @@ Init(void) CheckError(__LINE__); - InitUniforms(program, Uniforms); + SetUniformValues(program, Uniforms); + PrintUniforms(Uniforms); CheckError(__LINE__); diff --git a/progs/glsl/mandelbrot.c b/progs/glsl/mandelbrot.c index 38dffc3e74..729a6f125a 100644 --- a/progs/glsl/mandelbrot.c +++ b/progs/glsl/mandelbrot.c @@ -25,7 +25,7 @@ static GLuint program; static struct uniform_info Uniforms[] = { /* vert */ - { "LightPosition", 3, GL_FLOAT, { 0.1, 0.1, 9.0, 0}, -1 }, + { "LightPosition", 1, GL_FLOAT_VEC3, { 0.1, 0.1, 9.0, 0}, -1 }, { "SpecularContribution", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 }, { "DiffuseContribution", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 }, { "Shininess", 1, GL_FLOAT, { 20.0, 0, 0, 0 }, -1 }, @@ -34,9 +34,9 @@ static struct uniform_info Uniforms[] = { { "Zoom", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 }, { "Xcenter", 1, GL_FLOAT, { -1.5, 0, 0, 0 }, -1 }, { "Ycenter", 1, GL_FLOAT, { .005, 0, 0, 0 }, -1 }, - { "InnerColor", 3, GL_FLOAT, { 1, 0, 0, 0 }, -1 }, - { "OuterColor1", 3, GL_FLOAT, { 0, 1, 0, 0 }, -1 }, - { "OuterColor2", 3, GL_FLOAT, { 0, 0, 1, 0 }, -1 }, + { "InnerColor", 1, GL_FLOAT_VEC3, { 1, 0, 0, 0 }, -1 }, + { "OuterColor1", 1, GL_FLOAT_VEC3, { 0, 1, 0, 0 }, -1 }, + { "OuterColor2", 1, GL_FLOAT_VEC3, { 0, 0, 1, 0 }, -1 }, END_OF_UNIFORMS }; @@ -159,7 +159,8 @@ Init(void) glUseProgram(program); - InitUniforms(program, Uniforms); + SetUniformValues(program, Uniforms); + PrintUniforms(Uniforms); uZoom = glGetUniformLocation(program, "Zoom"); uXcenter = glGetUniformLocation(program, "Xcenter"); diff --git a/progs/glsl/mandelbrot.shtest b/progs/glsl/mandelbrot.shtest new file mode 100644 index 0000000000..4f4e5c747e --- /dev/null +++ b/progs/glsl/mandelbrot.shtest @@ -0,0 +1,13 @@ +vs CH18-mandel.vert +fs CH18-mandel.frag +uniform LightPosition GL_FLOAT_VEC3 0.1 0.1 9.0 +uniform SpecularContribution GL_FLOAT 0.5 +uniform DiffuseContribution GL_FLOAT 0.5 +uniform Shininess GL_FLOAT 20.0 +uniform Iterations GL_FLOAT 12 +uniform Zoom GL_FLOAT 0.125 +uniform Xcenter GL_FLOAT -1.5 +uniform Ycenter GL_FLOAT .005 +uniform InnerColor GL_FLOAT_VEC3 1 0 0 +uniform OuterColor1 GL_FLOAT_VEC3 0 1 0 +uniform OuterColor2 GL_FLOAT_VEC3 0 0 1 diff --git a/progs/glsl/multitex.c b/progs/glsl/multitex.c index 5e971716ad..6ec9c833e6 100644 --- a/progs/glsl/multitex.c +++ b/progs/glsl/multitex.c @@ -59,8 +59,8 @@ static GLint VertCoord_attr = -1, TexCoord0_attr = -1, TexCoord1_attr = -1; /* value[0] = tex unit */ static struct uniform_info Uniforms[] = { - { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 }, - { "tex2", 1, GL_INT, { 1, 0, 0, 0 }, -1 }, + { "tex1", 1, GL_SAMPLER_2D, { 0, 0, 0, 0 }, -1 }, + { "tex2", 1, GL_SAMPLER_2D, { 1, 0, 0, 0 }, -1 }, END_OF_UNIFORMS }; @@ -134,7 +134,7 @@ DrawPolygonArray(void) if (VertCoord_attr >= 0) { glVertexAttribPointer(VertCoord_attr, 2, GL_FLOAT, GL_FALSE, - 0, VertCoords); + 0, vertPtr); glEnableVertexAttribArray(VertCoord_attr); } else { @@ -143,11 +143,11 @@ DrawPolygonArray(void) } glVertexAttribPointer(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE, - 0, Tex0Coords); + 0, tex0Ptr); glEnableVertexAttribArray(TexCoord0_attr); glVertexAttribPointer(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE, - 0, Tex1Coords); + 0, tex1Ptr); glEnableVertexAttribArray(TexCoord1_attr); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); @@ -328,7 +328,8 @@ CreateProgram(const char *vertProgFile, const char *fragProgFile, glUseProgram(program); - InitUniforms(program, uniforms); + SetUniformValues(program, uniforms); + PrintUniforms(Uniforms); VertCoord_attr = glGetAttribLocation(program, "VertCoord"); if (VertCoord_attr > 0) { diff --git a/progs/glsl/multitex.shtest b/progs/glsl/multitex.shtest new file mode 100644 index 0000000000..5be45f6c7c --- /dev/null +++ b/progs/glsl/multitex.shtest @@ -0,0 +1,6 @@ +vs multitex.vert +fs multitex.frag +texture 0 ../images/tile.rgb +texture 1 ../images/tree2.rgba +uniform tex1 GL_SAMPLER_2D 0 +uniform tex2 GL_SAMPLER_2D 1 diff --git a/progs/glsl/noise.c b/progs/glsl/noise.c index 59f594e78b..8c36e1c59b 100644 --- a/progs/glsl/noise.c +++ b/progs/glsl/noise.c @@ -35,8 +35,8 @@ static const char *FragShaderText = static struct uniform_info Uniforms[] = { - { "Scale", 4, GL_FLOAT, { 0.5, 0.4, 0.0, 0}, -1 }, - { "Bias", 4, GL_FLOAT, { 0.5, 0.3, 0.0, 0}, -1 }, + { "Scale", 1, GL_FLOAT_VEC4, { 0.5, 0.4, 0.0, 0}, -1 }, + { "Bias", 1, GL_FLOAT_VEC4, { 0.5, 0.3, 0.0, 0}, -1 }, { "Slice", 1, GL_FLOAT, { 0.5, 0, 0, 0}, -1 }, END_OF_UNIFORMS }; @@ -179,7 +179,8 @@ Init(void) glUseProgram(program); - InitUniforms(program, Uniforms); + SetUniformValues(program, Uniforms); + PrintUniforms(Uniforms); assert(glGetError() == 0); diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c new file mode 100644 index 0000000000..97f6f9f8a4 --- /dev/null +++ b/progs/glsl/shtest.c @@ -0,0 +1,630 @@ +/* + * Simple shader test harness. + * Brian Paul + * 13 Aug 2009 + * + * Usage: + * shtest --vs vertShaderFile --fs fragShaderFile + * + * In this case the given vertex/frag shaders are read and compiled. + * Random values are assigned to the uniforms. + * + * or: + * shtest configFile + * + * In this case a config file is read that specifies the file names + * of the shaders plus initial values for uniforms. + * + * Example config file: + * + * vs shader.vert + * fs shader.frag + * uniform pi 3.14159 + * uniform v1 1.0 0.5 0.2 0.3 + * + */ + + +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <GL/glew.h> +#include <GL/glu.h> +#include <GL/glut.h> +#include "shaderutil.h" +#include "readtex.h" + + +typedef enum +{ + SPHERE, + CUBE, + NUM_SHAPES +} shape; + + +static char *FragShaderFile = NULL; +static char *VertShaderFile = NULL; +static char *ConfigFile = NULL; + +/* program/shader objects */ +static GLuint fragShader; +static GLuint vertShader; +static GLuint Program; + + +#define MAX_UNIFORMS 100 +static struct uniform_info Uniforms[MAX_UNIFORMS]; +static GLuint NumUniforms = 0; + + +#define MAX_ATTRIBS 100 +static struct attrib_info Attribs[MAX_ATTRIBS]; +static GLuint NumAttribs = 0; + + +/** + * Config file info. + */ +struct config_file +{ + struct name_value + { + char name[100]; + float value[4]; + int type; + } uniforms[100]; + + int num_uniforms; +}; + + +static GLint win = 0; +static GLboolean Anim = GL_FALSE; +static GLfloat TexRot = 0.0; +static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f; +static shape Object = SPHERE; + + +static float +RandomFloat(float min, float max) +{ + int k = rand() % 10000; + float x = min + (max - min) * k / 10000.0; + return x; +} + + +/** Set new random values for uniforms */ +static void +RandomUniformValues(void) +{ + GLuint i; + for (i = 0; i < NumUniforms; i++) { + if (Uniforms[i].type == GL_FLOAT) { + Uniforms[i].value[0] = RandomFloat(0.0, 1.0); + } + else { + Uniforms[i].value[0] = RandomFloat(-1.0, 2.0); + Uniforms[i].value[1] = RandomFloat(-1.0, 2.0); + Uniforms[i].value[2] = RandomFloat(-1.0, 2.0); + Uniforms[i].value[3] = RandomFloat(-1.0, 2.0); + } + } +} + + +static void +Idle(void) +{ + yRot += 2.0; + if (yRot > 360.0) + yRot -= 360.0; + glutPostRedisplay(); +} + + + +static void +SquareVertex(GLfloat s, GLfloat t, GLfloat size) +{ + GLfloat x = -size + s * 2.0 * size; + GLfloat y = -size + t * 2.0 * size; + GLuint i; + + glMultiTexCoord2f(GL_TEXTURE0, s, t); + glMultiTexCoord2f(GL_TEXTURE1, s, t); + glMultiTexCoord2f(GL_TEXTURE2, s, t); + glMultiTexCoord2f(GL_TEXTURE3, s, t); + + /* assign (s,t) to the generic attributes */ + for (i = 0; i < NumAttribs; i++) { + if (Attribs[i].location >= 0) { + glVertexAttrib2f(Attribs[i].location, s, t); + } + } + + glVertex2f(x, y); +} + + +/* + * Draw a square, specifying normal and tangent vectors. + */ +static void +Square(GLfloat size) +{ + GLint tangentAttrib = 1; + glNormal3f(0, 0, 1); + glVertexAttrib3f(tangentAttrib, 1, 0, 0); + glBegin(GL_POLYGON); +#if 1 + SquareVertex(0, 0, size); + SquareVertex(1, 0, size); + SquareVertex(1, 1, size); + SquareVertex(0, 1, size); +#else + glTexCoord2f(0, 0); glVertex2f(-size, -size); + glTexCoord2f(1, 0); glVertex2f( size, -size); + glTexCoord2f(1, 1); glVertex2f( size, size); + glTexCoord2f(0, 1); glVertex2f(-size, size); +#endif + glEnd(); +} + + +static void +Cube(GLfloat size) +{ + /* +X */ + glPushMatrix(); + glRotatef(90, 0, 1, 0); + glTranslatef(0, 0, size); + Square(size); + glPopMatrix(); + + /* -X */ + glPushMatrix(); + glRotatef(-90, 0, 1, 0); + glTranslatef(0, 0, size); + Square(size); + glPopMatrix(); + + /* +Y */ + glPushMatrix(); + glRotatef(90, 1, 0, 0); + glTranslatef(0, 0, size); + Square(size); + glPopMatrix(); + + /* -Y */ + glPushMatrix(); + glRotatef(-90, 1, 0, 0); + glTranslatef(0, 0, size); + Square(size); + glPopMatrix(); + + + /* +Z */ + glPushMatrix(); + glTranslatef(0, 0, size); + Square(size); + glPopMatrix(); + + /* -Z */ + glPushMatrix(); + glRotatef(180, 0, 1, 0); + glTranslatef(0, 0, size); + Square(size); + glPopMatrix(); +} + + +static void +Sphere(GLfloat radius, GLint slices, GLint stacks) +{ + static GLUquadricObj *q = NULL; + + if (!q) { + q = gluNewQuadric(); + gluQuadricDrawStyle(q, GLU_FILL); + gluQuadricNormals(q, GLU_SMOOTH); + gluQuadricTexture(q, GL_TRUE); + } + + gluSphere(q, radius, slices, stacks); +} + + +static void +Redisplay(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + 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); + + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); + glRotatef(TexRot, 0.0f, 1.0f, 0.0f); + glMatrixMode(GL_MODELVIEW); + + if (Object == SPHERE) { + Sphere(2.0, 20, 10); + } + else if (Object == CUBE) { + Cube(2.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) +{ + glDeleteShader(fragShader); + glDeleteShader(vertShader); + glDeleteProgram(Program); + glutDestroyWindow(win); +} + + +static void +Key(unsigned char key, int x, int y) +{ + const GLfloat step = 2.0; + (void) x; + (void) y; + + switch(key) { + case 'a': + Anim = !Anim; + if (Anim) + glutIdleFunc(Idle); + else + glutIdleFunc(NULL); + break; + case 'z': + zRot += step; + break; + case 'Z': + zRot -= step; + break; + case 'o': + Object = (Object + 1) % NUM_SHAPES; + break; + case 'r': + RandomUniformValues(); + SetUniformValues(Program, Uniforms); + PrintUniforms(Uniforms); + break; + case 27: + CleanUp(); + exit(0); + break; + } + glutPostRedisplay(); +} + + +static void +SpecialKey(int key, int x, int y) +{ + const GLfloat step = 2.0; + + (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 +InitUniforms(const struct config_file *conf, + struct uniform_info uniforms[]) +{ + int i; + + for (i = 0; i < conf->num_uniforms; i++) { + int j; + for (j = 0; uniforms[j].name; j++) { + if (strcmp(uniforms[j].name, conf->uniforms[i].name) == 0) { + uniforms[j].type = conf->uniforms[i].type; + uniforms[j].value[0] = conf->uniforms[i].value[0]; + uniforms[j].value[1] = conf->uniforms[i].value[1]; + uniforms[j].value[2] = conf->uniforms[i].value[2]; + uniforms[j].value[3] = conf->uniforms[i].value[3]; + } + } + } +} + + +static void +LoadTexture(GLint unit, const char *texFileName) +{ + GLint imgWidth, imgHeight; + GLenum imgFormat; + GLubyte *image = NULL; + GLuint tex; + GLenum filter = GL_LINEAR; + + image = LoadRGBImage(texFileName, &imgWidth, &imgHeight, &imgFormat); + if (!image) { + printf("Couldn't read %s\n", texFileName); + exit(1); + } + + printf("Load Texture: unit %d: %s %d x %d\n", + unit, texFileName, imgWidth, imgHeight); + + glActiveTexture(GL_TEXTURE0 + unit); + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + + gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imgWidth, imgHeight, + imgFormat, GL_UNSIGNED_BYTE, image); + free(image); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); +} + + +static GLenum +TypeFromName(const char *n) +{ + static const struct { + const char *name; + GLenum type; + } types[] = { + { "GL_FLOAT", GL_FLOAT }, + { "GL_FLOAT_VEC2", GL_FLOAT_VEC2 }, + { "GL_FLOAT_VEC3", GL_FLOAT_VEC3 }, + { "GL_FLOAT_VEC4", GL_FLOAT_VEC4 }, + { "GL_INT", GL_INT }, + { "GL_INT_VEC2", GL_INT_VEC2 }, + { "GL_INT_VEC3", GL_INT_VEC3 }, + { "GL_INT_VEC4", GL_INT_VEC4 }, + { "GL_SAMPLER_2D", GL_SAMPLER_2D }, + { NULL, 0 } + }; + GLuint i; + + for (i = 0; types[i].name; i++) { + if (strcmp(types[i].name, n) == 0) + return types[i].type; + } + abort(); + return GL_NONE; +} + + + +/** + * Read a config file. + */ +static void +ReadConfigFile(const char *filename, struct config_file *conf) +{ + char line[1000]; + FILE *f; + + f = fopen(filename, "r"); + if (!f) { + fprintf(stderr, "Unable to open config file %s\n", filename); + exit(1); + } + + conf->num_uniforms = 0; + + /* ugly but functional parser */ + while (!feof(f)) { + fgets(line, sizeof(line), f); + if (!feof(f) && line[0]) { + if (strncmp(line, "vs ", 3) == 0) { + VertShaderFile = strdup(line + 3); + VertShaderFile[strlen(VertShaderFile) - 1] = 0; + } + else if (strncmp(line, "fs ", 3) == 0) { + FragShaderFile = strdup(line + 3); + FragShaderFile[strlen(FragShaderFile) - 1] = 0; + } + else if (strncmp(line, "texture ", 8) == 0) { + char texFileName[100]; + int unit, k; + k = sscanf(line + 8, "%d %s", &unit, texFileName); + assert(k == 2); + LoadTexture(unit, texFileName); + } + else if (strncmp(line, "uniform ", 8) == 0) { + char name[1000], typeName[100]; + int k; + float v1 = 0.0F, v2 = 0.0F, v3 = 0.0F, v4 = 0.0F; + GLenum type; + + k = sscanf(line + 8, "%s %s %f %f %f %f", name, typeName, + &v1, &v2, &v3, &v4); + + type = TypeFromName(typeName); + + strcpy(conf->uniforms[conf->num_uniforms].name, name); + conf->uniforms[conf->num_uniforms].value[0] = v1; + conf->uniforms[conf->num_uniforms].value[1] = v2; + conf->uniforms[conf->num_uniforms].value[2] = v3; + conf->uniforms[conf->num_uniforms].value[3] = v4; + conf->uniforms[conf->num_uniforms].type = type; + conf->num_uniforms++; + } + else { + if (strlen(line) > 1) { + fprintf(stderr, "syntax error in: %s\n", line); + break; + } + } + } + } + + fclose(f); +} + + +static void +Init(void) +{ + struct config_file config; + memset(&config, 0, sizeof(config)); + + if (ConfigFile) + ReadConfigFile(ConfigFile, &config); + + if (!VertShaderFile) { + fprintf(stderr, "Error: no vertex shader\n"); + exit(1); + } + + if (!FragShaderFile) { + fprintf(stderr, "Error: no fragment shader\n"); + exit(1); + } + + if (!ShadersSupported()) + exit(1); + + vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertShaderFile); + fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragShaderFile); + Program = LinkShaders(vertShader, fragShader); + + glUseProgram(Program); + + NumUniforms = GetUniforms(Program, Uniforms); + if (config.num_uniforms) { + InitUniforms(&config, Uniforms); + } + else { + RandomUniformValues(); + } + SetUniformValues(Program, Uniforms); + PrintUniforms(Uniforms); + + NumAttribs = GetAttribs(Program, Attribs); + PrintAttribs(Attribs); + + //assert(glGetError() == 0); + + glClearColor(0.4f, 0.4f, 0.8f, 0.0f); + + glEnable(GL_DEPTH_TEST); + + glColor3f(1, 0, 0); +} + + +static void +Keys(void) +{ + printf("Keyboard:\n"); + printf(" a Animation toggle\n"); + printf(" r Randomize uniform values\n"); + printf(" o Change object\n"); + printf(" arrows Rotate object\n"); + printf(" ESC Exit\n"); +} + + +static void +Usage(void) +{ + printf("Usage:\n"); + printf(" shtest config.shtest\n"); + printf(" Run w/ given config file.\n"); + printf(" shtest --vs vertShader --fs fragShader\n"); + printf(" Load/compile given shaders.\n"); +} + + +static void +ParseOptions(int argc, char *argv[]) +{ + int i; + + if (argc == 1) { + Usage(); + exit(1); + } + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "--fs") == 0) { + FragShaderFile = argv[i+1]; + i++; + } + else if (strcmp(argv[i], "--vs") == 0) { + VertShaderFile = argv[i+1]; + i++; + } + else { + /* assume the arg is a config file */ + ConfigFile = argv[i]; + break; + } + } +} + + +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]); + glewInit(); + glutReshapeFunc(Reshape); + glutKeyboardFunc(Key); + glutSpecialFunc(SpecialKey); + glutDisplayFunc(Redisplay); + ParseOptions(argc, argv); + Init(); + Keys(); + glutMainLoop(); + return 0; +} + diff --git a/progs/glsl/texdemo1.c b/progs/glsl/texdemo1.c index d55f9e7dd9..5b1913a722 100644 --- a/progs/glsl/texdemo1.c +++ b/progs/glsl/texdemo1.c @@ -53,14 +53,14 @@ static int win = 0; static struct uniform_info ReflectUniforms[] = { - { "cubeTex", 1, GL_INT, { 0, 0, 0, 0 }, -1 }, - { "lightPos", 3, GL_FLOAT, { 10, 10, 20, 0 }, -1 }, + { "cubeTex", 1, GL_SAMPLER_CUBE, { 0, 0, 0, 0 }, -1 }, + { "lightPos", 1, GL_FLOAT_VEC3, { 10, 10, 20, 0 }, -1 }, END_OF_UNIFORMS }; static struct uniform_info SimpleUniforms[] = { - { "tex2d", 1, GL_INT, { 1, 0, 0, 0 }, -1 }, - { "lightPos", 3, GL_FLOAT, { 10, 10, 20, 0 }, -1 }, + { "tex2d", 1, GL_SAMPLER_2D, { 1, 0, 0, 0 }, -1 }, + { "lightPos", 1, GL_FLOAT_VEC3, { 10, 10, 20, 0 }, -1 }, END_OF_UNIFORMS }; @@ -382,7 +382,8 @@ CreateProgram(const char *vertProgFile, const char *fragProgFile, glUseProgram(program); - InitUniforms(program, uniforms); + SetUniformValues(program, uniforms); + PrintUniforms(uniforms); return program; } diff --git a/progs/glsl/toyball.c b/progs/glsl/toyball.c index 7fe27aebfe..89733d6175 100644 --- a/progs/glsl/toyball.c +++ b/progs/glsl/toyball.c @@ -24,18 +24,18 @@ static GLuint program; static struct uniform_info Uniforms[] = { - { "LightDir", 4, GL_FLOAT, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 }, - { "HVector", 4, GL_FLOAT, { 0.32506, 0.32506, 0.88808, 0.0 }, -1 }, - { "BallCenter", 4, GL_FLOAT, { 0.0, 0.0, 0.0, 1.0 }, -1 }, - { "SpecularColor", 4, GL_FLOAT, { 0.4, 0.4, 0.4, 60.0 }, -1 }, - { "Red", 4, GL_FLOAT, { 0.6, 0.0, 0.0, 1.0 }, -1 }, - { "Blue", 4, GL_FLOAT, { 0.0, 0.3, 0.6, 1.0 }, -1 }, - { "Yellow", 4, GL_FLOAT, { 0.6, 0.5, 0.0, 1.0 }, -1 }, - { "HalfSpace0", 4, GL_FLOAT, { 1.0, 0.0, 0.0, 0.2 }, -1 }, - { "HalfSpace1", 4, GL_FLOAT, { 0.309016994, 0.951056516, 0.0, 0.2 }, -1 }, - { "HalfSpace2", 4, GL_FLOAT, { -0.809016994, 0.587785252, 0.0, 0.2 }, -1 }, - { "HalfSpace3", 4, GL_FLOAT, { -0.809016994, -0.587785252, 0.0, 0.2 }, -1 }, - { "HalfSpace4", 4, GL_FLOAT, { 0.309116994, -0.951056516, 0.0, 0.2 }, -1 }, + { "LightDir", 1, GL_FLOAT_VEC4, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 }, + { "HVector", 1, GL_FLOAT_VEC4, { 0.32506, 0.32506, 0.88808, 0.0 }, -1 }, + { "BallCenter", 1, GL_FLOAT_VEC4, { 0.0, 0.0, 0.0, 1.0 }, -1 }, + { "SpecularColor", 1, GL_FLOAT_VEC4, { 0.4, 0.4, 0.4, 60.0 }, -1 }, + { "Red", 1, GL_FLOAT_VEC4, { 0.6, 0.0, 0.0, 1.0 }, -1 }, + { "Blue", 1, GL_FLOAT_VEC4, { 0.0, 0.3, 0.6, 1.0 }, -1 }, + { "Yellow", 1, GL_FLOAT_VEC4, { 0.6, 0.5, 0.0, 1.0 }, -1 }, + { "HalfSpace0", 1, GL_FLOAT_VEC4, { 1.0, 0.0, 0.0, 0.2 }, -1 }, + { "HalfSpace1", 1, GL_FLOAT_VEC4, { 0.309016994, 0.951056516, 0.0, 0.2 }, -1 }, + { "HalfSpace2", 1, GL_FLOAT_VEC4, { -0.809016994, 0.587785252, 0.0, 0.2 }, -1 }, + { "HalfSpace3", 1, GL_FLOAT_VEC4, { -0.809016994, -0.587785252, 0.0, 0.2 }, -1 }, + { "HalfSpace4", 1, GL_FLOAT_VEC4, { 0.309116994, -0.951056516, 0.0, 0.2 }, -1 }, { "InOrOutInit", 1, GL_FLOAT, { -3.0, 0, 0, 0 }, -1 }, { "StripeWidth", 1, GL_FLOAT, { 0.3, 0, 0, 0 }, -1 }, { "FWidth", 1, GL_FLOAT, { 0.005, 0, 0, 0 }, -1 }, @@ -173,7 +173,8 @@ Init(void) glUseProgram(program); - InitUniforms(program, Uniforms); + SetUniformValues(program, Uniforms); + PrintUniforms(Uniforms); assert(glGetError() == 0); diff --git a/progs/glsl/toyball.shtest b/progs/glsl/toyball.shtest new file mode 100644 index 0000000000..887663abd3 --- /dev/null +++ b/progs/glsl/toyball.shtest @@ -0,0 +1,17 @@ +vs CH11-toyball.vert +fs CH11-toyball.frag +uniform LightDir GL_FLOAT_VEC4 0.57737 0.57735 0.57735 0.0 +uniform HVector GL_FLOAT_VEC4 0.32506 0.32506 0.88808 0.0 +uniform BallCenter GL_FLOAT_VEC4 0.0 0.0 0.0 1.0 +uniform SpecularColor GL_FLOAT_VEC4 0.4 0.4 0.4 60.0 +uniform Red GL_FLOAT_VEC4 0.6 0.0 0.0 1.0 +uniform Blue GL_FLOAT_VEC4 0.0 0.3 0.6 1.0 +uniform Yellow GL_FLOAT_VEC4 0.6 0.5 0.0 1.0 +uniform HalfSpace0 GL_FLOAT_VEC4 1.0 0.0 0.0 0.2 +uniform HalfSpace1 GL_FLOAT_VEC4 .309016994 0.951056516 0.0 0.2 +uniform HalfSpace2 GL_FLOAT_VEC4 -0.809016994 0.587785252 0.0 0.2 +uniform HalfSpace3 GL_FLOAT_VEC4 -0.809016994 -0.587785252 0.0 0.2 +uniform HalfSpace4 GL_FLOAT_VEC4 .309116994 -0.951056516 0.0 0.2 +uniform InOrOutInit GL_FLOAT -3.0 +uniform StripeWidth GL_FLOAT 0.3 +uniform FWidth GL_FLOAT .005 diff --git a/progs/glsl/vert-tex.c b/progs/glsl/vert-tex.c index e791a5759a..4c8bfa587a 100644 --- a/progs/glsl/vert-tex.c +++ b/progs/glsl/vert-tex.c @@ -43,7 +43,7 @@ static GLfloat xRot = -70.0f, yRot = 0.0f, zRot = 0.0f; /* value[0] = tex unit */ static struct uniform_info Uniforms[] = { - { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 }, + { "tex1", 1, GL_SAMPLER_2D, { 0, 0, 0, 0 }, -1 }, END_OF_UNIFORMS }; |