PDA

View Full Version : Applying Texture To Cone Shape



glSnake
12-04-2009, 05:40 PM
What's the best way to apply a texture (bmp or raw) to a cone shape? I know it doesn't work with glutSolidCone. Thanks!

Not too sure how else to do it.

marshats
12-04-2009, 08:29 PM
Take a look at gluQuadric tutorial (http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial7.php) and then the see the code at Post268130 (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=268130#Post2681 30). Note, the cross-platform opensource library requirements are glew (http://glew.sourceforge.net/install.html) and DevIL (http://openil.sourceforge.net/about.php) and freeglut (http://freeglut.sourceforge.net/) .

There are just two simple changes you need to do to the Post268130 code ... supply two textures (just change the string names, and they do NOT have to be bitmaps), for instance you could download the opengl_logo (http://www.opengl.org/img/opengl_logo.jpg)


LoadImageDevIL ("Logo.bmp", &logo);
LoadImageDevIL ("Logo2.gif", &logo2);
could become
LoadImageDevIL ("opengl_logo.jpg", &logo);
LoadImageDevIL ("opengl_logo.jpg", &logo2);

and change the cylinder to a cone ie


replace
gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32); // cylinder
with
gluCylinder(IDquadric,10.0f,0.0f,10.0f,32,32); //cone


Why two textures? Well, this is another example showing how to blend two images ie for your question you could simply delete any line that references the "logo2" variable.

Note this code is cross platform but you may have to add a gratuitous "windows.h" or "stdfx.h" for the other MS OS (other than Linux that is:) to be happy.



// on linux with gcc: gcc main.c -lGL -lglut -lIL -lGLEW
// on windows your on your own :)
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

#include <IL/ilut.h>

GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;

struct TextureHandle
{
ILubyte *p;
ILuint id;
ILint w;
ILint h;
ILenum DestFormat;
ILenum DestType;
GLuint genID;

};

struct TextureHandle logo;

ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
{
ilEnable(IL_ORIGIN_SET);

ilOriginFunc(IL_ORIGIN_LOWER_LEFT);

ILuint ImageNameID;
ilGenImages(1, &amp;ImageNameID);
ilBindImage(ImageNameID);
if (!ilLoadImage(szFileName)) return 0;
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

T->id = ImageNameID;
T->p = ilGetData();
T->w = ilGetInteger(IL_IMAGE_WIDTH);
T->h = ilGetInteger(IL_IMAGE_HEIGHT);

T->DestFormat = ilGetInteger(IL_IMAGE_FORMAT);
T->DestType = ilGetInteger(IL_IMAGE_TYPE);

glGenTextures(1, &amp;T->genID);
glBindTexture(GL_TEXTURE_2D, T->genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, T->DestFormat, T->w, T->h, 0, T->DestFormat, T->DestType, T->p);

printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
return 1;
}


void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;

gAngle += dt*360./8.;

glutPostRedisplay();
}


void draw_cone()
{
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(gAngle,1.,0.,1.);
glRotatef(90,0.,1.,0.);
gluCylinder(IDquadric,10.0f,0.0f,10.0f,32,32);
glPopMatrix();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glBindTexture ( GL_TEXTURE_2D, logo.genID);
draw_cone();

glutSwapBuffers();
}


void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed\n" );
}


void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_CULL_FACE);

IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);

GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);

GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);

ilInit();
LoadImageDevIL ("opengl_logo.jpg", &amp;logo);

glEnable (GL_TEXTURE_2D);
}


void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
break;
}
}


int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("Multipass texturing Demo");
glewInit();

glutTimerFunc(0,timer,0);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

init();

glutMainLoop();
return 0;
}