Archived

This topic is now archived and is closed to further replies.

Andybean9

Texture problem in 2D

Recommended Posts

I am trying to make an application and part of it makes an object of any size, it''s square. I want to texture it with whatever texture is specified, but it doesn''t seem to be working, here is some code: glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex2i(x, 600); glTexCoord2f(1.0, 0.0); glVertex2i(width, 600); glTexCoord2f(0.0, 1.0); glVertex2i(width, 600-height); glTexCoord2f(1.0, 1.0); glVertex2i(x, 600-height); glEnd(); This is after the texture image is loaded and bound and everthing. All''s I get is a white filled in square! Any help would be appreciated!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
First, texture coordinates are given in the range 0-1, not in pixels. 0 means bottom/left and 1 means top/right.

Second, you need to enable the texture unit, bind the texture, upload the image, set the texture environment appropriately, and then perhaps you''ll see a texture.

Share this post


Link to post
Share on other sites
I think I already did that. Here is more of my code:
First, before I do that, I initialize with this:

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);

Share this post


Link to post
Share on other sites
I think I sort of found the problem, but i don''t get it. My texture object unsigned int texture, is defined in a class. In that class I have a function to load the texture:

BOOL Object::loadTexture(char* fileName)
{
LoadBitmapFile("menutext.bmp", &bitmapInfoHeader);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
return TRUE;
}

this function is called, and then the funtion to draw the object is called:

glBegin(GL_QUADS);
for(int i = 0; i < 640; i+=64)
{
for(int j = 0; j <480; j+=64)
{
glTexCoord2f(0.0, 0.0); glVertex2i(i, j);
glTexCoord2f(1.0, 0.0); glVertex2i(i+64, j);
glTexCoord2f(1.0, 1.0); glVertex2i(i+64, j+64);
glTexCoord2f(0.0, 1.0); glVertex2i(i, j+64);
}
}
glEnd();

I did some experementing and if I don''t call the Object class''s loadTexture function and put this code right before I draw the object, it works, here is the new code I did:

image = LoadBitmapFile("menutext.bmp", &bitmapInfoHeader);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);


glBegin(GL_QUADS);
for(int i = 0; i < 640; i+=64)
{
for(int j = 0; j <480; j+=64)
{
glTexCoord2f(0.0, 0.0); glVertex2i(i, j);
glTexCoord2f(1.0, 0.0); glVertex2i(i+64, j);
glTexCoord2f(1.0, 1.0); glVertex2i(i+64, j+64);
glTexCoord2f(0.0, 1.0); glVertex2i(i, j+64);
}
}
glEnd();

when I did a step through debug, I noticed in the first function, the texture object doesn''t become 1 like it''s supposed to, it stays garbage, but the new one does change it to a 1. That''s all great, but I don''t want to load the texture everytime I draw, and I can'' figure out why it is doing this!! Also, the object is a derived class of Object.

Share this post


Link to post
Share on other sites
Every function in OpenGL needs a rendering context (RC), which is usually created at the time you create your window. If you try to load the textures before you have this RC, it will fail. So make sure you have an RC before calling any OpenGL function.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think the answer to your problem is in the way you did the working code. In the working code you show the LoadBitmapFile function putting a return value into a variable called image.

image = LoadBitmapFile("menutext.bmp", &bitmapInfoHeader);

I am assuming this function does return something. In that case, in the method of your class the LoadBitmapFile function is not putting its return value into any variable which means that it is ending the method by returning something.

Try this:

BOOL Object::loadTexture(char* fileName)
{
// these lines assume that the return value is bool. you can substitute the correct return value and to the correct testing here instead.
bool x = LoadBitmapFile("menutext.bmp", &bitmapInfoHeader);

if(x == false)
return false;

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);

return TRUE;
}

Hope that helps.

Share this post


Link to post
Share on other sites
Brother Bob: I do have a RC because the textures load just fine, the one way I have the code.

The bool thing didn''t work either, so I still have no freakin'' clue what''s goin'' on! It just doesn''t make sense to me that one function would work in a base class, but not in a derived class

Share this post


Link to post
Share on other sites
Not only do you need a rendering context, you need to craete is before you upload the texture. You said that
quote:

the texture object doesn''t become 1 like it''s supposed to, it stays garbage, but the new one does change it to a 1


, which makes me believe you create the RC and upload the texture in the wrong order. I assume texture object is the textire ID generated by glGenTextures, and the ID is unchanged (garbage in, garbage out) if you don''t have a RC. The second code works because there you upload the texture at the time you draw your object, which means you DO have an RC at that time.

If this is not the problem, post the whole code of a minimal program demonstrating the problem.

Share this post


Link to post
Share on other sites
Man! I feel like an idiot, that was the problem! Anyway, thanks for your help everyone! Well, one more question, if I have a textured quad and I want it to look rounded and smooth, how would I do that?

Share this post


Link to post
Share on other sites