• 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
alkisbkn

Directional light cookies

8 posts in this topic

Hello everyone,

for my deferred rendering engine I am adding cookies for directional lights, the process should be easy - but I am not getting proper results.
Okay, so from the C++ side, I create my directional light View and Projection matrices as such:
[CODE]
mat4 projection = glm::ortho<float>(-1,1,-1,1,-1,1);
mat4 view = glm::lookAt(vec3(0,0,0), direction, vec3(0,1,0));
[/CODE]
where direction is normalized. The View and Projection matrices are passed in the shader as Projection * View.

Now we go in the directional light shader where the magic happens. I reconstruct the view space position from the depth buffer (which works as the light renders fine without the cookie), and I embark to sample the cookie texture like this:

[CODE]
float attenuateFromCookie(sampler2D cookieSampler, vec4 fragmentPos, mat4 lightMatrix)
{
vec4 lightSpacePos = lightMatrix * invView * fragmentPos;
vec2 projCoords = lightSpacePos.xy * projectionScale - 0.5/IN.GBufferSize;
return texture(cookieSampler, projCoords).r;
}
[/CODE]
where projectionScale is just a vec2 to scale the cookie, so it repeats several times to look good (clouds for example).

Now, the cookie renders fine, however when I move my camera, the cookie goes berserk, moving around very fast. It should be noted that the cookie does not move when my camera is rotated, since I multiply by invView. However, wouldn't this fix the camera translation issue as well?

My guess is I am doing something wrong in the light view matrix.
Can someone help me?

Thanks a lot [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
You have to manually divide the lightspace position by it's w-value unless you're using a texture function that does that for you, like textureProj (assuming that you don't account for that already within your scaling somewhere).

So without any scaling, your sampling coordinate would be

[CODE]
vec4 lightSpacePos = lightMatrix * invView * fragmentPos;
vec2 projCoords = 0.5 + 0.5 * lightSpacePos.xy / lightSpacePos.w;
[/CODE]

The 0.5 + 0.5 * brings the coordinates from [-1, 1] in the [0, 1] range.

I hope that fixes it [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by DrColossus
0

Share this post


Link to post
Share on other sites
Hi and thank you for your reply!

It still wobbles around when I move the camera. I might be doing the view/projection matrix in a wrong way, do you think I should take into account the center of my frustum as the light's position?

However you were right about the w division, I did not do it.
0

Share this post


Link to post
Share on other sites
Is the wobbling some kind of delay?
If so, check if you're using the inverse view matrix of your current frame, after you've already updated it.

It's kinda hard to guess what's wrong without seeing what's going on
0

Share this post


Link to post
Share on other sites
No, it is view dependant, as I move the camera around, the cookie moves as well.

I wish I could show a video :(
What about the directional light view and projection matrix? Any comments on those? I have a feeling that the light position must be in the center of the frustum.
0

Share this post


Link to post
Share on other sites
I just tried your matrix setup myself (using glm and the same values) and it works fine for me even when moving around.

Since it acts weird when you move your camera, how do you set up the inverse view matrix?
Try glm::inverse(viewMatrix) if you aren't doing that already and make sure your position reconstruction from depth is correct. MJP has some good posts about that on his blog: [url="http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/"]http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/[/url]

If the light view matrix was dependant on your frustum, then the projected cookie texture would move around as you move the camera.
0

Share this post


Link to post
Share on other sites
Okay, that is good that it doesn't move with my values, I will keep using those for now.

The light view matrix is the inverse of its world matrix, thus the inverse view is the world.
Below I am showing you how I calculate the position from depth for the directional light, it is actually taken from MJP.
[CODE]
vec3 PositionFromLinearZ (sampler2D depthTexture, vec2 texcoord, vec3 positionVS)
{
float depth = texture(depthTexture, texcoord).r;
return depth * positionVS;
}
[/CODE]

The depth in my GBuffer is calculated as ViewSpacePosition.z / farClipPlane, and the "positionVS" that I pass in the PositionFromLinearZ for the directional light position reconstruction is
[CODE]
PositionVS = (inverseProjection * vec4(in_Position, 0, 1)).xyz;
[/CODE]
for a fullscreen quad in NDC coordinates.
0

Share this post


Link to post
Share on other sites
Oh but it seems I don't need to divide by w, as it's already in clip space and w=1 for fullscreen quad.
Using this order of multiplication:
[CODE]
vec4 lightSpacePos = lightMatrix * invView * fragmentPos;
[/CODE]
the cookie does not move when I rotate the camera, however it does go berserk when I move the camera around.
0

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