Pro OpenGL ES for iOS

(singke) #1

80 CHAPTER 3: Building a 3D World^


//Get x-y-z for the first vertex of stack.

vPtr [0] = m_Scale*cosPhi0 * cosTheta; //10
vPtr [1] = m_Scale*sinPhi0*m_Squash;
vPtr [2] = m_Scale*cosPhi0 * sinTheta;

//The same but for the vertex immediately above the
previous one.

vPtr [3] = m_Scale*cosPhi1 * cosTheta;
vPtr [4] = m_Scale*sinPhi1*m_Squash;
vPtr [5] = m_Scale* cosPhi1 * sinTheta;

cPtr [0] = red; //11
cPtr [1] = 0;
cPtr [2] = blue;
cPtr [4] = red;
cPtr [5] = 0;
cPtr [6] = blue;
cPtr [3] = cPtr[7] = 255;

cPtr += 2*4; //12

vPtr += 2*3;
}

blue+=colorIncrment; //13
red-=colorIncrment;
}
}

return self;
}
Okay, so it takes a lot of code to create something as basic as a sphere. Using the
triangle lists is more involved than using the quads in standard OpenGL, but that’s what
we have to work with.
The basic algorithm divides the sphere into stacks and slices. Stacks are the lateral
pieces while slices are vertical. The boundaries of the stacks are generated two at a time
as partners. These form the boundaries of the triangle strip. So, stack A and B are
calculated and subdivided into triangles based on the number of slices around the
circle. The next time through, take stacks B and C and rinse and repeat. Two boundary
conditions apply:
Free download pdf