gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:31:14 25 4
gpt4 key购买 nike

我想通过仅使用框来创建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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com