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();

}

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();

}