From ae99e4c67ebc3adb0b71e427723f34085801c3ac Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 13 Aug 2009 12:52:13 -0600 Subject: progs/glsl: new shtest program, a simple shader test harness app This commit includes some sample config files (*.shtest) --- progs/glsl/shtest.c | 562 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 562 insertions(+) create mode 100644 progs/glsl/shtest.c (limited to 'progs/glsl/shtest.c') diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c new file mode 100644 index 0000000000..7eb7202274 --- /dev/null +++ b/progs/glsl/shtest.c @@ -0,0 +1,562 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include "shaderutil.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; + glTexCoord2f(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 0 + 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]; + } + } + } +} + + +/** + * 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 (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, "uniform ", 8) == 0) { + char name[1000]; + int k; + float v1, v2, v3, v4; + GLenum type; + + k = sscanf(line + 8, "%s %f %f %f %f", name, &v1, &v2, &v3, &v4); + + switch (k) { + case 1: + type = GL_NONE; + abort(); + break; + case 2: + type = GL_FLOAT; + break; + case 3: + type = GL_FLOAT_VEC2; + break; + case 4: + type = GL_FLOAT_VEC3; + break; + case 5: + type = GL_FLOAT_VEC4; + break; + } + + 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; +} + -- cgit v1.2.3 From 62d113216090cd093c7cc6373c9115e31f921e7c Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 13 Aug 2009 15:53:49 -0600 Subject: progs/glsl: add type field to shtest config files Plus, texture loading. --- progs/glsl/brick.shtest | 10 ++--- progs/glsl/mandelbrot.shtest | 23 +++++----- progs/glsl/shtest.c | 99 ++++++++++++++++++++++++++++++++++---------- progs/glsl/toyball.shtest | 30 +++++++------- 4 files changed, 108 insertions(+), 54 deletions(-) (limited to 'progs/glsl/shtest.c') diff --git a/progs/glsl/brick.shtest b/progs/glsl/brick.shtest index c806a0fc54..8a2152692e 100644 --- a/progs/glsl/brick.shtest +++ b/progs/glsl/brick.shtest @@ -1,8 +1,8 @@ vs CH06-brick.vert fs CH06-brick.frag -uniform LightPosition 0.1 0.1 9.0 -uniform BrickColor 0.8 0.2 0.2 -uniform MortarColor 0.6 0.6 0.6 -uniform BrickSize 1.0 0.3 -uniform BrickPct 0.9 0.8 +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/mandelbrot.shtest b/progs/glsl/mandelbrot.shtest index f5cca2295f..4f4e5c747e 100644 --- a/progs/glsl/mandelbrot.shtest +++ b/progs/glsl/mandelbrot.shtest @@ -1,14 +1,13 @@ vs CH18-mandel.vert fs CH18-mandel.frag -uniform LightPosition 0.1 0.1 9.0 -uniform SpecularContribution 0.5 -uniform DiffuseContribution 0.5 -uniform Shininess 20.0 -uniform Iterations 12 -uniform Zoom 0.125 -uniform Xcenter -1.5 -uniform Ycenter .005 -uniform InnerColor 1 0 0 -uniform OuterColor1 0 1 0 -uniform OuterColor2 0 0 1 - +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/shtest.c b/progs/glsl/shtest.c index 7eb7202274..09b2593841 100644 --- a/progs/glsl/shtest.c +++ b/progs/glsl/shtest.c @@ -35,6 +35,7 @@ #include #include #include "shaderutil.h" +#include "readtex.h" typedef enum @@ -361,6 +362,69 @@ InitUniforms(const struct config_file *conf, } +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. */ @@ -381,7 +445,7 @@ ReadConfigFile(const char *filename, struct config_file *conf) /* ugly but functional parser */ while (!feof(f)) { fgets(line, sizeof(line), f); - if (line[0]) { + if (!feof(f) && line[0]) { if (strncmp(line, "vs ", 3) == 0) { VertShaderFile = strdup(line + 3); VertShaderFile[strlen(VertShaderFile) - 1] = 0; @@ -390,32 +454,23 @@ ReadConfigFile(const char *filename, struct config_file *conf) 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]; + char name[1000], typeName[100]; int k; - float v1, v2, v3, v4; + float v1 = 0.0F, v2 = 0.0F, v3 = 0.0F, v4 = 0.0F; GLenum type; - k = sscanf(line + 8, "%s %f %f %f %f", name, &v1, &v2, &v3, &v4); + k = sscanf(line + 8, "%s %s %f %f %f %f", name, typeName, + &v1, &v2, &v3, &v4); - switch (k) { - case 1: - type = GL_NONE; - abort(); - break; - case 2: - type = GL_FLOAT; - break; - case 3: - type = GL_FLOAT_VEC2; - break; - case 4: - type = GL_FLOAT_VEC3; - break; - case 5: - type = GL_FLOAT_VEC4; - break; - } + type = TypeFromName(typeName); strcpy(conf->uniforms[conf->num_uniforms].name, name); conf->uniforms[conf->num_uniforms].value[0] = v1; diff --git a/progs/glsl/toyball.shtest b/progs/glsl/toyball.shtest index 2852ab043e..887663abd3 100644 --- a/progs/glsl/toyball.shtest +++ b/progs/glsl/toyball.shtest @@ -1,17 +1,17 @@ vs CH11-toyball.vert fs CH11-toyball.frag -uniform LightDir 0.57737 0.57735 0.57735 0.0 -uniform HVector 0.32506 0.32506 0.88808 0.0 -uniform BallCenter 0.0 0.0 0.0 1.0 -uniform SpecularColor 0.4 0.4 0.4 60.0 -uniform Red 0.6 0.0 0.0 1.0 -uniform Blue 0.0 0.3 0.6 1.0 -uniform Yellow 0.6 0.5 0.0 1.0 -uniform HalfSpace0 1.0 0.0 0.0 0.2 -uniform HalfSpace1 .309016994 0.951056516 0.0 0.2 -uniform HalfSpace2 -0.809016994 0.587785252 0.0 0.2 -uniform HalfSpace3 -0.809016994 -0.587785252 0.0 0.2 -uniform HalfSpace4 .309116994 -0.951056516 0.0 0.2 -uniform InOrOutInit -3.0 -uniform StripeWidth 0.3 -uniform FWidth .005 +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 -- cgit v1.2.3 From 08ecd863ee12601ea95818e02889a9807fd7a62d Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 13 Aug 2009 16:02:24 -0600 Subject: progs/glsl: set generic vertex attribute values --- progs/glsl/shtest.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'progs/glsl/shtest.c') diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c index 09b2593841..97f6f9f8a4 100644 --- a/progs/glsl/shtest.c +++ b/progs/glsl/shtest.c @@ -133,7 +133,20 @@ SquareVertex(GLfloat s, GLfloat t, GLfloat size) { GLfloat x = -size + s * 2.0 * size; GLfloat y = -size + t * 2.0 * size; - glTexCoord2f(s, t); + 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); } @@ -148,7 +161,7 @@ Square(GLfloat size) glNormal3f(0, 0, 1); glVertexAttrib3f(tangentAttrib, 1, 0, 0); glBegin(GL_POLYGON); -#if 0 +#if 1 SquareVertex(0, 0, size); SquareVertex(1, 0, size); SquareVertex(1, 1, size); -- cgit v1.2.3 From 680df529a323714013006aae9b3ad5298913a7b3 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Mon, 17 Aug 2009 12:57:37 -0600 Subject: demos/glsl: remove glutInitWindowPosition() calls --- progs/glsl/brick.c | 1 - progs/glsl/bump.c | 1 - progs/glsl/convolutions.c | 1 - progs/glsl/deriv.c | 1 - progs/glsl/fragcoord.c | 1 - progs/glsl/identity.c | 1 - progs/glsl/mandelbrot.c | 1 - progs/glsl/multinoise.c | 1 - progs/glsl/noise.c | 1 - progs/glsl/noise2.c | 1 - progs/glsl/pointcoord.c | 1 - progs/glsl/shadow_sampler.c | 1 - progs/glsl/shtest.c | 1 - progs/glsl/texaaline.c | 1 - progs/glsl/toyball.c | 1 - progs/glsl/trirast.c | 1 - progs/glsl/twoside.c | 1 - progs/glsl/vert-or-frag-only.c | 1 - 18 files changed, 18 deletions(-) (limited to 'progs/glsl/shtest.c') diff --git a/progs/glsl/brick.c b/progs/glsl/brick.c index 0653c592e5..20417aa462 100644 --- a/progs/glsl/brick.c +++ b/progs/glsl/brick.c @@ -184,7 +184,6 @@ 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]); diff --git a/progs/glsl/bump.c b/progs/glsl/bump.c index c0d39c049d..87669aec73 100644 --- a/progs/glsl/bump.c +++ b/progs/glsl/bump.c @@ -281,7 +281,6 @@ 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]); diff --git a/progs/glsl/convolutions.c b/progs/glsl/convolutions.c index 22ce7edcdc..c2fb76e1aa 100644 --- a/progs/glsl/convolutions.c +++ b/progs/glsl/convolutions.c @@ -448,7 +448,6 @@ int main(int argc, char **argv) { glutInit(&argc, argv); - glutInitWindowPosition(0, 0); glutInitWindowSize(400, 400); glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE); diff --git a/progs/glsl/deriv.c b/progs/glsl/deriv.c index 9cf1e40e3e..265a515715 100644 --- a/progs/glsl/deriv.c +++ b/progs/glsl/deriv.c @@ -220,7 +220,6 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); glutInitWindowSize(200, 200); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); win = glutCreateWindow(argv[0]); diff --git a/progs/glsl/fragcoord.c b/progs/glsl/fragcoord.c index 9f56a038c9..3dfcec87a5 100644 --- a/progs/glsl/fragcoord.c +++ b/progs/glsl/fragcoord.c @@ -166,7 +166,6 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); glutInitWindowSize(WinWidth, WinHeight); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); win = glutCreateWindow(argv[0]); diff --git a/progs/glsl/identity.c b/progs/glsl/identity.c index a772ccd716..526e9b82c1 100644 --- a/progs/glsl/identity.c +++ b/progs/glsl/identity.c @@ -187,7 +187,6 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); glutInitWindowSize(200, 200); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); win = glutCreateWindow(argv[0]); diff --git a/progs/glsl/mandelbrot.c b/progs/glsl/mandelbrot.c index 729a6f125a..b05ef37fae 100644 --- a/progs/glsl/mandelbrot.c +++ b/progs/glsl/mandelbrot.c @@ -199,7 +199,6 @@ 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]); diff --git a/progs/glsl/multinoise.c b/progs/glsl/multinoise.c index 0afe230801..0d4026e29c 100644 --- a/progs/glsl/multinoise.c +++ b/progs/glsl/multinoise.c @@ -262,7 +262,6 @@ 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]); diff --git a/progs/glsl/noise.c b/progs/glsl/noise.c index 8c36e1c59b..fdab263ea6 100644 --- a/progs/glsl/noise.c +++ b/progs/glsl/noise.c @@ -200,7 +200,6 @@ 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]); diff --git a/progs/glsl/noise2.c b/progs/glsl/noise2.c index e972b62673..7a28f09947 100644 --- a/progs/glsl/noise2.c +++ b/progs/glsl/noise2.c @@ -186,7 +186,6 @@ static void Init (void) int main (int argc, char *argv[]) { glutInit (&argc, argv); - glutInitWindowPosition ( 0, 0); glutInitWindowSize (200, 200); glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow (argv[0]); diff --git a/progs/glsl/pointcoord.c b/progs/glsl/pointcoord.c index 27b73a05de..5dced6fac3 100644 --- a/progs/glsl/pointcoord.c +++ b/progs/glsl/pointcoord.c @@ -187,7 +187,6 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); glutInitWindowSize(WinWidth, WinHeight); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); win = glutCreateWindow(argv[0]); diff --git a/progs/glsl/shadow_sampler.c b/progs/glsl/shadow_sampler.c index 0a4d04dd8c..0adc9d88ba 100644 --- a/progs/glsl/shadow_sampler.c +++ b/progs/glsl/shadow_sampler.c @@ -321,7 +321,6 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); glutInitWindowSize(400, 300); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); win = glutCreateWindow(argv[0]); diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c index 97f6f9f8a4..2622af1313 100644 --- a/progs/glsl/shtest.c +++ b/progs/glsl/shtest.c @@ -612,7 +612,6 @@ 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]); diff --git a/progs/glsl/texaaline.c b/progs/glsl/texaaline.c index 1f566c86a6..2481c0f36e 100644 --- a/progs/glsl/texaaline.c +++ b/progs/glsl/texaaline.c @@ -351,7 +351,6 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); glutInitWindowSize(WinWidth, WinHeight); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); win = glutCreateWindow(argv[0]); diff --git a/progs/glsl/toyball.c b/progs/glsl/toyball.c index 89733d6175..c502f24077 100644 --- a/progs/glsl/toyball.c +++ b/progs/glsl/toyball.c @@ -205,7 +205,6 @@ 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]); diff --git a/progs/glsl/trirast.c b/progs/glsl/trirast.c index f7546f25a2..53bd91ef97 100644 --- a/progs/glsl/trirast.c +++ b/progs/glsl/trirast.c @@ -239,7 +239,6 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); glutInitWindowSize(WinWidth, WinHeight); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); win = glutCreateWindow(argv[0]); diff --git a/progs/glsl/twoside.c b/progs/glsl/twoside.c index b6c1b477dd..a57484f96c 100644 --- a/progs/glsl/twoside.c +++ b/progs/glsl/twoside.c @@ -285,7 +285,6 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); glutInitWindowSize(WinWidth, WinHeight); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); win = glutCreateWindow(argv[0]); diff --git a/progs/glsl/vert-or-frag-only.c b/progs/glsl/vert-or-frag-only.c index 81fcab8c5b..148991ca83 100644 --- a/progs/glsl/vert-or-frag-only.c +++ b/progs/glsl/vert-or-frag-only.c @@ -173,7 +173,6 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition( 0, 0); glutInitWindowSize(400, 200); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); Win = glutCreateWindow(argv[0]); -- cgit v1.2.3 From 94d14f6cd223a2c90f2a177f5b1dfc55fb8d659a Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 20 Aug 2009 10:44:32 -0600 Subject: progs/glsl: more comments in shtest.c --- progs/glsl/shtest.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'progs/glsl/shtest.c') diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c index 2622af1313..54b12cc50b 100644 --- a/progs/glsl/shtest.c +++ b/progs/glsl/shtest.c @@ -21,6 +21,8 @@ * fs shader.frag * uniform pi 3.14159 * uniform v1 1.0 0.5 0.2 0.3 + * texture 0 texture0.rgb + * texture 1 texture1.rgb * */ -- cgit v1.2.3 From 174054c973eca95b9640c44f08da3da6e74de68e Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 20 Aug 2009 10:58:05 -0600 Subject: progs/glsl: tweaks to shtest.c 1. Larger sphere to match cube size 2. Allow -geometry option to override window size 3. Cube samplers --- progs/glsl/shtest.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'progs/glsl/shtest.c') diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c index 54b12cc50b..6b560bb2cd 100644 --- a/progs/glsl/shtest.c +++ b/progs/glsl/shtest.c @@ -257,7 +257,7 @@ Redisplay(void) glMatrixMode(GL_MODELVIEW); if (Object == SPHERE) { - Sphere(2.0, 20, 10); + Sphere(2.5, 20, 10); } else if (Object == CUBE) { Cube(2.0); @@ -426,6 +426,7 @@ TypeFromName(const char *n) { "GL_INT_VEC3", GL_INT_VEC3 }, { "GL_INT_VEC4", GL_INT_VEC4 }, { "GL_SAMPLER_2D", GL_SAMPLER_2D }, + { "GL_SAMPLER_CUBE", GL_SAMPLER_CUBE }, { NULL, 0 } }; GLuint i; @@ -613,8 +614,8 @@ ParseOptions(int argc, char *argv[]) int main(int argc, char *argv[]) { - glutInit(&argc, argv); glutInitWindowSize(400, 400); + glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); win = glutCreateWindow(argv[0]); glewInit(); -- cgit v1.2.3 From 0062bd68b3279936fa67e2429cc94a845fe8c27c Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 20 Aug 2009 14:19:01 -0600 Subject: progs/glsl: update shtest.c to handle 1D/3D/CUBE/RECT textures --- progs/glsl/shtest.c | 86 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 16 deletions(-) (limited to 'progs/glsl/shtest.c') diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c index 6b560bb2cd..76671726b9 100644 --- a/progs/glsl/shtest.c +++ b/progs/glsl/shtest.c @@ -21,8 +21,9 @@ * fs shader.frag * uniform pi 3.14159 * uniform v1 1.0 0.5 0.2 0.3 - * texture 0 texture0.rgb - * texture 1 texture1.rgb + * texture 0 2D texture0.rgb + * texture 1 CUBE texture1.rgb + * texture 2 RECT texture2.rgb * */ @@ -378,13 +379,14 @@ InitUniforms(const struct config_file *conf, static void -LoadTexture(GLint unit, const char *texFileName) +LoadTexture(GLint unit, GLenum target, const char *texFileName) { GLint imgWidth, imgHeight; GLenum imgFormat; GLubyte *image = NULL; GLuint tex; GLenum filter = GL_LINEAR; + GLenum objTarget; image = LoadRGBImage(texFileName, &imgWidth, &imgHeight, &imgFormat); if (!image) { @@ -392,21 +394,41 @@ LoadTexture(GLint unit, const char *texFileName) exit(1); } - printf("Load Texture: unit %d: %s %d x %d\n", - unit, texFileName, imgWidth, imgHeight); + printf("Load Texture: unit %d, target 0x%x: %s %d x %d\n", + unit, target, texFileName, imgWidth, imgHeight); + + if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && + target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) { + objTarget = GL_TEXTURE_CUBE_MAP; + } + else { + objTarget = target; + } glActiveTexture(GL_TEXTURE0 + unit); glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_2D, tex); + glBindTexture(objTarget, tex); + + if (target == GL_TEXTURE_3D) { + /* depth=1 */ + gluBuild3DMipmaps(target, 4, imgWidth, imgHeight, 1, + imgFormat, GL_UNSIGNED_BYTE, image); + } + else if (target == GL_TEXTURE_1D) { + gluBuild1DMipmaps(target, 4, imgWidth, + imgFormat, GL_UNSIGNED_BYTE, image); + } + else { + gluBuild2DMipmaps(target, 4, imgWidth, imgHeight, + imgFormat, GL_UNSIGNED_BYTE, image); + } - 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); + glTexParameteri(objTarget, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(objTarget, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(objTarget, GL_TEXTURE_MIN_FILTER, filter); + glTexParameteri(objTarget, GL_TEXTURE_MAG_FILTER, filter); } @@ -425,8 +447,11 @@ TypeFromName(const char *n) { "GL_INT_VEC2", GL_INT_VEC2 }, { "GL_INT_VEC3", GL_INT_VEC3 }, { "GL_INT_VEC4", GL_INT_VEC4 }, + { "GL_SAMPLER_1D", GL_SAMPLER_1D }, { "GL_SAMPLER_2D", GL_SAMPLER_2D }, + { "GL_SAMPLER_3D", GL_SAMPLER_3D }, { "GL_SAMPLER_CUBE", GL_SAMPLER_CUBE }, + { "GL_SAMPLER_2D_RECT", GL_SAMPLER_2D_RECT_ARB }, { NULL, 0 } }; GLuint i; @@ -471,11 +496,40 @@ ReadConfigFile(const char *filename, struct config_file *conf) FragShaderFile[strlen(FragShaderFile) - 1] = 0; } else if (strncmp(line, "texture ", 8) == 0) { - char texFileName[100]; + char target[100], texFileName[100]; int unit, k; - k = sscanf(line + 8, "%d %s", &unit, texFileName); - assert(k == 2); - LoadTexture(unit, texFileName); + k = sscanf(line + 8, "%d %s %s", &unit, target, texFileName); + assert(k == 3 || k == 8); + if (strcmp(target, "CUBE") == 0) { + char texFileNames[6][100]; + k = sscanf(line + 8, "%d %s %s %s %s %s %s %s", + &unit, target, + texFileNames[0], + texFileNames[1], + texFileNames[2], + texFileNames[3], + texFileNames[4], + texFileNames[5]); + LoadTexture(unit, GL_TEXTURE_CUBE_MAP_POSITIVE_X, texFileNames[0]); + LoadTexture(unit, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, texFileNames[1]); + LoadTexture(unit, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, texFileNames[2]); + LoadTexture(unit, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, texFileNames[3]); + LoadTexture(unit, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, texFileNames[4]); + LoadTexture(unit, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, texFileNames[5]); + } + else if (!strcmp(target, "2D")) { + LoadTexture(unit, GL_TEXTURE_2D, texFileName); + } + else if (!strcmp(target, "3D")) { + LoadTexture(unit, GL_TEXTURE_3D, texFileName); + } + else if (!strcmp(target, "RECT")) { + LoadTexture(unit, GL_TEXTURE_RECTANGLE_ARB, texFileName); + } + else { + printf("Bad texture target: %s\n", target); + exit(1); + } } else if (strncmp(line, "uniform ", 8) == 0) { char name[1000], typeName[100]; -- cgit v1.2.3 From a215da5e9c752e58d8cdd7e05c0f374dae5e72c0 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 20 Aug 2009 14:30:08 -0600 Subject: progs/glsl: report compile/link times in shtest.c --- progs/glsl/shtest.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'progs/glsl/shtest.c') diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c index 76671726b9..fa477d9eeb 100644 --- a/progs/glsl/shtest.c +++ b/progs/glsl/shtest.c @@ -566,7 +566,9 @@ ReadConfigFile(const char *filename, struct config_file *conf) static void Init(void) { + GLdouble vertTime, fragTime, linkTime; struct config_file config; + memset(&config, 0, sizeof(config)); if (ConfigFile) @@ -586,8 +588,16 @@ Init(void) exit(1); vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertShaderFile); + vertTime = GetShaderCompileTime(); fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragShaderFile); + fragTime = GetShaderCompileTime(); + Program = LinkShaders(vertShader, fragShader); + linkTime = GetShaderLinkTime(); + + printf("Time to compile vertex shader: %fs\n", vertTime); + printf("Time to compile fragment shader: %fs\n", fragTime); + printf("Time to link shaders: %fs\n", linkTime); glUseProgram(Program); -- cgit v1.2.3 From 72052210516b4cb0e082e0c56822cd33b1562630 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Wed, 26 Aug 2009 12:16:07 -0600 Subject: progs/glsl: asst. changes in shtest.c --- progs/glsl/shtest.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'progs/glsl/shtest.c') diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c index fa477d9eeb..628a7dd5b9 100644 --- a/progs/glsl/shtest.c +++ b/progs/glsl/shtest.c @@ -107,10 +107,18 @@ RandomUniformValues(void) { GLuint i; for (i = 0; i < NumUniforms; i++) { - if (Uniforms[i].type == GL_FLOAT) { + switch (Uniforms[i].type) { + case GL_FLOAT: Uniforms[i].value[0] = RandomFloat(0.0, 1.0); - } - else { + break; + case GL_SAMPLER_1D: + case GL_SAMPLER_2D: + case GL_SAMPLER_3D: + case GL_SAMPLER_CUBE: + case GL_SAMPLER_2D_RECT_ARB: + /* don't change sampler values - random values are bad */ + break; + default: 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); @@ -595,10 +603,15 @@ Init(void) Program = LinkShaders(vertShader, fragShader); linkTime = GetShaderLinkTime(); + printf("Read vert shader %s\n", VertShaderFile); + printf("Read frag shader %s\n", FragShaderFile); + printf("Time to compile vertex shader: %fs\n", vertTime); printf("Time to compile fragment shader: %fs\n", fragTime); printf("Time to link shaders: %fs\n", linkTime); + assert(ValidateShaderProgram(Program)); + glUseProgram(Program); NumUniforms = GetUniforms(Program, Uniforms); -- cgit v1.2.3