[SOLVED] Normal Vector for lighting.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

[SOLVED] Normal Vector for lighting.

Post 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.
Last edited by Benjamin100 on Sun Mar 16, 2014 4:08 pm, edited 1 time in total.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Normal Vector for lighting.

Post 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.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Normal Vector for lighting.

Post 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?
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Normal Vector for lighting.

Post 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.
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Normal Vector for lighting.

Post 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.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Normal Vector for lighting.

Post 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... ^_^
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Normal Vector for lighting.

Post 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.
User avatar
YourNerdyJoe
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 79
Joined: Sun Oct 02, 2011 3:28 pm
Current Project: Top secret (not really) Top-Down Shooter for GBA
Favorite Gaming Platforms: GBA, Gamecube, PC, 3DS
Programming Language of Choice: C/C++
Contact:

Re: Normal Vector for lighting.

Post 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:
See that?.....
Exactly
https://yournerdyjoe.github.io/
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Normal Vector for lighting.

Post 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?
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Normal Vector for lighting.

Post 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.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: [SOLVED] Normal Vector for lighting.

Post 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].
killall_sigsegv
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Mon Mar 24, 2014 4:36 am

Re: Normal Vector for lighting.

Post 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. :)
User avatar
ph0sph0ruz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 32
Joined: Sat Mar 22, 2014 3:52 am
Favorite Gaming Platforms: PC,Xbox,Dreamcast
Programming Language of Choice: C,C++
Contact:

Re: Normal Vector for lighting.

Post 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.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: [SOLVED] Normal Vector for lighting.

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: [SOLVED] Normal Vector for lighting.

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply