Results 1 to 2 of 2

Thread: Applying Texture To Cone Shape

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    1

    Applying Texture To Cone Shape

    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.

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408

    Re: Applying Texture To Cone Shape

    Take a look at gluQuadric tutorial and then the see the code at Post268130. Note, the cross-platform opensource library requirements are glew and DevIL and freeglut .

    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
    Code :
      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
    Code :
    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.

    Code :
    // 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;
    }

Similar Threads

  1. Applying 2 image textures to simple shape object?
    By mobileopengl in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 09-23-2015, 11:44 AM
  2. Replies: 1
    Last Post: 07-24-2014, 04:59 PM
  3. Replies: 7
    Last Post: 01-05-2013, 10:50 AM
  4. fast cone-cone-intersection
    By satan in forum OpenGL: General
    Replies: 2
    Last Post: 02-28-2006, 10:46 AM
  5. Applying border to a cone
    By vivekd in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 10-21-2005, 03:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean