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

问题描述 投票:1回答:1

我想仅通过使用框来创建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();
c++ opengl glut
1个回答
0
投票

对于块金字塔,您将需要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();
            }
        }
    }

    // [...]
© www.soinside.com 2019 - 2024. All rights reserved.