Ask your own question, for FREE!
Mathematics 15 Online
OpenStudy (konradzuse):

Question about normal vectors and lighting.

OpenStudy (konradzuse):

Hello all I am designing a 3d application, and one of the things that haS TO D WITH Custom models is "normal vectors." I am a bit confused on how it all works, so I'm curious if someone could help me out. http://mathworld.wolfram.com/NormalVector.html i see on here and on other sites the vectors point outwards, but what are they signifying? The light that shines from it or...?

OpenStudy (anonymous):

wow

OpenStudy (konradzuse):

I am basically looking to create a shadowing effect on the edges of my walls. I'm sure that finding the direction of each of the normals to those edges shouldn't be a big deal, but I'm a bit confused overall... I also see there is a formula, so I'm curious if I would need to use that if I know where each vector should be pointing. Natedog12, please don't spam.

OpenStudy (anonymous):

just to let u know i am in 6 grade

OpenStudy (kinggeorge):

Well the normal vector is just a vector (usually taken to be length 1) that is perpendicular to the surface at the point where it meets the surface.

OpenStudy (kinggeorge):

I don't really know anything about 3d modelling, but I would imagine that you would likely have to use some kind of formula to find the normal vectors.

OpenStudy (konradzuse):

hmm... The only thing I got from another topic I was replying to was this. For lighting to work, it has to know the direction of every point that is rendered. This can be done by interpolating normals between vertexes or by looking up normals from a normal map (which still requires an interpolated vertex normal for proper direction). Your mesh needs a normal vector for each vertex if you want lighting to work properly. A normal vector tells the lighting shader how light should reflect at a given vertex. It seems like the light would flow in the direction I set, but I'm not 100% sure as I'm using a shadow render now :p.

OpenStudy (konradzuse):

technically the edges of my walls are the vertices, but when I try to have the normals flow into the vertices I get a black wall instead of a white wall with black shading... very weird.

OpenStudy (konradzuse):

My brain is like a half melted salami atm, so this might be easier than I'm thinking but Idk.

OpenStudy (kinggeorge):

Hm. I can see how knowing the normal vector would help with reflecting light. Perhaps there's a minus sign that's gone wrong? I really don't know much about how shaders work, so I can't say anything for certain.

OpenStudy (konradzuse):

