Page 1 of 1

[SOLVED] Normal Vector for lighting.

Posted: Tue Mar 04, 2014 9:07 pm
by Benjamin100
I've been working a while on my vertex shader.
I've been having trouble with my lighting.

Everything on the screen appears dark unless I go in and manually set the color, in which case I can see that the cube does draw to the screen. I played around with different variables to figure out where the problem is, and it seems like it has something to do with my normal vector.

Is there anything that immediately stands out as wrong about this code?:

Code: Select all

 //GLSL
vec3 normalVector=normalize(modelviewInverseTranspose*vertexNormal);
vec3 lightVector= normalize(vec3(mainLight.position));
vec3 lighting=vec3(mainLight.color) * vec3(objectColor) * max(0.0, dot(normalVector, lightVector) );
fragmentColor=vec4(lighting, 1.0);    //Move final lighting to fragment shader.
I also thought perhaps my problem was specifically with the modelview matrix, (as the vertex normals all read correctly when I print them.)

the modelviewInverseTranspose matrix is defined this way;

Code: Select all

model=glm::translate(glm::mat4(1.0f) , glm::vec3(0.0, 0.0, -4.0) );
view= glm::lookAt(glm::vec3(0.0, 2.0, 0.0), glm::vec3(0.0, 0.0, -4.0), glm::vec3(0.0, 1.0, 0.0));
modelviewMatrix=model*view;
glm::mat3 modelviewInverseTranspose=glm::transpose(glm::inverse(glm::mat3(modelviewMatrix)));
Maybe this is a bigger problem with my program, in which case I will have to spend some more time. But I was just wondering if anything stands out as obviously wrong with these sections. I compare these to other examples online and I don't see much of a difference. Perhaps it is something simple that I am overlooking, but I don't see where the problem is.

Re: Normal Vector for lighting.

Posted: Wed Mar 05, 2014 1:03 pm
by qpHalcy0n
You need to have color on both the light and the object. If the light has no color, it has no intensity. If the object has no color it fully absorbs the light. So you need a diffuse and possibly specular color if you're going that route.

Where are you doing the lighting? View? World? It looks like you're not in the same space.

Re: Normal Vector for lighting.

Posted: Fri Mar 07, 2014 12:31 am
by Benjamin100
Both the object and light have a color.

I'm trying to make a directional light. I'm not sure I understand how to do that with a light vector. Which space should that be done in?

Re: Normal Vector for lighting.

Posted: Fri Mar 07, 2014 10:08 am
by qpHalcy0n
Alright, so that's good. Just ensure those color parameters are set.

Where you do the lighting calculation does not matter. You can do it in world space, view space, post projection space, NDC. What DOES matter is that all of your positions, normals, and so forth all be in the same space. You must be consistent.

What it looks like is that you're using a view space light vector but a world space normal. There isn't enough context to know what space you're in there.

I would suggest really understanding the various spaces, how they're shaped, and what they mean. This is Computer Graphics 101 and are things that you *must* understand to really be successful there. So make an effort to understand these concepts and it will help you immensely. Otherwise you're just hacking code together hoping it will work while never knowing why.

Re: Normal Vector for lighting.

Posted: Fri Mar 07, 2014 11:26 am
by bbguimaraes
qpHalcy0n wrote:So make an effort to understand these concepts and it will help you immensely. Otherwise you're just hacking code together hoping it will work while never knowing why.
The first step to computer graphics enlightenment is acceptance of this fact. It reminded me of this.

Re: Normal Vector for lighting.

Posted: Fri Mar 07, 2014 2:13 pm
by qpHalcy0n
bbguimaraes wrote:
qpHalcy0n wrote:So make an effort to understand these concepts and it will help you immensely. Otherwise you're just hacking code together hoping it will work while never knowing why.
The first step to computer graphics enlightenment is acceptance of this fact. It reminded me of this.
Then you are truly one with the Tao... ^_^

Re: Normal Vector for lighting.

Posted: Fri Mar 07, 2014 11:30 pm
by Benjamin100
OK. Thanks.
I have a couple of books on computer graphics that talk about the different spaces. I will review those sections very carefully again to make sure I understand them.

