PDA

View Full Version : lighting problems



Lurking
12-30-2003, 11:22 PM
using arb_vertex_program and arb_fragment_program im working on getting per-pixel lighting w/ bumpmapping working in my engine. The problem is that im using md2 models to test out my lighting. So im using the knight from nvidia cause it has the normal maps and decal already made for me. So i try to get it working and im running into a problem. it seems that its doing the lighting when its first created but as i rotate the model the lighting doesn't seem to change at all. I think that my tangent and binormals are ok cause i render the diffuse lighting and the bumps are there (http://blar2blar.tripod.com/bumpProb.bmp). Here are my shaders and if you need anything else to help me find out where i missed something or did something wrong just ask.

Vert Shader:
!!ARBvp1.0
ATTRIB iPosition = vertex.position;
ATTRIB iNormal = vertex.normal;
ATTRIB iColor = vertex.color;
ATTRIB iTex0 = vertex.texcoord[0];

ATTRIB tangent = vertex.attrib[8];
ATTRIB binormal = vertex.attrib[9];

PARAM lPos = state.light[0].position;
PARAM lHalf = state.light[0].half;

PARAM mvp[4] = { state.matrix.mvp };
PARAM mvinv[4] = { state.matrix.modelview.invtrans };

PARAM one = {1.0, 1.0, 1.0, 1.0};

OUTPUT oPosition = result.position;
OUTPUT oTex0 = result.texcoord[0];

TEMP light,temp,eyeNormal,tLight,tHalf;

DP4 oPosition.x, mvp[0],iPosition;
DP4 oPosition.y, mvp[1],iPosition;
DP4 oPosition.z, mvp[2],iPosition;
DP4 oPosition.w, mvp[3],iPosition;

DP3 eyeNormal.x, mvinv[0], iNormal;
DP3 eyeNormal.y, mvinv[1], iNormal;
DP3 eyeNormal.z, mvinv[2], iNormal;

SUB light, lPos, iPosition;
DP3 tLight.x, tangent,light;
DP3 tLight.y, binormal,light;
DP3 tLight.z, iNormal,light;
MOV tLight.w, one;

DP3 temp, tLight,tLight;
RSQ temp, temp.x;
MUL tLight,tLight,temp;

DP3 tHalf.x, tangent,lHalf;
DP3 tHalf.y, binormal,lHalf;
DP3 tHalf.z, iNormal,lHalf;
MOV tHalf.w, one;

DP3 temp, tHalf, tHalf;
RSQ temp, temp.x;
MUL tHalf, tHalf, temp;

MOV oTex0, iTex0;
MOV result.texcoord[1],tLight;
MOV result.texcoord[2],tHalf;
MOV result.color,vertex.color;
END

Frag Shader:
!!ARBfp1.0

ATTRIB tLight = fragment.texcoord[1];
ATTRIB tHalf = fragment.texcoord[2];

TEMP bump,decal;

TEX decal, fragment.texcoord[0], texture[0], 2D;
TEX bump, fragment.texcoord[0],texture[1], 2D;

TEMP temp,light,half,dots,final;

# Steps for Bumpmapping
# 1. remove scale/bias from bumpmap
# 2. normalize bumpmap
# 3. do dots/lit

# Step 1
SUB bump, bump, 0.5;
MUL bump, bump, 1.0;
# Step 2
DP3 temp, bump, bump;
RSQ temp, temp.r;
MUL bump, bump, temp;

DP3 temp, tLight,tLight;
RSQ temp, temp.r;
MUL light, tLight,temp;

DP3 temp, tHalf, tHalf;
RSQ temp, temp.r;
MUL half, tHalf, temp;

DP3 dots.r, bump,light;
DP3 dots.g, half, bump;
#LIT dots,dots;

#MUL_SAT final, decal, dots.r;
MOV final, dots.rrrr;
MOV result.color,final;
END

Thanks!

V-man
12-31-2003, 08:47 AM
It looks to me that you are treating the light as both a point light and a directional light.
So the lighting will be wrong.

Also, in the vp, it looks like you are considering the light to be in the objects space. So if you rotate the object, the light rotates with it.

If it's in eye space, first you transform by the inverse of the modelview, then the orthonormal basis matrix to bring it into tangent space.

JanHH
12-31-2003, 08:49 AM
thanks for the code example, I am just learning vp/fp right now.

I only could imagine this
http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/011214.html

to be your problem (as the writer before me in this thread). You would need a modelview matrix that is not affected by camera positioning, but at the stage you use it, it is already. so what you need is transform the light vector to object space and use that as parameter (at least, this is my guess http://www.opengl.org/discussion_boards/ubb/wink.gif ).

Jan

Lurking
12-31-2003, 11:15 AM
i guess another question i have is that is that when you use per-vertex normals and get their average for each vertex, do you do that for tangent and binormal as well?

Lurking
12-31-2003, 12:30 PM
Nevermind i got it working now thanks alot for helping!