lycanth.com
Welcome, Guest. Please login or register.
September 10, 2010, 03:35:46 AM

Login with username, password and session length
Search:     Advanced search
Destroying Spammers, one nonsensical user-name at a time.
2309 Posts in 319 Topics by 20 Members
Latest Member: TheWolf
* Home Help Search Calendar Login Register
+  lycanth.com
|-+  Lycanth General Forums
| |-+  Programming
| | |-+  GLut texture loading question
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: [1] Go Down Print
Author Topic: GLut texture loading question  (Read 1624 times)
shelai
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 1


View Profile
« on: October 03, 2006, 03:13:35 PM »

Hi all,

pls help me, i am a new to both MFC and OpenGL.

If i created a OpenGL MFC application same as the tutorial link,
http://www.kencocomputers.com/tutorials/

how do i load bitmap into the triangle?
Pls tell me as detail as possible because i am very new to this.

Thanks in advance.
Logged
deiussum
God
Administrator
Hero Member
*****

Karma: +1/-0
Offline Offline

Posts: 686



View Profile
« Reply #1 on: October 04, 2006, 12:08:53 PM »

Wow.  It's been a long time since I've answered any OpenGL questions.  All of this is off the top of my head, so I may be forgetting something, but here are the basic steps for using textures.

1. Enable texture mapping
This is done using:
Code:
glEnable(GL_TEXTURE);

This only needs to be done once, usually right after OpenGL is initialized.

2. Create a texture handle
You can either use hardcoded texture handles, or use glGenTextures to generate an unused one.  Using glGenTextures is usually best in apps where you have many textures.  If you only need to generate 1 texture, use it something like so:

Code:
int nTextureID;
glGenTextures(1, &nTextureID);

If you want to generate many textures handles at a time, you can put them into an array like so:

Code:
int vTextureIDs[100];
glGenTextures(100, vTextureIDs);

3. Bind the texture
Before you do anything with your texture (configure it, load data into it, use it), you need to bind it.  You do that by using glBindTexture with the texture ID you got in step 2.

Code:
glBindTexture(GL_TEXTURE_2D, nTextureID);

4. Configure the texture parameters
There are a number of parameters you can configure with regard to textures wrapping and what sort of resizing algorithms to use.  A typical set of configurations is something like the following:

Code:
glTexParameteri(GL_TEXTURE_MIN_FILTER, GL_LINEAR); // need to change this if you use mipmapping
glTexParameteri(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexparameteri(GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexparameteri(GL_TEXTURE_WRAP_T, GL_REPEAT);

If you look at the documentation for glTexParameteri you can find out what other parameters there are and values they can be set to.

5. Load the image
There are a number of ways to load the image data.  OpenGL itself doesn't have any functions for opening image format files like BMP, TGA, etc.  Since your subject mentions glut, I'll assume you want to use the glut functions.  Unfortunately, I don't think glut even has a BMP loading function.  I'm not sure if you are using standard glut or OpenGLUT, but it looks like the latter has at least proposed a function called glutReadImageFile.  There is also a glAux function for loading BMP files called auxDIBImageLoad, but it only works on Windows, and glAux is even less supported than the old glut and requires you to have the glaux DLLs.  (I'm not even sure if XP still ships with those and that function doesn't even show up in the latest version of MSDN I have for VS 2005.)

Anyway, I'll leave getting the actual image data up to you.  My suggestion would be to use TGA files and find one of the many examples of getting the image data from a TGA.  It is a pretty simple file format, and supports an alpha channel.

One more thing needs to be said regarding your textures.  They need have dimensions that are a power of 2.  (e.g. 2,4,8,16,32,64,128,256,512,etc.)  The width and height do not need to be the same, but they both need to be a power of 2. (e.g. 512x256, 1024x1024, etc.)  If you use mipmaps and gluBuild2DMipMaps you don't have to worry quite so much about the size as it will resize each mipmap to be a power of 2 for you.  Mipmapping is probably overkill for the simple texturing of a quad or triangle example, though.

Once you have your image data into an array of bytes, you can tell OpenGL to use that data with glTexImage2D:

Code:
glTexImage2D(
    GL_TEXTURE_2D,
    0, // mipmap level.  0 is base image
    GL_RGB8, // The format of the data as you want OpenGL to store it
    width, // width of your image
    height, // height of your image
    0, // border
    GL_RGB8, // The format of the data in your array, note this can be different from the internal format above
    GL_BYTE, // The data type your image is stored in
    pImageData // The char* holding your image data
);
   

6. Using the texture
Now that you have all of that out of the way, you have to actually use your image.  To do that, when you specify vertices, you first have to specify texture coordinates.  A texture coordinate is an X/Y value usually from 0.0 to 1.0, with 0 being one edge of your texture and 1.0 being the other side of your texture.  As an example, I'll show you how you would use them for a quad.

Code:
// Not necessarily needed when only using 1 texture, but good practice for when you have multiple textures loaded.
glBindTexture(GL_TEXTURE_2D, nTextureID); 

glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0); // Tex-coord must come before vertex position
glVertex2f(-1.0, -1.0);

glTexCoord2f(1.0, 0.0);
glVertex2f(1.0, -1.0);

glTexCoord2f(1.0, 1.0);
glVertex2f(1.0, 1.0);

glTexCoord2f(0.0, 1.0);
glTexture2f(-1.0, 1.0);

glEnd();

There can be a lot more to it, but that's the quick run through of one of the simplest usages of textures.  If you have any other questions, there are some good tutorials at NeHe Productions.  There are also some good forums at OpenGL.org.  Those ones are a lot more active than these.
Logged

leathrewulfe
Are You There God? It's Me, Margaret
Administrator
Hero Member
*****

Karma: +0/-0
Offline Offline

Posts: 593


COBRA!


View Profile
« Reply #2 on: October 17, 2006, 04:25:09 PM »

If that doesn't work, try:

Code:
texture.ApplyToMesh(true);

 Grin
Logged

Pages: [1] Go Up Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!