PDA

View Full Version : loading textures and applying to cylinder



pmr2004pmr
12-13-2004, 04:28 AM
I am using textures for the first time. I am trying to load 6 different textures and apply them to some cylinders.

The textures I am using are bitmaps. Does anyone have any sample code that will load bitmaps or apply a bitmap file to a cylinder.

Thanks

zukko
12-13-2004, 11:14 AM
read nehe\'s opengl tutorial (http://nehe.gamedev.net) on how to load textures, and how to texture map

Silkut
12-13-2004, 11:26 AM
Hi, that's quite easy, you know

you can check the NeHe lesson about that (http://nehe.gamedev.net)

in example you can do that:

AUX_RGBImageRec *LoadBMP(char *Filename) // Load a BMP
{
FILE *File=NULL; // create a file Handle

if(!Filename) // Verify if the file was given
{
return NULL; // if not, quits
}

File=fopen(Filename,"r"); // Verify the BMP existence

if(File)
{
fclose(File); // close the file handle created
return auxDIBImageLoad(Filename); // load Bmp and return a pointer
}

return NULL;
}

int LoadGLTexture() // load BMP and convert texture
{
int Status=FALSE;

AUX_RGBImageRec *TextureImage[4];// image record, store width,height and data

memset(TextureImage,0,sizeof(void *) *4);// image record cleared now (make sure it's empty)


if((TextureImage[0]=LoadBMP("myTexture_1.bmp")) && (TextureImage[1]=LoadBMP("myTexture_2.bmp")) && (TextureImage[2]=LoadBMP("myTexture_3.bmp")) && (TextureImage[3]=LoadBMP("myTexture_4.bmp")))
{
Status=TRUE;
glGenTextures(4, &texture[0]);// Create the texture
for (loop=0; loop<4; loop++)
{
glBindTexture(GL_TEXTURE_2D, texture[loop]); // texture generation using data
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // linear filtering (GL_NEAREST for slowest Cpu/Gfxcard)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, TextureImage[loop]->sizeX,TextureImage[loop]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data); // generate the texture
}
}


/*******************************************#
* ---Free()--- *
* Il ne faut surtout pas oublier de liberer *
* l'espace utilisť, donc les deux bmp et *
* aussi Textureimage[loop] qui la stockait *
********************************************/
for (loop=0; loop<4; loop++)
{
if(TextureImage[loop])
{
if(TextureImage[loop]->data)
{
free(TextureImage[loop]->data);
}
free(TextureImage[loop]);
}
}
return Status;
}

int InitGL(GLvoid) // Setup needed by OpenGL
{
if(!LoadGLTexture())
{
return FALSE;
}
BuildFont(); // our fonts builded here

glShadeModel(GL_SMOOTH);// Smooth shading enabled
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Set background as black
glClearDepth(1.0f); // Setup of Depth buffer
// Depth buffer test enabled
glDepthFunc(GL_LEQUAL);

glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glBindTexture(GL_TEXTURE_2D, texture[0]);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Correction for Perspective view(nice view, bad performance)

quadratic=gluNewQuadric();
gluQuadricNormals(quadratic, GLU_SMOOTH);
gluQuadricTexture(quadratic, GL_TRUE);


return TRUE; // In this case all Inits are OK, if they're not it return FALSE then the program quits
}

int DrawGLScene(GLvoid)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the Scene and the Depth buffer


glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glHint(GL_POLYGON_SMOOTH,GL_NICEST);
glBindTexture(GL_TEXTURE_2D, texture[2]);

glTranslatef(0.6f,0.57f,-10.0f); // tube position
glRotatef(xrot, 0.0f,0.0f,1.0f);
gluCylinder(quadratic,1.0f,1.0f,10.0f,64,64);

return TRUE; // In this case all operations are OK, if they're not it return FALSE then the program quits

} ok, this is not a perfect method, and when you compile that it doesn't work, and this is quite slow and and and...
hey, cool guys :D

anyway my method is developped from NeHe's method (which is developped from MR. OpenGL method i guess ;) )

I use it on a little OpenGL example
http://navilinux.free.fr/download/prod/sampleGL.zip

hope that helps juste a little :p

pmr2004pmr
12-14-2004, 03:38 AM
Thanks,

Once I have loaded the textures how can I apply them to a cylinder so that they wrap around.

I am using a function that draws a number of cylinders...

gluCylinder(quadric, 0.1, 0.1, (_3_depth[i])/3, 12, 1);

thanks

Silkut
12-14-2004, 12:27 PM
glBindTexture allows you to apply texture on the cylinder, but it apply texture on all the cylinder surface ! be careful :)

pmr2004pmr
12-14-2004, 01:12 PM
ok so I have:

glBindTexture(GL_TEXTURE_2D, texture[0]);

and gluCylinder(quadric, 0.1, 0.1, (_3_depth[i])/3, 12, 1);

which draws my cylinder... do i need to specify the coordinates for the texture or are they auto generated? is there anything else i need?

Thanks