PDA

View Full Version : how to calculate normals for a circle??



12-11-2002, 12:28 AM
with reference to my earlier posting(http://www.opengl.org/discussion_boards/ubb/Forum2/HTML/010995.html),
1. can anybody tell me how to calculate normals for a circle?
2. Is it possible to move the light position along with view point?
3. I'm trying to create an effect like this - holding a searchlight(spotlight) in hand and moving into a dark tunnel.

I checked nehe's demo on lighting and specular lighting and i tried out in the same way. But there is no effect of lighting. if possible, will you guys tell me what sort of components(global ambient light or ambient and diffuse of LIGHT0 or GL_SPOT_DIRECTION or GL_SPOT_CUTOFF etc.) to get the above mentioned effect.

Many may thanks in advance
pl

known quantity - centre of the circle.

M/\dm/\n
12-11-2002, 04:55 AM
As far as I know circle was flat figure http://www.opengl.org/discussion_boards/ubb/biggrin.gif so the normal is in the same direction(s) (to both sides so you must choose one http://www.opengl.org/discussion_boards/ubb/smile.gif ) as normal from plane holding this circe. You can get the normals mathematicaly by using 3 points of cirle, creating 2 vectors from them & calculating cross product from those vectors http://www.opengl.org/discussion_boards/ubb/smile.gif

Gavin
12-11-2002, 05:29 AM
or do you mean normals extruding outward from the centre?

satan
12-11-2002, 05:50 AM
Originally posted by Gavin:
or do you mean normals extruding outward from the centre?

He wants the normals pointing inside for lighting a cylinder on the inside.
I tried to tell him how to do this in his original thread and I have no idea why he started this one.
Perhaps he does not like my answers http://www.opengl.org/discussion_boards/ubb/frown.gif


[This message has been edited by satan (edited 12-11-2002).]

Deiussum
12-11-2002, 06:30 AM
I think satan already gave you this basic formula, but calculating the normal for any given point on a circle is easy. You obviously know the center, and you need to know the point on the circle you need to calculate the normal for, or else how do you know what to pass to glVertex3f?

Anyway, say that vert is the vertex on the circle, and center is the center. You calculate the normals something like so:

This will calculate normals pointing in, to calculate normals pointing out, you simply reverse the order you subtract.

norm.x = center.x - vert.x;
norm.y = center.y - vert.y;
norm.z = center.z - vert.z;

Then you set the normal to a lenght of 1 by doing something like so:

len = sqrt(norm.x*norm.x + norm.y*norm.y + norm.z*norm.z);

norm.x /= len;
norm.y /= len;
norm.z /= len;

satan
12-11-2002, 06:53 AM
Nearly, I explained it for a circle at the origin and told him that the center is just an offset to this.
To complicated???
Perhaps I should not think that people starting OGL know vector math http://www.opengl.org/discussion_boards/ubb/wink.gif

12-16-2002, 04:23 PM
once again special apologies to SATAN.
since my pc crashed i couldn't respond.

I started this thread to ask the following question
2. Is it possible to move the light position along with view point?
but i mixed up and typed everthing.

sorry guys.

Actually i'm confused about how to specify normals because already i specified the normals in the way you guys said. But the lighting effect was different one.

Here is my coding for lighting and draw functions

lighting

{
GLfloat globalambient[]={0.830,0.830,0.830,1.0},
diffuse0[]={0.5,0.5,0.5,1.0},
ambient0[]={0.30,0.30,0.30,1.0}, shine=5.0,
speclight[]={0.3,0.3,0.3},
pos0[]={camera aim(x),camera aim(y),camera aim(z),1.0};

glLightModelfv(GL_LIGHT_MODEL_AMBIENT,globalambien t);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,1.0);
//glLightfv(GL_LIGHT0,GL_AMBIENT,ambi0);
glLightfv(GL_LIGHT0,GL_POSITION,pos0);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diff0);

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,speclig ht);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,&shine);
}


draw

