Sign in to follow this  
nightangel

Creating two texture mapped (2 BMP) from lesson 6

Recommended Posts

I'm doing a project that required a techniques of texture mapping. From the tutorial lesson 6 from Nehe, it only texture with 1 Bitmap, how do i insert another 1 inside the lesson 6 with Keyboard control (press T and the picture from Nehe.bmp change to Crate.bmp). Below is what i'm code about 2 texture, but it seem not work. any sugguestion? ----------------------------------------------------------------------------- int Status=FALSE; AUX_RGBImageRec *TextureImage[2]; memset(TextureImage,0,sizeof(void *)*1); TextureImage[0] = LoadBMP("Data/NeHe.bmp"); TextureImage[1] = LoadBMP("Data/Crate.bmp"); glGenTextures(2, &texture[0]); if (TextureImage[0]=LoadBMP("Data/NeHe.bmp")) { Status=TRUE; glBindTexture(GL_TEXTURE_2D, texture[0]); 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, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); } if (TextureImage[1]=LoadBMP("Data/Crate.bmp")) { Status=TRUE; glBindTexture(GL_TEXTURE_2D, texture[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, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data); } for (int t = 0; t < 2; t++) if (TextureImage[t]) { if (TextureImage[t]->data) { free(TextureImage[t]->data); } free(TextureImage[t]); } return Status; ------------------------------------------------------------------------------ And in the int WINAPI WinMain what condition i should include? Example: if (keys['T'] && !tp) { tp=TRUE; //any suggestion? if (//what condition to change the bmp?) { glEnable ???? } else ?? glDisable ???? } if (!keys['T']) { tp=FALSE; } ------------------------------------------------------------------------------ if there i have missing something pls let me know, im appreciate it, Thanks for helps...

Share this post


Link to post
Share on other sites
Hi,
Later in NeHe's tutorials, he introduces a better loading function which loopls through the textures and loads as many textures as you want, just add one line per texture.

The code below loads 4 textures. All you have to do to add a texture is to add a line under the line that loads Data/Intro2.bmp, that loads whatever texture you want, and change the 4 in the lines

AUX_RGBImageRec *TextureImage[4];
glGenTextures(4, &texture[0]);
for(loop=0;loop<4;loop++)
for(loop=0;loop<4;loop++)

To the appropriate number. If you have one texture, then it's 1, two textures, 2, and so on.



int LoadTextures()
{
int Status = FALSE;
AUX_RGBImageRec *TextureImage[4];
memset(TextureImage, 0, sizeof(void *)*4);
if ( (TextureImage[0]=LoadBMP("Data/Earth.bmp")) &&
(TextureImage[1]=LoadBMP("Data/Particle.bmp"))&&
(TextureImage[2]=LoadBMP("Data/Intro1.bmp")) &&
(TextureImage[3]=LoadBMP("Data/Intro2.bmp")))

{
Status=TRUE;

glGenTextures(4, &texture[0]); // Create The Texture
for(loop=0;loop<4;loop++)
{
glBindTexture(GL_TEXTURE_2D, texture[loop]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[loop]->sizeX,
TextureImage[loop]->sizeY, GL_RGB, GL_UNSIGNED_BYTE,TextureImage[loop]->data);
}
for(loop=0;loop<4;loop++)
{
if (TextureImage[loop]) // If Texture Exists
{
if (TextureImage[loop]->data) // If Texture Image Exists
free(TextureImage[loop]->data); // Free The Texture Image Memory
free(TextureImage[loop]); // Free The Image Structure
}
}
}
return Status;
}




Now for your changing code. Define an integer(we'll call it "whichtexture"), that will be the index of the texture to bind. Have pressing the designated button change the integer to another index number. For example...

//Global variable whichtexture
int whichtexture=0;
.........................
.........................
//Right before you draw your textured object
glBindTexture(GL_TEXTURE_2D, texture[whichtexture]);
.........................
.........................
//Now for your changing button code
if(keys['T'])
{
///This will change the index of the texture to bind to the object
//between the two texture indices
if(whichtexture==0)whichtexture=1;
else whichtexture=0;
//This is another way of making it so one button only changes it one time, so
//the next time the program loops around, keys['T'] will be FALSE, and you will
//have to press the button again, or hold it down to change the texture again.
keys['T']=FALSE;
}







Hope this helps...
Levi

Share this post


Link to post
Share on other sites
I had a nice long reply worked out but levjs has just posted exactly what I had [lol]. Nice explanation mate, rating++ for you!

One thing though Kevin, if you're going to be posting large chunks of code, say more than five lines, then you should use source tags, they are explained in this part of the FAQ. [smile].

Share this post


Link to post
Share on other sites
OMG!!! I very appropriate it, it working!!! [grin]
Thanks Thanks levjs, and thanks Darren for ur advicing, using the source lang and /source rite? okok, it's great i have so many things here, thanks for all!

Share this post


Link to post
Share on other sites
Quote:
Original post by nightangel
OMG!!! I very appropriate it, it working!!! [grin]
Thanks Thanks levjs, and thanks Darren for ur advicing, using the source lang and /source rite? okok, it's great i have so many things here, thanks for all!


Glad to help. Glad to see I was right. [smile]


@eSCHEn

Thanks. Sorry I beat you to it. [grin]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this