Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: Texturing Sphere

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2005
    Posts
    17

    Texturing Sphere

    hello all
    i need an know how to texture a sphere, i am using glutSolidSphere function,

    can you give me an example

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2005
    Posts
    17

    Re: Texturing Sphere

    many thanks for your quick response, but this is a task that required to be done using glu, glut, if you have an example that uses these lib's , please send it

  4. #4
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Texturing Sphere

    When you created your glu object you could ask it for texture coordinates.

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2005
    Posts
    17

    Re: Texturing Sphere

    Quote Originally Posted by BionicBytes
    When you created your glu object you could ask it for texture coordinates.
    i need an example

  6. #6
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Texturing Sphere

    Plenty on the net.
    Try this:
    Code :
      GLUquadricObj *sphere=NULL;
      sphere = gluNewQuadric();
      gluQuadricDrawStyle(sphere, GLU_FILL);
      gluQuadricTexture(sphere, TRUE);
      gluQuadricNormals(sphere, GLU_SMOOTH);
      //Making a display list
      mysphereID = glGenLists(1);
      glNewList(mysphereID, GL_COMPILE);
      gluSphere(sphere, 1.0, 20, 20);
      glEndList();
      gluDeleteQuadric(sphere);
      //-----------------
      //and whenever you want to render, call glCallList(mysphereID)
      //to kill the display list, glDeleteLists(mysphereID, 1);

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2005
    Posts
    17

    Re: Texturing Sphere

    Quote Originally Posted by BionicBytes
    Plenty on the net.
    Try this:
    Code :
      GLUquadricObj *sphere=NULL;
      sphere = gluNewQuadric();
      gluQuadricDrawStyle(sphere, GLU_FILL);
      gluQuadricTexture(sphere, TRUE);
      gluQuadricNormals(sphere, GLU_SMOOTH);
      //Making a display list
      mysphereID = glGenLists(1);
      glNewList(mysphereID, GL_COMPILE);
      gluSphere(sphere, 1.0, 20, 20);
      glEndList();
      gluDeleteQuadric(sphere);
      //-----------------
      //and whenever you want to render, call glCallList(mysphereID)
      //to kill the display list, glDeleteLists(mysphereID, 1);
    nothing but a black screen, i don't know what is wrong
    here is my draw function

    void Draw()
    {
    GLuint texture = LoadTextureRAW("EarthMap.bmp",1);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -12.0f);
    GLUquadricObj *sphere=NULL;
    sphere = gluNewQuadric();
    gluQuadricDrawStyle(sphere, GLU_FILL);
    gluQuadricTexture(sphere, TRUE);
    gluQuadricNormals(sphere, GLU_SMOOTH);
    texture = glGenLists(1);
    glNewList(texture, GL_COMPILE);
    gluSphere(sphere, 1.0, 20, 20);
    glEndList();
    gluDeleteQuadric(sphere);
    glutSwapBuffers();
    glDisable(GL_TEXTURE_2D);
    }

    here is the loading texture function


    GLuint LoadTextureRAW( const char * filename, int wrap )
    {
    GLuint texture;
    int width, height;
    BYTE * data;
    FILE * file;
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    file = fopen( filename, "r" );
    if ( file == NULL )return 0;
    width = 256;
    height = 256;
    data = (byte*) malloc( width * height * 3 );
    fread( data, width * height * 3, 1, file );
    fclose( file );
    glGenTextures( 1, &texture );
    glBindTexture( GL_TEXTURE_2D, texture );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP );
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_BGR_EXT, GL_UNSIGNED_BYTE, data );
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    glEnable( GL_TEXTURE_2D );
    return texture;
    }

  8. #8
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421

    Re: Texturing Sphere

    Did you notice this line?

    //and whenever you want to render, call glCallList(mysphereID)

    Also, read this
    http://www.opengl.org/wiki/Common_Mi...ender_function
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  9. #9
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Texturing Sphere

    You are not following any tutorials are you?
    You have not yet grasped the basics at all.
    You are creating textures and glushpere each and every frame. The creation of these objects should be done exactly once. When you render you need to bind the texture then call the displaylist for the sphere. Also be sure to translate further into the Z because the sphere will be clipped against the near plane otherwise

Posting Permissions

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