Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: Bind Texture to a sphere?

  1. #11
    Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    407

    Bind Texture ... (update to use glBindtexture)

    Based on another Post267363 I realized that the previously posted code was ineffiecient in sending texture data from the CPU memory to the GPU with each iteration. If it used glBindtexture() the texture could be uploaded to the GPU and hence stop the need for CPU-to-GPU data transfers with each loop iteration. Here's the code for those interested
    Code :
    //******************************************************************************
    //*********************   OpenGL - Bitmap Loading Demo   ***********************
    //*********************        MaxH - May 18, 2009       ***********************
    //*********************      Updated -June 3, 2009       ***********************
    //******************************************************************************
    //*********************      Modified Cross Platform     ***********************
    //*********************    marshats - November 22, 2009  ***********************
    //******************************************************************************
     
    //   This generalized version of the code was posted by 'marshats' on OpenGL
    //   Beginners Forum. It is supposed to run on Mac and Linux platforms as well
    //   as Windows. My original version runs only on Windows platforms because I
    //   use Windows-specific image loading utilities. This version uses the DevIL
    //   library to load images. DevIL handles many different image types including
    //   bmp, png, jpg, tga ... 
     
    //   According to marshats, the command to compile and link this code on a
    //   Linux box is -
    //                 g++ Linux_Version.cpp  -I/usr/include/GL  -lGL -lglut -lIL
     
    //   For info on DevIL Library see:  [url]http://openil.sourceforge.net/about.php[/url]
     
     
    #  ifdef WIN32
    #    include <windows.h>
    #    include <wingdi.h>
    #  endif
     
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    #include <glut.h>
    #include <IL/ilut.h> //http://openil.sourceforge.net/tuts/tut_step/index.htm
     
     
    GLfloat  xrot = 0, yrot = 0, erot = 0, frot = 0;
     
    GLUquadric *earth = 0;
     
    struct TextureHandle {
     ILubyte *p;   /* pointer to image data loaded into CPU memory */
     ILuint id;    /* unique DevIL id of image */
     ILint w;      /* image width */
     ILint h;      /* image height */
     GLuint genID; /* handle to GPU memory ID, from glGenTextures() */
    };
    TextureHandle wmap, logo, frac;
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //---------------------------   cleanup_images   -------------------------------
     
    //  Clear out the memory used by loading image files.
     
    void cleanup_images(void) {
      printf("cleanup image memory:{");
     
      if (logo.id) {
        printf(" %d",logo.id); 
        ilDeleteImages(1, &amp;logo.id);
        glDeleteTextures(	1, &amp;logo.genID);
      } 
      if (wmap.id) {
        printf(" %d",wmap.id); 
        ilDeleteImages(1, &amp;wmap.id);
        glDeleteTextures(	1, &amp;wmap.genID);
      } 
      if (frac.id) {
        printf(" %d",frac.id); 
        ilDeleteImages(1, &amp;frac.id);
        glDeleteTextures(	1, &amp;frac.genID);
      } 
     
      printf(" }\n");
    }
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //-------------------------   cleanup_Quadrics   -------------------------------
     
    //  Clear out the memory created by gluNewQuadric() calls.
     
    void cleanup_Quadrics(void) {
      printf("cleanup gluQuadric memory:{");
     
      if (earth) {
        printf(" %p",earth); 
        gluDeleteQuadric(earth);
      } 
     
      printf(" }\n");
    }
     
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //-------------------------------   Animate   ----------------------------------
     
    //  Animate rotation of earth and counter-rotation of floor.
     
    void Animate (void)
    {
        erot += 0.4;
        frot -= 0.3;
     
        glutPostRedisplay();
    }
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //-------------------------------   Keyboard   ---------------------------------
     
    //  Use 'r' key to reset viewing rotations to zero.
    //  Use 'a' key to toggle earth and floor animation.
    //  Use <Escape> key to exit.
     
    void Keyboard (unsigned char key, int q, int s)
    {
        static int c = 0;
     
        switch (key)  {
     
           case 'a': if (++c%2) glutIdleFunc (Animate);
    		             else       glutIdleFunc (  NULL );
           break;
     
           case 'r': xrot = 0; yrot = 0; erot = 0; frot = 0;
           break;
     
           case 27: exit(0);
           break;
     
           default : printf ("   Keyboard %c == %d\n", key, key);
        }
     
        glutPostRedisplay();
    }
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //-----------------------------   Change_View   --------------------------------
     
    void Change_View (int key, int q, int s)
    {
        switch (key)  {
     
           case GLUT_KEY_DOWN  :  xrot += 2.0;  break;
           case GLUT_KEY_UP    :  xrot -= 2.0;  break;
     
           case GLUT_KEY_RIGHT :  yrot += 2.0;  break;
           case GLUT_KEY_LEFT  :  yrot -= 2.0;  break;
     
           default: printf ("   Special key %c == %d\n", key, key);
        }
     
        glutPostRedisplay();
    }
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //--------------------------------   Triad   -----------------------------------
     
    void Triad (char xlab, char ylab, char zlab, float siz, float color[3])
    {
        static const float XUP[3] = {1,0,0},
                           YUP[3] = {0,1,0},
                           ZUP[3] = {0,0,1},
                           ORG[3] = {0,0,0};
     
        glLineWidth (2.0);
        glColor3fv  (color);
        glDisable   (GL_LIGHTING);
        glDisable   (GL_TEXTURE_2D);
     
        glPushMatrix ();
     
           glScalef (siz, siz, siz);
     
           glBegin (GL_LINES);
              glVertex3fv (ORG); glVertex3fv (XUP);
              glVertex3fv (ORG); glVertex3fv (YUP);
              glVertex3fv (ORG); glVertex3fv (ZUP);
           glEnd ();
     
           glRasterPos3f (1.1, 0.0, 0.0);
           glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, xlab);
     
           glRasterPos3f (0.0, 1.1, 0.0);
           glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, ylab);
     
           glRasterPos3f (0.0, 0.0, 1.1);
           glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, zlab);
     
        glPopMatrix ();
    }
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //------------------------------   Draw_FLoor   --------------------------------
     
    void Draw_Floor (void)
    {
        glDisable (GL_LIGHTING  );
        glColor3f (1.0, 1.0, 1.0);
     
        glEnable (GL_TEXTURE_2D);
        glBindTexture ( GL_TEXTURE_2D, frac.genID);
     
        glBegin (GL_QUADS);
           glTexCoord2s (0, 0);  glVertex3d (-2,-2,-1);
           glTexCoord2s (1, 0);  glVertex3d ( 2,-2,-1);
           glTexCoord2s (1, 1);  glVertex3d ( 2, 2,-1);
           glTexCoord2s (0, 1);  glVertex3d (-2, 2,-1);
        glEnd ();
    }
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //------------------------------   Draw_Earth   --------------------------------
     
    void Draw_Earth (void)
    {
        static const GLfloat potamb[3] = {0.8,0.8,0.8},
                             potdfs[3] = {0.9,0.9,0.9},
                             potspc[3] = {1.0,1.0,1.0};
     
        glEnable (GL_LIGHTING  );
     
        glMaterialfv (GL_FRONT, GL_AMBIENT  , potamb);
        glMaterialfv (GL_FRONT, GL_DIFFUSE  , potdfs);
        glMaterialfv (GL_FRONT, GL_SPECULAR , potspc);
        glMateriali  (GL_FRONT, GL_SHININESS, 50);
     
        glEnable (GL_TEXTURE_2D);
        glBindTexture ( GL_TEXTURE_2D, wmap.genID);
     
        gluSphere (earth, 1.0, 36, 72);
    }
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //-----------------------------   Render_Scene   -------------------------------
     
    void Render_Scene (void)
    {
        static GLfloat yelo[3] = {1,1,0};
     
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
     
        // Put OpenGL logo in upper left corner of window.
     
        glMatrixMode   (GL_PROJECTION);
        glLoadIdentity ();
        gluOrtho2D     (0, 1, 0, 1);
        glMatrixMode   (GL_MODELVIEW);
        glLoadIdentity ();
     
        glDisable (GL_DEPTH_TEST);
     
        glEnable (GL_TEXTURE_2D);
        glBindTexture ( GL_TEXTURE_2D, logo.genID);
     
        glTranslatef(0.01,0.83,-1.);
        glScalef(0.25,0.3,1.0);
        glBegin (GL_QUADS);
           glTexCoord2s (0, 0);  glVertex2f (0,0);
           glTexCoord2s (1, 0);  glVertex2f (1,0);
           glTexCoord2s (1, 1);  glVertex2f (1,0.5); // original logo has height half of width
           glTexCoord2s (0, 1);  glVertex2f (0,0.5); // keeps original aspect ratio
        glEnd ();
     
        // Set up perspective view of earth floating above a floor.
     
        glEnable (GL_DEPTH_TEST);
     
        glMatrixMode   (GL_PROJECTION);
        glLoadIdentity ();
        gluPerspective (60, 1.33, 1, 10);
     
        glMatrixMode (GL_MODELVIEW);
        glLoadIdentity ();
        glTranslatef (0, 0, -4);      // Move objects away from camera.
     
        glRotatef (xrot, 1,0,0);      // Tip  objects in scene with up/down keys.
        glRotatef (yrot, 0,1,0);      // Turn objects in scene with up/down keys.
        glRotatef ( -90, 1,0,0);
     
        glPushMatrix ();
           glRotatef (erot, 0,0,1);
           Draw_Earth ();
           glRotatef (-90, 0,0,1);               // Put 'X' axis thru 0 degs lon.
           Triad ('x', 'y', 'z', 1.5, yelo);
        glPopMatrix ();
     
        glPushMatrix ();
           glRotatef (frot, 0,0,1);
           Draw_Floor ();
        glPopMatrix ();
     
        glutSwapBuffers();
    }
     
     
    //---+----3----+----2----+----1----+---><---+----1----+----2----+----3----+----4
    //----------------------------   LoadImageDevIL   ------------------------------
     
    // use devIL, cross platform image loading library(openil.sourceforge.net/about.php)
     
    ILuint LoadImageDevIL (const char *szFileName, struct TextureHandle *T)
    {
        //When IL_ORIGIN_SET enabled, the origin is specified at an absolute 
        //position, and all images loaded or saved adhere to this set origin.
        ilEnable(IL_ORIGIN_SET);
        //sets the origin to be IL_ORIGIN_LOWER_LEFT when loading all images, so 
        //that any image with a different origin will be flipped to have the set 
        //origin.
        ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
     
        //Now load the image file
        ILuint ImageNameID;
        ilGenImages(1, &amp;ImageNameID);
        ilBindImage(ImageNameID);
        if (!ilLoadImage(szFileName)) return 0; // failure 
     
        T->id = ImageNameID;
        T->p = ilGetData(); 
        T->w = ilGetInteger(IL_IMAGE_WIDTH);
        T->h = ilGetInteger(IL_IMAGE_HEIGHT);
     
        // openGL specific part now
        // download image data to GPU memory to improve perfromance
        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, 3, T->w, T->h, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, T->p);
     
        printf("%s %d %d %d\n",szFileName,T->id,T->w,T->h);
        return 1; // success
    }
     
    //---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
    //---------------------------------   main   -----------------------------------
     
    int main (int argc, char **argv)
    {
        static const GLfloat lite_pos[4] = {5.0, 5.0, 5.0, 0.0};
     
        glutInit               (&amp;argc, argv);
        glutInitWindowSize     (800, 600);
        glutInitWindowPosition (300, 300);
        glutInitDisplayMode    (GLUT_DEPTH | GLUT_DOUBLE);
     
        glutCreateWindow ("Image Loading - MaxH - May 18, 2009");
        glutDisplayFunc  (Render_Scene);
        glutSpecialFunc  (Change_View );
        glutKeyboardFunc (  Keyboard  );
     
        ilInit();
        assert( LoadImageDevIL ("Logo.bmp", &amp;logo) );
        assert( LoadImageDevIL ("wmap.bmp", &amp;wmap) );
        assert( LoadImageDevIL ("Frac.bmp", &amp;frac) );
        atexit(cleanup_images);
     
        // set geometry
        earth = gluNewQuadric();
        atexit(cleanup_Quadrics);
        gluQuadricTexture (earth, GL_TRUE);
     
        glEnable  (GL_LIGHT0);
        glLightfv (GL_LIGHT0, GL_POSITION, lite_pos);
     
        glutMainLoop  ();
     
        return 1;
    }
     
    //------------------------------------------------------------------------------

  2. #12
    Intern Contributor
    Join Date
    Feb 2010
    Posts
    59

    Re: Bind Texture to a sphere?

    thank a lot i fount you discussion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •