been messing with OpenGL and SDL for a pair of weeks.
The thing is quite weird. I have been loading a texture from a BMP and using a really easy shader to make it work, and so far it has worked very well.
Now, i've refractored my code and made a heightmap loader, with is pretty cool and works nice. I have a ResourceManager class which successfully loads shaders but fails at loading textures.
The structure of that class is simple:
- Constructor (empty)
- Destructor (empty)
- AddTexture (const char* FileName, const char* indexName)
- GetTexture (const char* indexName)
- typedef map TextureMap
- typedef TextureMap::iterator TextureIt
- TextureMap Textures
I tried using SDL Load BMP function and SDL_img Load IMG function. The first one makes this weird result:
When the real image is a simple BMP in 32bit format, as always:
And the second method, using SDL_image, simply doesn't show the image.
The 'AddTexture' function:
bool TEXTURE::TextureManager::AddTexture(const char* fileName, const char* indexName)
{
SDL_Surface* img = SDL_LoadBMP(fileName);
unsigned int id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, img->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SDL_FreeSurface(img);
Textures[indexName] = id;
return true;
}
And the 'AddTexture' SDL_image version:
GLuint id = 0;
SDL_Surface* Surface = IMG_Load(fileName);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
int Mode = NULL;
if(Surface->format->BytesPerPixel == 4)
Mode = GL_RGBA;
else
Mode = GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
std::cout << "The ID of the tex is " << id << std::endl;
Textures[indexName] = id;
return true;
I create the texture here:
Res->TexManager->AddTextureAlt("res/sum.bmp", "Brick");
And the following code is used to bind the texture:
GLuint TEX = Res->TexManager->GetTexture("Brick");
glBindTexture(GL_TEXTURE_2D, TEX);
The normals and tex coords are alright, i've been using it all the time and it was displaying the tex and light perfectly well before.
I can't find the problem. I just can't. Is someone able to help a poor noob?