1

opengl プログラムに問題があります。キューブを 1 つしか表示できません。2 つの立方体を表示しようとしていますが、1 つの立方体しか表示されない理由がわかりません。誰かが理由とそれを修正するために何をする必要があるか教えてもらえますか? 使用する必要があるキューブ関数は次のとおりです。

void myCube(double length)
{ 

double half = length/2;
// face 1
glBegin(GL_POLYGON);
   glVertex3d(half, -half, half); 
   glVertex3d(half, half, half); 
   glVertex3d(-half, half, half); 
   glVertex3d(-half, -half, half); 
glEnd();
// face 2
glPushMatrix();
glBegin(GL_POLYGON);
   glVertex3d(half, -half, -half); 
   glVertex3d(half, half, -half); 
   glVertex3d(-half, half, -half); 
   glVertex3d(-half, -half, -half); 
glEnd();
// face 3
glBegin(GL_POLYGON);
   glVertex3d(half, half, half); 
   glVertex3d(half, -half, half); 
   glVertex3d(half, -half, -half); 
   glVertex3d(half, half, -half); 
glEnd();
// face 4
glBegin(GL_POLYGON);
   glVertex3d(-half, -half, half); 
   glVertex3d(-half, half, half); 
   glVertex3d(-half, half, -half); 
   glVertex3d(-half, -half, -half); 
glEnd();
// face 5
glBegin(GL_POLYGON);
   glVertex3d(half, half, half); 
   glVertex3d(half, half, -half); 
   glVertex3d(-half, half, -half); 
   glVertex3d(-half, half, half); 
glEnd();
// face 6
glBegin(GL_POLYGON);
   glVertex3d(half, -half, half); 
   glVertex3d(half, -half, -half); 
   glVertex3d(-half, -half, -half); 
   glVertex3d(-half, -half, half); 
glEnd();

}

そして、ここに私の表示機能があります:

void myDisplay(void)
{
//cubeCount = (rand() % 15) + 7;
//int r = rand()%18;
//r = r - 9;
//double rd = (double)r / 2.0;
//glMatrixMode(GL_PROJECTION); // set the view volume shape
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glTranslatef(1.0f, 4.0f, 2.0f);
//double factor = 05;
gluPerspective(60, screenWidth/screenHeight, 0.1, 100);
gluLookAt(3.0, 3.0, -3.0, 3.0, 3.0, 0.0, 0.0, 1.0, 0.0);

//for (int i = 0; i < 5; i++)
//{
    glPushMatrix();
    glTranslatef(4.9f, 1.2f, 1.0f);
    glRotated(45.0, 0.0, 1.0, 0.0);
    glColor3d(1, 1, 1); // draw white cube
    glEnable(GL_POLYGON_OFFSET_FILL);
    glPolygonOffset(1.0, 1.0);  
    myCube(0.5);
    glDisable(GL_POLYGON_OFFSET_FILL);
    glColor3d(0, 0, 0); // draw black lines
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    myCube(0.5);
    glPopMatrix();
    //glFlush();

    glPushMatrix();
    glTranslatef(2.0f, 4.0f, 2.0f);
    glScalef(1.0, 2.0, 1.0);
    //glRotated(45.0, 0.0, 1.0, 0.0);
    glColor3d(1, 1, 1); // draw white cube
    glEnable(GL_POLYGON_OFFSET_FILL);
    glPolygonOffset(1.0, 1.0);  
    myCube(0.5);
    glDisable(GL_POLYGON_OFFSET_FILL);
    glColor3d(0, 0, 0); // draw black lines
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    myCube(0.5);
    glPopMatrix();
//}
}
//glutSwapBuffers();
glFlush();
}

ディスプレイの左下隅に 1 つの立方体が表示されますが、もう 1 つの立方体はどこにも表示されません。必要に応じてさらにコードをアップロードします。

4

1 に答える 1

2

glPushMatrix()現在、一致しないesが繰り返されてモデルビュー マトリックス スタックが吹き飛ばされています。

そのglPushMatrix()コールを から取り出しmyCube()ます:

...
   glVertex3d(-half, -half, half); 
glEnd();
// face 2
glPushMatrix();
^^^^^^^^^^^^^^ What's this guy doing here?  
               Why doesn't it have a matching glPopMatrix()?
glBegin(GL_POLYGON);
   glVertex3d(half, -half, -half); 
.....

完全なコード:

#include <GL/glut.h>

void myCube(double length)
{ 
    double half = length/2;
    // face 1
    glBegin(GL_POLYGON);
       glVertex3d(half, -half, half); 
       glVertex3d(half, half, half); 
       glVertex3d(-half, half, half); 
       glVertex3d(-half, -half, half); 
    glEnd();
    // face 2
    glBegin(GL_POLYGON);
       glVertex3d(half, -half, -half); 
       glVertex3d(half, half, -half); 
       glVertex3d(-half, half, -half); 
       glVertex3d(-half, -half, -half); 
    glEnd();
    // face 3
    glBegin(GL_POLYGON);
       glVertex3d(half, half, half); 
       glVertex3d(half, -half, half); 
       glVertex3d(half, -half, -half); 
       glVertex3d(half, half, -half); 
    glEnd();
    // face 4
    glBegin(GL_POLYGON);
       glVertex3d(-half, -half, half); 
       glVertex3d(-half, half, half); 
       glVertex3d(-half, half, -half); 
       glVertex3d(-half, -half, -half); 
    glEnd();
    // face 5
    glBegin(GL_POLYGON);
       glVertex3d(half, half, half); 
       glVertex3d(half, half, -half); 
       glVertex3d(-half, half, -half); 
       glVertex3d(-half, half, half); 
    glEnd();
    // face 6
    glBegin(GL_POLYGON);
       glVertex3d(half, -half, half); 
       glVertex3d(half, -half, -half); 
       glVertex3d(-half, -half, -half); 
       glVertex3d(-half, -half, half); 
    glEnd();
}

void display()
{
    glClearColor( 0.3, 0.3, 0.3, 1 );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    gluPerspective( 60, w / h, 0.1, 100 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    gluLookAt(3.0, 3.0, -3.0, 3.0, 3.0, 0.0, 0.0, 1.0, 0.0);

    glPushMatrix();
    {
        glTranslatef(4.9f, 1.2f, 1.0f);
        glRotated(45.0, 0.0, 1.0, 0.0);

        glColor3d(1, 1, 1);
        glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
        glEnable( GL_POLYGON_OFFSET_FILL );
        glPolygonOffset( 1.0, 1.0 );  
        myCube( 0.5 );
        glDisable( GL_POLYGON_OFFSET_FILL );

        glColor3d(0, 0, 0);
        glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
        myCube( 0.5 );
    }
    glPopMatrix();

    glPushMatrix();
    {
        glTranslatef(2.0f, 4.0f, 2.0f);
        glScalef(1.0, 2.0, 1.0);

        glColor3d(1, 1, 1);
        glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
        glEnable( GL_POLYGON_OFFSET_FILL );
        glPolygonOffset( 1.0, 1.0 );  
        myCube( 0.5 );
        glDisable( GL_POLYGON_OFFSET_FILL );

        glColor3d(0, 0, 0);
        glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
        myCube( 0.5 );
    }
    glPopMatrix();

    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "GLUT" );
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}

キューブ

于 2013-10-10T15:03:57.080 に答える