I am drawing a sphere using quads. I plot an extra vertex, just to divide the quad into 2 triangles. So it goes like this:
1 ----> 2
| |
| |
4 ----> 3
but after 3 I plot 1 again. So imagine an extra line from 3-->1.
I am now trying to calculate each vertex' normal. Here is my code:
//calculate normals
for (no_vertice=0; no_vertice<12887; no_vertice+=1)
{
//getting the sphere's vertices
x=sphere_vertices[no_vertice].position[0];
y=sphere_vertices[no_vertice].position[1];
z=sphere_vertices[no_vertice].position[2];
//normalising vector "norm(Vertex - Center)"
magnitude = sqrt((x*x) + (y*y) + (z*z));
sphere_vertices[no_vertice].normal[0] = (x/magnitude);
sphere_vertices[no_vertice].normal[1] = (y/magnitude);
sphere_vertices[no_vertice].normal[2] = (z/magnitude);
printf("Normal at vertice %d = X:%f, Y:%f, Z:%f. \n", no_vertice, sphere_vertices[no_vertice].normal[0], sphere_vertices[no_vertice].normal[1], sphere_vertices[no_vertice].normal[2]);
}
I am calculating the magnitude for each vertex, and then dividing each component of that vertex with the magnitude so I get a unit vector. The problem is that I get a lot of zero vectors. that is vertices with x=0, y=0, z=0... When I pass the normal to the vertex shader,
//my vertex structure
struct Vertex {
GLdouble position[3];
GLfloat color[3];
GLdouble normal[3];
};
....
..
.
/* Enable attribute index 2 as being used */
glEnableVertexAttribArray ( 2 );
glVertexAttribPointer ( ( GLuint ) 2, 3, GL_FLOAT, GL_FALSE, sizeof ( struct Vertex ), ( const GLvoid* )
offsetof(struct Vertex, normal) );
...
..
.
//pass the normal to vertex shader
glBindAttribLocation(shaderprogram, 2, "in_Normal");
and do my light calculation I get all weird kind of effects.
Am I doing anything wrong?
The most confusing part is I am asked to do this:
"For the sphere, work out the surface normal direction and augment your wire-frame drawing with short lines representing the normal direction of each vertex The sphere should now appear to be a hedge hog."
"Note: The surface normal is the unit vector at right angles to the surface patch, assuming it is flat."
So is it basically the normal to a vertex, or to the quad surface that I have to draw? I am confused because it says,
"work out the surface normal direction"
and then
"drawing with short lines representing the normal direction of each vertex"
So where the lines should be drawn??? on the vertex? or in the middle of the quad? Thanks
EDIT: Vertex Calculation
for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
for (phi=0;phi<=360-dphi;phi+=dphi) {
//calculating Vertex 1
x = cos(theta*DTOR) * cos(phi*DTOR);
y = cos(theta*DTOR) * sin(phi*DTOR);
z = sin(theta*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
//calculating Vertex 2
x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
z = sin((theta+dtheta)*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
//calculating Vertex 3
x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
z = sin((theta+dtheta)*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
//adding Vertex_1 again to divide the Quad into 2 triangles
//calculating Vertex 1
x = cos(theta*DTOR) * cos(phi*DTOR);
y = cos(theta*DTOR) * sin(phi*DTOR);
z = sin(theta*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
if (theta > -90 && theta < 90) {
//calculating Vertex 4
x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
z = sin(theta*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
}
}
}