PDA

View Full Version : Shading a sphere in opengl Help please!



Fenix1186
03-19-2009, 08:18 PM
Hey I have a sphere that I created manually and am now trying to calculate the normals so that it shades correctly. I am shining a spotlight on the sphere from the camera's position. The light shines but instead of the sphere looking like a sphere it looks like it has a grid on it. If anyone knows what I might be doing wrong it would be a life saver!

Here is a link to a picture of the light on the sphere:
http://i173.photobucket.com/albums/w80/Tiburon1186/Untitled-1.jpg
And a pic of the light up close on the sphere:
http://i173.photobucket.com/albums/w80/Tiburon1186/Untitled2.jpg

Below is the code for creating the sphere, calculating the normals, and drawing the sphere.

// Calculates the sphere points.
void make_sphere( OBJECT *po, double ray, int rs, int vs ) {
double r,u,t,t_inc,u_inc;
int i,j, level_count;
r = ray;
u_inc = 2*M_PI/rs;
u = -M_PI/2;
for(i=0; i <=rs; i++)
{
t_inc = M_PI/vs;
t=-M_PI/2;
for(j=0; j <=vs; j++)
{
po->vertices[i][j][0] = (r*cos(t)*cos(u));
po->vertices[i][j][1] = (r*sin(t));
po->vertices[i][j][2] = (-(r * cos(t)*sin(u)));
po->vertices[i][j][3] = 1;
t= t+t_inc;
}
u=u+u_inc;

}
Normalize(po, ray); // Calculates Normal vectors
}

// Calculates the normal vectors
void Normalize(OBJECT *po, GLfloat radius)
{
int i,j;
for(i=0; i <= crt_rs; i++)
{
for(j=0; j <= bcrt_vs; j++)
{
po->normals[i][j][0] = po->vertices[i][j][0] / radius;
po->normals[i][j][1] = po->vertices[i][j][1] / radius;
po->normals[i][j][2] = po->vertices[i][j][2] / radius;
po->normals[i][j][3] = po->vertices[i][j][3] / radius;
}
}
}
// Draws the sphere and normals.
void draw_sphere(OBJECT *po, int rs, int vs)
{
int i, j;
glBegin(crt_render_mode);
{
glColor3fv(colors[BLUE]);

for(i=0; i < rs; i++)
{

for(j=0;j < vs;j++)
{
glNormal3f(po->normals[i][j][0],po->normals[i][j][1],po->normals[i][j][2]);
glVertex4f(po->vertices[i][j][0],po->vertices[i][j][1],po->vertices[i][j][2], po->vertices[i][j][3]);
glVertex4f(po->vertices[i+1][j][0],po->vertices[i+1][j][1],po->vertices[i+1][j][2], po->vertices[i+1][j][3]);
glVertex4f(po->vertices[i+1][j+1][0],po->vertices[i+1][j+1][1],po->vertices[i+1][j+1][2], po->vertices[i+1][j+1][3]);
glVertex4f(po->vertices[i][j+1][0],po->vertices[i][j+1][1],po->vertices[i][j+1][2], po->vertices[i][j+1][3]);

}
}

}
glEnd();
}

Mike Werner
03-19-2009, 10:18 PM
Just from your figures, I'm guessing that your quads are messed up. Looks like 1 vertex for every quad is at the center of the sphere. Try generating a simpler sphere made up of a dozen or so quads. And don't generate all of the quads, just generate the first one. I'm betting it will be messed up.

You could also try generating a simpler sphere with your algorithm but rendering it wireframe with no lighting. I think you're going see a lot of lines (quad edges) going from the surface of the sphere into the center. Good luck.

Fenix1186
03-20-2009, 12:08 AM
I generated a wireframe with no lighting and the only abnormality I saw was a single line down the center of the sphere from north pole to south pole. No other criss-crosses or anything like that. Which vertex is at the center? For some reason I haven't been able to find it.

Fenix1186
03-20-2009, 12:20 AM
I also did an output of all the points in the array and none are 0,0,0 (which would be the center of the sphere. I'm not sure how it's getting to the center...?

MaxH
03-20-2009, 09:41 AM
Sounds like your vertices are correct but the poly definitions are messed up. What happens if you try to draw one quad only?

Fenix1186
03-20-2009, 10:21 AM
Drawing only one quad I get an extremely slender pie shaped quad going from the center of where the sphere would be to where the radius would be.

MaxH
03-20-2009, 11:33 AM
Drawing only one quad I get an extremely slender pie shaped quad going from the center of where the sphere would be to where the radius would be. Which is exactly what was brought up in an earlier post. All of your polygons are messed up in the same way. I leave it up to you to debug your code. Looks like you are computing the vertex coordinates correctly, but that something is off in the indexing you are using in the glVertex commands between glBegin and glEnd.