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

Shading changes when camera moves

8 posts in this topic

Hi guys,

I've got this problem where the shading of my objects changes when my camera moves. The obvious solution is that I'm not transforming my light into eye space before sending it to the shader, but I am doing this. Another thing to note is that when I leave my light AND normals in world space, it works fine.

[CODE]

glm::mat4 modelView = m_camera->GetView() * glm::mat4(1.0);

m_graphics->SetUniform(id, "u_ModelViewMatrix", 1, false, modelView);
m_graphics->SetUniform(id, "u_NormalMatrix", 1, false, glm::mat3(glm::vec3(modelView[0]), glm::vec3(modelView[1]), glm::vec3(modelView[2])));
m_graphics->SetUniform(id, "u_MVP", 1, false, m_graphics->GetProjMatrix() * modelView);

m_graphics->SetUniform(id, "Light.Position", modelView * glm::vec4(-100.0f, 50.0f, 30.0f, 1.0f));
[/CODE]

This is how I'm calculating my view matrix

[CODE]
void ViewFrustum::Update()
{
m_direction = glm::vec3(cos(m_yaw) * sin(m_pitch),
sin(m_yaw),
cos(m_yaw) * cos(m_pitch));
m_view = glm::lookAt(m_position, m_position + m_direction, glm::vec3(0, 1, 0));
m_right = glm::cross(m_up, m_direction);
}
[/CODE]

The camera works fine and I move around the world with no troubles.

Here's my shader code. It's actually just storing the values in a GBuffer for deferred rendering.

Vertex
[CODE]
#version 400

layout (location = 0) in vec3 in_Position;
layout (location = 1) in vec3 in_Normal;
layout (location = 2) in vec3 in_TexCoord;

out vec3 Position;
out vec3 Normal;
out vec3 TexCoord;

uniform mat4 u_ModelViewMatrix;
uniform mat3 u_NormalMatrix;
uniform mat4 u_MVP;

void main()
{
Position = in_Position;
Normal = normalize(u_NormalMatrix * in_Normal);
TexCoord = in_TexCoord;

gl_Position = u_MVP * vec4(Position, 1.0);
}
[/CODE]

In the second pass I use the Light.Position variable:

[CODE]
vec3 shadePixel(vec3 pos, vec3 norm, vec3 diff)
{
if (diff == vec3(1.0, 1.0, 1.0)) return vec3(1.0, 1.0, 1.0);

vec3 s = normalize(vec3(Light.Position) - pos);
float sDotN = max(dot(s, norm), 0.0);
vec3 diffuse = Light.Intensity * diff * sDotN;

return diffuse;
}
// get the values from GBuffer
vec4 diffusePass()
{
vec3 pos = vec3(texture2D(positionTexture, UV));
vec3 norm = vec3(texture2D(normalTexture, UV));
vec3 diff = vec3(texture2D(diffuseTexture, UV));

return vec4(shadePixel(pos, norm, diff), 1.0);
}
[/CODE]

If I don't multiply the normal by u_NormalMatrix and I don't multiply the light position by the view matrix it works fine. Can't spot why it wont work in view space.

Thanks for the help in advance. Edited by rocklobster
0

Share this post


Link to post
Share on other sites
The MVP-Matrix consists of 3 components:
1. Modelmatrix is the scaling, rotation and translation of the object
2. Viewmatrix is the position, lookat, rotation of the camera
3. Projectionmatrix is the "presentation to the 2d screen"

Seems like you use the model*view-matrix for your normalmatrix. Try to use just the modelmatrix for transforming the normal in your shader.
The position of object and light is independent from the position of the camera.
0

Share this post


Link to post
Share on other sites
http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/

This link explains pretty well why I'm using the view matrix to do lighting calculations. I don't want to do the calculations in world/model space.
0

Share this post