{
int INCR=1,i;
double ax,ay,az,bx,by,bz,cx,cy,cz,dx,dy,dz;
float veclen=0,veclen2=0;
for (i=0;i<360;i=i+INCR)
{
ax=x1+r1*cos(i*pi/180);
ay=y1+r1*(sin(i*pi/180))*(cos(ang1*pi/180));
az=z1+r1*(sin(i*pi/180))*(sin(ang1*pi/180));

bx=x1+r1*cos((i+INCR)*pi/180);
by=y1+r1*(sin((i+INCR)*pi/180))*(cos(ang1*pi/180));
bz=z1+r1*(sin((i+INCR)*pi/180))*(sin(ang1*pi/180));

cx=x2+r2*cos((i+INCR)*pi/180);
cy=y2+r2*(sin((i+INCR)*pi/180))*(cos(ang2*pi/180));
cz=z2+r2*(sin((i+INCR)*pi/180))*(sin(ang2*pi/180));

dx=x2+r2*cos(i*pi/180);
dy=y2+r2*(sin(i*pi/180))*(cos(ang2*pi/180));
dz=z2+r2*(sin(i*pi/180))*(sin(ang2*pi/180));

veclen=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
glNormal3f((x2-x1)/veclen,(y2-y1)/veclen,(z2-z1)/veclen);
//veclen=sqrt(pow((ax-x1),2)+pow((ay-y1),2)+pow((az-z1),2));
//glNormal3f((ax-x1)/veclen,(ay-y1)/veclen,(az-z1)/veclen);
//veclen2=sqrt(pow((dx-x2),2)+pow((dy-y2),2)+pow((dz-z2),2));
//glNormal3f((dx-x2)/veclen,(dy-y2)/veclen,(dz-z2)/veclen);

glBegin(GL_POLYGON);
glVertex3f(ax,ay,az);
glVertex3f(bx,by,bz);
glVertex3f(cx,cy,cz);
glVertex3f(dx,dy,dz);
glEnd();
}
}

x1,y1,z1-centre of circle1
x2,y2,z2-centre of circle2
ang1,ang2 - angle of the plane which holds the circles with respect to world cooordinates
r1,r2-radius of circles

method of drawing the tunnel
using centre points & radius, vertices of circles are calculated and they are joined by polygons

now my problem I can't get the lighting effect with shades and shininess of the surface


Hope everybody understood what my problem is.

pl

zeckensack
12-17-2002, 12:30 AM
1)Be extra careful if you seperate lighting setup from rendering. If you update your modelview/projection matrices, you may need to update your light position. Depends on what you want to accomplish.

Submitting a light position once and keeping it through matrix updates kind of makes your light 'attached to the camera'.

If you want your light position to be fixed relative to the scene, you'll have to respecify the light position each time after updating matrices, and before actually rendering the geometry.

2)My wild guess regarding your problem: Your sphere is centered around the origin. Your light is positioned at the origin. All normals point to the origin. Result? All surfaces get the exact same light intensity, thus no shades and lighting looks as if it doesn't work.

Try moving your light position around and see if it's getting better. You may also need to experiment with the light's falloff parameters, but try changing position first.

12-17-2002, 08:33 AM
Actually i'm moving my light along with camera position.

the object is a dark tunnel.

the effect i want is
.....
3. I'm trying to create an effect like this - holding a searchlight(spotlight) in hand and moving into a dark tunnel.

normals point away from light position

Deiussum
12-17-2002, 10:45 AM
I haven't taken a close look at your code yet, but I don't think you don't want all the normals to point away from your light source. If I understand correctly what you want, you want them all to point IN towards the center of the cylinder.

Terrible ASCII art example viewing cylinder along edge:




X------X-----X-----X
| | | |
v v v v
---L---------------- <- center path through cylinder
^ ^ ^ ^
| | | |
X------X-----X-----X



The Xs above represent the location of vertices on the top and bottom of the cylinder, the L represents the location of your Light, the center line being the path the light is moving. the lines coming off the Xs are the direction you want the normals pointing. Note they are pointing in TOWARDS THE CENTER LINE, NOT THE LIGHT.

Hope that helps somewhat.

Edit: Ugh. I hate how the code tags sometimes choose to use different font sizes in different spots.

[This message has been edited by Deiussum (edited 12-17-2002).]