在opengl中渲染迷宫

时间:2013-05-07 17:41:18

标签: c++ opengl graphics 3d

所以我通过实现Prim的算法成功创建了我的迷宫。结果存储在一个2D单元格中,每个单元格有一个北,南,东和西(代表4个不同的墙)。

我真正挣扎的部分是使用立方体在3D中渲染。每个立方体代表一个墙,我只想在那个墙那边渲染立方体。下面是我当前的代码,但它无法正常工作(导致绘制太多立方体/不存在迷宫)。

任何帮助将不胜感激。如果需要更多信息,请告诉我,我会尽快发布。

void Maze::drawMaze(vector<vector<Cell> > maze) {    
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (maze[i][j].south == 1) {
                glColor4f(0, .2, 0, 0);
                glPushMatrix();
                glTranslatef(j * 2 - 2, 0, i * 2);
                glutSolidCube(2);
                glPopMatrix();
            }
            if (maze[i][j].north == 1) {
                glColor4f(0, .2, 0, 0);
                glPushMatrix();
                glTranslatef(j * 2 + 2, 0, i * 2);
                glutSolidCube(2);
                glPopMatrix();
            }
            if (maze[i][j].east == 1) {
                glColor4f(0, .2, 0, 0);
                glPushMatrix();
                glTranslatef(j * 2, 0, i * 2 + 2);
                glutSolidCube(2);
                glPopMatrix();
            }
            if (maze[i][j].west == 1) {
                glColor4f(0, .2, 0, 0);
                glPushMatrix();
                glTranslatef(j * 2, 0, i * 2 - 2);
                glutSolidCube(2);
                glPopMatrix();
            }
        }
    }
}

这是一个打印方法,我必须显示正确的迷宫,只是无法弄清楚如何将其转换为正确的opengl。

void Maze::PrintMaze(){
    for(int i = 0; i < 2*10; ++i){
        cout << "_";
    }
    cout << endl;
    for (int i = 0; i < 10; ++i){
        cout << "|";
        for (int j = 0; j < 10; ++j){
            Cell c = maze[i][j];
            cout << (c.south == 0 ? " " : "_");
            if (c.east == 0)
                cout << " ";
            else cout << "|";
        }
        cout << endl;
    }
}
____________________
| | | |_  | | |  _  |
|  _|_   _   _  | |_|
|      _|_  |_    | |
| |_| | |_ _  |_|_ _|
|   |  _ _  |_ _|  _|
|_|_|  _| |_|  _|  _|
|_     _ _  | |_  | |
| |_| |   |  _ _ _  |
|      _|_|      _| |
|_|_|_ _|_ _|_|_|_ _|

1 个答案:

答案 0 :(得分:2)

两点:

  • 您是否正确放置相机位置以查看ZX平面?
  • 您的立方体宽度不是太宽?尝试做一个更好的立方体并使用glScale使它更高

修改

试试这段代码:

void Maze::drawMaze(vector<vector<Cell> > maze) {    
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (maze[i][j].south == 1) {
                glColor4f(0, .2, 0, 0);
                glPushMatrix();
                glTranslatef(j * 2 - 2, 0, i * 2);
                glScalef(1, 1, 0.25);   // Added here
                glutSolidCube(2);
                glPopMatrix();
            }
            if (maze[i][j].north == 1) {
                glColor4f(0, .2, 0, 0);
                glPushMatrix();
                glTranslatef(j * 2 + 2, 0, i * 2);
                glScalef(1, 1, 0.25);   // Added here
                glutSolidCube(2);
                glPopMatrix();
            }
            if (maze[i][j].east == 1) {
                glColor4f(0, .2, 0, 0);
                glPushMatrix();
                glTranslatef(j * 2, 0, i * 2 + 2);
                glScalef(0.25, 1, 1);   // Added here
                glutSolidCube(2);
                glPopMatrix();
            }
            if (maze[i][j].west == 1) {
                glColor4f(0, .2, 0, 0);
                glPushMatrix();
                glTranslatef(j * 2, 0, i * 2 - 2);
                glScalef(0.25, 1, 1);  // Added here
                glutSolidCube(2);
                glPopMatrix();
            }
        }
    }
}