Pro OpenGL ES for iOS

(singke) #1

CHAPTER 3: Building a 3D World (^79)
//Vertices
GLfloat vPtr = m_VertexData =
(GLfloat
)malloc(sizeof(GLfloat) 3 ((m_Slices2+2) (m_Stacks))); //3
//Color data
GLubyte cPtr = m_ColorData=
(GLubyte
)malloc(sizeof(GLubyte) 4 ((m_Slices 2+2) (m_Stacks))); //4
unsigned int phiIdx, thetaIdx;
//latitude
for(phiIdx=0; phiIdx < m_Stacks; phiIdx++) //5
{
//Starts at -1.57 goes up to +1.57 radians.
//The first circle.
//6
float phi0 = M_PI ((float)(phiIdx+0) (1.0/(float)( m_Stacks)) - 0.5);
//The next, or second one.
//7
float phi1 = M_PI ((float)(phiIdx+1) (1.0/(float)( m_Stacks)) - 0.5);
float cosPhi0 = cos(phi0); //8
float sinPhi0 = sin(phi0);
float cosPhi1 = cos(phi1);
float sinPhi1 = sin(phi1);
float cosTheta, sinTheta;
//longitude
for(thetaIdx=0; thetaIdx < m_Slices; thetaIdx++) //9
{
//Increment along the longitude circle each "slice."
float theta = 2.0M_PI ((float)thetaIdx) * (1.0/(float)( m_Slices -
1));
cosTheta = cos(theta);
sinTheta = sin(theta);
//We're generating a vertical pair of points, such
//as the first point of stack 0 and the first point of stack 1
//above it. This is how TRIANGLE_STRIPS work,
//taking a set of 4 vertices and essentially drawing two triangles
//at a time. The first is v0-v1-v2 and the next is v2-v1-v3, and so on.

Free download pdf