如何绘制立方体而不是四边形?

时间:2011-10-31 05:11:11

标签: opengl graphics glut cube glu

以下代码在OpenGL中绘制迷宫或迷宫,结果是2D迷宫,我现在需要做的是绘制立方体而不是这些四边形,我该怎么做?

function drawmaze() {
    int x,y,dl;
    glNewList(dl=glGenLists(1),GL_COMPILE);
    glPushAttrib(GL_TEXTURE_BIT | GL_LIGHTING_BIT);
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);
    glColor3f(1.0f,1.0f,1.0f);

    glBegin(GL_QUADS);
//  glPushMatrix();
    float i = 0;
    for(y=0;y < mazedata.size();y++) {
        for(x=0;x < mazedata[y].size();x++) {
            bool dibujar = false;
            if(wall(x,y)) {             
                glColor3ub(46,151,208);
                drawable = true;                
            }                       
            else
            if (entry(x,y)) {
                glColor3f(0.0f,0.184f,0.792f);
                drawable = true;
            }
            else
            if (mazexit(x,y)) {
                glColor3f(0.811f,0.188f,0.176f);
                drawable = true;
            }
            else 
            if (thing(x,y)) {
                glColor3ub( 151, 204, 0 );
                drawable = true;
            }
            else 
            if (visited(x,y)) {
                glColor3ub( 66, 66, 66 );
                drawable = true;
            }

            if (drawable) {
                //glPushMatrix();
                /*
                                This is a try
                                glTranslatef(1.0,0., 0.0f );

                    glutSolidCube(0.5);*/
                //glPopMatrix();
                glVertex3f(x+0.0f ,y+0.0f ,0.0f );
                glVertex3f(x+0.0f ,y+1.0f ,0.0f );
                glVertex3f(x+1.0f ,y+1.0f ,0.0f );
                glVertex3f(x+1.0f ,y+0.0f ,0.0f );
                // topside:
                glVertex3f(x+0.0f ,y+0.0f ,1.0f );
                glVertex3f(x+1.0f ,y+0.0f ,1.0f );
                glVertex3f(x+1.0f ,y+1.0f ,1.0f );
                glVertex3f(x+0.0f ,y+1.0f ,1.0f );
            }

        }
        i++;
    }
    glEnd();
//  glPopMatrix();
    glPopAttrib();
    glEndList();
    return(dl);
}

2 个答案:

答案 0 :(得分:8)

glBegin(GL_QUADS);
    // front
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    // back
    glVertex3f(0.0f, 0.0f, -1.0f);
    glVertex3f(1.0f, 0.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(0.0f, 1.0f, -1.0f);
    // right
    glVertex3f(1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    // left
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, -1.0f);
    glVertex3f(0.0f, 1.0f, -1.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    // top
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(0.0f, 1.0f, -1.0f);
    // bottom
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, -1.0f);
    glVertex3f(0.0f, 0.0f, -1.0f);
glEnd();

答案 1 :(得分:6)

没有可以绘制立方体的原生opengl方法。

所以你必须绘制6个四边形来绘制一个立方体。