Jump to content
  • Advertisement
Sign in to follow this  
Hellhound

OpenGL Problems with shadows using cg dot3 shader

This topic is 4967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, since several days i try to develop an cg fragment and vertex shader for dot3 based bumpmapping. The shaders looks like working, i got my bumpmapped object, but i've problems with the light and shadowing ... Because an Image tells more than thousand words, here is the one: http://img.photobucket.com/albums/v211/Hellhound_01/shadow.jpg What you see is a simple object, the object looks like a dot3 bumpmapped object, but the shadows are too sharpen at the edges and when i move the light arround the object often the shadows gets translucent. The other problem is, that the shadows often switch to fast and sometimes the light is suddently on the wrong side... I think there could be a problem in my calculations, perhaps someone of you could help me ... Here is my vertex shader:
// input structure
struct appl2vert
{
	float3 Position			: POSITION;
	float3 Normal			: NORMAL;
	float2 TextureCoords	         : TEXCOORD0;
	float3 Tangents     	         : TEXCOORD1;
};

// output structure
struct vert2frag
{
	float4 Position				: POSITION;
	float2 TextureCoords		         :TEXCOORD0;
	float3 TangentSpaceLightPos 	         :TEXCOORD1;
};


void main(const appl2vert IN, out vert2frag OUT,
		  const uniform float4x4 ModelViewProj,
		  const uniform float4x4 InvModelViewMatrix,
		  const uniform float4   LightPos,
		  )
{
	// Rotate vertices to clip-space
	OUT.Position = mul(ModelViewProj, float4(IN.Position, 1) );

	// get Light form object space to  view-space
	float4	lightPosOS = mul(InvModelViewMatrix, LightPos);
	float3  surface2light = normalize(lightPosOS.xyz - IN.Position.xyz);

	// calculate the TangentSpaceMatrix (TBN)
	float3 binormal = cross( IN.Normal, IN.Dot3Texture );
	float3x3 tbn_matrix = float3x3( IN.Dot3Texture, binormal, IN.Normal );

	// get light direction L 
	OUT.TangentSpaceLightPos = mul(tbn_matrix, surface2light.xyz);
	
	// transfer coords
	OUT.TextureCoords = IN.TextureCoords;
}
And here my fragment shader:
// input structure
struct vert2frag
{
    float2 TextureCoords		:TEXCOORD0;
    float3 TangentSpaceLightPos	:TEXCOORD1;
};

void main(const vert2frag IN, out float3 oColor:COLOR,
		  float4 AmbientLight,
		  float4 LMs : TEXCOORD4,
		  const uniform sampler2D NormalMap,
		  const uniform sampler2D DecalMap )
{
	// normalize Light
	float3 normTangentSpaceLightPos = normalize(IN.TangentSpaceLightPos);

	// Do Dot3 calculation (N.L)  (multiplication for more reflection)
	float3 nmap= 2*(tex2D(NormalMap,IN.TextureCoords)-0.5).rgb;
        
    //adding decal color (multiplication for better color results)
	oColor = dot(nmap.xyz, normTangentSpaceLightPos)*tex2D(DecalMap,IN.TextureCoords);
}
And here is my openGL code fragment:
// Bind the programs
cgGLBindProgram(m_pCgDot3VertexShader);
cgGLBindProgram(m_pCgDot3FragmentShader);
	
// Update matrix
cgGLSetStateMatrixParameter(m_cgpMdlViewProjMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
	cgGLSetStateMatrixParameter(m_cgpInvModelViewMatrix, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE);
	
// Update Light Pos
cgGLSetParameter3f(m_cgpLightPos, m_pLightSource->m_pvLightPosition[0],m_pLightSource->m_pvLightPosition[1] , m_pLightSource->m_pvLightPosition[2]);
	
// Enable the profiles
cgGLEnableProfile(m_pCgVertexProfile);
cgGLEnableProfile(m_pCgFragmentProfile);

// Set Decal Texture (Param1 TEXCOORDS0 as TextureCoords)
texIter = pDmgLvTexturesToMap->find(CTextureSetElement::TYPE_DECAL);

//Aktivate array for VBOs
glClientActiveTextureARB(GL_TEXTURE0_ARB);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, getGPUTextureOffsetID());
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL );
glEnable(GL_TEXTURE_2D);

