Sign in to follow this  
Schmidget

Simple per-pixel lighting shader issues.

Recommended Posts

Schmidget    100
Hey everyone.

As a disclaimer, I don't know what I'm doing - mostly, anyway. I'm new to shaders and 3d graphics in general. I've "played" with shaders before by taking example code from Rendermonkey and messing about with it to see what kind of cool things you can do. Then I wrote some very simple stuff using the proper math, but never expanded on it.

Well, now I'm working on a project where I have to render terrain. Actually coming up with some terrain is pretty easy. I'm just going to use perlin noise as a heightmap to come up with the geometry. But then when it came to texturing, I figured, why not use shaders? So I loaded up some sample code in Rendermonkey that essentially starts me off with basic light+diffmap+normalmap. No attenuation or anything. It was a good start.

I added some code to blend two different terrain textures (both w/ diff and norm) based on the normal of the model. Parts with a normal that faces mostly up (y) will have one kind of texture, and parts that face more to the side (x/z) will have another kind of texture. Next, since this terrain is actually underwater, I added a really awful fake water caustics effect.

This all looked great in Rendermonkey's preview window. Then I transferred the code into my project and it "almost" worked. I bound the shader, set the appropriate variables, and rendered a glut teapot. The teapot itself was mostly transparent, though the caustics effect was visible (although not on the y facing normals like I expected). Moving the camera closer to the teapot revealed that, upon entering the inside, things were -not- transparent and there was some sort of multitexturing going on, but like the caustics, they didn't really work the way I wanted.

The fact that the shader is almost working is killing me. I'm pretty sure it has to do with the fact that the way the shader is calculating normals with respect to the view of the camera is messing things up. I wish I new more of what I was doing, but I'm at a loss. Does anyone see something that really jumps out at them with this shader code?

Vertex:

uniform vec3 fvLightPosition;

varying vec2 Texcoord;
varying vec3 LightDirection;
varying vec3 objNormal;

attribute vec3 rm_Binormal;
attribute vec3 rm_Tangent;

void main( void )
{
gl_Position = ftransform();
Texcoord = gl_MultiTexCoord0.xy;

vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;

vec3 fvLightDirection = fvLightPosition - fvObjectPosition.xyz;

objNormal=gl_Normal;

vec3 fvNormal = gl_NormalMatrix * gl_Normal;
vec3 fvBinormal = gl_NormalMatrix * rm_Binormal;
vec3 fvTangent = gl_NormalMatrix * rm_Tangent;

LightDirection.x = dot( fvTangent, fvLightDirection.xyz );
LightDirection.y = dot( fvBinormal, fvLightDirection.xyz );
LightDirection.z = dot( fvNormal, fvLightDirection.xyz );
}



Frag:

uniform vec4 fvAmbient;
uniform vec4 fvDiffuse;

uniform sampler2D baseMap;
uniform sampler2D bumpMap;
uniform sampler2D baseMap2;
uniform sampler2D bumpMap2;

varying vec2 Texcoord;
varying vec3 LightDirection;
varying vec3 objNormal;

void main( void )
{
vec4 mixedbump=mix(texture2D( bumpMap, Texcoord ),texture2D(bumpMap2,Texcoord),abs(objNormal.x+objNormal.z));

vec3 fvLightDirection = normalize( LightDirection );
vec3 fvNormal = normalize( ( mixedbump.xyz * 2.0 ) - 1.0 );
float fNDotL = dot( fvNormal, fvLightDirection );

vec4 mixedbase=mix(texture2D( baseMap, Texcoord ),texture2D( baseMap2, Texcoord ),abs(objNormal.x+objNormal.z));

vec4 fvBaseColor = mixedbase;

vec4 fvTotalAmbient = fvAmbient * fvBaseColor;
vec4 fvTotalDiffuse = fvDiffuse * fNDotL * fvBaseColor;

gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse);
}



I know that code is a mess (probably along with the rest of this post), I haven't been able to clean it up yet.
Any suggestions are welcome!

Thanks.

Edit:
Some pics:
[img]http://imgur.com/bHQKH.jpg[/img]
Strange transparency...

[img]http://imgur.com/4raEr.jpg[/img]
On the inside, it's almost correct.

[Edited by - Schmidget on October 17, 2010 11:01:11 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this