Re: Normal Vector for lighting.

Posted: Sat Mar 08, 2014 3:41 pm
by YourNerdyJoe
bbguimaraes wrote:
qpHalcy0n wrote:So make an effort to understand these concepts and it will help you immensely. Otherwise you're just hacking code together hoping it will work while never knowing why.
The first step to computer graphics enlightenment is acceptance of this fact. It reminded me of this.
This here made my day :lol:

Re: Normal Vector for lighting.

Posted: Wed Mar 12, 2014 8:57 pm
by Benjamin100
Well, I think I understand the different spaces.
I was working in view space for the lighting.
My problem is with the vertex normals.
When I just use the vertex normals, things look as I would expect.
The trick is that I will eventually want to rotate the object, and so I need to make sure the normals change with the moving faces. Do I multiply them by the model matrix? They are normals, not vertices. Will it work just as well?

Re: Normal Vector for lighting.

Posted: Sun Mar 16, 2014 4:08 pm
by Benjamin100
OK, seems the problem was with my uniform binding.
Some sort of mistake with the matrix I was binding.
All working fine now.

Re: [SOLVED] Normal Vector for lighting.

Posted: Sun Mar 16, 2014 8:56 pm
by qpHalcy0n
For transforming vectors, if there are translations or non-uniform shears in the matrix (assume there are), then you use homogenous coordinates. For a vector you would use [X Y Z 0]. For a point you would use [X Y Z 1].

Re: Normal Vector for lighting.

Posted: Tue Apr 22, 2014 8:40 am
by killall_sigsegv
Benjamin100 wrote:OK. Thanks.
I have a couple of books on computer graphics that talk about the different spaces. I will review those sections very carefully again to make sure I understand them.
Not sure if you've heard of it, or if you're looking for another resource, but this is a really good eBook which has been helpful to a lot of people, including myself. It goes deeply into the fundamentals and teaches the reader everything they need to know in order to really dive into graphics on an intuitive level. :)

Re: Normal Vector for lighting.

Posted: Tue Apr 22, 2014 6:19 pm
by ph0sph0ruz
killall_sigsegv wrote:
Benjamin100 wrote:OK. Thanks.
I have a couple of books on computer graphics that talk about the different spaces. I will review those sections very carefully again to make sure I understand them.
Not sure if you've heard of it, or if you're looking for another resource, but this is a really good eBook which has been helpful to a lot of people, including myself. It goes deeply into the fundamentals and teaches the reader everything they need to know in order to really dive into graphics on an intuitive level. :)
I can attest to reading that book just recently and it's very good. I learned quite a bit.

Re: [SOLVED] Normal Vector for lighting.

Posted: Sun May 11, 2014 3:22 pm
by Benjamin100
Oh, I didn't notice people had posted more on this topic.
Thanks.
I did figure it out a while ago.
I was able to make an object loader for the basic shapes. Another big problem came when I added textures. Then the code just got messy and out of hand. I forget what my problem was, but I stopped and went into studying other aspects of programming outside of graphics. If I ever make an object loader again I'll probably do it from scratch because I almost forget what all the code did in my old one, (does anybody have that problem looking over their old code?) I'm always angry at myself over what I decided needed commenting and what I decided didn't.

Re: [SOLVED] Normal Vector for lighting.

Posted: Mon May 12, 2014 4:45 pm
by dandymcgee
Benjamin100 wrote:If I ever make an object loader again I'll probably do it from scratch because I almost forget what all the code did in my old one, (does anybody have that problem looking over their old code?).
While I occasionally have to re-familiarize myself with *why* I did something a particular way, I very pretty much never forget what the code is doing. Then again, I don't write many test / demo projects these days; most of my code has a pretty solid purpose. ;)
Benjamin100 wrote:I'm always angry at myself over what I decided needed commenting and what I decided didn't.
That skill comes with practice, but my general rule of thumb is:
If it wouldn't be immediately obvious to a competent programmer who is not working on this project what the code is doing or why something is the way it is, add a concise comment explaining the purpose of the code segment.