TyphoonLabs OpenGL ES tutorials
Tutorial 3: Texturing
This tutorial will cover one of the most important features of every 3D
API: texturing. Without it, our applications won’t look realistic, because we
would have to use smoothed colors for all things, and a lot of triangles. This is
not really a good solution, so this is why texturing was ‘invented’.
After a brief theoretical introduction about texture objects, texture
creation, and texture mapping, we are going to write a simple program that
loads a texture from file and maps it over a mesh (the same cube of the tutorial
2). Then we will learn how to use a new capacity: multitexturing. Multitexture
means, apply two or more textures over the same triangle at the same time,
combining them in different manners. Let’s start.
First we’ve removed the handling code for the clicks management, at
WndProc function. We won’t change from ortho to perspective anymore.
We need a function that takes a path to a texture image and load it as an
OpenGL ES texture. We will load TGA textures, but only those ones that
has 24 bits and are uncompressed.
In the render.h file, this is new function:
bool LoadTexture(const char *fileName, GLuint *id); //function
to load 24-bit uncompressed TGA textures
Let’s talk about this new function for a while. This function will receive a texture
filename and a pointer to a texture identifier. This identifier will be initialized
from inside the function (if proceed). This function will return true if the texture
was successfully loaded, otherwise will return false.
bool LoadTexture(const char *fileName, GLuint *id)
{
FILE *f = fopen(fileName, "rb");
GLubyte *pixels = NULL;
if(!f) return false;
WORD width = 0, height = 0;
byte headerLength = 0;
byte imageType = 0;
byte bits = 0;
int format= 0;
int lineWidth = 0;
fread(&headerLength, sizeof(byte), 1, f);
//skip next byte
fseek(f,1,SEEK_CUR);
//read in the imageType (RLE, RGB, etc...)
fread(&imageType, sizeof(byte), 1, f);
//skip information we don't care about
fseek(f, 9, SEEK_CUR);
Real Time Technologies
TyphoonLabs www.typhoonlabs.com typhoonlabs@typhoonlabs.com
- 1
- 2
- 3
前往页