Advertisement

Unable to switch textures

Started by June 26, 2004 03:20 PM
1 comment, last by aaron_ds 20 years, 5 months ago
Hey! I recently decided to encapsulate some things into a TEXTURE class. Really simple, has a GLuint, loads bitmaps, all the regular stuff a texture class should have.
class TEXTURE{
    public:
	TEXTURE();
	TEXTURE(char*);
	~TEXTURE();
	bool loadTexture(char*);
	void applyTexture(void);

	int width;
	int height;
	int bits;
    private:
	GLuint* texid;
};
TEXTURE::TEXTURE():width(0), height(0), bits(0), texid(new GLuint){
}
TEXTURE::TEXTURE(char* fn):width(0), height(0), bits(0), texid(new GLuint){
    loadTexture(fn);
}
void TEXTURE::applyTexture(void){
    glBindTexture(GL_TEXTURE_2D, *texid);
}
bool TEXTURE::loadTexture(char* szFileName){
    HBITMAP hBMP;
    BITMAP	BMP;
    glGenTextures(1, texid);
    hBMP=(HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );
    if (!hBMP){
        MessageBox(NULL, "texture creation failed","TEXTURE ERROR", MB_OK | MB_ICONINFORMATION);
	return false;
    }
    GetObject(hBMP, sizeof(BMP), &BMP);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glBindTexture(GL_TEXTURE_2D, *texid);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D( GL_TEXTURE_2D, 0,  3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits );
    //gluBuild2DMipmaps( GL_TEXTURE_2D, 3, BMP.bmWidth, BMP.bmHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits );
    DeleteObject(hBMP);
    width=BMP.bmWidth;
    height=BMP.bmHeight;
    bits=BMP.bmBitsPixel;
    return true;
}
TEXTURE::~TEXTURE(){
    glDeleteTextures(1, texid);
}
The problem is, that I'm loading up all of these textures during my GUI initialization. Then When I try to draw, I'm locked into the last texture that was loaded. Yeah, you're probably thinking, dumbass, he forgot to add in glBindTexture throughout his code. But I didnt!
glNewList(draw_list,GL_COMPILE_AND_EXECUTE);
	    glBegin(GL_QUADS);
	    glColor4f(1.0, 1.0, 1.0, 1.0);
	    /*One QUAD for each tile in the window*/
	    /*0,1*/
	    /*3,2*/
	    /*TopLeftCorner: TLC is a TEXTURE*/
	    glNormal3f(0, 0, 1);
	    TLC->applyTexture();
	    glTexCoord2d(0, 0);
	    glVertex2f(window.x1,            window.y1 );
	    glTexCoord2d(1, 0);
	    glVertex2f(window.x1+TLC->width, window.y1);
	    glTexCoord2d(1, 1);
	    glVertex2f(window.x1+TLC->width, window.y1+TLC->height);
	    glTexCoord2d(0, 1);
	    glVertex2f(window.x1,            window.y1+TLC->height);
/*Source continues...*/
I'm calling glBindTexture through the applyTexture() Anyone have similar expirences? I've written much more complicated gl code than this, and its worked fine. Maybe someone can pointout a dumb mistake. ;) Thanks!
You can't bind a texture inside a glBegin block, iirc.
Advertisement
Thanks so much. I was trying to get as many vertex calles between the begin and end that I didn't even realize that I couldn't switch textures between them. Thanks again.

This topic is closed to new replies.

Advertisement