• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

## Recommended Posts

HEllo again. I have yet another 3d question, surprise surprise. I wantt o do gourad shading pretty soon. I udnerstand the concept behind it, I understand how to get the vertex normals, but I dont really know HOW to get them. I have an object class set up with _3DV *Vertices (where 3DV has float x, y, z, scrx, scry), and Face *fIndex (where face has int Vert[5], int NumVertices, unsigned int ambient_color) so in order to draw a face I get the vertices like so: Vertices[fIndex[Face#].V[point]].x, where point is any point in the poly from 0 to MaxVertices-1. So you have to get the vertex normals by getting the normals of all the faces that are associated with a vertex. So how exactly would I do that using this setup??? I guess a could do a few nested for loops to figure it out, but that can''t be the way it''s done, it''ll be way to slow. - Rob ByteMe95::~ByteMe95()

##### Share on other sites
To get a face normal of a triangle, you take the cross product 2 vectors, cross(vertex3-vertex1, vertex3-vertex2) just make sure your triangles are defined in a consistent manner (either CW, or CCW). Make sure you normalize this "normal" after you get this cross product.

To get a VERTEX normal of a polygon mesh if you want to simulate a smooth surface (bad for a cube, good for spheres, etc). You take the average of ALL adjacent polygons' face normals..
I think there are different ways of calculating the average, you can just add them all up and normalize it, I think.

Well, it's up to you how you want to organize your data structure to provide adjacency info

Hope this helps

Edited by - jho on July 19, 2000 2:45:39 PM

##### Share on other sites
JHO, thanks but I already knew pretty much everything you werote down.

I know how to find a face normal, I know how to do flat lighting, I KNOW how to do gourad (in theory, havent done it yet), what I dont know is how to find what faces are sharing a vertex to get the vertex normal, USING the data structure I have now as described in my first post. I dont want to rearrange my data structures, so I want to know how it''s done.

Any ideas?

ByteMe95::~ByteMe95()

##### Share on other sites
Assuming the add all, then normalize method is what you want (someone wrote about it above).

Here''s some pseudo code:

Loop through the Vertex array and for each vertex v do   Let n be the vertex normal for v.   n := (0,0,0)   Loop through the Face array and for each face f do      if face f contains a reference to vertex v         n := normal(f) + v      End if.   End loop.   n := normalize(n)End loop.

Well, something like that at least. I wrote this straight out of my head, so quite possibly it contains a number of errors

##### Share on other sites
Guys, a stupid question. What is a normal? As far as I know, cross product of two vectors gives us an orthogonal vector (a vector that''s perpendicular to the other two). Ok, so that''s a normal. Why is it that it''s so important? What can u do with it?

##### Share on other sites
Anonymous: Thanks for the code. Do I have to do that for every frame or could I do that when I initialize and then rotate and translate the vertex normals with the vertices together, when applying transformations??
What is usually done? I was thinking of pre-calculating it and rotating it with the vertices, would that work though?

Kill: I just started 3d, but the norla does seem to be a BIG issue. From what I have seen normals are used for lighting (need the normal of a plane and light vector to get the angle between them which gives you the light intensity), and for backface removal (if a polygon is facing away from the viewer, then you dont have to draw it assuming all your objects are solid).

ByteMe95::~ByteMe95()

##### Share on other sites
nate miller http://nate.scuzzy.net/programming.html
has a very nice demo/tutorial on how to do it

• ### Forum Statistics

• Total Topics
628282
• Total Posts
2981822

• 10
• 10
• 11
• 17
• 15