c++ - 我想通过OpenGL创建3D金字塔

标签 c++ opengl glut

我想通过仅使用框来创建3d金字塔。

我知道有一个功能可以显示金字塔,但是我想尝试仅使用带for循环的框。但是我坚持使用这种编码。所以我想问一个可以通过所示代码帮助我解决这个问题的人。

这是我的代码。

int i = 1;
int x;

for(i=1;i<7;i++)
{
    {
        for(x=0; x<i ; x++)
        {
            glPushMatrix();
            translate(1-x+sqrt(i)+(i)+1/6,0,1/6);
            glutSolidCube(1.0);
            glPopMatrix();
        }
        translate(-1, -1, 0);
    }
    rotate(rotation,0,1,0);
}
glPopMatrix();

最佳答案

对于金字塔金字塔,您将需要3个嵌套循环。我为每个维度循环播放。外循环遍历金字塔的各个层。层的大小取决于高度:

float angle = 0;

void display(void)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90, 1, 0.1, 50);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 10, 5, 0, 0, 0, 0, 0, 1);

    glClear(GL_COLOR_BUFFER_BIT);

    glRotatef(angle, 0, 0, 1);
    angle += 1.0f;

    for(int z=0; z<7; z++)
    {
        int size = 7 - z;
        for (int x = 0; x < size; x++)
        {
            for (int y = 0; y < size; y++)
            {
                glPushMatrix();
                glTranslatef(-(float)size/2 + x, -(float)size/2 + y, (float)z);
                glutSolidCube(1.0);
                glPopMatrix();
            }
        }
    }

    // [...]

关于c++ - 我想通过OpenGL创建3D金字塔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62164912/

相关文章:

c++ - C - 使用 lseek() 获得的不准确的 read() 位置

c++ - 按需渲染帧

c++ - 从 Glut 中的滚轮获取输入?

c - 如何将简单的 opengl 线图代码转换为使用顶点数组

c++ - 在C++中将数字添加到数组变量

C++ if 语句不打印所需的输出

c++ - 如何使用 OpenCV 将齐次点转换为非齐次点(C++ 中)

opengl - 为什么存在 glVertexAttribPointer 的不同变体?

python - 如何在 PyOpenGL 中将 png 图像作为图像叠加层进行 blit?

r - R 中的高性能 2D OpenGL 图形用于使用 qtpaint (qt) 或 rdyncall (SDL/OpenGL) 包快速显示光栅图像?