• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
PhillipClark

Pulling my hair out over D3D11 lighting bug

10 posts in this topic

I have a simple model loaded and a point light source that I am trying to get to work correctly, but it's just not happening. I have probably spent around 12 hours over the past two days trying to figure out what is wrong, and I'm starting to get really frustrated, so I thought I would ask here as a last resort.

I have checked and double checked my lighting equation many times with at least 3 different sources. At the moment I am using constant lighting coefficient so that I know for a fact my object will be lit no matter what the distance. My light is EXPLICITLY fixed at the -30.0 position on the Z-axis, yet my object appears as if it is lit from a different direction. My camera is located at ( 0.0, 0.0, -20.0 ) so that I can verify the model is not swallowing the light, and it is not. The light is plenty far away from my model. I've changed the light/camera distance many times and have gotten the same result.

The entire front side of my object should be lit, instead it is lit from a different angle.

I use the DirectXMath library and I was VERY, VERY careful to store the const buffer matrices correctly and with the right alignment. I also made sure my HLSL byte alignment was correct by using float4. I use the row-major compiler options for HLSL so I do not need to use matrix transpose, and my code reflects that.

Please give my code a look and tell me where I've gone wrong, because I obviously cannot figure it out! [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

[size=5][b][u]Bugged lighting:[/u][/b][/size]
[attachment=12680:bug.jpg]

[size=5][b][u]Matrix update function:[/u][/b][/size]
[attachment=12677:update.jpg]

[size=5][u][b]Vertex shader:[/b][/u][/size]
[attachment=12678:vs.jpg]

[size=5][b][u]Pixel shader:[/u][/b][/size]
[attachment=12679:ps.jpg] Edited by eowdaoc
0

Share this post


Link to post
Share on other sites
Make sure that normal.w = 0 in your vert shader before transforming it into world space

Also you need to normalize the normal that comes in to your fragment shader from the interpolators (since it linearly interpolates over the triangle

Otherwise, start shader debugging by visualizing different terms at every step in the shader (just like normal debugging) to isolate your problem.
0

Share this post


Link to post
Share on other sites
Also, it seems your light position is in world space, but the surface position you're using is not.
0

Share this post


Link to post
Share on other sites
I just added the two things you said in your first post, to make sure the normal is truly normalized at all steps. Unfortunately, it did not fix the bug. Could you further explain your last post concerning world space light position vs surface? In the simple lighting examples I've seen on the internet they have made no distinction between the different view spaces when doing lighting in the pixel shader, yet their lighting was displayed correctly.

[size=5][b][u]Updated normal transform in vertex shader:[/u][/b][/size]
[CODE]
float4 tmp_norm = float4( input.norm.x, input.norm.y, input.norm.z, 0.0 );
output.norm = mul( tmp_norm , world_matrix );
output.norm = normalize( output.norm );
[/CODE]

[size=5][b][u]Updated normal declaration in pixel shader:[/u][/b][/size]
[CODE]
float4 norm_norm = normalize( input.norm );
float3 tmp_norm = float3( norm_norm.x, norm_norm.y, norm_norm.z );
[/CODE] Edited by eowdaoc
0

Share this post


Link to post
Share on other sites
I actually just discovered that if I move my light source a ridiculous distance away (z value of -5000.0) then it works properly. I'm cool with this, but I'm really curious as to why it would not work at a distance that should have given the object enough room, and of course I would like to be able to set my light position to normal values without having to multiply by 5000.
0

Share this post


Link to post
Share on other sites
To rule out the possibility that your cbuffer data somehow is invalid: do things appear correct when you hard-code the matrices and/or light position directly into the shaders?
0

Share this post


Link to post
Share on other sites
You may declare your normals as float3 from the beginning. Ie. for the input vertex structure.

Also, when using HLSL it is more or less pointless to write code such as :

float3 tmp_norm = float3(input.norm.x, ....) since you may as well write just float3 tmp_norm = input.norm.xyz.

Since your normal is still declared as float4, it will be rotated AND translated by the world matrix, which will give you wrong normals. It doesn't help to set the w-component to 0 in this case.

Here is the code which works for me: (i.vNormal is defined as float3 ):

float3 vNormal = mul(i.vNormal,(float3x3)mWorld);
vNormal = normalize(vNormal);

Cheers! Edited by kauna
1

Share this post


Link to post
Share on other sites
Thanks for the suggestions, especially the tip about using input.norm.xyz, I feel ignorant now. So, letting HLSL align the buffer automatically should be okay? My old code was float3 for the vertex shader input, but I got paranoid and switched to float4 [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

Is it okay for the input position to be float3, since it can only be passed to the pixel shader as a float4? Should I always set the w-component to zero when I don't need it?
0

Share this post


Link to post
Share on other sites
In your fragment shader, the input.pos (surface position) that you're using is in projection space. However your light position is in world space. So when you compute the 'surface to light' vector (tmp_light - tmp_pos), you're doing it with coordinates in different spaces, and certainly not getting the world space direction you're looking for. Send the world position into your fragment shader and use that instead.
1

Share this post


Link to post
Share on other sites
Gotcha, so I guess that should explain why the light does not appear to be on the z-axis like it should be, it is instead a bit to the left.

When you say to send the world position to the pixel shader, do you mean in place of the one I'm already sending, or as an extra?

EDIT: Just updated my code and all of a sudden everything works like a charm! Thanks, dude, and thanks to the guy who first mentioned it, I was just too thick-headed to think about passing another position to the pixel buffer. I've definitely learnt my lesson about mixing world/view/proj spaces [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img] That being said, is there a more elegant way to handle this instead of passing two positions? Edited by eowdaoc
0

Share this post


Link to post
Share on other sites
Typically you'll need either the position in world space or position in view space, so there isn't really a way to skip that.

Cheers!
1

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  
Followers 0