Jump to content

  • Log In with Google      Sign In   
  • 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.

  • You cannot reply to this topic
6 replies to this topic

#1 eee   Members   -  Reputation: 127

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.

 

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.



Sponsor:

#2 spazzarama   Members   -  Reputation: 600

Like
1Likes
Like

Posted 02 June 2014 - 04:42 AM

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.


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

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

Projects: Direct3D Hook, EasyHookAfterglow, C#raft

@spazzarama

 

#3 Jason Z   Crossbones+   -  Reputation: 4872

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 :)



#4 Buckeye   Crossbones+   -  Reputation: 4113

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.


#5 Adam_42   Crossbones+   -  Reputation: 2425

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.



#6 eee   Members   -  Reputation: 127

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.



#7 Jason Z   Crossbones+   -  Reputation: 4872

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.






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.



PARTNERS