The example given is "Example: Using Meshes With Lighting.j3md The previous examples used the mesh together with the Unshaded.j3md material. If you want to use the mesh with a Phong illuminated material (such as Lighting.j3md), the mesh must include information about its Normals. (Normal Vectors encode in which direction a mesh polygon is facing, which is important for calculating light and shadow!) float[] normals = new float[12]; normals = new float[]{0,0,1, 0,0,1, 0,0,1, 0,0,1}; mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals)); You need to specify as many normals as the polygon has vertices. For a flat quad, the four normals point in the same direction. In this case, the direction is the Z unit vector (0,0,1), this means our quad is facing the camera. If the mesh is more complex or rounded, calculate cross products of neighbouring vertices to identify normal vectors!" Each quad is "different" facing another direction since they are walls, I tried using just this and it's black. going from 0,0,1 to 0,1,0 and to 1,0,0 yeild almost the same about of "shade black." Really weird Id on't get it :(

OpenStudy (kinggeorge):

Their advice about taking cross products sounds like it might be useful to you. If you know what the edges are (in vector form) then you can take the cross product to get a normal vector. If you just use the vectors 0,0,1 0,1,0 and 1,0,0 your faces can only be facing three different directions.

OpenStudy (konradzuse):

boo cross product.... Basically I guess I should mention these walls are quads connected... 4 vertices each quad. 2 vertices per each "corner/edge." 0,0,1 is what I used for all of them, then switched to 1,0,0 for all of them, then 0,1,0 for all of them... to test them. Granted each of the walls WIL be facing another direction, so each will have differences.

OpenStudy (anonymous):

my grade 8 brain is burning

OpenStudy (kinggeorge):

When you take the cross product of two vectors you'll be able to get a vector that is always normal to the surface formed by those two vectors. So even if you have more than 2 vectors bordering your surface, you can take any two that intersect at a corner, take their cross product, and you'll get a normal vector. If you do it too arbitrarily though, you could get a normal vector in the opposite direction that you want, so you have to take a little care. It might just be that you got unlucky, and every face you looked at with those normal vectors just happened to be facing the wrong direction.

OpenStudy (kinggeorge):

Unfortunately, I need to take off now. I'll check back later tonight.

OpenStudy (konradzuse):

so if I share the top of the corner with 1 vector, and then the bottom shares a corner with the other vector, should I get the cross product of the top 2, and then the cross of the bottom 2?

OpenStudy (konradzuse):

Thanks for the help King G. Hopefully I'll ge to figure this out soon. The cross product idea helped, and hopefully it works out.

OpenStudy (anonymous):

wow

OpenStudy (anonymous):

can u help me

OpenStudy (kinggeorge):

You should only need to do a single cross-product for each face. Just look at a corner of any given face. It'll be formed by two vectors. Take the cross product of those two vectors, and you'll get a normal vector. If you chose a different corner and did the same thing, you would actually get almost the same vector. The only possible difference could be gotten rid of by multiplying by a scalar.

OpenStudy (kinggeorge):

Wikipedia actually has some decent diagrams that help with visualizing cross products. http://en.wikipedia.org/wiki/Cross_product

OpenStudy (konradzuse):

seems like you just got home as well, had to do somethings before replying though. So 1 cross product per each 2 vectors and then use the same value for both? The top, and the bottom are 2 diff ones though?

OpenStudy (kinggeorge):

You don't need to take more than one cross product for every face.

OpenStudy (konradzuse):

Face as in each quad, or each corner?

OpenStudy (konradzuse):

oic so you're saying that in every corner of the face it would be the same cross product basically?

OpenStudy (konradzuse):

Even on the different sides?

OpenStudy (kinggeorge):

I mean face as in quad (I think). Every flat polygon that is part of your surface. So a square would have 6 faces. For each face, choose a corner, and take the cross product of the two vectors forming that corner. But each face should have a different cross product.

OpenStudy (konradzuse):

well uh.. If 2 faces are touching then wouldn't they both have the same normals? This is a video of an error I was having, but this is for you to get an idea of what I'm talking about. http://tinypic.com/r/243kaon/8

OpenStudy (konradzuse):

As for faces normally they would be each side of w/e and yes in a cube/box it would be 6 faces. This however is a bit different.

OpenStudy (kinggeorge):

If two faces are touching they wouldn't necessarily have the same normal vector. For example, the top of a cube, and one of the 4 adjacent sides share an edge, but they would have very different normal vectors. What it almost looks like to me in that video, is that some of your faces seem to have a line through the middle. If that's the case, then those sides may actually have the same normal vector.

OpenStudy (konradzuse):

Oh I was super confused, I kept thinking you were saying the vector at the corner, but you're talking about each side as a vector... Ic.... So it would be the length of each side = the length of the vector?

OpenStudy (konradzuse):

I'm normally thinking of point vectors when doing 3d :P.

OpenStudy (kinggeorge):

Ah. whoops. But yes, I'm considering each edge as a vector.

OpenStudy (konradzuse):

Lets see how this works now, brb, thanks.

OpenStudy (konradzuse):

oh and I just read that last comment about the video, yeah that's the error that appeared without having normals so it all bugged out. I basically want the corners as you see to be black, but now everything is black grrr :P. TOO MUCH SHADOW :O.

OpenStudy (konradzuse):

hmm gotta see if there is an easier way to work the edges... I can get a "distance" from 1 vector point to another, but that's a floating point value, we need vectors to do cross products right?

OpenStudy (konradzuse):

I mean I guess if I know the original vector, and the distance, it's just a matter of using that"distance float" as x y or z then right? I feel like I'm askign such basic questions, but that's what happens when brain is mush :P.

OpenStudy (kinggeorge):

You need the vectors for a cross product. But if you know the two vector points forming an edge, you can easily find the vector that goes between them, and that will be one of the vectors involved int he cross product. And in the case of your construction, since everything seems more or less like a square, your vectors will end up being basically values of x, y, or z.

OpenStudy (konradzuse):

to get the vector we just subtract one from the other right?

OpenStudy (kinggeorge):

Right.

OpenStudy (konradzuse):

now the question is it says Example: Using Meshes With Lighting.j3md The previous examples used the mesh together with the Unshaded.j3md material. If you want to use the mesh with a Phong illuminated material (such as Lighting.j3md), the mesh must include information about its Normals. (Normal Vectors encode in which direction a mesh polygon is facing, which is important for calculating light and shadow!) float[] normals = new float[12]; normals = new float[]{0,0,1, 0,0,1, 0,0,1, 0,0,1}; mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals)); You need to specify as many normals as the polygon has vertices. For a flat quad, the four normals point in the same direction. In this case, the direction is the Z unit vector (0,0,1), this means our quad is facing the camera. If the mesh is more complex or rounded, calculate cross products of neighbouring vertices to identify normal vectors!

OpenStudy (konradzuse):

Getting back to this you were saying that we only need to use the same value all 4 times, or do we need to get all 4?

OpenStudy (kinggeorge):

These seem like slightly different normal vectors than I'm thinking of.

OpenStudy (konradzuse):

It is like you said "If the mesh is more complex or rounded, calculate cross products of neighbouring vertices to identify normal vectors!"

OpenStudy (konradzuse):

ONLY 1 WAY TO FIND OUT LETS TRY IT :)

OpenStudy (kinggeorge):

But assuming your mesh/object is made of flat quads, you should be able to use the same normal for all 4 vertices.

OpenStudy (konradzuse):

yeah that's what is said above actually, ugh brain mega mush sorry haha.

OpenStudy (konradzuse):

hmm so apparently only 1 corner is black.... LOL kind of success? :P.

OpenStudy (kinggeorge):

Do you want them all black?

OpenStudy (konradzuse):

I want the walls to be white normally, and each of the edges to show shadows off of each other.

OpenStudy (kinggeorge):

Hmm. I can get the normal vectors, but as for actually coding the shading, I'm probably not much help :P

OpenStudy (konradzuse):

Well I basically have something enabled called Screen Space Ambient Occlusion. It's supposed to give real world shading based on other objects. in their example all edges were shaded, and that's what I need... I don't know what's up, I basically did my second vector minus the first and then the third minus the first. |dw:1397787083112:dw|

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!