programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

Fun with normals and trig values in JOGL

Ranch Hand
Posts: 815
• Number of slices to send:
Optional 'thank-you' note:
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?

Wanderer
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
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 ]

Nick George
Ranch Hand
Posts: 815
• Number of slices to send:
Optional 'thank-you' note:
Thanks Jim... I assure you I've flipped which one's negative more times than... 3. Well, at least I know the problem is with my understanding now.

Nick George
Ranch Hand
Posts: 815
• Number of slices to send:
Optional 'thank-you' note:
Ha! I had instantaneously dismissed your magnitude suggestion as irrelevant in this situation. Sure enough, that's what did the trick.

Much Thanks.