//Aktivate and bind decaltexture
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, getTextureOffsetID());

// bind texture to fragment-shader
cgGLSetTextureParameter(m_cgpBaseTexture, getTextureOffsetID());
cgGLEnableTextureParameter(m_cgpBaseTexture);

// bind tangents to vertex-shader (Param2 TEXTURECOORDS1)
for (int i=0; i<m_pMeshesToRender->getVertexCount(); i++) {
		glMultiTexCoord3fARB(GL_TEXTURE1_ARB, m_pMeshesToRender->m_pvTangents.m_fX, m_pMeshesToRender->m_pvTangents.m_fY, m_pMeshesToRender->m_pvTangents.m_fZ);
	}

	
// bind normal texture to fragment-shader
cgGLSetTextureParameter(m_cgpNormalMapTexture, getNormalTextureOffsetID());
cgGLEnableTextureParameter(m_cgpNormalMapTexture);
Thanks for any help, Christian

Share this post


Link to post
Share on other sites
Advertisement
I've tested the shaders again and again and figured out, that only two states of light are computed. When i place the position of the light on the right side of the object i got the correct lighting of the object from the right side.

When i now move the light position near to the object, normaly the reflection must be more intensive but there is no approximation computed. On the other hand, when i move the light more far a way the object must be lighted darker, but nothing happends ...

When i move the light position through the object on the other side, the lighting switches hardly at the "zero" point to the other side.

I've no idea what could be wrong. The Light position looks like correct computed in the shader. Has anyone an idea what could be wrong?

Share this post


Link to post
Share on other sites
I can see 2 things wrong in the vertex shader:

1)The light vector:

float4 lightPosOS = mul(InvModelViewMatrix, LightPos);
float3 surface2light = normalize(lightPosOS.xyz - IN.Position.xyz);

LightPosOS is in eye space, but IN.Position is in object space. Multiply In.Position with the ModelView matrix, so it will be too in eye space.

2)The TBN matrix:

Again, the tangent and normal vectors are in object space. You must multiply them with InvModelViewMatrix so they are in eyespace, since the lightvector is also in eyespace before you multiply it with the TBN matrix. The binormal will automatically be in eyespace, since it will be computed based on T,N vectors.

Generally remember that for vector calculations to be correct, all vectors must be in the same space.

Share this post


Link to post
Share on other sites
Thanks Mikeman for your fast reply. I've followed your instructions and changed the vertex-shader code, but this was not the failure. Nothing has changed. The problem with the lighting is still the same...

Here are my changes in changed shader code:


// Rotate vertex, tangents and normals to eye-space
float4 vertPosES = mul(ModelViewMatrix, float4(IN.Position, 1));
float3 normalsES = mul(InvModelViewMatrix, float4(IN.Normal, 1));
float3 tangentsES = mul(InvModelViewMatrix, float4(IN.Tangents,1));

// Calculate Lightdirection L
float3 surface2light = normalize(lightPosES.xyz - vertPosES.xyz);

// Calculate binormals and TangentSpaceMatrix (TBN)
float3 binormal = cross( normalsES, tangentsES);
float3x3 tbn_matrix = float3x3( tangentsES, binormal, normalsES);

// Calculate tangent-space lightposition
OUT.TangentSpaceLightPos = mul(tbn_matrix, surface2light.xyz);


I've corrected another failure with no effect: The parameter values of the light source in the vertex-shader are float4, but i've hooked only an float3 Light positon. Now i send from the OpenGL applicatioin 4 float values.

[Edited by - Hellhound on January 8, 2005 6:26:54 AM]

Share this post


Link to post
Share on other sites
Quote:

float3 normalsES = mul(InvModelViewMatrix, float4(IN.Normal, 1));
float3 tangentsES = mul(InvModelViewMatrix, float4(IN.Tangents,1));


Normal and Tangent are directions, not points. The fact that you do float4(IN.Normal, 1) sets the w=1, which is for points. That means they get affected
by translations, which they shouldn't. The correct would be:

