Sign in to follow this  
shadowvv

light problem

Recommended Posts

shadowvv    118
hi guys

i am learning lighting with directx.I coded a applicationto implement directional light.but when i changed teapot face direction from (0.0f,0.0f,1.0f) to (0.0f,0.0f,-1.0f) .there are something wrong with my application.


face = (0.0f,0.0f,1.0f)
[img]http://i1120.photobucket.com/albums/l492/shadowvv/QQ20110606015539.jpg[/img]



face = (0.0f,0.0f,-1.0f)
[img]http://i1120.photobucket.com/albums/l492/shadowvv/QQ20110606015454.jpg[/img]


this is partial code:
[code]


struct SF3DOBJECTSTATE
{
SF3DOBJECTSTATE():position(D3DXVECTOR3(0.0f,0.0f,0.0f)),face(0.0f,0.0f,-1.0f),up(0.0f,1.0f,0.0f),right(D3DXVECTOR3(1.0f,0.0f,0.0f))
{
material.ambient = material.diffuse = material.specular = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
}

SFMATERIAL material;
D3DXVECTOR3 position;
D3DXVECTOR3 face;
D3DXVECTOR3 up;
D3DXVECTOR3 right;
};


_SFC3DObject::_SFC3DObject(SF3DOBJECTSTATE* objectState):m_3DObjectState(*objectState)
{
m_worldMatrix(0,0) = m_3DObjectState.right.x; m_worldMatrix(0, 1) = m_3DObjectState.right.y; m_worldMatrix(0, 2) = m_3DObjectState.right.z; m_worldMatrix(0, 3) = 0.0f;
m_worldMatrix(1,0) = m_3DObjectState.up.x; m_worldMatrix(1, 1) = m_3DObjectState.up.y; m_worldMatrix(1, 2) = m_3DObjectState.up.z; m_worldMatrix(1, 3) = 0.0f;
m_worldMatrix(2,0) = m_3DObjectState.face.x; m_worldMatrix(2, 1) = m_3DObjectState.face.y; m_worldMatrix(2, 2) = m_3DObjectState.face.z; m_worldMatrix(2, 3) = 0.0f;
m_worldMatrix(3,0) = m_3DObjectState.position.x; m_worldMatrix(3, 1) = m_3DObjectState.position.y; m_worldMatrix(3, 2) = m_3DObjectState.position.z; m_worldMatrix(3, 3) = 1.0f;
}



struct SFCLIGHTSTATE
{
SFCLIGHTSTATE():ambient(1.0f,1.0f,1.0f,1.0f),diffuse(1.0f,1.0f,1.0f,1.0f),specular(1.0f,1.0f,1.0f,1.0f){}

D3DXCOLOR ambient;
D3DXCOLOR diffuse;
D3DXCOLOR specular;
};

struct SFDIRECTIONALLIGHTSTATE
{
SFDIRECTIONALLIGHTSTATE():lightColor(),direction(0.0f,0.0f,-1.0f){}

SFCLIGHTSTATE lightColor;
D3DXVECTOR3 direction;
};


[/code]

this is my effect code:
[code]


uniform extern float4x4 gWorld;
uniform extern float4x4 gWorldInverseTranspose;
uniform extern float4x4 gWVP;

uniform extern float4 gAmbientMtrl;
uniform extern float4 gAmbientLight;
uniform extern float4 gDiffuseMtrl;
uniform extern float4 gDiffuseLight;
uniform extern float4 gSpecularMtrl;
uniform extern float4 gSpecularLight;
uniform extern float gSpecularPower;
uniform extern float3 gLightVecW;
uniform extern float3 gEyePosW;

struct OutputVS
{
float4 posH : POSITION0;
float4 color : COLOR0;
};

OutputVS AmbientDiffuseSpecVS(float3 posL : POSITION0, float3 normalL : NORMAL0)
{
// Zero out our output.
OutputVS outVS = (OutputVS)0;

// Transform normal to world space.
float3 normalW = mul(float4(normalL, 0.0f), gWorldInverseTranspose).xyz;
normalW = normalize(normalW);

// Transform vertex position to world space.
float3 posW = mul(float4(posL, 1.0f), gWorld).xyz;

//=======================================================
// Compute the color: Equation 10.3.

// Compute the vector from the vertex to the eye position.
float3 toEye = normalize(gEyePosW - posW);

// Compute the reflection vector.
float3 r = reflect(-gLightVecW, normalW);

// Determine how much (if any) specular light makes it into the eye.
float t = pow(max(dot(r, toEye), 0.0f), gSpecularPower);

// Determine the diffuse light intensity that strikes the vertex.
float s = max(dot(gLightVecW, normalW), 0.0f);

// Compute the ambient, diffuse and specular terms separatly.
float3 spec = t*(gSpecularMtrl*gSpecularLight).rgb;
float3 diffuse = s*(gDiffuseMtrl*gDiffuseLight).rgb;
float3 ambient = gAmbientMtrl*gAmbientLight;

// Sum all the terms together and copy over the diffuse alpha.
outVS.color.rgb = ambient + diffuse + spec;
outVS.color.a = gDiffuseMtrl.a;
//=======================================================

// Transform to homogeneous clip space.
outVS.posH = mul(float4(posL, 1.0f), gWVP);

// Done--return the output.
return outVS;
}

float4 AmbientDiffuseSpecPS(float4 c : COLOR0) : COLOR
{
return c;
}

technique AmbientDiffuseSpecTech
{
pass P0
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 AmbientDiffuseSpecVS();
pixelShader = compile ps_2_0 AmbientDiffuseSpecPS();
}
}
[/code]


any help is appreciated

Share this post


Link to post
Share on other sites
DJTN    207
What is the problem? It looks to me like your moving the teapot to face the negative Z direction and it's getting farther away from the light.

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