1

我正在尝试使用天空盒创建太阳系的背景;但是它没有输出我想的那样。

首先是显示问题。下面的图片显示了我遇到的问题。 在此处输入图像描述

不同的角度: 在此处输入图像描述

另一个角度: 在此处输入图像描述

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

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

这是我用来渲染天空盒和太阳系的代码。(请记住,太阳系按我的预期工作)

这是天空盒的代码:

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;


}
4

1 回答 1

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

不要那样做。确保你和你Pop一样多Push

于 2013-05-29T19:50:32.390 回答