Skip to main content
gamedev.net
🔒 Locked

Texture problem in 2D

Started by Andybean9 11 replies 650+ views
Original Post
Andybean9
Andybean9
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!
Andybean9
Andybean9
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);
Andybean9
Andybean9
Yeah, I did the glEnable!
Trienco
Trienco
then theres my personal favorite question to ask: at what point in your program are you creating/binding your texture?
f@dzhttp://festini.device-zero.de
Andybean9
Andybean9
first, I call a load texture function, which loads it and binds it, then I call the draw function, which is the code you see.
Andybean9
Andybean9
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.
Brother Bob
Brother Bob
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.
Andybean9
Andybean9
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
Brother Bob
Brother Bob
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.
Andybean9
Andybean9
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?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.