Results 1 to 3 of 3

Thread: texturing a sphere

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

    texturing a sphere

    Can anyone please help me with this ?? I'm trying to draw a 3D image of earth, or in other words bind the pictre of our planet to a sphere, I've tried several codes and finally got to this one down here, but the problem is that it just won't bind the texture to the sphere! it displays a white sphere with no texture :S I don't know where I went wrong.. I converted the BMP image to .RAW and linked it.. but nothing seems to change..
    could anyone please reiew this code and tell me what my mistake is ??
    the code is the following :


    #include <stdlib.h>//declares malloc
    #include <GL/glut.h>
    #include <GL/gl.h>
    //#include <windows.h>
    #include <stdio.h>

    #include <math.h>
    //#include <iostream.h>

    GLuint texture[1];

    double angle = 0;

    typedef struct
    {
    int X;
    int Y;
    int Z;

    double U;
    double V;
    }VERTICES;

    const double PI = 3.1415926535897;

    const int space = 10;

    const int VertexCount = (90 / space) * (360 / space) * 4;

    VERTICES VERTEX[VertexCount];

    GLuint LoadTextureRAW( const char * filename );

    void DisplaySphere (double R, int texture){
    int b;

    glScalef (0.0125 * R, 0.0125 * R, 0.0125 * R);

    glRotatef (90, 1, 0, 0);

    glBindTexture (GL_TEXTURE_2D, texture);

    glBegin (GL_TRIANGLE_STRIP);
    for ( b = 0; b <= VertexCount; b++){
    glTexCoord2f (VERTEX[B].U, VERTEX[B].V);
    glVertex3f (VERTEX[B].X, VERTEX[B].Y, -VERTEX[B].Z);
    }

    for ( b = 0; b <= VertexCount; b++){
    glTexCoord2f (VERTEX[B].U, -VERTEX[B].V);
    glVertex3f (VERTEX[B].X, VERTEX[B].Y, VERTEX[B].Z);
    }
    glEnd();
    }

    void CreateSphere (double R, double H, double K, double Z) {
    int n;
    double a;
    double b;

    n = 0;

    for( b = 0; b <= 90 - space; b+=space){
    for( a = 0; a <= 360 - space; a+=space){

    VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b) / 180 * PI) - H;
    VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b) / 180 * PI) + K;
    VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;
    VERTEX[n].V = (2 * b) / 360;
    VERTEX[n].U = (a) / 360;
    n++;

    VERTEX[n].X = R * sin((a) / 180 * PI) * sin((b + space) / 180 * PI
    ) - H;
    VERTEX[n].Y = R * cos((a) / 180 * PI) * sin((b + space) / 180 * PI
    ) + K;
    VERTEX[n].Z = R * cos((b + space) / 180 * PI) - Z;
    VERTEX[n].V = (2 * (b + space)) / 360;
    VERTEX[n].U = (a) / 360;
    n++;

    VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b) / 180 * PI
    ) - H;
    VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b) / 180 * PI
    ) + K;
    VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;
    VERTEX[n].V = (2 * b) / 360;
    VERTEX[n].U = (a + space) / 360;
    n++;

    VERTEX[n].X = R * sin((a + space) / 180 * PI) * sin((b + space) /
    180 * PI) - H;
    VERTEX[n].Y = R * cos((a + space) / 180 * PI) * sin((b + space) /
    180 * PI) + K;
    VERTEX[n].Z = R * cos((b + space) / 180 * PI) - Z;
    VERTEX[n].V = (2 * (b + space)) / 360;
    VERTEX[n].U = (a + space) / 360;
    n++;

    }
    }
    }

    void display (void) {
    glClearDepth(1);
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(0,0,-10);

    glRotatef(angle,0,1,0);
    DisplaySphere(3, texture[0]);

    glutSwapBuffers();
    //angle++;
    }

    void init (void) {
    glEnable(GL_DEPTH_TEST);
    glEnable( GL_TEXTURE_2D );
    glDepthFunc(GL_LEQUAL);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glEnable(GL_CULL_FACE);
    texture[0] = LoadTextureRAW( "wmap.raw" );
    CreateSphere(70,0,0,0);
    }
    void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
    glMatrixMode (GL_MODELVIEW);
    }

    int main (int argc, char **argv) {
    glutInit (&amp;argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Planet Earth");
    init();
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);
    glutMainLoop ();
    return 0;
    }

    GLuint LoadTextureRAW( const char * filename )
    {
    GLuint texture;
    int width, height;
    unsigned char* data;
    FILE * file;

    file = fopen( filename, "rb");
    if ( file == NULL ) return 0;
    //allocate memory
    width = 512;//width of wmap
    height = 256;//height of wmap
    data = (unsigned char *)malloc( width * height * 3 );

    fread( data, width * height * 3, 1, file );
    fclose( file );

    glGenTextures( 1, &amp;texture );

    glBindTexture( GL_TEXTURE_2D, texture );

    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
    GL_MODULATE );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
    GL_LINEAR_MIPMAP_NEAREST );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
    GL_LINEAR );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
    GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
    GL_REPEAT );

    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
    GL_RGB, GL_UNSIGNED_BYTE, data );

    free( data );

    return texture;

    }


    Thanks in advance people I really need this !!

  2. #2
    Senior Member Regular Contributor
    Join Date
    Aug 2009
    Posts
    126

    Re: texturing a sphere

    Why don't you use Blender + modelloader ?

    Would be much easier ^^

    PS: Happy Christmas ^^


    ^^


  3. #3
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: texturing a sphere

    Another suggestion is to look at and/or use the demo I posted which does exactly what you are trying to do. See -

    Spherical Earth Demo - MaxH
    Am I doing your homework for you?

Similar Threads

  1. Texturing Sphere
    By AhmedSabry in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 04-21-2012, 02:25 PM
  2. texturing a geodesic sphere
    By Snow Spider in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 01-16-2012, 04:51 AM
  3. Texturing a sphere
    By pjyelton in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-06-2009, 11:01 PM
  4. Texturing a Sphere or Teapot.
    By MrShoe in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 12-05-2000, 06:24 AM
  5. Partial sphere texturing.
    By Q in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 08-10-2000, 06:21 PM

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