Using textures

I am using the following code to use a texture on a quad and I am having no luck. Any suggestions would be greatly appreciated!! Thanks.

TexBits = LoadDIBitmap(“REDBRICK.bmp”, &TexInfo);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TexInfo->bmiHeader.biWidth,
TexInfo->bmiHeader.biHeight, 0, GL_BGR_EXT,
GL_UNSIGNED_BYTE, TexBits);

glEnable(GL_TEXTURE_2D);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);

 glTexCoord2f(0.0,0.0);
 glVertex2f(0.0,0.0);
 glTexCoord2f(0.0,1.0);
 glVertex2f(0.0, 1.0);
 glTexCoord2f(1.0,1.0);
 glVertex2f(1.0, 1.0);
 glTexCoord2f(1.0,0.0);
 glVertex2f(1.0, 0.0);

glEnd();

SwapBuffers(hdc);

needs to work like this:::

///do this once , do not do this every frame
glEnable(GL_TEXTURE_2D);

glGenTextures(1, &texture[0]); //handle to the texture
glBindTexture(GL_TEXTURE_2D, texture[0]); //must bind it so we are using it

TexBits = LoadDIBitmap(“REDBRICK.bmp”, &TexInfo);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TexInfo->bmiHeader.biWidth,
TexInfo->bmiHeader.biHeight, 0, GL_BGR_EXT,
GL_UNSIGNED_BYTE, TexBits);

glBindTexture(GL_TEXTURE_2D,0); //bind to nothing so we dont use it when we dont want to

DeleteObject(TexBits); //since the bitmap is on video ram, we dont need it

/// ok this is going to happen every frame

while(…){ //this stuff goes in our animation loop

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, texture[0]); //must bind it so we are using it

glBegin(GL_QUADS);

glTexCoord2f(0.0,0.0);
glVertex2f(0.0,0.0);
glTexCoord2f(0.0,1.0);
glVertex2f(0.0, 1.0);
glTexCoord2f(1.0,1.0);
glVertex2f(1.0, 1.0);
glTexCoord2f(1.0,0.0);
glVertex2f(1.0, 0.0);
glEnd();

glBindTexture(GL_TEXTURE_2D,0); //bind to nothing so we dont use it when we dont want to

SwapBuffers(hdc);

} // end loop

ps:

make sure you make a call to glDeleteTextures(1, &texture[0]);

at the end of your opengl window so you dont have a memory leak
remeber, if you make something you also must free it up
goes fro drawing list, vertex buffer objects, bitmaps, etc etc

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.