Example #1
0
static void GetSphereArrays(GLfloat **vertex_array_p, GLfloat **normal_array_p)
{
    static GLfloat vertex_array[SPHERE_NUM_VERTICES*3];
    static GLfloat normal_array[SPHERE_NUM_VERTICES*3];
    static GLboolean arrays_initialized = 0;

    if (!arrays_initialized)
    {
        GLfloat cos_table[SPHERE_RESOLUTION+1];
        GLfloat sin_table[SPHERE_RESOLUTION+1];
        int theta_index, phi_index;

        // Fill out tables.
        for (theta_index = 0; theta_index <= SPHERE_RESOLUTION; theta_index++)
        {
            cos_table[theta_index]=cosf((2*M_PI/SPHERE_RESOLUTION)*theta_index);
            sin_table[theta_index]=sinf((2*M_PI/SPHERE_RESOLUTION)*theta_index);
        }

        // Compute the vertices and normals.
        GLfloat *vertex_p = vertex_array;
        GLfloat *normal_p = normal_array;
        for (phi_index = 0; phi_index < SPHERE_RESOLUTION/2; phi_index++)
        {
            for (theta_index = 0; theta_index<SPHERE_RESOLUTION; theta_index++)
            {
#define SET_VERT(t_index, p_index) \
    normal_p[0] = cos_table[t_index]*sin_table[p_index]; \
    normal_p[1] = sin_table[t_index]*sin_table[p_index]; \
    normal_p[2] = cos_table[p_index]; \
    vertex_p[0] = normal_p[0]*SPHERE_RADIUS; \
    vertex_p[1] = normal_p[1]*SPHERE_RADIUS; \
    vertex_p[2] = normal_p[2]*SPHERE_RADIUS; \
    normal_p += 3; vertex_p += 3;
                SET_VERT(theta_index, phi_index);
                SET_VERT(theta_index, phi_index+1);
                SET_VERT(theta_index+1, phi_index+1);
                SET_VERT(theta_index+1, phi_index);
            }
        }

        arrays_initialized = 1;
    }

    *vertex_array_p = vertex_array;
    *normal_array_p = normal_array;
}
Example #2
0
void render_quarter(int n)
{
	int	i, j;
	GLfloat	step = 1.0/((GLfloat)n-1);
	GLfloat	x[2];
	GLfloat	y[2];
	GLfloat	normal[2][2][3];
	for(i = 0 ; i < n-1 ; i++)
	{
		for(j = 0 ; j < n-1 ; j++)
		{
			x[0] = step*((GLfloat)i);
			x[1] = step*((GLfloat)(i+1));
			y[0] = step*((GLfloat)j);
			y[1] = step*((GLfloat)(j+1));

			interpolate_normal(x[0],y[0],normal[0][0]);
			interpolate_normal(x[0],y[1],normal[0][1]);
			interpolate_normal(x[1],y[0],normal[1][0]);
			interpolate_normal(x[1],y[1],normal[1][1]);

#define	SET_VERT(ix,iy)				\
	glNormal3fv(normal[ix][iy]);	\
	glVertex3f(x[ix],y[iy],0);
#define	SCALE	.2
			if(flag & FLAG_NORMAL)
			{
				glDisable(GL_LIGHTING);
				glColor3f(1,1,1);
				glBegin(GL_LINES);
					glVertex3f(x[0],y[0],0);	glVertex3f(x[0]+SCALE*normal[0][0][0], y[0]+SCALE*normal[0][0][1], SCALE*normal[0][0][2]);
					glVertex3f(x[0],y[1],0);	glVertex3f(x[0]+SCALE*normal[0][1][0], y[1]+SCALE*normal[0][1][1], SCALE*normal[0][1][2]);
					glVertex3f(x[1],y[0],0);	glVertex3f(x[1]+SCALE*normal[1][0][0], y[0]+SCALE*normal[1][0][1], SCALE*normal[1][0][2]);
					glVertex3f(x[1],y[1],0);	glVertex3f(x[1]+SCALE*normal[1][1][0], y[1]+SCALE*normal[1][1][1], SCALE*normal[1][1][2]);
				glEnd();
				glEnable(GL_LIGHTING);
			}
			glBegin(GL_QUADS);
				SET_VERT(0,0);
				SET_VERT(1,0);
				SET_VERT(1,1);
				SET_VERT(0,1);
			glEnd();
		}
	}
}