سؤال

I am trying to create the background of my solar system using skybox; however it's not outputting what I thought it would.

First of all, the displaying issue. The pictures below show the problem I'm having. enter image description here

different angle: enter image description here

another angle: enter image description here

As you can see, the texture is mapped to only one side of the cube. That would be OK if the side it was being mapped to was the inside one. I don't get why it's not being mapped properly.

The second issue is that (as you can see from the pictures) as I rotate the camera, the box rotates with it and as I zoom out I can see the full box. I want the box to stay in the background always, and be only able to zoom in/out of my solar system. That way the stars are always in the background. I am not sure how to accomplish it.

Here is the code I'm using to render the skybox and solar system. (keep in my that the solar system works the way I intended it to)

this is the code for the skybox:

void Skybox::displaySkybox() 
{

    Images::RGBImage test[6]; //6 pictures for 6 sides
    test[0]=Images::readImageFile(fileName); //I'll only use one for testing purposes
    glEnable(GL_TEXTURE_2D);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    test[0].glTexImage2D(GL_TEXTURE_2D,0,GL_RGB);

    // Save Current Matrix
    glPushMatrix();

    // Second Move the render space to the correct position (Translate)
    glTranslatef(0,0,0);

    // First apply scale matrix
    glScalef(10000,10000,10000);

    float cz = -0.0f,cx = 1.0f;
    float r = 1.0f; // If you have border issues change this to 1.005f
    // Common Axis Z - FRONT Side

    glBegin(GL_QUADS);  
        glTexCoord2f(cx, cz); glVertex3f(-r,1.0f,-r);
        glTexCoord2f(cx, cx); glVertex3f(-r,1.0f, r);
        glTexCoord2f(cz, cx); glVertex3f( r,1.0f, r); 
        glTexCoord2f(cz, cz); glVertex3f( r,1.0f,-r);
    glEnd();

    // Common Axis Z - BACK side

    glBegin(GL_QUADS);      
        glTexCoord2f(cx,cz);  glVertex3f(-r,-1.0f,-r);
        glTexCoord2f(cx,cx);  glVertex3f(-r,-1.0f, r);
        glTexCoord2f(cz,cx);  glVertex3f( r,-1.0f, r); 
        glTexCoord2f(cz,cz);  glVertex3f( r,-1.0f,-r);
    glEnd();

// Common Axis X - Left side

glBegin(GL_QUADS);      
    glTexCoord2f(cx,cx); glVertex3f(-1.0f, -r, r);  
    glTexCoord2f(cz,cx); glVertex3f(-1.0f,  r, r); 
    glTexCoord2f(cz,cz); glVertex3f(-1.0f,  r,-r);
    glTexCoord2f(cx,cz); glVertex3f(-1.0f, -r,-r);      
glEnd();

// Common Axis X - Right side

glBegin(GL_QUADS);      
    glTexCoord2f(cx, cx); glVertex3f(1.0f, -r, r);  
    glTexCoord2f(cz, cx); glVertex3f(1.0f,  r, r); 
    glTexCoord2f(cz, cz); glVertex3f(1.0f,  r,-r);
    glTexCoord2f(cx, cz); glVertex3f(1.0f, -r,-r);
glEnd();

// Common Axis Y - Draw Up side

glBegin(GL_QUADS);      
    glTexCoord2f(cz, cz); glVertex3f( r, -r, 1.0f);
    glTexCoord2f(cx, cz); glVertex3f( r,  r, 1.0f);
    glTexCoord2f(cx, cx); glVertex3f(-r,  r, 1.0f);
    glTexCoord2f(cz, cx); glVertex3f(-r, -r, 1.0f);
glEnd();

// Common Axis Y - Down side

glBegin(GL_QUADS);      
    glTexCoord2f(cz, cz); glVertex3f( r, -r, -1.0f);
    glTexCoord2f(cx, cz); glVertex3f( r,  r, -1.0f);
    glTexCoord2f(cx, cx); glVertex3f(-r,  r, -1.0f);
    glTexCoord2f(cz, cx); glVertex3f(-r, -r, -1.0f);
glEnd();

// Load Saved Matrix
glPopMatrix();


}

Here is the code for the Solar System:

void SolarSystem::display(GLContextData& contextData) const
{   

glDisable(GL_LIGHTING);
Skybox test("images/test.jpg");
test.displaySkybox();

drawCircle(800, 720, 2, 100);
//SUN
        //Picture location, major radius, minor radius, major orbit, minor orbit, angle
Planet Sun ("images/Sun.jpg", 
                 100, 99, 200.0, 0.0, 0.0);
double sunOrbS = 0;
double sunRotS = rotatSpeed/10;

//orbit speed, rotation speed, moon reference coordinates (Parent planet's major and minor Axis)
Sun.displayPlanet(sunOrbS, sunRotS, 0.0, 0.0);


//EARTH

GLfloat light_diffuse[] = { 1.5, 1.5, 1.5, 1.5 };
GLfloat pos[] = { 200.0, 0.0, 0.0, 1.0};
glEnable(GL_LIGHTING);  
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, pos);


Planet Earth ("images/earth.jpg", 
           50, 49, 500.0, 450.0, 23.5);
double eaOrbS = orbitSpeed;
double eaRotS = rotatSpeed*3;

Earth.displayPlanet(eaOrbS, eaRotS, 0.0, 0.0);


//EARTH'S MOON
Planet Moon ("images/moon.jpg", 
           25, 23, 100.0, 100.0, 15);
double moOrbS = rotatSpeed*4;
double moRotS = eaOrbS;

Moon.displayPlanet(moOrbS, moRotS, Earth.getMajorAxis(), Earth.getMinorAxis());

orbitSpeed+=.3;
if (orbitSpeed > 359.0)
orbitSpeed = 0.0;


rotatSpeed+=1.0;
if (rotatSpeed > 7190.0)
rotatSpeed = 0.0;


}
هل كانت مفيدة؟

المحلول

void Skybox::displaySkybox() 
{
    ...
    glPushMatrix();
    ...
    glPushMatrix();
    ...
    glPopMatrix();
    ...
    // huh?  where's the second glPopMatrix()?
}

Don't do that. Make sure you Pop as much as you Push.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top