Pro OpenGL ES for iOS

(singke) #1

100 CHAPTER 4: Turning On the Lights^


// Color data

GLubyte *cPtr = m_ColorData =
(GLubyte*)malloc(sizeof(GLubyte) * 4 * ((m_Slices*2+2) * (m_Stacks)));

// Normal pointers for lighting.

GLfloat *nPtr = m_NormalData = //1
(GLfloat*)malloc(sizeof(GLfloat) * 3 * ((m_Slices*2+2) *
(m_Stacks)));
unsigned int phiIdx, thetaIdx;

// Latitude

for(phiIdx=0; phiIdx < m_Stacks; phiIdx++)
{
// Starts at -1.57 and goes up to +1.57 radians.

// The first circle.

float phi0 = M_PI * ((float)(phiIdx+0) * (1.0/(float)(m_Stacks)) - 0.5);

// The next, or second one.

float phi1 = M_PI * ((float)(phiIdx+1) * (1.0/(float)(m_Stacks)) - 0.5);
float cosPhi0 = cos(phi0);
float sinPhi0 = sin(phi0);
float cosPhi1 = cos(phi1);
float sinPhi1 = sin(phi1);

float cosTheta, sinTheta;

// Longitude

for(thetaIdx=0; thetaIdx < m_Slices; thetaIdx++)
{
// Increment along the longitude circle each "slice."

float theta = 2.0*M_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, etc.
Free download pdf