diff options
Diffstat (limited to 'progs/glsl')
-rw-r--r-- | progs/glsl/CH11-bumpmaptex.frag | 47 | ||||
-rw-r--r-- | progs/glsl/SConscript | 20 | ||||
-rw-r--r-- | progs/glsl/bump.c | 55 | ||||
-rw-r--r-- | progs/glsl/deriv.c | 15 |
4 files changed, 114 insertions, 23 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/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 50a0900f1c..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 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/deriv.c b/progs/glsl/deriv.c index 30f2b75fef..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(); @@ -163,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 = @@ -181,6 +189,7 @@ Init(void) program = LinkShaders(vertShader, fragShader); glUseProgram(program); + WinSizeUniform = glGetUniformLocation(program, "WinSize"); /*assert(glGetError() == 0);*/ @@ -220,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(); |