PDA

View Full Version : 2D texture mapping in openGL and vc++(source code)



rashmii
04-15-2009, 11:04 AM
can anyone please help me in mapping a texture on a 2D object (to be more specific on a simple figure like a square)in openGL and vc++.I need a complete source code for it.Its really urgent....I will be really thankful if somebody does send me one

lobbel
04-16-2009, 08:49 AM
Hello,

first you have to load a texture. Wether by the auxilery lib or with LoadImage



CFile File;
ULONGLONG Length = 0;
GLuint tempID = 0;


Length = File.GetLength();
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, Filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);

if(hBmp == NULL)
{
return FALSE;
}

BITMAP BM; // Get bitmap info.
GetObject (hBmp,sizeof (BM),&BM); //

if (BM.bmBitsPixel != 24) // 24Bit-Bitmap?
{
return FALSE;
}

glGenTextures(1,&tempID);
glBindTexture(GL_TEXTURE_2D, tempID);
//glPixelStorei(GL_UNPACK_ALIGNMENT,4);
//glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
//glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
//glPixelStorei(GL_UNPACK_SKIP_PIXELS,0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0 , GL_RGB,BM.bmWidth,BM.bmHeight,0,GL_RGB,GL_UNSIGNED _BYTE,BM.bmBits);
glBindTexture(GL_TEXTURE_2D, 0);

// bind texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,tempID);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);

glTexCoord2f(1.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);

glTexCoord2f(1.0f,1.0f);
glVertex3f(1.0f,1.0f,0.0f);

glTexCoord2f(0.0f,1.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);

dletozeun
04-16-2009, 09:49 AM
Useful link (http://www.opengl.org/wiki/Texture_Mapping)