在阅读了很多关于围绕相机轨道运行的指南并询问了一些额外的问题后,我想出了SSCCE到目前为止我所拥有的内容。也许通过这种方式,其他人更容易理解我需要的东西,对我来说是什么答案。
到目前为止看起来像这样:
绘制对象的代码:
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时,每个对象都会按照自己的轴心点旋转。
修改
我一直在研究我的代码,之前我成功实现了一些目标。虽然它仍然不是我正在寻找的。 p>
我的代码:
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();
所以你可以看到,我能够移动旋转点,但同时我已将整个组移动到框内。我不想这样做。
答案 0 :(得分:1)
经过多次改变和想法后,我终于找到了实现我所寻求目标的正确方法。事实证明,我需要切换翻译顺序:
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();
现在,无论相机在哪里,旋转点都在我的前方。
答案 1 :(得分:0)
OpenGL rotating a camera around a point也许这个答案可以帮到你。我认为你必须:translate-rotate-translate。