Jump to content
  • Advertisement
Sign in to follow this  
uvBear

OpenGL Problem with glBindTexture()

This topic is 2480 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

EDIT: I noticed now that nowhere in my code do I make a call to glGenTextures(). I'm guessing this is the problem (I'm pretty new to OpenGL) but I'm unsure how to do so correctly.
EDIT2: Yup. Problem solved. glGenTextures was the issue.. Annoying how I spend hours getting frustrated at this, then find the answer as soon as I ask for help. Oh well, leaving the original question up so you can laugh at my stupidity!


Hello there.
I am writing a small game engine loosely based upon Seoushi's Kore Engine tutorial: [color="#000000"]link
I've managed to get my textures loaded in, but I have a problem. After loading multiple textures, every object I draw uses the last texture that was loaded. I must be missing something.

Here's some code:
struct sTexture
{
GLuint texture[1];
std::string filename;
GLfloat width;
GLfloat height;
};

std::map< uint, sTexture > m_Textures;


uint loadTextureFromFile
{
GLint numColors;
GLenum textureFormat;
sTexture texture;

SDL_Surface* surface;
surface = IMG_Load( filename.c_str() );
if ( surface != NULL )
{
// check its a power of 2
if( ( surface->w & ( surface->w - 1 )) != 0 )
{
std::cout << "Warning: " + filename + "'s width is not a power of 2!\n";
}

if( ( surface->h & ( surface->h - 1 )) != 0 )
{
std::cout << "Warning: " + filename + "'s height is not a power of 2!\n";
}

// get number of channels in SDL surface
numColors = surface->format->BytesPerPixel;
if( numColors == 4 ) // contains alpha channel
{
if( surface->format->Rmask == 0x000000ff )
{
textureFormat = GL_RGBA;
}
else
{
textureFormat = GL_BGRA;
}
}
else if( numColors == 3 )
{
if( surface->format->Rmask == 0x000000ff )
{
textureFormat = GL_RGBA;
}
else
{
textureFormat = GL_BGRA;
}
}
else
{
//incorrect image type
std::cout << "Warning: the image is not truecolor!..\n";
SDL_FreeSurface( surface );
return 0;
}

//fill our sTexture
texture.filename = filename;
texture.width = surface->w;
texture.height = surface->h;

//LINEAR
// bind texture
glBindTexture( GL_TEXTURE_2D, texture.texture[0]);
// set stretching properties
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// edit the texture's image data
glTexImage2D(GL_TEXTURE_2D, 0, numColors, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
}
else
{
//error loading file
std::cout << "SDL could not load " + filename + ": " << SDL_GetError() << "\n";
return 0;
}

// free the SDL surface
if ( surface != NULL)
{
SDL_FreeSurface(surface);
}

uint ID = m_Textures.size() + 1;
m_Textures[ID] = texture;
std::cout << "Texture created with ID of " << ID << "\n";
return ID;

}


void drawTexture( uint textureID, GLfloat x, GLfloat y )
{
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, m_Textures[textureID].texture[0]);

glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(x, y);
glTexCoord2i(0, 1); glVertex2i(x, y + m_Textures[textureID].height);
glTexCoord2i(1, 1); glVertex2i(x + m_Textures[textureID].width, y + m_Textures[textureID].height);
glTexCoord2i(1, 0); glVertex2i(x + m_Textures[textureID].width, y);
glEnd();

}


The code has been slightly simplified (I removed some calls in the draw method that I know to work, to save space). You can see that I'm storing each texture's information in a structure, which I'm mapping to a uint to give it an ID. I'm then using that ID to find the texture I wish to bind.

Please ask if more information regarding the issue is required to form an adequate response. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!