所以我通过执行 Prim 的算法成功地创建了我的迷宫。结果存储在二维单元阵列中,其中每个单元都有北、南、东和西(代表 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;
}
}
____________________
| | | |_ | | | _ |
| _|_ _ _ | |_|
| _|_ |_ | |
| |_| | |_ _ |_|_ _|
| | _ _ |_ _| _|
|_|_| _| |_| _| _|
|_ _ _ | |_ | |
| |_| | | _ _ _ |
| _|_| _| |
|_|_|_ _|_ _|_|_|_ _|
最佳答案
两点:
- 你把相机的位置放对了看 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();
}
}
}
}
关于c++ - 在 opengl 中渲染迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16425280/