gpt4 book ai didi

c++ - 围绕 OpenGL 场景中的对象旋转有困难

转载 作者:行者123 更新时间:2023-11-30 05:35:49 30 4
gpt4 key购买 nike

在阅读了很多关于绕相机旋转的指南并询问了一些关于 SO 的其他问题后,我想到了 SSCCE我到目前为止所拥有的。也许这样其他人会更容易理解我需要什么,对我来说答案是什么。

到目前为止它看起来像这样:

animation

绘制对象的代码:

glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0,0,0);
glColor3f(1.0,0.0,0.0);
fig.DrawBox(5.0f,5.0f,-5.0f,-5.0f,5.0f,-5.0f);
glPopMatrix();

glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);

glTranslatef(2,0,0);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0,0,0);
glColor3f(0.0,1.0,1.0);
fig.DrawBox(1.5f,1.5f,-1.5f,-1.5f,1.5f,-1.5f);

glTranslatef(-4, 1.5, 0);
glColor3f(1.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);

glTranslatef(4-1, 1.5-0.5, 0);
glColor3f(0.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();

translate 是在导航期间更新位置,rot 是旋转。

红色框是一个主容器,所有东西都在里面。 “相机”可以在它外面,但在旋转过程中它也应该旋转。至于小盒子,我希望能够围绕它们中的每一个旋转。到目前为止,我可以围绕这个绿色的轨道运行,但是当我移动到紫色时,它就不起作用了。

在我的最终应用程序中,我将有数百个对象,所以我不确定当前只使用一个 glPushMatrix() glPopMatrix() 的方法是否是最好的主意(我担心它们的正确放置),但是当我对每个对象使用 push pop 时,每个对象都会根据自己的轴心点旋转。

编辑:

我一直在研究我的代码,并且我设法取得了比以前更多的成就。虽然它仍然不是我要找的东西。

我的代码:

glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0,0,0);
glColor3f(1.0,0.0,0.0);
fig.DrawBox(5.0f,5.0f,-5.0f,-5.0f,5.0f,-5.0f);
glPopMatrix();

glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
glRotatef(-rot, 0.0, 1.0, 0.0);
glTranslatef(4,0,0);
glPushMatrix();

glPushMatrix();
glTranslatef(2,0,0);
glColor3f(0.0,1.0,1.0);
fig.DrawBox(1.5f,1.5f,-1.5f,-1.5f,1.5f,-1.5f);
glPopMatrix();

glPushMatrix();
glTranslatef(-4, 1.5, 0);
glColor3f(1.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();

glPushMatrix();
glTranslatef(-1, -0.5, 0);
glColor3f(0.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();

glPopMatrix();
glPopMatrix();

现在的样子: enter image description here

如您所见,我能够移动旋转点,但同时我已将整个组移动到框内。我不想那样做。

最佳答案

经过多次更改和想法后,我终于找到了实现我所寻找目标的正确方法。原来我需要切换翻译顺序:

glPushMatrix();
glTranslatef(0, translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(translate[0], 0,0);
glColor3f(1.0,0.0,0.0);
fig.DrawBox(5.0f,5.0f,-5.0f,-5.0f,5.0f,-5.0f);
glPopMatrix();

glPushMatrix();
glTranslatef(0, translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(translate[0],0,0);

glPushMatrix();
glPushMatrix();
glTranslatef(2,0,0);
glColor3f(0.0,1.0,1.0);
fig.DrawBox(1.5f,1.5f,-1.5f,-1.5f,1.5f,-1.5f);
glPopMatrix();

glPushMatrix();
glTranslatef(-4, 1.5, 0);
glColor3f(1.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();

glPushMatrix();
glTranslatef(-1, -0.5, 0);
glColor3f(0.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();
glPopMatrix();
glPopMatrix();

现在无论相机在哪里,旋转点都在我面前。

关于c++ - 围绕 OpenGL 场景中的对象旋转有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33787192/

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