Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to calculate the normal for a hemisphere.

I draw the hemi-sphere by this code:

for(float phi = 0.0; phi < 1.567; phi += factor) {

        glBegin(GL_QUAD_STRIP);

        for(float theta = 0.0; theta < 2*3.14 + factor; theta += factor) 
        {
            x = rh * sin(phi) * cos(theta);
            z = rh * sin(phi) * sin(theta);
            y = -rh * cos(phi);

            gl::vertex(Vec3f(x, y, z));

            x = rh * sin(phi + factor) * cos(theta);
            z = rh * sin(phi + factor) * sin(theta);
            y = -rh * cos(phi + factor);


            gl::vertex(Vec3f(x, y, z));

        }
        glEnd();
    }
share|improve this question
    
Do you want smooth or hard edges? –  stephelton Apr 17 '12 at 19:57
    
can you tell me in both cases ? –  Moaz Apr 17 '12 at 20:18
    
eBusiness has the right way for smooth edges. Smooth edges would be a bit more difficult (ask if you really care for them...) –  stephelton Apr 17 '12 at 20:45
1  
-1 this shows no research effort. It has nothing to do with being new to OpenGL, this is a basic math question that could easily be found with a little research. –  Byte56 Apr 17 '12 at 20:58

1 Answer 1

up vote 2 down vote accepted

Same way you get the normals of a sphere, the vector from the centre to any surface point is a normal for that surface point. For a lot of uses you probably want them normalized, but that is all there is to it. Just remove the rh factor from your position calculations and you get the normalized normals.

Spheres turn out to be mathematical entities that are incredibly easy to work with.

share|improve this answer
    
But after removing the rh factor, how to set the radius of the sphere?. Should I calculate the normals In both sections of the codes that draw the spheres ie. x = rh * sin(phi) * cos(theta); z = rh * sin(phi) * sin(theta); y = -rh * cos(phi); gl::vertex(Vec3f(x, y, z)); –  Moaz Apr 17 '12 at 20:44
    
@AhmedSaleh You'll have to separate the two calculations, one for the position and one for the normal, the mathematics just happen to be identical except for the position needing the radius factor. –  eBusiness Apr 17 '12 at 20:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.