Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
107 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Preface
 Uploading Textures
 Using the Texture
 Conclusion

 Printable version

 


Uploading Textures

So you have some raw RGB image data in a buffer and you want to apply it to your geometry in OpenGL? The first thing you have to do before OpenGL can use this raw texture data is upload it to the video memory. Once a texture is uploaded to the video memory it can be used throughout the time in which your application is running. Before a texture can be uploaded to the video memory there is some setup that must take place so OpenGL knows what to do with the image data that is passed to it. Below I will outline the order that certain calls need to be made in order to upload your texture. Note that these calls should be made once per texture when the application is started. Please check the pseudo code below for a better idea.

glBindTexture

The first thing that must take place in the process of uploading the texture is a call to glBindTexture. What glBindTexture does is it tells OpenGL which texture "id" we will be working with. A texture "id" is just a number that you will use to access your textures. Here is a sample call.
glBindTexture(GL_TEXTURE_2D, 13);
This call will make texture that is associated with the ID of 13 the active texture. Any calls that have to do with OpenGL texture mapping will effect this texture. It is important that you remember this number since it will be needed again later on to actually apply the texture to geometry.
glPixelStorei
The glPixelStorei call tells OpenGL how the data that is going to be uploaded is aligned. A call to glPixelSrotei is shown below.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
This call tells OpenGL that the pixel data which is going to be passed to it is aligned in byte order, this means that the data has one byte for each component, one for red, green and blue. Stick to the call above unless you have some sort of exotic data which I highly doubt. The alignment of data will probably change as you advance into OpenGL texture mapping.
glTexParameteri
The glTexParameteri sets the various parameters for the current OpenGL texture. The parameters that are passed and their effects on the texture are an advanced topic. If you would like to further research what each call does please take a look at the links provided at the end of this document. Each of these lines are important so make sure to get each in your application.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Why did I leave out a description of what these do if they are so important? The properties that you can change with these calls are important to someone who would be more advanced. For someone who is just learning they should be seen as the "voodoo" that makes things work. Once you have a solid understanding of how texture mapping works in OpenGL then you should take a look at these properties.
glTexEnvf
The glTexEnvf call sets environment variables for the current texture. What this does is tell OpenGL how the texture will act when it is rendered into a scene. Below is a sample call which I use in my applications.
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
What this does is sets the active texture to GL_MODULATE. The GL_MODULATE attribute allows you to apply effects such as lighting and coloring to your texture. If you do not want lighting and coloring to effect your texture and you would like to display the texture unchanged when coloring is applied replace GL_MODULATE with GL_DECAL.
glTexImage2D
The glTexImage2D call is our goal. This call will upload the texture to the video memory where it will be ready for us to use in our programs. I am going to explain this call parameter by parameter since it is so important to what we are doing.
  • target - The target of this call, it will always be GL_TEXTURE_2D.
  • level - The level of detail number, this should be left at 0 for our purposes. Once you become more adept at OpenGL texture mapping this parameter will be something that you might change.
  • internalformat - Internal components parameter. This tells OpenGL how many color components to represent internally from the texture that is uploaded. There are many symbolic constants for this parameter but the one which is most widely used is GL_RGB; this constant is equal to 3.
  • width & height - The width and height of the image data. These must be integers that are equal to 2n+2(border) for some integer n. What this basically means is that the texture width and height must be a power of two (2,4,8,16,32,63,128,256,512, etc).
  • border - Image border, must be 0 or 1. I always use 0 in my code since I do not use image borders.
  • format - Format of the pixel data that will be uploaded. There are many constants which are accepted but GL_RGB is the value that is widely used.
  • type - Type of data that will be uploaded. Again there are several symbolic constants but the one which I use is GL_UNSIGNED_BYTE.
  • pixels - Pointer to the image data. This is the image data that will be uploaded to the video memory. Note that after your call to glTexImage2D you can free this memory since the texture is already uploaded into video memory.
Now that you know the parameters for glTexImage2D here is a sample call:

glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);

Thats all folks! After you have done everything above the texture will be uploaded and ready to be applied to your geometry. The next section will discuss the application of the texutre to geometry. If this does not seem clear to you look at the links section at the end of this document for a texture mapping example.


Next : Using the Texture