i didn't want to revive my old funky normals thread but i've got some new infos that may help to solve my problem. For those who haven't read my old thread:
(I thought that!!)The problem is that my function for calculating the normals of my mesh doesn't work right. I first tested it with 2 cubes(both cubes in 1 Vertexbuffer) and the normals of the first cube were alright while the ones from the second cube weren't. For testing purpose i simply exported 2 seperated triangles lying on the x/z plane and all normals(for both triangles) were wrong. I tested if my code calculates the right normals and noticed that even in the vertex buffer the normals are correct(see screenshot below). But when i show the normals as colors with my pixelshader i get this:
clicky
but when i lock my buffer just before rendering and look at the memory inside the buffer everything looks fine:(the floats surrounded by the blue boxes are the normals)
clicky
and the vertex decl used is:
clicky
(also checked right before rendering with
D3DVERTEXELEMENT9 decl[256];
UINT numElements;
HRESULT hr = vertexdecl->GetDeclaration( decl, &numElements);
)
my rendering function looks like this:
void Draw(ngVertexBuffer *pvbuffer,ngIndexBuffer *pibuffer)
{
ngEngine &engine=ngEngine::getInstance();
int vertexsize=pvbuffer->getVertexsize();
int triangleCount=pibuffer->getLength()/3;
LPDIRECT3DVERTEXBUFFER9 vbuffer=pvbuffer->getD3DVBuffer();
LPDIRECT3DINDEXBUFFER9 ibuffer=pibuffer->getD3DIBuffer();
DWORD fvf=pvbuffer->getFVF(); //just switched 2 vertexdecls recently...
DWORD vlength=pvbuffer->getLength();
LPDIRECT3DVERTEXDECLARATION9 vertexdecl=pvbuffer->getVertexDecl();
D3DVERTEXELEMENT9 decl[256];
UINT numElements;
HRESULT hr = vertexdecl->GetDeclaration( decl, &numElements);
void *pOut;
//vbuffer->Lock(0,0,&pOut,0);
//engine.g_pD3DDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
engine.g_pD3DDevice->SetVertexDeclaration(vertexdecl);
//engine.g_pD3DDevice->SetFVF(fvf);
engine.g_pD3DDevice->SetStreamSource(0,vbuffer,0,vertexsize);
engine.g_pD3DDevice->SetIndices(ibuffer);
engine.g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,pvbuffer->getLength(),0,triangleCount);
}
All that looks quite correct to me. So maybe the error is in the shader:
float3 lightVector;
float4x4 matView : View;
float4x4 matProjection : Projection;
float4x4 matWorld : World;
float4x4 matWorldViewProjection : WorldViewProjection;
struct VS_INPUT
{
float3 Position : POSITION0;
float2 uv : TEXCOORD0;
float3 Normal : NORMAL0;
};
struct VS_OUTPUT
{
float3 normal : TEXCOORD1;
float2 uv : TEXCOORD0;
float4 Position : POSITION0;
};
struct PS_INPUT
{
float3 normal : TEXCOORD1;
float2 uv : TEXCOORD0;
float4 Position : POSITION0;
};
VS_OUTPUT Position_Pass_0_Vertex_Shader_vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
float4 n=mul(float4(Input.Normal,1.0f), matWorld);
//Output.normal = float3(n.x, n.y, n.z);
Output.uv=Input.uv;
Output.normal = Input.Normal;
Output.Position = mul(float4(Input.Position,1.0f), matWorldViewProjection);
return( Output );
}
float4 Position_Pass_0_Pixel_Shader_ps_main(PS_INPUT Input) : COLOR0
{
//float3 light=normalize(float3(1.0f,1.0f,1.0f));
//return float4(1.0f,0.0f,0.0f,1.0f)*dot(normalize(normal),lightVector);
return float4(Input.normal,1.0f);
}
//--------------------------------------------------------------//
// Technique Section for Position
//--------------------------------------------------------------//
technique Position
{
pass Pass_0
{
PixelShader = compile ps_2_0 Position_Pass_0_Pixel_Shader_ps_main();
VertexShader = compile vs_2_0 Position_Pass_0_Vertex_Shader_vs_main();
}
}
hopefully somebody spots the problem. I really have no clue how that is possible.
regards,
m4gnus
[Edited by - m4gnus on April 23, 2006 3:57:20 AM]
"There are 10 types of people in the world... those who understand binary and those who don't."