summaryrefslogtreecommitdiff
path: root/progs/glsl
diff options
context:
space:
mode:
Diffstat (limited to 'progs/glsl')
-rw-r--r--progs/glsl/CH11-bumpmaptex.frag47
-rw-r--r--progs/glsl/CH18-mandel.frag2
-rw-r--r--progs/glsl/SConscript20
-rw-r--r--progs/glsl/bump.c57
-rw-r--r--progs/glsl/convolutions.c2
-rw-r--r--progs/glsl/deriv.c16
-rw-r--r--progs/glsl/multinoise.c11
-rw-r--r--progs/glsl/multitex.c7
-rw-r--r--progs/glsl/noise.c3
-rw-r--r--progs/glsl/shtest.c7
-rw-r--r--progs/glsl/texaaline.c4
-rw-r--r--progs/glsl/texdemo1.c7
-rw-r--r--progs/glsl/trirast.c6
-rw-r--r--progs/glsl/vert-tex.c9
14 files changed, 138 insertions, 60 deletions
diff --git a/progs/glsl/CH11-bumpmaptex.frag b/progs/glsl/CH11-bumpmaptex.frag
new file mode 100644
index 0000000000..b1f93b784d
--- /dev/null
+++ b/progs/glsl/CH11-bumpmaptex.frag
@@ -0,0 +1,47 @@
+//
+// Fragment shader for procedural bumps
+//
+// Authors: John Kessenich, Randi Rost
+//
+// Copyright (c) 2002-2006 3Dlabs Inc. Ltd.
+//
+// See 3Dlabs-License.txt for license information
+//
+// Texture mapping/modulation added by Brian Paul
+//
+
+varying vec3 LightDir;
+varying vec3 EyeDir;
+
+uniform float BumpDensity; // = 16.0
+uniform float BumpSize; // = 0.15
+uniform float SpecularFactor; // = 0.5
+
+sampler2D Tex;
+
+void main()
+{
+ vec3 ambient = vec3(0.25);
+ vec3 litColor;
+ vec2 c = BumpDensity * gl_TexCoord[0].st;
+ vec2 p = fract(c) - vec2(0.5);
+
+ float d, f;
+ d = p.x * p.x + p.y * p.y;
+ f = inversesqrt(d + 1.0);
+
+ if (d >= BumpSize)
+ { p = vec2(0.0); f = 1.0; }
+
+ vec3 SurfaceColor = texture2D(Tex, gl_TexCoord[0].st).xyz;
+
+ vec3 normDelta = vec3(p.x, p.y, 1.0) * f;
+ litColor = SurfaceColor * (ambient + max(dot(normDelta, LightDir), 0.0));
+ vec3 reflectDir = reflect(LightDir, normDelta);
+
+ float spec = max(dot(EyeDir, reflectDir), 0.0);
+ spec *= SpecularFactor;
+ litColor = min(litColor + spec, vec3(1.0));
+
+ gl_FragColor = vec4(litColor, 1.0);
+}
diff --git a/progs/glsl/CH18-mandel.frag b/progs/glsl/CH18-mandel.frag
index a472d81252..a972d68bcf 100644
--- a/progs/glsl/CH18-mandel.frag
+++ b/progs/glsl/CH18-mandel.frag
@@ -31,7 +31,7 @@ void main()
float iter;
// for (iter = 0.0; iter < MaxIterations && r2 < 4.0; ++iter)
- for (iter = 0.0; iter < 12 && r2 < 4.0; ++iter)
+ for (iter = 0.0; iter < 12.0 && r2 < 4.0; ++iter)
{
float tempreal = real;
diff --git a/progs/glsl/SConscript b/progs/glsl/SConscript
index 7a4549cd70..8f2ebcf69c 100644
--- a/progs/glsl/SConscript
+++ b/progs/glsl/SConscript
@@ -1,23 +1,5 @@
Import('*')
-if not env['GLUT']:
- Return()
-
-env = env.Clone()
-
-env.Prepend(CPPPATH = [
- '../util',
-])
-
-env.Prepend(LIBS = [
- util,
- '$GLUT_LIB'
-])
-
-if env['platform'] == 'windows':
- env.Append(CPPDEFINES = ['NOMINMAX'])
- env.Prepend(LIBS = ['winmm'])
-
progs = [
'array',
'bitmap',
@@ -48,7 +30,7 @@ progs = [
]
for prog in progs:
- env.Program(
+ progs_env.Program(
target = prog,
source = prog + '.c',
)
diff --git a/progs/glsl/bump.c b/progs/glsl/bump.c
index 87669aec73..e31afab939 100644
--- a/progs/glsl/bump.c
+++ b/progs/glsl/bump.c
@@ -12,15 +12,20 @@
#include <GL/glew.h>
#include <GL/glut.h>
#include "shaderutil.h"
+#include "readtex.h"
static char *FragProgFile = "CH11-bumpmap.frag";
+static char *FragTexProgFile = "CH11-bumpmaptex.frag";
static char *VertProgFile = "CH11-bumpmap.vert";
+static char *TextureFile = "../images/tile.rgb";
/* program/shader objects */
static GLuint fragShader;
+static GLuint fragTexShader;
static GLuint vertShader;
static GLuint program;
+static GLuint texProgram;
static struct uniform_info Uniforms[] = {
@@ -32,13 +37,26 @@ static struct uniform_info Uniforms[] = {
END_OF_UNIFORMS
};
+static struct uniform_info TexUniforms[] = {
+ { "LightPosition", 1, GL_FLOAT_VEC3, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
+ { "Tex", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
+ { "BumpDensity", 1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 },
+ { "BumpSize", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 },
+ { "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
+ END_OF_UNIFORMS
+};
+
static GLint win = 0;
static GLfloat xRot = 20.0f, yRot = 0.0f, zRot = 0.0f;
-static GLuint tangentAttrib;
+static GLint tangentAttrib;
+static GLint tangentAttribTex;
+
+static GLuint Texture;
static GLboolean Anim = GL_FALSE;
+static GLboolean Textured = GL_FALSE;
static void
@@ -135,6 +153,11 @@ Redisplay(void)
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glRotatef(zRot, 0.0f, 0.0f, 1.0f);
+ if (Textured)
+ glUseProgram(texProgram);
+ else
+ glUseProgram(program);
+
Cube(1.5);
glPopMatrix();
@@ -163,8 +186,10 @@ static void
CleanUp(void)
{
glDeleteShader(fragShader);
+ glDeleteShader(fragTexShader);
glDeleteShader(vertShader);
glDeleteProgram(program);
+ glDeleteProgram(texProgram);
glutDestroyWindow(win);
}
@@ -181,6 +206,9 @@ Key(unsigned char key, int x, int y)
Anim = !Anim;
glutIdleFunc(Anim ? Idle : NULL);
break;
+ case 't':
+ Textured = !Textured;
+ break;
case 'z':
zRot += step;
break;
@@ -254,6 +282,26 @@ Init(void)
CheckError(__LINE__);
+
+ /*
+ * As above, but fragment shader also uses a texture map.
+ */
+ fragTexShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragTexProgFile);
+ texProgram = LinkShaders(vertShader, fragTexShader);
+ glUseProgram(texProgram);
+ assert(glIsProgram(texProgram));
+ assert(glIsShader(fragTexShader));
+ SetUniformValues(texProgram, TexUniforms);
+ PrintUniforms(TexUniforms);
+
+ /*
+ * Load tex image.
+ */
+ glGenTextures(1, &Texture);
+ glBindTexture(GL_TEXTURE_2D, Texture);
+ LoadRGBMipmaps(TextureFile, GL_RGB);
+
+
glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
glEnable(GL_DEPTH_TEST);
@@ -268,10 +316,13 @@ ParseOptions(int argc, char *argv[])
int i;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-fs") == 0) {
- FragProgFile = argv[i+1];
+ FragProgFile = argv[++i];
}
else if (strcmp(argv[i], "-vs") == 0) {
- VertProgFile = argv[i+1];
+ VertProgFile = argv[++i];
+ }
+ else if (strcmp(argv[i], "-t") == 0) {
+ TextureFile = argv[++i];
}
}
}
diff --git a/progs/glsl/convolutions.c b/progs/glsl/convolutions.c
index c2fb76e1aa..350e61bbdc 100644
--- a/progs/glsl/convolutions.c
+++ b/progs/glsl/convolutions.c
@@ -369,7 +369,7 @@ static void keyPress(unsigned char key, int x, int y)
case 27:
exit(0);
default:
- return;
+ break;
}
glutPostRedisplay();
}
diff --git a/progs/glsl/deriv.c b/progs/glsl/deriv.c
index 265a515715..588246b71a 100644
--- a/progs/glsl/deriv.c
+++ b/progs/glsl/deriv.c
@@ -27,11 +27,15 @@ static GLuint SphereList, RectList, CurList;
static GLint win = 0;
static GLboolean anim = GL_TRUE;
static GLfloat xRot = 0.0f, yRot = 0.0f;
+static GLint WinSize[2];
+static GLint WinSizeUniform = -1;
static void
Redisplay(void)
{
+ glUniform2iv(WinSizeUniform, 1, WinSize);
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
@@ -55,6 +59,8 @@ Idle(void)
static void
Reshape(int width, int height)
{
+ WinSize[0] = width;
+ WinSize[1] = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
@@ -140,6 +146,7 @@ MakeSphere(void)
glNewList(SphereList, GL_COMPILE);
gluSphere(obj, 2.0f, 30, 15);
glEndList();
+ gluDeleteQuadric(obj);
}
@@ -162,8 +169,10 @@ static void
Init(void)
{
static const char *fragShaderText =
+ "uniform ivec2 WinSize; \n"
"void main() {\n"
- " gl_FragColor = abs(dFdy(gl_TexCoord[0])) * 50.0;\n"
+ " vec2 d = dFdy(gl_TexCoord[0].xy) * vec2(WinSize); \n"
+ " gl_FragColor = vec4(d.x, d.y, 0.0, 1.0);\n"
" // gl_FragColor = gl_TexCoord[0];\n"
"}\n";
static const char *vertShaderText =
@@ -180,6 +189,7 @@ Init(void)
program = LinkShaders(vertShader, fragShader);
glUseProgram(program);
+ WinSizeUniform = glGetUniformLocation(program, "WinSize");
/*assert(glGetError() == 0);*/
@@ -219,8 +229,10 @@ ParseOptions(int argc, char *argv[])
int
main(int argc, char *argv[])
{
+ WinSize[0] = WinSize[1] = 200;
+
glutInit(&argc, argv);
- glutInitWindowSize(200, 200);
+ glutInitWindowSize(WinSize[0], WinSize[1]);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
win = glutCreateWindow(argv[0]);
glewInit();
diff --git a/progs/glsl/multinoise.c b/progs/glsl/multinoise.c
index 0d4026e29c..d504ba1cc4 100644
--- a/progs/glsl/multinoise.c
+++ b/progs/glsl/multinoise.c
@@ -22,22 +22,22 @@ static const char *FragShaderText[ 4 ] = {
"void main()\n"
"{\n"
" gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].w ) * 0.5 + 0.5;\n"
- " gl_FragColor.a = 1;\n"
+ " gl_FragColor.a = 1.0;\n"
"}\n",
"void main()\n"
"{\n"
" gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xw ) * 0.5 + 0.5;\n"
- " gl_FragColor.a = 1;\n"
+ " gl_FragColor.a = 1.0;\n"
"}\n",
"void main()\n"
"{\n"
" gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyw ) * 0.5 + 0.5;\n"
- " gl_FragColor.a = 1;\n"
+ " gl_FragColor.a = 1.0;\n"
"}\n",
"void main()\n"
"{\n"
" gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyzw ) * 0.5 + 0.5;\n"
- " gl_FragColor.a = 1;\n"
+ " gl_FragColor.a = 1.0;\n"
"}\n"
};
@@ -125,6 +125,7 @@ Key(unsigned char key, int x, int y)
case 'a':
Anim = !Anim;
glutIdleFunc(Anim ? Idle : NULL);
+ break;
case 's':
Slice -= step;
break;
@@ -193,7 +194,7 @@ LoadAndCompileShader(GLuint shader, const char *text)
GLchar log[1000];
GLsizei len;
glGetShaderInfoLog(shader, 1000, &len, log);
- fprintf(stderr, "noise: problem compiling shader: %s\n", log);
+ fprintf(stderr, "multinoise: problem compiling shader: %s\n", log);
exit(1);
}
else {
diff --git a/progs/glsl/multitex.c b/progs/glsl/multitex.c
index bf46fd5210..49b32253ee 100644
--- a/progs/glsl/multitex.c
+++ b/progs/glsl/multitex.c
@@ -369,12 +369,9 @@ InitPrograms(void)
static void
InitGL(void)
{
- const char *version = (const char *) glGetString(GL_VERSION);
+ if (!ShadersSupported())
+ exit(1);
- 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));
printf("Usage:\n");
printf(" a - toggle arrays vs. immediate mode rendering\n");
diff --git a/progs/glsl/noise.c b/progs/glsl/noise.c
index fdab263ea6..1148580ff4 100644
--- a/progs/glsl/noise.c
+++ b/progs/glsl/noise.c
@@ -28,7 +28,7 @@ static const char *FragShaderText =
" vec4 p;\n"
" p.xy = gl_TexCoord[0].xy;\n"
" p.z = Slice;\n"
- " p.w = 0;\n"
+ " p.w = 0.0;\n"
" vec4 n = noise4(p * scale);\n"
" gl_FragColor = n * Scale + Bias;\n"
"}\n";
@@ -119,6 +119,7 @@ Key(unsigned char key, int x, int y)
case 'a':
Anim = !Anim;
glutIdleFunc(Anim ? Idle : NULL);
+ break;
case 's':
Slice -= step;
break;
diff --git a/progs/glsl/shtest.c b/progs/glsl/shtest.c
index 628a7dd5b9..e9800c307f 100644
--- a/progs/glsl/shtest.c
+++ b/progs/glsl/shtest.c
@@ -492,9 +492,8 @@ ReadConfigFile(const char *filename, struct config_file *conf)
conf->num_uniforms = 0;
/* ugly but functional parser */
- while (!feof(f)) {
- fgets(line, sizeof(line), f);
- if (!feof(f) && line[0]) {
+ while (fgets(line, sizeof(line), f) != NULL) {
+ if (line[0]) {
if (strncmp(line, "vs ", 3) == 0) {
VertShaderFile = strdup(line + 3);
VertShaderFile[strlen(VertShaderFile) - 1] = 0;
@@ -627,7 +626,7 @@ Init(void)
NumAttribs = GetAttribs(Program, Attribs);
PrintAttribs(Attribs);
- //assert(glGetError() == 0);
+ /* assert(glGetError() == 0); */
glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
diff --git a/progs/glsl/texaaline.c b/progs/glsl/texaaline.c
index 2e4f932ad6..00edab7310 100644
--- a/progs/glsl/texaaline.c
+++ b/progs/glsl/texaaline.c
@@ -310,8 +310,8 @@ MakeMipmap(void)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4);
- ////glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 5);
+ /* glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4); */
+ /* glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 5); */
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#undef SZ
diff --git a/progs/glsl/texdemo1.c b/progs/glsl/texdemo1.c
index 5b1913a722..2076e6aef9 100644
--- a/progs/glsl/texdemo1.c
+++ b/progs/glsl/texdemo1.c
@@ -400,11 +400,8 @@ InitPrograms(void)
static void
Init(GLboolean useImageFiles)
{
- const char *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);
}
printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
diff --git a/progs/glsl/trirast.c b/progs/glsl/trirast.c
index 53bd91ef97..857342636d 100644
--- a/progs/glsl/trirast.c
+++ b/progs/glsl/trirast.c
@@ -179,9 +179,9 @@ Init(void)
"\n"
"void main() {\n"
" vec2 p = gl_FragCoord.xy; \n"
- " if (crs(v1 - v0, p - v0) >= 0 && \n"
- " crs(v2 - v1, p - v1) >= 0 && \n"
- " crs(v0 - v2, p - v2) >= 0) \n"
+ " if (crs(v1 - v0, p - v0) >= 0.0 && \n"
+ " crs(v2 - v1, p - v1) >= 0.0 && \n"
+ " crs(v0 - v2, p - v2) >= 0.0) \n"
" gl_FragColor = vec4(1.0); \n"
" else \n"
" gl_FragColor = vec4(0.5); \n"
diff --git a/progs/glsl/vert-tex.c b/progs/glsl/vert-tex.c
index 4c8bfa587a..2b93c78888 100644
--- a/progs/glsl/vert-tex.c
+++ b/progs/glsl/vert-tex.c
@@ -40,15 +40,6 @@ static GLboolean Anim = GL_TRUE;
static GLboolean WireFrame = GL_TRUE;
static GLfloat xRot = -70.0f, yRot = 0.0f, zRot = 0.0f;
-
-/* value[0] = tex unit */
-static struct uniform_info Uniforms[] = {
- { "tex1", 1, GL_SAMPLER_2D, { 0, 0, 0, 0 }, -1 },
- END_OF_UNIFORMS
-};
-
-
-
static void
Idle(void)
{