Texturing with TGA images

ok so I have a 3D environment and the camera motion works and you can move around freely. I am now trying to texture some primatives, and I ran into this problem: The texture I loaded onto a quad does not appear in the correct colour.

I have a function that draws some lines across the floor to represent the ground:

void DrawFloor()
{
PushMatrix;
color3f(1,0,0)
BEGIN (LINES)
glVertex(0,0,-1)
etc…
END
PopMatrix;
}

Then I Load my Texture. This is already loaded in a TGA image loader. The load function loads the specified fril from the hard drive, which is pre loaded into the Structures within the TGA loading function. I have used an unsigned int TexturOne, to refer to when calling GenTextures(). But I dont think it works.

Render()
{

STGA tgaFile;
if (loadTGA("font.tga", tgaFile))
{
   unsigned int TextureOne;
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	glGenTextures(1, &TextureOne);

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

Now here is the problem: When I use the code above, the texture is gone and the quad is the same colour as the lines I drew earlier.
IF I comment out the Color3f() function, the texture appears in its true colours but the lines are the same colour as the main area of the texture!

IF I then add:
glBindTexture(GL_TEXTURE_2D,TextureOne);
to the Render() function, they both turn an eggshell colour!

Am I setting up textures correctly? I tried this method in a similar project which didnt have any camera manipulations. I loaded two seperate textures onto quads but once i try to bind the texture, it goes eggshell again!

I know its a lot to read but i really am stuck and would really welcome some help.
Cheers

it don’t work because you missed some concepts about textures.
in opengl (i think i became core with version >= 1.1) there is the concept of “texture object” and “current texture”: you can have as may textures you want, but only one of these is current.
this is a simplification since there are also multitextures, but the concept can be considered still valid.

you made:

unsigned int TextureOne;
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	glGenTextures(1, &TextureOne);
glTexImage2D( ... );

what you actually told opengl is:

  • set texture object 0 (current by default) mag filter
  • set texture object 0 min filter
  • generate one new texture object (call it 1)
  • upload pixels into texture object 0

wich is not good. you forgot the glBindTexture call, wich makes the texture object current.
instead, you should:

unsigned int TextureOne;
glGenTextures(1, &TextureOne);
glBindTexture( TextureOne );
glTexImage2D( ... );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	

wich translates to:

  • generate one new texture object (call it 1)
  • make texture object 1 current
  • upload pixels into texture object 1
  • set texture object 1 mag filter
  • set texture object 1 min filter

generally, in your application, after loading the texture in system ram you must:

  • glGenTextures
  • glBindTexture

then you can use in any order

  • glTexImage…
  • glEnv…
  • other texure functions

and you can consider your texture ready.
then, after, you make current the texture with glBindTexture, and you can start issuing render ing commands.

Thats great info mate thank a lot. Iv been up for about 2 days doing that. So the order really matters then? My openGL book didnt mention that(nice one Astle!)

Thanks anyway you saved me lots of time. It seems to be working now, as for the colour problem i called Color3f again and cleared it to white. I forgot it stays the one colour until its changed again.

Cheers

Actually that colour trick doesnt work. The textures display fine but the lines are still the same overall colour of the texture i load.

Any ideas???
thanks

hm…i’m a bit surprised that the texture is displayed correctly, since you are using GL_RGB in glTexImage2D, but in a tga-file the first color component of each pixel is blue, not red.

anyway, before drawing your lines you should call glDisable(GL_TEXTURE_2D).

Yeh i have a function which re-arranges the order of the colour bits. Anyway i tried disabling textures before drawing my lines, but then i couldnt set the colour of the lines they were just white. Not sure what it is. If i diable lighting it seems to work but is that gonna affect my scene someway?
cheers

you can at any time enable or disable light or textures. you can disable light & textures, draw the lines, enable light & textures, and draw the quads.

btw- you don’t have to reorder the bytes of the tga image. just use GL_BGR instead of GL_RGB in the glTexture2D call.

Cheers for the help, but am gonna bug you some more.

I have this done this to load more than one texture…
I have a room and am texturing the roof, floor, and walls. I will have a function for the walls using the same texture. And a seperate function for the roof and floor. Here is my process for each texture.

STGA tgaFile;
if (loadTGA("Data/walls.tga", tgaFile))
{
   unsigned int TextureOne;
   glGenTextures(1, &TextureOne);	
   glBindTexture(GL_TEXTURE_2D,TextureOne);
	
   glTexImage2D(pointing to the struct);
}
glBegin()
{
    text coords + vertex data
}
glEnd()

Thats the one for the walls and the floor looks like this:

STGA tgaFile;
if (loadTGA("Data/floor.tga", tgaFile))
{
   unsigned int TextureTwo;
   glGenTextures(1, &TextureTwo);	
   glBindTexture(GL_TEXTURE_2D,TextureTwo);
	
   glTexImage2D(pointing to the struct);
}
glBegin()
{
    text coords + vertex data
}
glEnd()

Now i have tried this and it loads all the textures, but…after a while of moving round the room, the frame rate starts to decrease, then it takes a big hit an my system starts losing memory fast! The only thing I could think of was to comment out the “glGenTextures(1, &TextureX);”
in each function. This seems to remedy the frameRate but I read in my book that you should always always use texture objects. By not calling GenTextures I am not making objects am I?

Long one I know but I hope you can help me. Thanks :slight_smile:

Are you loading the textures every frame? That would explain the poor performance and loss of memory :wink:

You only need to load and submit the texture data to OpenGL once.

Cheers

:slight_smile:

ahh do you mean dont call these functions in the Render() function in my program?

Well, it doesn’t really matter where you load your textures, just don’t load them every frame. You only need to generate your texture id and call glTexImage once for each unique texture, otherwise you’re continually allocating resources to fulfill the subsequent loads.

Does that make sense?

Cheers.

Yeh i think i getcha so would this be how you would load a texture?..

  glGenTextures(1, &TextureTwo);
  glTexImage2D(pointing to the struct);

that ignores the bind function but it does create a texture object am i correct?
cheers mate

The steps that Dmy laid out for you will do very nicely.

If you’re still confused, this chapter has everything you need to get started with texture mapping, including some examples of how and where to load your textures (though they’re generated algorithmically for simplicity, the idea’s the same).
http://www.rush3d.com/reference/opengl-redbook-1.1/chapter09.html

Cheers

:slight_smile: