Building a mini 3D animation game using OpenGL
In this recipe, we will load a texture and apply it to the square. Later, we will make a cube, and finally we will learn how to implement three-dimensional animation by rotating our cube in three-dimensions.
How to do it
Now we will start from the place we have left before and will load all the textures. To load the textures, follow the following steps:
- First, in our vertex structure, we need to include texture coordinate information:
typedef struct { GLKVector3 position; // the location of each vertex in space GLKVector2 textureCoordinates; // the texture coordinates for each vertex } Vertex; const Vertex SquareVertices[] = { {{-1, -1 , 0}, {0,0}}, // bottom left {{1, -1 , 0}, {1,0}}, // bottom right {{1, 1 , 0}, {1,1}}, // top right {{-1, 1 , 0}, {0,1}}, // top left };
- Next, add an image (any) in our project, rename it as
Texture.png
, and then add the following code inviewDidLoad
:NSString* imagePath = [[NSBundle mainBundle] pathForResource:...