summaryrefslogtreecommitdiffstats
path: root/progs/glsl/brick.c
diff options
context:
space:
mode:
authorBrian <[email protected]>2008-04-09 22:28:23 -0600
committerBrian <[email protected]>2008-04-09 22:30:42 -0600
commit2dca3373ae6207d2deb375148f4dce1817cfd5b7 (patch)
treecac9680283f396afaf16712d9355d40f0dd51e75 /progs/glsl/brick.c
parent90caba3d766406db32e51387f830ce55836f0709 (diff)
Replace duplicated code with new shaderutil.c functions
Diffstat (limited to 'progs/glsl/brick.c')
-rw-r--r--progs/glsl/brick.c134
1 files changed, 13 insertions, 121 deletions
diff --git a/progs/glsl/brick.c b/progs/glsl/brick.c
index 522698b5d43..4be266622b5 100644
--- a/progs/glsl/brick.c
+++ b/progs/glsl/brick.c
@@ -13,6 +13,7 @@
#include <GL/glut.h>
#include <GL/glext.h>
#include "extfuncs.h"
+#include "shaderutil.h"
static char *FragProgFile = "CH06-brick.frag.txt";
@@ -23,23 +24,15 @@ static GLuint fragShader;
static GLuint vertShader;
static GLuint program;
-
-struct uniform_info {
- const char *name;
- GLuint size;
- GLint location;
- GLfloat value[4];
-};
-
static struct uniform_info Uniforms[] = {
/* vert */
- { "LightPosition", 3, -1, { 0.1, 0.1, 9.0, 0} },
+ { "LightPosition", 3, GL_FLOAT, { 0.1, 0.1, 9.0, 0}, -1 },
/* frag */
- { "BrickColor", 3, -1, { 0.8, 0.2, 0.2, 0 } },
- { "MortarColor", 3, -1, { 0.6, 0.6, 0.6, 0 } },
- { "BrickSize", 2, -1, { 1.0, 0.3, 0, 0 } },
- { "BrickPct", 2, -1, { 0.9, 0.8, 0, 0 } },
- { NULL, 0, 0, { 0, 0, 0, 0 } }
+ { "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 },
+ END_OF_UNIFORMS
};
static GLint win = 0;
@@ -146,121 +139,20 @@ SpecialKey(int key, int x, int y)
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, "brick: problem compiling shader: %s\n", log);
- exit(1);
- }
- else {
- printf("Shader compiled OK\n");
- }
-}
-
-
-/**
- * Read a shader from a file.
- */
-static void
-ReadShader(GLuint shader, const char *filename)
-{
- const int max = 100*1000;
- int n;
- char *buffer = (char*) malloc(max);
- FILE *f = fopen(filename, "r");
- if (!f) {
- fprintf(stderr, "brick: Unable to open shader file %s\n", filename);
- exit(1);
- }
-
- n = fread(buffer, 1, max, f);
- printf("brick: read %d bytes from shader file %s\n", n, filename);
- if (n > 0) {
- buffer[n] = 0;
- LoadAndCompileShader(shader, buffer);
- }
-
- fclose(f);
- free(buffer);
-}
-
-
-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);*/
- }
+ if (!ShadersSupported())
+ exit(1);
GetExtensionFuncs();
- vertShader = glCreateShader_func(GL_VERTEX_SHADER);
- ReadShader(vertShader, VertProgFile);
-
- fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
- ReadShader(fragShader, FragProgFile);
+ vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
+ fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
+ program = LinkShaders(vertShader, fragShader);
- program = glCreateProgram_func();
- glAttachShader_func(program, fragShader);
- glAttachShader_func(program, vertShader);
- glLinkProgram_func(program);
- CheckLink(program);
glUseProgram_func(program);
- for (i = 0; Uniforms[i].name; i++) {
- Uniforms[i].location
- = glGetUniformLocation_func(program, Uniforms[i].name);
- printf("Uniform %s location: %d\n", Uniforms[i].name,
- Uniforms[i].location);
- switch (Uniforms[i].size) {
- case 1:
- glUniform1fv_func(Uniforms[i].location, 1, Uniforms[i].value);
- break;
- case 2:
- glUniform2fv_func(Uniforms[i].location, 1, Uniforms[i].value);
- break;
- case 3:
- glUniform3fv_func(Uniforms[i].location, 1, Uniforms[i].value);
- break;
- case 4:
- glUniform4fv_func(Uniforms[i].location, 1, Uniforms[i].value);
- break;
- default:
- abort();
- }
- }
+ InitUniforms(program, Uniforms);
assert(glGetError() == 0);