Jump to content
  • Advertisement

Archived

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

Andybean9

Texture problem in 2D

This topic is 5701 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
then theres my personal favorite question to ask: at what point in your program are you creating/binding your texture?

Share this post


Link to post
Share on other sites
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.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!