• Create Account

## Beginners problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1eee  Members

269
Like
0Likes
Like

Posted 02 June 2014 - 02:47 AM

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.

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?

### #2spazzarama  GDNet+

1630
Like
1Likes
Like

Posted 02 June 2014 - 04:42 AM

Sounds like something is zero

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.

Edited by spazzarama, 02 June 2014 - 04:49 AM.

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

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

@spazzarama

### #3Jason Z  Members

6417
Like
0Likes
Like

Posted 02 June 2014 - 07:34 PM

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

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #4Buckeye  GDNet+

10739
Like
2Likes
Like

Posted 02 June 2014 - 08:05 PM

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.

3482
Like
1Likes
Like

Posted 03 June 2014 - 05:18 PM

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.

### #6eee  Members

269
Like
0Likes
Like

Posted 04 June 2014 - 02:05 AM

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.

### #7Jason Z  Members

6417
Like
0Likes
Like

Posted 04 June 2014 - 02:28 PM

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

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.