Link to post
Share on other sites
Render the normals to the final color buffer instead of the diffuse so that you can see the actual normal X, Y, and Z values.
As you rotate the camera, each surface of each object should change color. Floors should be green if you are looking straight, but turn black if you look down.
If they do not, the problem is in your normal matrix (which should be the inverse-transpose of the model-view matrix, not just the model-view matrix).

If they do look fine, the problem is in your light direction.


L. Spiro Edited by L. Spiro
1

Share this post


Link to post
Share on other sites
[quote name='L. Spiro' timestamp='1354584056' post='5006885']
Render the normals to the final color buffer instead of the diffuse so that you can see the actual normal X, Y, and X values.
As you rotate the camera, each surface of each object should change color. Floors should be green if you are looking straight, but turn black if you look down.
If they do not, the problem is in your normal matrix (which should be the inverse-transpose of the model-view matrix, not just the model-view matrix).

If they do look fine, the problem is in your light direction.


L. Spiro
[/quote]

The floor was green when the camera is in normal position, as I pitch down the floor fades from green -> teal -> blue. That shouldn't be right? Blue would mean (0, 0, 1) normal value which isn't correct. I tried putting a glm::inverse() around my normal matrix but had the same results.
0

Share this post


Link to post
Share on other sites
Actually that is correct. Green to blue.
And the normal matrix is modelView.Transpose().Inverse(), not modelView.Inverse().

It means your light’s position is wrong. The light’s position should be transformed by the view.Inverse() matrix, not the modelView matrix.


L. Spiro Edited by L. Spiro
0

Share this post


Link to post
Share on other sites
Ok I've changed my normal matrix to

[CODE]
m_graphics->SetUniform(id, "u_NormalMatrix", 1, false,
glm::inverse(glm::transpose(glm::mat3(glm::vec3(viewMatrix[0]), g lm::vec3(viewMatrix[1]), glm::vec3(viewMatrix[2])))));
[/CODE]

and the light is

[CODE]
glm::vec4 lightPosViewSpace = glm::inverse(g->GetViewMatrix()) * glm::vec4(-50.0f, 150.0f, 30.0f, 1.0f);
g->SetUniform(id, "Light.Position", lightPosViewSpace);
[/CODE]

Perhaps I should multiply the position in my frag shader by model view matrix such as:

[CODE]
vec3 s = normalize(vec3(Light.Position) - (u_ModelViewMatrix * vec4(pos, 1.0)));
[/CODE]

Because I'm just passing it through my geom buffer shader unchanged:

[CODE]
Position = in_Position;
[/CODE]

EDIT: The shading still changes when the camera moves. If I turn around from origin in the scene and start tilting down, the floor and everything just turns black. And actually i think putting the inverse after the transpose didn't actually change a thing. Edited by rocklobster
0

Share this post


Link to post
Share on other sites
It won’t change anything unless you have non-uniform scaling in either the world matrix or the view matrix.


Also I might have mislead you in my previous post. I meant to say the inverse of the camera matrix, which is in itself the view matrix, so rather than taking the inverse of the view matrix it should be just the view matrix. It was minutes before I passed out of sheer sleepiness and I wasn’t thinking straight.

It should likely fix your problem to multiply the light position by the raw view matrix.


L. Spiro
0

Share this post


Link to post
Share on other sites
No problem, I often do too much coding whilst half asleep :P

So I've multiplied the light position by just the view matrix and the floor doesn't change to black any more when I rotate and look down. The other objects still seem to shade differently when I translate and rotate around the world. I'm still kind of confused because when I leave the normal/light in world space the shading is all static no matter where I move, but as soon as I multiply the normals and the light by the normal/view matrix the shading changes based on my camera (this sounds like it is expected, but I never see this in games anywhere?). I'm not multiplying the position by the modelview matrix before sending it to the Geom Buffer, which is ok because if i do the world looks and behaves weirdly. But even if I multiply the position by the modelview matrix in the frag shader just to calculate the direction of the light to that position in the texture it seems to be have the same way.
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