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 10 of 20

Thread: Texture on a sphere

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    11

    Texture on a sphere

    Hi, I have a problem applying a texture to a sphere.
    In the setup phase I do this:
    Code :
    	earth = gluNewQuadric();
     
    	gluQuadricTexture( earth, GL_TRUE);
     
    	earthTexture = LoadBitmap("data/EarthTexture.bmp");
    And when I draw the I use this code
    Code :
    	    glEnable ( GL_TEXTURE_2D );
    	    glBindTexture ( GL_TEXTURE_2D, earthTexture);
    	    gluSphere( earth, 0.9, 36, 72);
    	  glDisable ( GL_TEXTURE_2D );

    But something goes wrong and I obtain this http://yfrog.com/14screenshot20091119at105j

    What am I doing wrong?

    If I try to move the sphere the result is getting even worst.

  2. #2
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    11

    Re: Texture on a sphere

    If I try to draw only the texture on a square it works, but if I try to draw the texture on the square after I call the gluSphere also the image on the square is screwed :-(

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

    Re: Texture on a sphere

    The image mapped on the sphere appears like it is rotated 90. Can you rotate your image 90 degrees in some image editor and try again. Is LoadBitmap() a custom function or winOS library function?

    Do you have some code affecting the texture Matrix ie do you call the following somewhere in your code?
    Code :
    glMatrixMode(GL_TEXTURE);

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    11

    Re: Texture on a sphere

    Thanks for your answer. I never use the matrix mode GL_TEXTURE explicitly, is there any GL command that makes that matrix change?

    The code for the load bitmap was found on internet and it is this:
    Code :
    int LoadBitmap(char *filename)
    {
        FILE * file;
        char temp;
        long i;
     
        BITMAPINFOHEADER infoheader;
     
        GLuint num_texture;
     
        if( (file = fopen(filename, "rb"))==NULL) return (-1); // Open the file for reading
     
        fseek(file, 18, SEEK_CUR);  /* start reading width & height */
        fread(&infoheader.biWidth, sizeof(int), 1, file);
     
        fread(&infoheader.biHeight, sizeof(int), 1, file);
     
        fread(&infoheader.biPlanes, sizeof(short int), 1, file);
        if (infoheader.biPlanes != 1) {
    	    printf("Planes from %s is not 1: %u\n", filename, infoheader.biPlanes);
    	    return 0;
        }
     
        // read the bpp
        fread(&infoheader.biBitCount, sizeof(unsigned short int), 1, file);
        if (infoheader.biBitCount != 24) {
          printf("Bpp from %s is not 24: %d\n", filename, infoheader.biBitCount);
          return 0;
        }
     
        fseek(file, 24, SEEK_CUR);
     
        // read the data
        if(infoheader.biWidth<0){
    	infoheader.biWidth = -infoheader.biWidth;
        }
        if(infoheader.biHeight<0){
    	infoheader.biHeight = -infoheader.biHeight;
        }
        infoheader.data = (char *) malloc(infoheader.biWidth * infoheader.biHeight * 3);
        if (infoheader.data == NULL) {
    	    printf("Error allocating memory for color-corrected image data\n");
    	    return 0;
        }
     
        if ((i = fread(infoheader.data, infoheader.biWidth * infoheader.biHeight * 3, 1, file)) != 1) {
    	    printf("Error reading image data from %s.\n", filename);
    	    return 0;
        }
     
        for (i=0; i<(infoheader.biWidth * infoheader.biHeight * 3); i+=3) { // reverse all of the colors. (bgr -> rgb)
    	    temp = infoheader.data[i];
    	    infoheader.data[i] = infoheader.data[i+2];
    	    infoheader.data[i+2] = temp;
        }
     
     
        fclose(file); // Closes the file stream
     
        glGenTextures(1, &amp;num_texture);
        glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter
     
        // The next commands sets the texture parameters
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //The minifying function
     
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
     
        // Finally we define the 2d texture
        glTexImage2D(GL_TEXTURE_2D, 0, 3, infoheader.biWidth, infoheader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, infoheader.data);
     
        // And create 2d mipmaps for the minifying function
        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, infoheader.biWidth, infoheader.biHeight, GL_RGB, GL_UNSIGNED_BYTE, infoheader.data);
     
        free(infoheader.data); // Free the memory we used to load the texture
     
        return (num_texture); // Returns the current texture OpenGL ID
    }

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    11

    Re: Texture on a sphere

    I tried to rotate the texture, but it doesn't help

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

    Re: Texture on a sphere

    Mhhm, the glMatrixMode(GL_TEXTURE) question was a remote possibility because you can affect texture coordinates with glTranslate/glRotate etc with it. So it is good to rule that out right away.

    I am surprised rotating the image 90 degrees did nothing.

    Some versions of openGL don't accept non-power of two sizes ie what are the actual values of infoheader.biWidth and infoheader.biHeight? If they are not 64, 128,256 ... 2^integer then try scaling your image file to the nearest power of 2 before loadBMP called.

Posting Permissions

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