# [Solved]Polygon Normal calculation

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

EDIT THE PROBLEM IS SOLVED I UPDATED THE CODE WITH CORRECT CODE FOR FUTURE REFERENCE TY I seem to have a bit of a problem with normals. I "borrowed" a piece of code from some website todo the trick for me but for some reason its ****** up. Normal Calculation function:
void calculatenormal(float v1[3],float v2[3],float v3[3],float &normalx, float &normaly,float &normalz){

float d1[3] = { v2[0] - v1[0],   v2[1] - v1[1],   v2[2] - v1[2] };
float d2[3] = { v3[0] - v1[0],   v3[1] - v1[1],   v3[2] - v1[2] };

float cross[3];
cross[0] = d1[1] * d2[2]   -   d1[2] * d2[1];
cross[1] = d1[2] * d2[0]   -   d1[0] * d2[2];
cross[2] = d1[0] * d2[1]   -   d1[1] * d2[0];
float dist;
dist = 1/sqrt( (cross[0]*cross[0]) + (cross[1]*cross[1]) + (cross[2]*cross[2]));

normalx = -cross[0] * dist;
normaly = -cross[1] * dist;
normalz = -cross[2] * dist;


And the call to it:
..........

glBegin(GL_TRIANGLE_STRIP);
float v1[3]={0,1,0};
float v2[3]={1,0,0};
float v3[3]={1,0,1};

float normal[3];
calculatenormal(v1,v2,v3,normal[0],normal[1],normal[2]);

glNormal3fv(normal);
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v3);

glEnd();

........

I'd appreciate the help. [Edited by - Aidamina on June 30, 2005 4:30:20 PM]

##### Share on other sites
Instead of calculating v3 - v2, do v3 - v1 like you are doing v2 - v1.

Also, refrain from using pow() functions for simple x*x + y*y + z*z math.
You could also invert your sqrt right there (divide 1 by it) so that instead of 3 divs, you have 3 muls.

##### Share on other sites
Changed it, but it still doesnt work (Updated code in post)

##### Share on other sites
What exactly is the problem? Are you able to post a screenshot? (can use a free image hosting service like imageshack)

There are a number of reasons, even down to the point.. did you enable lighting and set up a light properly?

A screenshot or a detailed explanation would be the most helpful -- rather than just "this doesn't work. why?"

##### Share on other sites
OK ill post a screenshot

http://img282.imageshack.us/img282/3659/normal5xt.jpg

glBegin(GL_LINES);float start[3]={(v1[0]+v2[0]+v3[0])/3,(v1[1]+v2[1]+v3[1])/3,(v1[2]+v2[2]+v3[2])/3};float stop[3]={start[0]+normal[0],start[1]+normal[1],start[2]+normal[2]};glVertex3fv(start);glVertex3fv(stop);	glEnd();

##### Share on other sites
this may be a redundant question but do you have any lights in the scene? are they enabled? what are their lighting values?

...this usually screws me up

##### Share on other sites
What do the values of the light setup have todo with the normal calculations. I need some sample codes to calculate normals from 3 vertices or a well documented link.

##### Share on other sites
...

normal[0] = cross[0] * dist;
normal[1] = cross[1] * dist;
normal[1] = cross[2] * dist;

...

look at this line...

normal[1] = cross[2] * dist;

normal index 1!?!?!?!?!?!

i don't think so!

##### Share on other sites
calculatenormal(float v1[3],float v2[3],float v3[3]){    float normal[3];    ....    return normal;}

This wont work, you are returning a pointer to data that is erased when the function exits.

change:
float  calculatenormal(float v1[3],float v2[3],float v3[3])

to:
void calculatenormal(float v1[3],float v2[3],float v3[3], float n[3])

Then instead of saying: normal = calculatenormal(v1,v2,v3)
Do: calculatenormal(v1,v2,v3,normal)

or consider using structures to hold your data.

struct vec3{    float x, y, z;}

##### Share on other sites
Arghhhhh....

I've been staring myself blind on my code for hours and then its a **** typo :(

Thx a lot for your time every1 that looked

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633297
• Total Posts
3011251
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!