PDA

View Full Version : Texture Mapping Help2



Aliorithm
04-09-2005, 03:12 PM
Hi,
Below is my display method with a flat polygon defined before the gluLookAt() command. I cant figure out how to add a simple bitmap from paint to its front surface?? Any Help would be appreciated! Thanks

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(100.0, 1.4, 5.0, 50.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
glTranslatef(0.0, 0.0, -5.0);

//Need to combine bmp texture to this polygon!!!
glBegin(GL_POLYGON);
glVertex2f(8.0, -3.0);
glVertex2f(-8.0, -3.0);
glVertex2f(-8.0, -6.0);
glVertex2f(8.0, -6.0);
glEnd();
glPopMatrix();

gluLookAt(0.0, 5.0, 15.0, 3.0, 0.0, 0.0, 0.0, 1.0, 0.0);
/*glRotatef(angle, 0.0, 1.0, 0.0);*/

glPushMatrix();
lan();
glPopMatrix();

glPushMatrix();
glTranslatef(12.5, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
wan();
glPopMatrix();

glutSwapBuffers();

}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutInitWindowSize(1000,700);
glutInitWindowPosition(12.5,10);
glutCreateWindow("Square");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(update);
glutSpecialFunc(specialKeys);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

ALI

dorbie
04-10-2005, 02:19 AM
Do this, this only works for 24 bit rgb BMP images, and maybe not them all (Microsoft has a real problem designing anything as trivial as an image format without turning it into a baroque disaster), it's kinda old code. This includes the bmp loader, a texture creation code fragment and modifications to your code to make it use the texture. The glBindTexture call isn't strictly required but it will come in handy if you decide to create more than one texture. Note that using the bind call you should take the texture loading out and call it once at the start only to create the textures, with the bind call at load time, then you can use the bind call any time later to bind that texture ready for use.


/* add this code to allow quick and simple BMP loading to an image in memory ready for OpenGL, limited to 24 bit RGB BMPs */

unsigned char *LoadBMP(LPCTSTR fname, long *x, long *y)
{
BYTE *image_data = NULL;

HANDLE hFileHandle;
BITMAPINFO *BitmapInfo = NULL;
unsigned long InfoSize = 0;
unsigned long BitSize = 0;

BITMAPFILEHEADER bitmapHeader;
DWORD dwBytes;

*x = 0;
*y = 0;

/* Open the Bitmap file */
hFileHandle = CreateFile(fname,GENERIC_READ,FILE_SHARE_READ,
NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL) ;

/* make sure file exists */
if(hFileHandle == INVALID_HANDLE_VALUE)
return NULL;

/* read common header data */
ReadFile(hFileHandle,&bitmapHeader,sizeof(BITMAPFI LEHEADER),
&dwBytes,NULL);

if(dwBytes == sizeof(BITMAPFILEHEADER) && bitmapHeader.bfType == 'MB')
{

/* Read in rest of bitmap information structure */
InfoSize = bitmapHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
BitmapInfo = (BITMAPINFO *) malloc( sizeof(BYTE) * InfoSize );

ReadFile(hFileHandle,BitmapInfo,InfoSize,&dwBytes, NULL);

if(dwBytes == InfoSize) /* did we read what we asked for */
{
if( /* check for RGB uncompressed (non RLE) */
BitmapInfo->bmiHeader.biCompression == BI_RGB &&
/* check for 24 bit data */
BitmapInfo->bmiHeader.biBitCount == 24 )
{
*x = BitmapInfo->bmiHeader.biWidth;
*y = BitmapInfo->bmiHeader.biHeight;
/* docs say this may be zero for BI_RGB */
BitSize = BitmapInfo->bmiHeader.biSizeImage;

/* read size for BI_RGB if required,
we know it's a 3 byte per pixel image */
if(BitSize == 0)
BitSize = (*x * *y * 3);

image_data = (BYTE *) malloc(sizeof (BYTE)*BitSize);

/* Read in the bitmap bits */
ReadFile(hFileHandle,image_data,BitSize,&dwBytes,N ULL);

if(BitSize != dwBytes)
{
if(image_data != NULL)
{
free (image_data);
image_data = NULL;
}
}
}
}
}

/* always close file (NULL already returned if file open failed) */
CloseHandle(hFileHandle);

if(BitmapInfo != NULL)
free (BitmapInfo);

return image_data;
}

.
.
.
.
.


/* Call this somewhere at the start once you have a context*/

unsigned char *image;
long x_image, y_image;

image = LoadBMP("MyCoolImage.bmp", &x_image, &y_image);

glBindTexture(GL_TEXTURE_2D, 1);
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, x_image, y_image, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
free(image);

.
.
.
.
.
.
.
.

/* change your polygon drawing code to this */
glBindTexture(GL_TEXTURE_2D, 1);
glEnable(GL_TEXTURE_2D);

glBegin(GL_POLYGON);
glTexCoord2f(1.0, 1.0);
glVertex2f(8.0, -3.0);
glTexCoord2f(0.0, 1.0);
glVertex2f(-8.0, -3.0);
glTexCoord2f(0.0, 0.0);
glVertex2f(-8.0, -6.0);
glTexCoord2f(1.0, 0.0);
glVertex2f(8.0, -6.0);
glEnd();

glDisable(GL_TEXTURE_2D);

dorbie
04-10-2005, 02:21 AM
P.S. you should really check the return value for that loader before using the image pointer to create a texture.

Aliorithm
04-10-2005, 09:02 AM
Dear Dorbie,
You are a star!!!

Thank you very much for all your help!!

Ive tried going through most online tutorials and the red book v1.4 but found all there examples really confusing!!

I shall try your code now and get back to you.

Thanks again
ALI
P.s: Do you know how to play a wav file when a mouse button is pressed down??
I know it should be in some mouse update function but i dont know how to play a wav file!!???
Please help ME!!! PLEASE!!!