float3 normalsES = mul(InvModelViewMatrix, float4(IN.Normal, 0));
float3 tangentsES = mul(InvModelViewMatrix, float4(IN.Tangents,0));

Share this post


Link to post
Share on other sites
Ups, copy and paste failure ... I've corrected it, but nothing happends, it's the same problem. Could it be possible that my tangents are wrong? Here is the
code of my tangents calculation:


Object::calcTangentForTris(unsigned int index0, unsigned int index1, unsigned int index2)
{
float fu21 = m_pvTextureCoords[index1].m_fX - m_pvTextureCoords[index0].m_fX;
float fv21 = m_pvTextureCoords[index1].m_fY - m_pvTextureCoords[index0].m_fY;

float fu31 = m_pvTextureCoords[index2].m_fX - m_pvTextureCoords[index0].m_fX;
float fv31 = m_pvTextureCoords[index2].m_fY - m_pvTextureCoords[index0].m_fY;

CVector3f vA(m_pvVerticesCoords[index1].m_fX - m_pvVerticesCoords[index0].m_fX, fu21, fv21);
CVector3f vB(m_pvVerticesCoords[index2].m_fX - m_pvVerticesCoords[index0].m_fX, fu31, fv31);
CVector3f vC = CVector3f::getCrossProduct(vA, vB);

CVector3f vTangent = CVector3f();

if(vC.m_fX > 0.00001f)
vTangent.m_fX = -vC.m_fY / vC.m_fX;

vA.setVectorCoords(m_pvVerticesCoords[index1].m_fY - m_pvVerticesCoords[index0].m_fY, fu21, fv21);
vB.setVectorCoords(m_pvVerticesCoords[index2].m_fY - m_pvVerticesCoords[index0].m_fY, fu31, fv31);
vC = CVector3f::getCrossProduct(vA, vB);

if(vC.m_fX > 0.00001f)
vTangent.m_fY = -vC.m_fY / vC.m_fX;

vA.setVectorCoords(m_pvVerticesCoords[index1].m_fZ - m_pvVerticesCoords[index0].m_fZ, fu21, fv21);
vB.setVectorCoords(m_pvVerticesCoords[index2].m_fZ - m_pvVerticesCoords[index0].m_fZ, fu31, fv31);
vC = CVector3f::getCrossProduct(vA, vB);

if(vC.m_fX > 0.00001f)
vTangent.m_fZ = -vC.m_fY / vC.m_fX;

vTangent.normalizeVector();
return vTangent;

Share this post


Link to post
Share on other sites
I've added some additional pictures which shows the lighting failure in more detail (The purple sphere represents the position of the light):

http://img.photobucket.com/albums/v211/Hellhound_01/rightLight.jpg
http://img.photobucket.com/albums/v211/Hellhound_01/leftLight.jpg
http://img.photobucket.com/albums/v211/Hellhound_01/FailureLight.jpg

The last image shows the failure of lighting in detail. I've marked the edge of the "texture switch" with red elipses. The lower part shows the texture by lighting from left, the upper part shows the texture by lighting from right...

Share this post


Link to post
Share on other sites
looks like the fault lies with the model ( eg normals ) try loading it in another program eg http://www.typhoonlabs.com/

Share this post


Link to post
Share on other sites
That was my thougths too, so i've tested my environment with an easyer model, a simple tris where i can check the vertices an normals... There is the same failure ...

Share this post


Link to post
Share on other sites
Hey, I just saw that:
Quote:

// bind tangents to vertex-shader (Param2 TEXTURECOORDS1)
for (int i=0; i<m_pMeshesToRender->getVertexCount(); i++) {
glMultiTexCoord3fARB(GL_TEXTURE1_ARB, m_pMeshesToRender->m_pvTangents.m_fX, m_pMeshesToRender->m_pvTangents.m_fY, m_pMeshesToRender->m_pvTangents.m_fZ);
}


This can't be right, especially since you use VBOs. It's just glMultiTexCoord commands one after another, with no vertices send to the GPU. It basically does nothing but constantly changing the current texcoord, and in the end it "stays" with the last value.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!