[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

[SOLVED] Normal Vector for lighting.

Postby Benjamin100 on Tue Mar 04, 2014 11:07 pm

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 5:08 pm, edited 1 time in total.
Benjamin100
ES Beta Backer
ES Beta Backer
 
Posts: 255
Joined: Tue Jul 19, 2011 10:37 pm

Re: Normal Vector for lighting.

Postby qpHalcy0n on Wed Mar 05, 2014 3:03 pm

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

Re: Normal Vector for lighting.

Postby Benjamin100 on Fri Mar 07, 2014 2:31 am

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

Re: Normal Vector for lighting.

Postby qpHalcy0n on Fri Mar 07, 2014 12:08 pm

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

Re: Normal Vector for lighting.

Postby bbguimaraes on Fri Mar 07, 2014 1:26 pm

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.
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
 
Posts: 324
Joined: Wed Apr 11, 2012 5:34 pm
Location: Brazil

Re: Normal Vector for lighting.

Postby qpHalcy0n on Fri Mar 07, 2014 4:13 pm

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... ^_^
qpHalcy0n
Respected Programmer
Respected Programmer
 
Posts: 388
Joined: Fri Dec 19, 2008 5:33 pm
Location: Dallas

Re: Normal Vector for lighting.

Postby Benjamin100 on Sat Mar 08, 2014 1:30 am

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

Re: Normal Vector for lighting.

Postby YourNerdyJoe on Sat Mar 08, 2014 5:41 pm

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:
User avatar
YourNerdyJoe
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
 
Posts: 83
Joined: Sun Oct 02, 2011 4:28 pm

Re: Normal Vector for lighting.

Postby Benjamin100 on Wed Mar 12, 2014 9:57 pm

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: 255
Joined: Tue Jul 19, 2011 10:37 pm

Re: Normal Vector for lighting.

Postby Benjamin100 on Sun Mar 16, 2014 5:08 pm

OK, seems the problem was with my uniform binding.
Some sort of mistake with the matrix I was binding.
All working fine now.
Benjamin100
ES Beta Backer
ES Beta Backer
 
Posts: 255
Joined: Tue Jul 19, 2011 10:37 pm

Re: [SOLVED] Normal Vector for lighting.

Postby qpHalcy0n on Sun Mar 16, 2014 9:56 pm

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].
qpHalcy0n
Respected Programmer
Respected Programmer
 
Posts: 388
Joined: Fri Dec 19, 2008 5:33 pm
Location: Dallas

Re: Normal Vector for lighting.

Postby killall_sigsegv on Tue Apr 22, 2014 9:40 am

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

Re: Normal Vector for lighting.

Postby ph0sph0ruz on Tue Apr 22, 2014 7:19 pm

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.
User avatar
ph0sph0ruz
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 39
Joined: Sat Mar 22, 2014 4:52 am

Re: [SOLVED] Normal Vector for lighting.

Postby Benjamin100 on Sun May 11, 2014 4:22 pm

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

Re: [SOLVED] Normal Vector for lighting.

Postby dandymcgee on Mon May 12, 2014 5:45 pm

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:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire


Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron