Skybox not getting rendered

Hi there, please response asap…

Here is the code of mine, trying to render sky box…
But dont know why ?? its not getting displayed on the screen…
Just getting white background !!

If i am going wrong !! Please leave your pointers here…


#include"windows.h"
#include"GL/glut.h"
#include"stdio.h"
#include"math.h"

bool* keyBuffer = new bool[256],wired=false;        // Key buffer to process arrow keys
float theta[]={0.0,0.0,0.0};                        // Rotating sphere
float size=20.0;                                    // Sphere longitude and latitude size
float radius=0.2;                                   // Sphere radius
float eyeX=0.0,eyeY=0.5,eyeZ=4.0;                   // Camera eye
float atX=0.0,atY=0.5,atZ=0.0;                      // Center of projection : Look at
char* texPath[]={"front.bmp","left.bmp","right.bmp","bottom.bmp","top.bmp","back.bmp"};

// Loads textures
void loadBitmap(char* filename,int texNumber){
    unsigned char* data;
    FILE* file;
    int i,j=0;

    BITMAPFILEHEADER bmpHeader;
    BITMAPINFOHEADER bmpInfo;
    RGBTRIPLE rgb;

    if((file=fopen(filename,"rb"))==NULL){
        printf("%s file not found !!",filename);
        exit(1);
    }

    fread(&bmpHeader,sizeof(bmpHeader),1,file);
    fseek(file,sizeof(bmpHeader),SEEK_SET);
    fread(&bmpInfo,sizeof(bmpInfo),1,file);

    data=(byte *) malloc(bmpInfo.biWidth * bmpInfo.biHeight * 4);
    memset(data,0,bmpInfo.biWidth * bmpInfo.biHeight * 4);

    for(i=0;i<bmpInfo.biWidth * bmpInfo.biHeight;i++){
        fread(&rgb,sizeof(rgb),1,file);
        data[j+0] = rgb.rgbtRed;
        data[j+1] = rgb.rgbtGreen;
        data[j+2] = rgb.rgbtBlue;
        data[j+3] = 155;
        j+=4;
    }

    fclose(file);

    glBindTexture(GL_TEXTURE_2D, texNumber);

    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bmpInfo.biWidth, bmpInfo.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    free(data);
}

// Delay functionality
void causeDelay(){
    for(int i=0;i<4000;i++)
    for(int j=0;j<4500;j++);
}

// Spins marble around based on axis and amount of fraction
void spin(int axis,float fraction){
    theta[axis]=fmod((theta[axis]+fraction),360.0);
}

// Moves marble forward and backward based on fraction param
void moveSphere(float fraction){
    float dirX,dirZ;
    dirX=atX-eyeX;
    dirZ=atZ-eyeZ;
    eyeX += dirX * fraction;
    eyeZ += dirZ * fraction;
    atX += dirX * fraction;
    atZ += dirZ * fraction;
}

// Strafes marble left and right based on fraction param
void strafeSphere(float fraction){
    float orthoX,orthoZ;
    orthoX = -(atZ-eyeZ);
    orthoZ = atX-eyeX;
    eyeX += orthoX * fraction;
    eyeZ += orthoZ * fraction;
    atX += orthoX * fraction;
    atZ += orthoZ * fraction;
}

// Rotate the camera
void rotateView(float fraction){
    float dirX,dirZ;
//    dirX = atX - eyeX;
//    dirZ = atZ - eyeZ;
//    atZ = eyeZ + sin(fraction) * dirX + cos(fraction) * dirZ;
//    atX = eyeX + cos(fraction) * dirX - sin(fraction) * dirZ;
    dirX = eyeX - atX;
    dirZ = eyeZ - atZ;
    eyeZ = atZ + sin(fraction) * dirX + cos(fraction) * dirZ;
    eyeX = atX + cos(fraction) * dirX - sin(fraction) * dirZ;
}

// Procesess the active arrow keys
void processKeys(){
    if(keyBuffer[GLUT_KEY_UP]){
        spin(0,1.0);
        moveSphere(0.01);
    }
    if(keyBuffer[GLUT_KEY_DOWN]){
        spin(0,-1.0);
        moveSphere(-0.01);
    }
    if(keyBuffer[GLUT_KEY_LEFT]){
        spin(2,0.5);
        strafeSphere(-0.01);
    }
    if(keyBuffer[GLUT_KEY_RIGHT]){
        spin(2,-0.5);
        strafeSphere(0.01);
    }
}

// Renders the marble
void renderSphere(){
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glPushMatrix();
    glColor3f(1.0,1.0,1.0);
    if(wired)
        glutWireSphere(radius,size<3?3:size,size<3?3:size);
    else
        glutSolidSphere(radius,size<3?3:size,size<3?3:size);
    glPopMatrix();
    glPopAttrib();
}

// Renders the map
void renderBase(){
    glColor3f(0.5,0.5,1.0);
    glBegin(GL_QUADS);
        glVertex3f(-3.0f, -1.0f, -50.0f);
        glVertex3f(-3.0f, -1.0f, -1.0f);
        glVertex3f( 3.0f, -1.0f, -1.0f);
        glVertex3f( 3.0f, -1.0f, -50.0f);
    glEnd();
}

// Render skybox
void renderSkybox(){
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glPushMatrix();
    glDisable(GL_DEPTH_TEST);
    glDepthMask(false);
    // Drawing skybox sides //
    glColor3f(1.0,1.0,1.0);
    // Front
    glBindTexture(GL_TEXTURE_2D,1);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,0.0);    glVertex3f( -0.5f, -0.5f,  0.5f );;
    glTexCoord2f(1.0,0.0);    glVertex3f(  0.5f, -0.5f,  0.5f );
    glTexCoord2f(1.0,1.0);    glVertex3f(  0.5f,  0.5f,  0.5f );
    glTexCoord2f(0.0,1.0);    glVertex3f( -0.5f,  0.5f,  0.5f );
    glEnd();
    // left
    glBindTexture(GL_TEXTURE_2D,2);
    glBegin(GL_QUADS);
    glTexCoord2f(1.0,0.0);    glVertex3f( -0.5f, -0.5f, -0.5f );
    glTexCoord2f(1.0,1.0);    glVertex3f( -0.5f, -0.5f,  0.5f );
    glTexCoord2f(0.0,1.0);    glVertex3f( -0.5f,  0.5f,  0.5f );
    glTexCoord2f(0.0,0.0);    glVertex3f( -0.5f,  0.5f, -0.5f );
    glEnd();
    // Right
    glBindTexture(GL_TEXTURE_2D,3);
    glBegin(GL_QUADS);
    glTexCoord2f(1.0,1.0);    glVertex3f(  0.5f, -0.5f,  0.5f );
    glTexCoord2f(0.0,1.0);    glVertex3f(  0.5f, -0.5f, -0.5f );
    glTexCoord2f(0.0,0.0);    glVertex3f(  0.5f,  0.5f, -0.5f );
    glTexCoord2f(1.0,0.0);    glVertex3f(  0.5f,  0.5f,  0.5f );
    glEnd();
    // Bottom
    glBindTexture(GL_TEXTURE_2D,4);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,1.0);    glVertex3f( -0.5f, -0.5f, -0.5f );
    glTexCoord2f(0.0,0.0);    glVertex3f( -0.5f, -0.5f,  0.5f );
    glTexCoord2f(1.0,0.0);    glVertex3f(  0.5f, -0.5f,  0.5f );
    glTexCoord2f(1.0,1.0);    glVertex3f(  0.5f, -0.5f, -0.5f );
    glEnd();
    // Top
    glBindTexture(GL_TEXTURE_2D,5);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,0.0);    glVertex3f( -0.5f,  0.5f, -0.5f );
    glTexCoord2f(1.0,0.0);    glVertex3f( -0.5f,  0.5f,  0.5f );
    glTexCoord2f(1.0,1.0);    glVertex3f(  0.5f,  0.5f,  0.5f );
    glTexCoord2f(0.0,1.0);    glVertex3f(  0.5f,  0.5f, -0.5f );
    glEnd();
    // Back
    glBindTexture(GL_TEXTURE_2D,6);
    glBegin(GL_QUADS);
    glTexCoord2f(1.0,0.0);    glVertex3f(  0.5f, -0.5f, -0.5f );
    glTexCoord2f(1.0,1.0);    glVertex3f( -0.5f, -0.5f, -0.5f );
    glTexCoord2f(0.0,1.0);    glVertex3f( -0.5f,  0.5f, -0.5f );
    glTexCoord2f(0.0,0.0);    glVertex3f(  0.5f,  0.5f, -0.5f );
    glEnd();

    glDepthMask(true);
    glEnable(GL_DEPTH_TEST);
    glPopMatrix();
    glPopAttrib();
}

// Main display function
void renderScene(){
    processKeys();
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    renderSkybox();
    gluLookAt(  eyeX,eyeY,eyeZ,
                atX,atY,atZ,
                0.0,1.0,0.0);
    renderBase();
    glTranslatef(atX,atY-1.3,atZ-2.0);
    glRotatef(theta[0],1.0,0.0,0.0);
    glRotatef(theta[1],0.0,1.0,0.0);
    glRotatef(theta[2],0.0,0.0,1.0);
    renderSphere();
    glFlush();
    glutSwapBuffers();
}

// Resets the game
void resetScene(){
    theta={0.0,0.0,0.0};
    eyeX=0.0,eyeY=0.5,eyeZ=4.0;
    atX=0.0,atY=0.5,atZ=0.0;
}

// Sphere pitches as you wish
void pitchSphere(){
    int i=0;
    while(i<10){
        atY+=0.1;
        eyeY+=0.1;
        i++;
        renderScene();
        causeDelay();
    }
    while(i>0){
        atY-=0.1;
        eyeY-=0.1;
        i--;
        renderScene();
        causeDelay();
    }
}

// Catch the marble !! Its falling !!
void pushSphereDown(){
    float dirZ = atZ - eyeZ;
    for(int i=0;i<20;i++){
        spin(0,0.1);
        atY -= 0.05;
        atZ -= 0.1;
        eyeY += 0.01;
        eyeZ -= 0.1;
        renderScene();
        causeDelay();
    }
    resetScene();
}

// Reshape function done using perspective
void reshape(int w,int h){
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f,(w*1.0)/(h>0?h:1.0),0.1,100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

// Initialization of depth and lighting (TO BE DONE..)
void init(){
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    ShowCursor(false);
    for(int i=0;i<6;i++)
        loadBitmap(texPath[i],i+1);
    glutIgnoreKeyRepeat(0);
}

// Call back functions registered with keyboard

void keyDown(int key,int xx,int yy){
    keyBuffer[key]=true;
}

void keyUp(int key,int xx,int yy){
    keyBuffer[key]=false;
}

void keyPressed(unsigned char key,int xx,int yy){
    if(key==27)
        exit(0);
    if(key==32)
        pitchSphere();
    if(key=='+')
        size++;
    if(key=='-')
        size=(size>3?size-1:3);
    if(key=='q'||key=='Q')
        rotateView(-0.05);
    if(key=='e'||key=='E')
        rotateView(0.05);
    if(key=='w'||key=='W')
        wired=!wired;
    if(key=='c'||key=='C')
        eyeY += 0.1;
    if(key=='x'||key=='X')
        eyeY -= 0.1;
    if(key=='d'||key=='D')
        pushSphereDown();
    if(key=='r'||key=='R')
        resetScene();
}

// Entry for game
int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
    glutGameModeString("1240x780:64@72");
    glutEnterGameMode();
    init();
    glutReshapeFunc(reshape);
    glutDisplayFunc(renderScene);
    //glutIdleFunc(spin);
    glutIdleFunc(renderScene);
    glutKeyboardFunc(keyPressed);
    glutSpecialFunc(keyDown);
    glutSpecialUpFunc(keyUp);
    glutMainLoop();
}

Why on earth do you post all that code? Is posting the loading of bitmaps really necessary and pertinant to debugging why the skybox is not rendering?

Anyway, your issue lies in the fact that you render the skybox before glulookat, when it should be immediately after.

void renderScene(){
processKeys();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
renderSkybox();
gluLookAt( eyeX,eyeY,eyeZ,
atX,atY,atZ,
0.0,1.0,0.0);

I forgot to add that you should use glTranslatef after glulookat so that the skybox is always folloing the camera position.
something like this:


void renderScene(){
processKeys();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( eyeX,eyeY,eyeZ,atX,atY,atZ,0.0,1.0,0.0);
glpushmatrix;
gltranslatef (eyeX,eyeY,eyeZ);//translate to camera world position
renderSkybox();  //ensure you draw a unit cube
glpopmatrix;

Thank BionicBytes !! got it done !!

And i had missed enabling GL_TEXTURE_2D…

Ya code is fully messed up, should structure it properly…
Any other changes needed to be done !!
Actually i am into my assignment and have chosen a game, in which a sphere rolls as the user inputs !!

Hey !! Any pointers on implementing a startup screen for game ??