PDA

View Full Version : Try Create Texture



vomtung
05-20-2011, 08:11 AM
Hi all
when I try create the Textute http://www.cityintherain.com/howtotex.html ,I reiceve a white rectangle ,I don'n know what happen with my code, I think I miss something , can you help me. this is my code
void SetTextureFilter(int newfilter)
{
if (newfilter >= 0 &amp;&amp; newfilter <= 1)
{
Filter = newfilter;
}
}


void LoadBitmapTexture(int id)
{
HBITMAP hBmp = NULL;

hBmp = (HBITMAP) ::LoadImage(NULL,
MAKEINTRESOURCE(id), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);

//get info about the bitmap
BITMAP BM;
::GetObject(hBmp, sizeof(BM), &amp;BM);

//tell OpenGL to ignore padding at ends of rows
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

if (Filter ==TF_NONE)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
else
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, BM.bmWidth, BM.bmHeight,
0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BM.bmBits);

DeleteObject((HGDIOBJ) hBmp); //avoid memory leak (Windows)
}
void display()
{
glEnable(GL_TEXTURE_2D);
SetTextureFilter(TF_NONE);
LoadBitmapTexture(IDB_BITMAP1);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//use GL_MODULATE instead of GL_REPLACE if lighting is being used

//draw a square with specified texture coordinates

float xvals[] = {0.0, 0.0, 40.0, 40.0};
float yvals[] = {40.0, 0.0, 0.0, 40.0};

float svals[] = {0, 0, 1, 1};
float tvals[] = {1, 0, 0, 1};

glPolygonMode(GL_FRONT_AND_BACK, GL_POLYGON);
glBegin(GL_POLYGON);
for (int i=0; i < 4; i++)
{
glVertex2f(xvals[i], yvals[i]);
glTexCoord2f(svals[i], tvals[i]);
}
glEnd();
glFlush();
}

int main(int argc, char* argv[]){
........
glutDisplayFunc(display);
.......
}