# Fun with normals and trig values in JOGL

Nick George

Ranch Hand

Posts: 815

posted 12 years ago

alright... let a 'hoop' be a cylinder without the two end circles

my normals aren't quite working out here... perhaps it would be easiest to post my code and how I got the normals...

(the hoop is facing the front as drawn, the height running along the z axis, rotation being handled elsewhere)

Since z is always 0, that's easy. I figure, the plane therefore has a slope of (y1-y2)/(x1-x2), and the normal then has a slope of -(x1-x2)/(y1-y2), the value I am using. However, it ain't a-lightin' right. Where's me mistake?

my normals aren't quite working out here... perhaps it would be easiest to post my code and how I got the normals...

(the hoop is facing the front as drawn, the height running along the z axis, rotation being handled elsewhere)

Since z is always 0, that's easy. I figure, the plane therefore has a slope of (y1-y2)/(x1-x2), and the normal then has a slope of -(x1-x2)/(y1-y2), the value I am using. However, it ain't a-lightin' right. Where's me mistake?

I've heard it takes forever to grow a woman from the ground

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 12 years ago

Well, your logic sounds right to me, and the code looks good. However I'm not really familiar with OpenGL (or whatever library you're using here). I gave up on trying to find a good online API that had any actual description of what the methods actually do - is there one? But just as a guess, you might try replacing

gl.glNormal3d(y2-y1,-(x2-x1),0);

with

gl.glNormal3d(-(y2-y1),x2-x1,0);

I'm guessing that the purpose of the normal is to specify which side of the surface is solid, and which is open to air. So you'd need to choose between

(y2-y1, x1-x2, 0)

which is a vector pointing towards the center of the cylinder, and

(y1-y2, x2-x1, 0)

which is a vector pointing away from the center. I'm guessing the second is what you want.

Other than that - is the magnitude of the vector used for anything? Does it make any difference if you put in something like

(0.1*(y1-y2), 0.1*(x2-x1), 0)

or

(10*(y1-y2), 10*(x2-x1), 0)

? I'm guessing the answer is no, but if I'm wrong, it would be good to get an understanding of what effect the magnitude is supposed to have.

An unrelated comment - after you get this working basically the way you want, it may be worthwhile to revisit the calculations to see if there aren't simple ways to reduce the number of calculations you're making (assuming that performance is an issue at all, whihc may or may not be the case). In particular it seems that you're doing twice as many trig calls as you need. Each time you go through the loop (save the first), you shouldn't need to calculate a new x1 and y1 - they are the same as x2 and y2 from the previous iteration, right? Plus, many of the other calculations are being performed multiple times, unnecessarily. The multiplications and divisions are probably minor compared to sin() and cos(), which in turn may be minor compared to the time it takes OpenGL to render a cylinder graphically. But I think it's good to get in the habit of not performing more computations than you need to.

[ October 29, 2004: Message edited by: Jim Yingst ]

gl.glNormal3d(y2-y1,-(x2-x1),0);

with

gl.glNormal3d(-(y2-y1),x2-x1,0);

I'm guessing that the purpose of the normal is to specify which side of the surface is solid, and which is open to air. So you'd need to choose between

(y2-y1, x1-x2, 0)

which is a vector pointing towards the center of the cylinder, and

(y1-y2, x2-x1, 0)

which is a vector pointing away from the center. I'm guessing the second is what you want.

Other than that - is the magnitude of the vector used for anything? Does it make any difference if you put in something like

(0.1*(y1-y2), 0.1*(x2-x1), 0)

or

(10*(y1-y2), 10*(x2-x1), 0)

? I'm guessing the answer is no, but if I'm wrong, it would be good to get an understanding of what effect the magnitude is supposed to have.

An unrelated comment - after you get this working basically the way you want, it may be worthwhile to revisit the calculations to see if there aren't simple ways to reduce the number of calculations you're making (assuming that performance is an issue at all, whihc may or may not be the case). In particular it seems that you're doing twice as many trig calls as you need. Each time you go through the loop (save the first), you shouldn't need to calculate a new x1 and y1 - they are the same as x2 and y2 from the previous iteration, right? Plus, many of the other calculations are being performed multiple times, unnecessarily. The multiplications and divisions are probably minor compared to sin() and cos(), which in turn may be minor compared to the time it takes OpenGL to render a cylinder graphically. But I think it's good to get in the habit of not performing more computations than you need to.

[ October 29, 2004: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, *Twister*

Nick George

Ranch Hand

Posts: 815