## Drawing a Sphere (OpenGL ES compatible)

Discuss programming topics that involve the OpenGL API.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

### Drawing a Sphere (OpenGL ES compatible)

Creates a vertex array with sphere vertex and texture coordinates with glu stuffs.

Code: Select all

``````const float degreeIncrement			= 10;		// 10 degrees between
const int	sphereVertexCount		= (180 / degreeIncrement) * (360 / degreeIncrement) * 4;

fbool isSpherePopulated = False;

GLfloat sphereVertex3f   [sphereVertexCount][3];
GLfloat sphereTexCoord2f [sphereVertexCount][2];

// Based on http://www.swiftless.com/tutorials/opengl/sphere.html
// Overhauled and streamlined.

const float M_PI_Divided_By_180 = M_PI/180;

void zCreateSphere (void)
{
if (isSpherePopulated == False)
{
int vertNum = 0;

for (float z = 0; z <= 180 - degreeIncrement; z += degreeIncrement) // Iterate through height of sphere of Z
for (float c = 0; c <= 360 - degreeIncrement; c += degreeIncrement) // Each point of the circle X, Y of "C"
{
sphereVertex3f   [vertNum][0] = sinf( (c) * M_PI_Divided_By_180 ) * sinf( (z) * M_PI_Divided_By_180 );
sphereVertex3f   [vertNum][1] = cosf( (c) * M_PI_Divided_By_180 ) * sinf( (z) * M_PI_Divided_By_180 );
sphereVertex3f   [vertNum][2] = cosf( (z) * M_PI_Divided_By_180 );
sphereTexCoord2f [vertNum][0] = (c)     / 360;
sphereTexCoord2f [vertNum][1] = (2 * z) / 360;
vertNum ++; // ^ Top Left

sphereVertex3f	 [vertNum][0] = sinf( (c) * M_PI_Divided_By_180 ) * sinf( (z + degreeIncrement) * M_PI_Divided_By_180 );
sphereVertex3f	 [vertNum][1] = cosf( (c) * M_PI_Divided_By_180 ) * sinf( (z + degreeIncrement) * M_PI_Divided_By_180 );
sphereVertex3f	 [vertNum][2] = cosf( (z + degreeIncrement) * M_PI_Divided_By_180 );
sphereTexCoord2f [vertNum][0] = (c)               / 360;
sphereTexCoord2f [vertNum][1] = (2 * (z + degreeIncrement)) / 360;
vertNum ++; // ^ Top Right

sphereVertex3f	 [vertNum][0] = sinf( (c + degreeIncrement) * M_PI_Divided_By_180 ) * sinf( (z) * M_PI_Divided_By_180 );
sphereVertex3f	 [vertNum][1] = cosf( (c + degreeIncrement) * M_PI_Divided_By_180 ) * sinf( (z) * M_PI_Divided_By_180 );
sphereVertex3f	 [vertNum][2] = cosf( (z) * M_PI_Divided_By_180 );
sphereTexCoord2f [vertNum][0] = (c + degreeIncrement) / 360;
sphereTexCoord2f [vertNum][1] = (2 * z)     / 360;

vertNum ++; // ^ Bottom Left

sphereVertex3f	 [vertNum][0] = sinf( (c + degreeIncrement) * M_PI_Divided_By_180 ) * sinf( (z + degreeIncrement) * M_PI_Divided_By_180 );
sphereVertex3f	 [vertNum][1] = cosf( (c + degreeIncrement) * M_PI_Divided_By_180 ) * sinf( (z + degreeIncrement) * M_PI_Divided_By_180 );
sphereVertex3f	 [vertNum][2] = cosf( (z + degreeIncrement) * M_PI_Divided_By_180 );

sphereTexCoord2f [vertNum][0] = (c + degreeIncrement)       / 360;
sphereTexCoord2f [vertNum][1] = (2 * (z + degreeIncrement)) / 360;

vertNum ++; // ^ Bottom Right
}

isSpherePopulated = True;
}

}

void GL_ModelRenderSphere_TexScale (const int glTextureSlot, const vec3_t repeatcount /*ignore this for now*/)
{
glDisable (GL_CULL_FACE);

zCreateSphere (); // Makes our sphere if it isn't made

{
glEnable  				(GL_TEXTURE_2D);
glBindTexture			(GL_TEXTURE_2D, TexSlot);

// size (Specifies  the  number  of  coordinates per array element), type (GL_FLOAT, etc), stride, pointer
glTexCoordPointer		(2, GL_FLOAT, 0, sphereTexCoord2f);
glEnableClientState		(GL_TEXTURE_COORD_ARRAY);

glVertexPointer			(3, GL_FLOAT, 0, sphereVertex3f);
glEnableClientState		(GL_VERTEX_ARRAY);

glDrawArrays			(GL_TRIANGLE_STRIP, 0, sphereVertexCount);

glDisableClientState	(GL_VERTEX_ARRAY);
glDisableClientState	(GL_TEXTURE_COORD_ARRAY);
}
}``````
The night is young. How else can I annoy the world before sunsrise? Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..