Replace duplicated code with new shaderutil.c functions
This commit is contained in:
parent
90caba3d76
commit
2dca3373ae
|
@ -45,6 +45,7 @@ default: $(PROGS)
|
||||||
extfuncs.h: $(TOP)/progs/util/extfuncs.h
|
extfuncs.h: $(TOP)/progs/util/extfuncs.h
|
||||||
cp $< .
|
cp $< .
|
||||||
|
|
||||||
|
|
||||||
readtex.c: $(TOP)/progs/util/readtex.c
|
readtex.c: $(TOP)/progs/util/readtex.c
|
||||||
cp $< .
|
cp $< .
|
||||||
|
|
||||||
|
@ -54,24 +55,97 @@ readtex.h: $(TOP)/progs/util/readtex.h
|
||||||
readtex.o: readtex.c readtex.h
|
readtex.o: readtex.c readtex.h
|
||||||
$(CC) -c -I$(INCDIR) $(CFLAGS) readtex.c
|
$(CC) -c -I$(INCDIR) $(CFLAGS) readtex.c
|
||||||
|
|
||||||
bitmap.c: extfuncs.h
|
|
||||||
|
|
||||||
brick.c: extfuncs.h
|
shaderutil.c: $(TOP)/progs/util/shaderutil.c
|
||||||
|
cp $< .
|
||||||
|
|
||||||
bump.c: extfuncs.h
|
shaderutil.h: $(TOP)/progs/util/shaderutil.h
|
||||||
|
cp $< .
|
||||||
|
|
||||||
mandelbrot.c: extfuncs.h
|
shaderutil.o: shaderutil.c shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) shaderutil.c
|
||||||
|
|
||||||
points.c: extfuncs.h
|
|
||||||
|
|
||||||
toyball.c: extfuncs.h
|
|
||||||
|
|
||||||
texdemo1: texdemo1.o readtex.o
|
bitmap.o: bitmap.c extfuncs.h shaderutil.h
|
||||||
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) texdemo1.o readtex.o $(LIBS) -o $@
|
$(CC) -c -I$(INCDIR) $(CFLAGS) bitmap.c
|
||||||
|
|
||||||
texdemo1.o: texdemo1.c readtex.h extfuncs.h
|
bitmap: bitmap.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) bitmap.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
brick.o: brick.c extfuncs.h shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) brick.c
|
||||||
|
|
||||||
|
brick: brick.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) brick.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
bump.o: bump.c extfuncs.h shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) bump.c
|
||||||
|
|
||||||
|
bump: bump.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) bump.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
deriv.o: deriv.c extfuncs.h shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) deriv.c
|
||||||
|
|
||||||
|
deriv: deriv.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) deriv.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
mandelbrot.o: mandelbrot.c extfuncs.h shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) mandelbrot.c
|
||||||
|
|
||||||
|
mandelbrot: mandelbrot.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) mandelbrot.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
noise.o: noise.c extfuncs.h shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) noise.c
|
||||||
|
|
||||||
|
noise: noise.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) noise.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
points.o: points.c extfuncs.h shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) points.c
|
||||||
|
|
||||||
|
points: points.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) points.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
texdemo1.o: texdemo1.c readtex.h extfuncs.h shaderutil.h
|
||||||
$(CC) -c -I$(INCDIR) $(CFLAGS) texdemo1.c
|
$(CC) -c -I$(INCDIR) $(CFLAGS) texdemo1.c
|
||||||
|
|
||||||
|
texdemo1: texdemo1.o readtex.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) texdemo1.o readtex.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
toyball.o: toyball.c extfuncs.h shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) toyball.c
|
||||||
|
|
||||||
|
toyball: toyball.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) toyball.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
twoside.o: twoside.c extfuncs.h shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) twoside.c
|
||||||
|
|
||||||
|
twoside: twoside.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) twoside.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
trirast.o: trirast.c extfuncs.h shaderutil.h
|
||||||
|
$(CC) -c -I$(INCDIR) $(CFLAGS) trirast.c
|
||||||
|
|
||||||
|
trirast: trirast.o shaderutil.o
|
||||||
|
$(CC) -I$(INCDIR) $(CFLAGS) $(LDFLAGS) trirast.o shaderutil.o $(LIBS) -o $@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
-rm -f $(PROGS)
|
-rm -f $(PROGS)
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static GLuint FragShader;
|
static GLuint FragShader;
|
||||||
|
@ -246,40 +247,6 @@ MakeBitmapTextures(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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, "fslight: problem compiling shader:\n%s\n", log);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
|
@ -306,28 +273,16 @@ Init(void)
|
||||||
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
|
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
|
||||||
" gl_FrontColor = gl_Color; \n"
|
" gl_FrontColor = gl_Color; \n"
|
||||||
"}\n";
|
"}\n";
|
||||||
const char *version;
|
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
if (!ShadersSupported())
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
|
||||||
printf("This program requires OpenGL 2.x, found %s\n", version);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
|
||||||
printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
FragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
|
||||||
LoadAndCompileShader(FragShader, fragShaderText);
|
FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
|
||||||
|
Program = LinkShaders(VertShader, FragShader);
|
||||||
|
|
||||||
VertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
|
||||||
LoadAndCompileShader(VertShader, vertShaderText);
|
|
||||||
|
|
||||||
Program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(Program, FragShader);
|
|
||||||
glAttachShader_func(Program, VertShader);
|
|
||||||
glLinkProgram_func(Program);
|
|
||||||
CheckLink(Program);
|
|
||||||
glUseProgram_func(Program);
|
glUseProgram_func(Program);
|
||||||
|
|
||||||
uScale = glGetUniformLocation_func(Program, "scale");
|
uScale = glGetUniformLocation_func(Program, "scale");
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static char *FragProgFile = "CH06-brick.frag.txt";
|
static char *FragProgFile = "CH06-brick.frag.txt";
|
||||||
|
@ -23,23 +24,15 @@ static GLuint fragShader;
|
||||||
static GLuint vertShader;
|
static GLuint vertShader;
|
||||||
static GLuint program;
|
static GLuint program;
|
||||||
|
|
||||||
|
|
||||||
struct uniform_info {
|
|
||||||
const char *name;
|
|
||||||
GLuint size;
|
|
||||||
GLint location;
|
|
||||||
GLfloat value[4];
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct uniform_info Uniforms[] = {
|
static struct uniform_info Uniforms[] = {
|
||||||
/* vert */
|
/* vert */
|
||||||
{ "LightPosition", 3, -1, { 0.1, 0.1, 9.0, 0} },
|
{ "LightPosition", 3, GL_FLOAT, { 0.1, 0.1, 9.0, 0}, -1 },
|
||||||
/* frag */
|
/* frag */
|
||||||
{ "BrickColor", 3, -1, { 0.8, 0.2, 0.2, 0 } },
|
{ "BrickColor", 3, GL_FLOAT, { 0.8, 0.2, 0.2, 0 }, -1 },
|
||||||
{ "MortarColor", 3, -1, { 0.6, 0.6, 0.6, 0 } },
|
{ "MortarColor", 3, GL_FLOAT, { 0.6, 0.6, 0.6, 0 }, -1 },
|
||||||
{ "BrickSize", 2, -1, { 1.0, 0.3, 0, 0 } },
|
{ "BrickSize", 2, GL_FLOAT, { 1.0, 0.3, 0, 0 }, -1 },
|
||||||
{ "BrickPct", 2, -1, { 0.9, 0.8, 0, 0 } },
|
{ "BrickPct", 2, GL_FLOAT, { 0.9, 0.8, 0, 0 }, -1 },
|
||||||
{ NULL, 0, 0, { 0, 0, 0, 0 } }
|
END_OF_UNIFORMS
|
||||||
};
|
};
|
||||||
|
|
||||||
static GLint win = 0;
|
static GLint win = 0;
|
||||||
|
@ -145,122 +138,21 @@ 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
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
const char *version;
|
if (!ShadersSupported())
|
||||||
GLint i;
|
exit(1);
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
|
||||||
printf("Warning: this program expects OpenGL 2.0\n");
|
|
||||||
/*exit(1);*/
|
|
||||||
}
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
vertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
|
||||||
ReadShader(vertShader, VertProgFile);
|
fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
|
||||||
|
program = LinkShaders(vertShader, fragShader);
|
||||||
|
|
||||||
fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
|
||||||
ReadShader(fragShader, FragProgFile);
|
|
||||||
|
|
||||||
program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(program, fragShader);
|
|
||||||
glAttachShader_func(program, vertShader);
|
|
||||||
glLinkProgram_func(program);
|
|
||||||
CheckLink(program);
|
|
||||||
glUseProgram_func(program);
|
glUseProgram_func(program);
|
||||||
|
|
||||||
for (i = 0; Uniforms[i].name; i++) {
|
InitUniforms(program, Uniforms);
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(glGetError() == 0);
|
assert(glGetError() == 0);
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <GL/glu.h>
|
#include <GL/glu.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static char *FragProgFile = "CH11-bumpmap.frag.txt";
|
static char *FragProgFile = "CH11-bumpmap.frag.txt";
|
||||||
|
@ -24,20 +25,13 @@ static GLuint vertShader;
|
||||||
static GLuint program;
|
static GLuint program;
|
||||||
|
|
||||||
|
|
||||||
struct uniform_info {
|
|
||||||
const char *name;
|
|
||||||
GLuint size;
|
|
||||||
GLint location;
|
|
||||||
GLfloat value[4];
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct uniform_info Uniforms[] = {
|
static struct uniform_info Uniforms[] = {
|
||||||
{ "LightPosition", 3, -1, { 0.57737, 0.57735, 0.57735, 0.0 } },
|
{ "LightPosition", 3, GL_FLOAT, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
|
||||||
{ "SurfaceColor", 3, -1, { 0.8, 0.8, 0.2, 0 } },
|
{ "SurfaceColor", 3, GL_FLOAT, { 0.8, 0.8, 0.2, 0 }, -1 },
|
||||||
{ "BumpDensity", 1, -1, { 10.0, 0, 0, 0 } },
|
{ "BumpDensity", 1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 },
|
||||||
{ "BumpSize", 1, -1, { 0.125, 0, 0, 0 } },
|
{ "BumpSize", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 },
|
||||||
{ "SpecularFactor", 1, -1, { 0.5, 0, 0, 0 } },
|
{ "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
|
||||||
{ NULL, 0, 0, { 0, 0, 0, 0 } }
|
END_OF_UNIFORMS
|
||||||
};
|
};
|
||||||
|
|
||||||
static GLint win = 0;
|
static GLint win = 0;
|
||||||
|
@ -232,100 +226,18 @@ 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, "bump: 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, "bump: Unable to open shader file %s\n", filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
n = fread(buffer, 1, max, f);
|
|
||||||
printf("bump: 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
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
const char *version;
|
if (!ShadersSupported())
|
||||||
GLint i;
|
exit(1);
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
|
||||||
printf("Warning: this program expects OpenGL 2.0\n");
|
|
||||||
/*exit(1);*/
|
|
||||||
}
|
|
||||||
printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
vertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
|
||||||
ReadShader(vertShader, VertProgFile);
|
fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
|
||||||
|
program = LinkShaders(vertShader, fragShader);
|
||||||
|
|
||||||
fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
|
||||||
ReadShader(fragShader, FragProgFile);
|
|
||||||
|
|
||||||
program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(program, fragShader);
|
|
||||||
glAttachShader_func(program, vertShader);
|
|
||||||
glLinkProgram_func(program);
|
|
||||||
CheckLink(program);
|
|
||||||
glUseProgram_func(program);
|
glUseProgram_func(program);
|
||||||
|
|
||||||
assert(glIsProgram_func(program));
|
assert(glIsProgram_func(program));
|
||||||
|
@ -336,28 +248,7 @@ Init(void)
|
||||||
|
|
||||||
CheckError(__LINE__);
|
CheckError(__LINE__);
|
||||||
|
|
||||||
for (i = 0; Uniforms[i].name; i++) {
|
InitUniforms(program, Uniforms);
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CheckError(__LINE__);
|
CheckError(__LINE__);
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static char *FragProgFile = NULL;
|
static char *FragProgFile = NULL;
|
||||||
|
@ -159,68 +160,6 @@ MakeRect(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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, "fslight: problem compiling shader:\n%s\n", log);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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, "fslight: Unable to open shader file %s\n", filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
n = fread(buffer, 1, max, f);
|
|
||||||
printf("fslight: 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
|
@ -234,33 +173,16 @@ Init(void)
|
||||||
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
|
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
|
||||||
" gl_TexCoord[0] = gl_MultiTexCoord0;\n"
|
" gl_TexCoord[0] = gl_MultiTexCoord0;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
const char *version;
|
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
if (!ShadersSupported())
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
|
||||||
printf("This program requires OpenGL 2.x, found %s\n", version);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
|
||||||
if (FragProgFile)
|
fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
|
||||||
ReadShader(fragShader, FragProgFile);
|
program = LinkShaders(vertShader, fragShader);
|
||||||
else
|
|
||||||
LoadAndCompileShader(fragShader, fragShaderText);
|
|
||||||
|
|
||||||
vertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
|
||||||
if (VertProgFile)
|
|
||||||
ReadShader(vertShader, VertProgFile);
|
|
||||||
else
|
|
||||||
LoadAndCompileShader(vertShader, vertShaderText);
|
|
||||||
|
|
||||||
program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(program, fragShader);
|
|
||||||
glAttachShader_func(program, vertShader);
|
|
||||||
glLinkProgram_func(program);
|
|
||||||
CheckLink(program);
|
|
||||||
glUseProgram_func(program);
|
glUseProgram_func(program);
|
||||||
|
|
||||||
/*assert(glGetError() == 0);*/
|
/*assert(glGetError() == 0);*/
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static char *FragProgFile = "CH18-mandel.frag.txt";
|
static char *FragProgFile = "CH18-mandel.frag.txt";
|
||||||
|
@ -24,28 +25,21 @@ static GLuint vertShader;
|
||||||
static GLuint program;
|
static GLuint program;
|
||||||
|
|
||||||
|
|
||||||
struct uniform_info {
|
|
||||||
const char *name;
|
|
||||||
GLuint size;
|
|
||||||
GLint location;
|
|
||||||
GLfloat value[4];
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct uniform_info Uniforms[] = {
|
static struct uniform_info Uniforms[] = {
|
||||||
/* vert */
|
/* vert */
|
||||||
{ "LightPosition", 3, -1, { 0.1, 0.1, 9.0, 0} },
|
{ "LightPosition", 3, GL_FLOAT, { 0.1, 0.1, 9.0, 0}, -1 },
|
||||||
{ "SpecularContribution", 1, -1, { 0.5, 0, 0, 0 } },
|
{ "SpecularContribution", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
|
||||||
{ "DiffuseContribution", 1, -1, { 0.5, 0, 0, 0 } },
|
{ "DiffuseContribution", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
|
||||||
{ "Shininess", 1, -1, { 20.0, 0, 0, 0 } },
|
{ "Shininess", 1, GL_FLOAT, { 20.0, 0, 0, 0 }, -1 },
|
||||||
/* frag */
|
/* frag */
|
||||||
{ "MaxIterations", 1, -1, { 12, 0, 0, 0 } },
|
{ "MaxIterations", 1, GL_FLOAT, { 12, 0, 0, 0 }, -1 },
|
||||||
{ "Zoom", 1, -1, { 0.125, 0, 0, 0 } },
|
{ "Zoom", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 },
|
||||||
{ "Xcenter", 1, -1, { -1.5, 0, 0, 0 } },
|
{ "Xcenter", 1, GL_FLOAT, { -1.5, 0, 0, 0 }, -1 },
|
||||||
{ "Ycenter", 1, -1, { .005, 0, 0, 0 } },
|
{ "Ycenter", 1, GL_FLOAT, { .005, 0, 0, 0 }, -1 },
|
||||||
{ "InnerColor", 3, -1, { 1, 0, 0, 0 } },
|
{ "InnerColor", 3, GL_FLOAT, { 1, 0, 0, 0 }, -1 },
|
||||||
{ "OuterColor1", 3, -1, { 0, 1, 0, 0 } },
|
{ "OuterColor1", 3, GL_FLOAT, { 0, 1, 0, 0 }, -1 },
|
||||||
{ "OuterColor2", 3, -1, { 0, 0, 1, 0 } },
|
{ "OuterColor2", 3, GL_FLOAT, { 0, 0, 1, 0 }, -1 },
|
||||||
{ NULL, 0, 0, { 0, 0, 0, 0 } }
|
END_OF_UNIFORMS
|
||||||
};
|
};
|
||||||
|
|
||||||
static GLint win = 0;
|
static GLint win = 0;
|
||||||
|
@ -157,99 +151,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, "mandelbrot: 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, "mandelbrot: Unable to open shader file %s\n", filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
n = fread(buffer, 1, max, f);
|
|
||||||
printf("mandelbrot: 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
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
const char *version;
|
|
||||||
GLint i;
|
GLint i;
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
if (!ShadersSupported())
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
exit(1);
|
||||||
printf("Warning: this program expects OpenGL 2.0\n");
|
|
||||||
/*exit(1);*/
|
|
||||||
}
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
vertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
|
||||||
ReadShader(vertShader, VertProgFile);
|
fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
|
||||||
|
program = LinkShaders(vertShader, fragShader);
|
||||||
|
|
||||||
fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
|
||||||
ReadShader(fragShader, FragProgFile);
|
|
||||||
|
|
||||||
program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(program, fragShader);
|
|
||||||
glAttachShader_func(program, vertShader);
|
|
||||||
glLinkProgram_func(program);
|
|
||||||
CheckLink(program);
|
|
||||||
glUseProgram_func(program);
|
glUseProgram_func(program);
|
||||||
|
|
||||||
for (i = 0; Uniforms[i].name; i++) {
|
for (i = 0; Uniforms[i].name; i++) {
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static const char *VertShaderText =
|
static const char *VertShaderText =
|
||||||
|
@ -34,18 +35,11 @@ static const char *FragShaderText =
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
|
|
||||||
struct uniform_info {
|
|
||||||
const char *name;
|
|
||||||
GLuint size;
|
|
||||||
GLint location;
|
|
||||||
GLfloat value[4];
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct uniform_info Uniforms[] = {
|
static struct uniform_info Uniforms[] = {
|
||||||
{ "Scale", 4, -1, { 0.5, 0.4, 0.0, 0} },
|
{ "Scale", 4, GL_FLOAT, { 0.5, 0.4, 0.0, 0}, -1 },
|
||||||
{ "Bias", 4, -1, { 0.5, 0.3, 0.0, 0} },
|
{ "Bias", 4, GL_FLOAT, { 0.5, 0.3, 0.0, 0}, -1 },
|
||||||
{ "Slice", 1, -1, { 0.5, 0, 0, 0} },
|
{ "Slice", 1, GL_FLOAT, { 0.5, 0, 0, 0}, -1 },
|
||||||
{ NULL, 0, 0, { 0, 0, 0, 0 } }
|
END_OF_UNIFORMS
|
||||||
};
|
};
|
||||||
|
|
||||||
/* program/shader objects */
|
/* program/shader objects */
|
||||||
|
@ -174,95 +168,21 @@ 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, "noise: problem compiling shader: %s\n", log);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printf("Shader compiled OK\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
const char *version;
|
if (!ShadersSupported())
|
||||||
GLint i;
|
exit(1);
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
|
||||||
printf("Warning: this program expects OpenGL 2.0\n");
|
|
||||||
/*exit(1);*/
|
|
||||||
}
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
vertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
|
||||||
LoadAndCompileShader(vertShader, VertShaderText);
|
fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
|
||||||
|
program = LinkShaders(vertShader, fragShader);
|
||||||
|
|
||||||
fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
|
||||||
LoadAndCompileShader(fragShader, FragShaderText);
|
|
||||||
|
|
||||||
program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(program, fragShader);
|
|
||||||
glAttachShader_func(program, vertShader);
|
|
||||||
glLinkProgram_func(program);
|
|
||||||
CheckLink(program);
|
|
||||||
glUseProgram_func(program);
|
glUseProgram_func(program);
|
||||||
|
|
||||||
for (i = 0; Uniforms[i].name; i++) {
|
InitUniforms(program, Uniforms);
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(glGetError() == 0);
|
assert(glGetError() == 0);
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static GLuint FragShader;
|
static GLuint FragShader;
|
||||||
|
@ -181,40 +182,6 @@ 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, "fslight: problem compiling shader:\n%s\n", log);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
|
@ -254,28 +221,16 @@ Init(void)
|
||||||
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
|
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
|
||||||
" gl_FrontColor = gl_Color; \n"
|
" gl_FrontColor = gl_Color; \n"
|
||||||
"}\n";
|
"}\n";
|
||||||
const char *version;
|
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
if (!ShadersSupported())
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
|
||||||
printf("This program requires OpenGL 2.x, found %s\n", version);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
|
||||||
printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
FragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
|
||||||
LoadAndCompileShader(FragShader, fragShaderText);
|
FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
|
||||||
|
Program = LinkShaders(VertShader, FragShader);
|
||||||
|
|
||||||
VertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
|
||||||
LoadAndCompileShader(VertShader, vertShaderText);
|
|
||||||
|
|
||||||
Program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(Program, FragShader);
|
|
||||||
glAttachShader_func(Program, VertShader);
|
|
||||||
glLinkProgram_func(Program);
|
|
||||||
CheckLink(Program);
|
|
||||||
glUseProgram_func(Program);
|
glUseProgram_func(Program);
|
||||||
|
|
||||||
uViewportInv = glGetUniformLocation_func(Program, "viewportInv");
|
uViewportInv = glGetUniformLocation_func(Program, "viewportInv");
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#include "GL/glut.h"
|
#include "GL/glut.h"
|
||||||
#include "readtex.h"
|
#include "readtex.h"
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
static const char *Demo = "texdemo1";
|
static const char *Demo = "texdemo1";
|
||||||
|
|
||||||
|
@ -50,38 +51,19 @@ static GLfloat EyeDist = 10;
|
||||||
static GLboolean Anim = GL_TRUE;
|
static GLboolean Anim = GL_TRUE;
|
||||||
|
|
||||||
|
|
||||||
struct uniform_info {
|
|
||||||
const char *name;
|
|
||||||
GLuint size;
|
|
||||||
GLint location;
|
|
||||||
GLenum type; /**< GL_FLOAT or GL_INT */
|
|
||||||
GLfloat value[4];
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct uniform_info ReflectUniforms[] = {
|
static struct uniform_info ReflectUniforms[] = {
|
||||||
{ "cubeTex", 1, -1, GL_INT, { 0, 0, 0, 0 } },
|
{ "cubeTex", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
|
||||||
{ "lightPos", 3, -1, GL_FLOAT, { 10, 10, 20, 0 } },
|
{ "lightPos", 3, GL_FLOAT, { 10, 10, 20, 0 }, -1 },
|
||||||
{ NULL, 0, 0, 0, { 0, 0, 0, 0 } }
|
END_OF_UNIFORMS
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct uniform_info SimpleUniforms[] = {
|
static struct uniform_info SimpleUniforms[] = {
|
||||||
{ "tex2d", 1, -1, GL_INT, { 1, 0, 0, 0 } },
|
{ "tex2d", 1, GL_INT, { 1, 0, 0, 0 }, -1 },
|
||||||
{ "lightPos", 3, -1, GL_FLOAT, { 10, 10, 20, 0 } },
|
{ "lightPos", 3, GL_FLOAT, { 10, 10, 20, 0 }, -1 },
|
||||||
{ NULL, 0, 0, 0, { 0, 0, 0, 0 } }
|
END_OF_UNIFORMS
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
CheckError(int line)
|
|
||||||
{
|
|
||||||
GLenum err = glGetError();
|
|
||||||
if (err) {
|
|
||||||
printf("GL Error %s (0x%x) at line %d\n",
|
|
||||||
gluErrorString(err), (int) err, line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
DrawGround(GLfloat size)
|
DrawGround(GLfloat size)
|
||||||
{
|
{
|
||||||
|
@ -386,132 +368,19 @@ InitTextures(GLboolean useImageFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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, "%s: problem compiling shader: %s\n", Demo, 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, "%s: Unable to open shader file %s\n", Demo, filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
n = fread(buffer, 1, max, f);
|
|
||||||
printf("%s: read %d bytes from shader file %s\n", Demo, 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 GLuint
|
static GLuint
|
||||||
CreateProgram(const char *vertProgFile, const char *fragProgFile,
|
CreateProgram(const char *vertProgFile, const char *fragProgFile,
|
||||||
struct uniform_info *uniforms)
|
struct uniform_info *uniforms)
|
||||||
{
|
{
|
||||||
GLuint fragShader = 0, vertShader = 0, program = 0;
|
GLuint fragShader, vertShader, program;
|
||||||
GLint i;
|
|
||||||
|
|
||||||
program = glCreateProgram_func();
|
vertShader = CompileShaderFile(GL_VERTEX_SHADER, vertProgFile);
|
||||||
if (vertProgFile) {
|
fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, fragProgFile);
|
||||||
vertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
program = LinkShaders(vertShader, fragShader);
|
||||||
ReadShader(vertShader, vertProgFile);
|
|
||||||
glAttachShader_func(program, vertShader);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fragProgFile) {
|
|
||||||
fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
|
||||||
ReadShader(fragShader, fragProgFile);
|
|
||||||
glAttachShader_func(program, fragShader);
|
|
||||||
}
|
|
||||||
|
|
||||||
glLinkProgram_func(program);
|
|
||||||
CheckLink(program);
|
|
||||||
|
|
||||||
glUseProgram_func(program);
|
glUseProgram_func(program);
|
||||||
|
|
||||||
assert(glIsProgram_func(program));
|
InitUniforms(program, uniforms);
|
||||||
assert(glIsShader_func(fragShader));
|
|
||||||
assert(glIsShader_func(vertShader));
|
|
||||||
|
|
||||||
CheckError(__LINE__);
|
|
||||||
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:
|
|
||||||
if (uniforms[i].type == GL_INT)
|
|
||||||
glUniform1i_func(uniforms[i].location,
|
|
||||||
(GLint) uniforms[i].value[0]);
|
|
||||||
else
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CheckError(__LINE__);
|
|
||||||
|
|
||||||
return program;
|
return program;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static char *FragProgFile = "CH11-toyball.frag.txt";
|
static char *FragProgFile = "CH11-toyball.frag.txt";
|
||||||
|
@ -24,30 +25,23 @@ static GLuint vertShader;
|
||||||
static GLuint program;
|
static GLuint program;
|
||||||
|
|
||||||
|
|
||||||
struct uniform_info {
|
|
||||||
const char *name;
|
|
||||||
GLuint size;
|
|
||||||
GLint location;
|
|
||||||
GLfloat value[4];
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct uniform_info Uniforms[] = {
|
static struct uniform_info Uniforms[] = {
|
||||||
{ "LightDir", 4, -1, { 0.57737, 0.57735, 0.57735, 0.0 } },
|
{ "LightDir", 4, GL_FLOAT, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
|
||||||
{ "HVector", 4, -1, { 0.32506, 0.32506, 0.88808, 0.0 } },
|
{ "HVector", 4, GL_FLOAT, { 0.32506, 0.32506, 0.88808, 0.0 }, -1 },
|
||||||
{ "BallCenter", 4, -1, { 0.0, 0.0, 0.0, 1.0 } },
|
{ "BallCenter", 4, GL_FLOAT, { 0.0, 0.0, 0.0, 1.0 }, -1 },
|
||||||
{ "SpecularColor", 4, -1, { 0.4, 0.4, 0.4, 60.0 } },
|
{ "SpecularColor", 4, GL_FLOAT, { 0.4, 0.4, 0.4, 60.0 }, -1 },
|
||||||
{ "Red", 4, -1, { 0.6, 0.0, 0.0, 1.0 } },
|
{ "Red", 4, GL_FLOAT, { 0.6, 0.0, 0.0, 1.0 }, -1 },
|
||||||
{ "Blue", 4, -1, { 0.0, 0.3, 0.6, 1.0 } },
|
{ "Blue", 4, GL_FLOAT, { 0.0, 0.3, 0.6, 1.0 }, -1 },
|
||||||
{ "Yellow", 4, -1, { 0.6, 0.5, 0.0, 1.0 } },
|
{ "Yellow", 4, GL_FLOAT, { 0.6, 0.5, 0.0, 1.0 }, -1 },
|
||||||
{ "HalfSpace0", 4, -1, { 1.0, 0.0, 0.0, 0.2 } },
|
{ "HalfSpace0", 4, GL_FLOAT, { 1.0, 0.0, 0.0, 0.2 }, -1 },
|
||||||
{ "HalfSpace1", 4, -1, { 0.309016994, 0.951056516, 0.0, 0.2 } },
|
{ "HalfSpace1", 4, GL_FLOAT, { 0.309016994, 0.951056516, 0.0, 0.2 }, -1 },
|
||||||
{ "HalfSpace2", 4, -1, { -0.809016994, 0.587785252, 0.0, 0.2 } },
|
{ "HalfSpace2", 4, GL_FLOAT, { -0.809016994, 0.587785252, 0.0, 0.2 }, -1 },
|
||||||
{ "HalfSpace3", 4, -1, { -0.809016994, -0.587785252, 0.0, 0.2 } },
|
{ "HalfSpace3", 4, GL_FLOAT, { -0.809016994, -0.587785252, 0.0, 0.2 }, -1 },
|
||||||
{ "HalfSpace4", 4, -1, { 0.309116994, -0.951056516, 0.0, 0.2 } },
|
{ "HalfSpace4", 4, GL_FLOAT, { 0.309116994, -0.951056516, 0.0, 0.2 }, -1 },
|
||||||
{ "InOrOutInit", 1, -1, { -3.0, 0, 0, 0 } },
|
{ "InOrOutInit", 1, GL_FLOAT, { -3.0, 0, 0, 0 }, -1 },
|
||||||
{ "StripeWidth", 1, -1, { 0.3, 0, 0, 0 } },
|
{ "StripeWidth", 1, GL_FLOAT, { 0.3, 0, 0, 0 }, -1 },
|
||||||
{ "FWidth", 1, -1, { 0.005, 0, 0, 0 } },
|
{ "FWidth", 1, GL_FLOAT, { 0.005, 0, 0, 0 }, -1 },
|
||||||
{ NULL, 0, 0, { 0, 0, 0, 0 } }
|
END_OF_UNIFORMS
|
||||||
};
|
};
|
||||||
|
|
||||||
static GLint win = 0;
|
static GLint win = 0;
|
||||||
|
@ -171,128 +165,21 @@ 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, "toyball: 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, "toyball: Unable to open shader file %s\n", filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
n = fread(buffer, 1, max, f);
|
|
||||||
printf("toyball: 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
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
const char *version;
|
if (!ShadersSupported())
|
||||||
GLint i;
|
exit(1);
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
|
||||||
printf("Warning: this program expects OpenGL 2.0\n");
|
|
||||||
/*exit(1);*/
|
|
||||||
}
|
|
||||||
printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
vertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
|
||||||
ReadShader(vertShader, VertProgFile);
|
fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
|
||||||
|
program = LinkShaders(vertShader, fragShader);
|
||||||
|
|
||||||
fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
|
||||||
ReadShader(fragShader, FragProgFile);
|
|
||||||
|
|
||||||
program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(program, fragShader);
|
|
||||||
glAttachShader_func(program, vertShader);
|
|
||||||
glLinkProgram_func(program);
|
|
||||||
CheckLink(program);
|
|
||||||
glUseProgram_func(program);
|
glUseProgram_func(program);
|
||||||
|
|
||||||
assert(glIsProgram_func(program));
|
InitUniforms(program, Uniforms);
|
||||||
assert(glIsShader_func(fragShader));
|
|
||||||
assert(glIsShader_func(vertShader));
|
|
||||||
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(glGetError() == 0);
|
assert(glGetError() == 0);
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static GLint WinWidth = 300, WinHeight = 300;
|
static GLint WinWidth = 300, WinHeight = 300;
|
||||||
|
@ -168,67 +169,6 @@ Key(unsigned char 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, "fslight: problem compiling shader:\n%s\n", log);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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, "fslight: Unable to open shader file %s\n", filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
n = fread(buffer, 1, max, f);
|
|
||||||
printf("fslight: 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
|
@ -252,33 +192,16 @@ Init(void)
|
||||||
"void main() {\n"
|
"void main() {\n"
|
||||||
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
|
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
const char *version;
|
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
if (!ShadersSupported())
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
|
||||||
printf("This program requires OpenGL 2.x, found %s\n", version);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
|
||||||
if (FragProgFile)
|
fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
|
||||||
ReadShader(fragShader, FragProgFile);
|
program = LinkShaders(vertShader, fragShader);
|
||||||
else
|
|
||||||
LoadAndCompileShader(fragShader, fragShaderText);
|
|
||||||
|
|
||||||
vertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
|
||||||
if (VertProgFile)
|
|
||||||
ReadShader(vertShader, VertProgFile);
|
|
||||||
else
|
|
||||||
LoadAndCompileShader(vertShader, vertShaderText);
|
|
||||||
|
|
||||||
program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(program, fragShader);
|
|
||||||
glAttachShader_func(program, vertShader);
|
|
||||||
glLinkProgram_func(program);
|
|
||||||
CheckLink(program);
|
|
||||||
glUseProgram_func(program);
|
glUseProgram_func(program);
|
||||||
|
|
||||||
uv0 = glGetUniformLocation_func(program, "v0");
|
uv0 = glGetUniformLocation_func(program, "v0");
|
||||||
|
|
|
@ -16,9 +16,9 @@
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/glext.h>
|
#include <GL/glext.h>
|
||||||
#include "extfuncs.h"
|
#include "extfuncs.h"
|
||||||
|
#include "shaderutil.h"
|
||||||
|
|
||||||
|
|
||||||
static const char *Prog = "twoside";
|
|
||||||
static GLint WinWidth = 300, WinHeight = 300;
|
static GLint WinWidth = 300, WinHeight = 300;
|
||||||
static char *FragProgFile = NULL;
|
static char *FragProgFile = NULL;
|
||||||
static char *VertProgFile = NULL;
|
static char *VertProgFile = NULL;
|
||||||
|
@ -167,67 +167,6 @@ Key(unsigned char 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, "%s: problem compiling shader:\n%s\n", Prog, log);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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, "%s: Unable to open shader file %s\n", Prog, filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
n = fread(buffer, 1, max, f);
|
|
||||||
printf("%s: read %d bytes from shader file %s\n", Prog, 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
Init(void)
|
Init(void)
|
||||||
{
|
{
|
||||||
|
@ -255,33 +194,16 @@ Init(void)
|
||||||
" } \n"
|
" } \n"
|
||||||
" gl_Position = ftransform(); \n"
|
" gl_Position = ftransform(); \n"
|
||||||
"} \n";
|
"} \n";
|
||||||
const char *version;
|
|
||||||
|
|
||||||
version = (const char *) glGetString(GL_VERSION);
|
if (!ShadersSupported())
|
||||||
if (version[0] != '2' || version[1] != '.') {
|
|
||||||
printf("This program requires OpenGL 2.x, found %s\n", version);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
|
||||||
|
|
||||||
GetExtensionFuncs();
|
GetExtensionFuncs();
|
||||||
|
|
||||||
fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
|
vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
|
||||||
if (FragProgFile)
|
fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
|
||||||
ReadShader(fragShader, FragProgFile);
|
program = LinkShaders(vertShader, fragShader);
|
||||||
else
|
|
||||||
LoadAndCompileShader(fragShader, fragShaderText);
|
|
||||||
|
|
||||||
vertShader = glCreateShader_func(GL_VERTEX_SHADER);
|
|
||||||
if (VertProgFile)
|
|
||||||
ReadShader(vertShader, VertProgFile);
|
|
||||||
else
|
|
||||||
LoadAndCompileShader(vertShader, vertShaderText);
|
|
||||||
|
|
||||||
program = glCreateProgram_func();
|
|
||||||
glAttachShader_func(program, fragShader);
|
|
||||||
glAttachShader_func(program, vertShader);
|
|
||||||
glLinkProgram_func(program);
|
|
||||||
CheckLink(program);
|
|
||||||
glUseProgram_func(program);
|
glUseProgram_func(program);
|
||||||
|
|
||||||
u_fragface = glGetUniformLocation_func(program, "fragface");
|
u_fragface = glGetUniformLocation_func(program, "fragface");
|
||||||
|
|
Loading…
Reference in New Issue