Game Development Reference
In-Depth Information
7. Now, it's time to create OpenGL context. Drawing triangles in OpenGL is easier
than drawing squares because triangles are always coplanar, that is, all of the
points in the shape are on the same plane. So, to draw a square, we first draw two
triangles that share an edge.
8. To inform OpenGL about where the vertices are, we use a structure for vertices
and for making an array. This will later show on GLView as follows:
typedef struct {
GLKVector3 position;
} Vertex;
const Vertex SquareVertices[] = {
{-1, -1 , 0},// vertex 0: bottom left
{1, -1 , 0}, // vertex 1: bottom right
{1, 1 , 0}, // vertex 2: top right
{-1, 1 , 0}, // vertex 4: top left
};
9. Now we need to define which triangle uses which vertices. In OpenGL, we do
this by numbering each vertex, and then describing triangles by giving OpenGL
three numbers at a time:
const GLubyte SquareTriangles[] = {
0, 1, 2, // BL->BR->TR
2, 3, 0 // TR->TL->BL
};
In this case, Glubyte is the type in which the first triangle uses vertices 0 , 1 ,
and 2 , and the second triangle uses vertices 2 , 3 , and 0 . Note that both triangles
use vertices 0 and 2 . This means that they share an edge, which means that there
won't be any gap between the two triangles.
10. Both the SquareVertices and SquareTriangles arrays need to be stored
in a buffer so that OpenGL can use them for rendering as follows:
@interface ViewController () {
GLuint _vertexBuffer;
GLuint _indexBuffer;
GLKBaseEffect* _squareEffect;
}
@end
Search WWH ::




Custom Search