OpenGL texturing

////////////
// global //
GLuint tex; // texture object name
int width=100, height=100; // image size (100x100)
unsigned char* bitmapData; // image data

/////////////////////
// pre-step (once) //
// memory allocation on system memory (RGBA per pixel)
bitmapData = new unsigned char[width*height*4];
// loading image data from a file
LoadBitmapFromFile( bitmapData, "testImage.bmp" );
// 2D texturing activation
glEnable( GL_TEXTURE_2D );
// texture object creationg
glGenTextures( 1, &tex );
// set to the current texture
glBindTexture( GL_TEXTURE_2D, tex );
// filter when the display size is smaller than the size of the image
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
// filter when the display size is larger than the size of the image
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// other filter setting
// ...
 // memory allocation & texture data loading on GPU memory
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmapData );

//////////////////////////
// drawing (per frame ) //
glBegin( GL_QUADS );
 glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -0.5f, 0.5f, 0.5f );
 glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 0.5f, 0.5f, 0.5f );
 glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 0.5f, 0.5f, -0.5f );
 glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -0.5f, 0.5f, -0.5f );
glEnd();

//////////////////////
// post-step (once) //
glDeleteTextures( 1, &tex );
delete[] bitmapData;

답글 남기기