Beginners problem

Started by
5 comments, last by Jason Z 9 years, 10 months ago

Hi to all, I've been interested in game development for quite some time now and found this forum. Since it seems there are helpful people here, I thought I'd try my luck asking for help for a problem I'm having.

Without further ado, the problem is that I'm drawing some models using D3D11 and recently started using normals and texture coordinates for drawing (I'm just a beginner) in addition to just using vertice positions. I am trying to implement very simple lighting. The problem is, that when run, all objects appear completely black, even though I set my ambient light to completely white. Playing around with light values has no change at all - everything is black.

Pixel shader:


float4 PS( VS_OUTPUT input ) : SV_Target
{
	  return ObjTexture.Sample(ObjSamplerState, input.TexCoord );
	  input.normal = normalize(input.normal);
	  float4 diffuse = ObjTexture.Sample(ObjSamplerState, input.TexCoord );
	  float3 finalColor = diffuse*light.ambient;
	  finalColor += saturate(dot(light.dir,input.normal)*light.diffuse*diffuse);
	  return float4(finalColor,diffuse.a);
}

When I comment out the first line (return), all objects are black, otherwise everything is drawn without lighting. This is weird because even if my normals are wrong, ambient light should still light up the objects, right?

Thanks in advance.

Advertisement

Sounds like something is zero smile.png

Try outputting the light.ambient, input.normal and possibly light.dir or light.diffuse:


float4 PS( VS_OUTPUT input ) : SV_Target
{
    return light.ambient;
    //return input.normal;
    //return light.dir;
    //return light.diffuse;
}

My guess is that at least light.ambient is zero. You're correct, even if the normal vector or light direction is zero (dot product with zero vector = 0) you are adding this to the finalColor so += 0 should still return you the diffuse * light.ambient.

What edition of Visual Studio are you using? If VS 2012 or VS 2013 there is a built-in graphics debugger that would allow you to step through and evaluate some of the values within your shader. When compiling the shader you must supply D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION and the shader file name - see this topic for an example. Alternatively there are a number of other debuggers available see this GameDev topic about free HLSL debugging tools.

Justin Stenning | Blog | Book - Direct3D Rendering Cookbook (using C# and SharpDX)

Projects: Direct3D Hook, EasyHook, Shared Memory (IPC), SharpDisasm (x86/64 disassembler in C#)

@spazzarama

 

Usually when you add something to your vertex layout, if the rendering goes out or you get totally unexplainable results, then you probably have missed something in your input layout declaration that is causing the vertex data to not line up properly. Can you post your input layout before and after you added your new vertex data? We can double check it and point out anything that doesn't look quite right.

And welcome to the forums by the way - I hope you like it here :)

You also have to ensure that you're interpreting the light direction correctly. The dot product of the light vector with the normal really tests the light direction anti-parallel to the vector you set for "light direction."

E.g. a normal pointing along the plus X axis (1,0,0) will produce a positive dot product with a light direction (1,0,0), which is pointing in the same direction as the normal!

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

You should also make sure that you're using D3D11_CREATE_DEVICE_DEBUG at least in debug builds so D3D will pick up obvious errors for you and tell you about them.

Thanks for everyone, who has answered, the problem turned out to be with setting the constant buffers - I have 2 : one for each frame and one for each object. When I was setting the buffers I had forgotten to change the register in the call.


m_pDevCon->PSSetConstantBuffers(0,1,&m_pCBufferPerFrame); 

Offtopic: If i have other problems, am I supposed to create a new thread for each one, or can I just use this thread?

Thanks.

You would create a new thread for each issue, unless they are somehow related topics.

This topic is closed to new replies.

Advertisement