[Solved]Polygon Normal calculation

Started by
8 comments, last by Aidamina 18 years, 9 months ago
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]
-->My Site<--
Advertisement
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.
Changed it, but it still doesnt work (Updated code in post)
-->My Site<--
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?"
OK ill post a screenshot

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

(I added addictional code to render the normal)

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();
-->My Site<--
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
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.

Some please help
-->My Site<--
...

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!
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;}
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
-->My Site<--

This topic is closed to new replies.

Advertisement