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

发布于 2024-12-12 11:23:17 字数 2066 浏览 0 评论 0原文

下面的代码在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);
}

The following code draws a labyrinth or maze in OpenGL, the result is a 2D labyrinth, what I need to do now is to draw cubes instead these quads, how can I do it?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是偏爱你 2024-12-19 11:23:18
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();
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();
南渊 2024-12-19 11:23:18

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

所以你必须画6个四边形才能画一个立方体。

There is no native opengl method that will draw a cube.

So you will have to draw 6 quads to draw a cube.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文