使用opengl c ++的skybox问题

时间:2013-05-29 19:46:59

标签: c++ opengl skybox

我正在尝试使用skybox创建太阳系的背景;但它并没有输出我的想法。

首先,显示问题。下面的图片显示了我遇到的问题。 enter image description here

不同的角度: enter image description here

另一个角度: enter image description here

如您所见,纹理仅映射到多维数据集的一侧。如果它被映射到的那一方是内部的,那就没问题。我不明白为什么没有正确映射。

第二个问题是(从图片中可以看到)当我旋转相机时,盒子随之旋转,当我缩小时,我可以看到整个盒子。我希望盒子始终留在后台,只能放大/缩小我的太阳系。这样,星星总是在背景中。我不知道如何实现它。

这是我用来渲染天空盒和太阳系的代码。 (保持我的意思是太阳系按照我的意图运作)

这是天空盒的代码:

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();


}

以下是太阳系的代码:

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;


}

1 个答案:

答案 0 :(得分:2)

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

不要那样做。确保PopPush一样多。