OpenGL ES 3d 旋转导致意外平移?
我正在开发一个 iPhone 项目,我需要水平和垂直布局。我已经在 2D 中完美地工作了,对我的投影矩阵使用以下设置:
if(isHoriz()){
glRotatef(90.0f,0.0f,0.0f,1.0f);
SOrtho(0, 480, 0, 320, -1, 1);
}
else
SOrtho(0, 320, 0, 480, -1, 1);
问题在于当我移动到 3D 时
glViewport(0,0,320,480);
glScissor(0,0,320,480);
gluPerspective(28.8f,320/480,0.1f,1000.0f);
if(isHoriz())
glRotatef(90.0f,0.0f,0.0f,1.0f);
这似乎有效,但视图在垂直方向上偏移错误。我想要发生的是垂直显示的上半部分成为水平显示的中心。相反,垂直显示的中间似乎成为水平显示的中心。
因为这是一部 iPhone,所以无论旋转如何,窗口高度 (480) 和宽度 (320) 都保持不变。我尝试将相同的代码构建到 Windows 程序中,并且获得我想要的水平旋转效果的代码很简单。我只是改变了窗口形状,这样我就有了一个横向窗口,然后删除了调用
if(isHoriz())
glRotatef(90.0f,0.0f,0.0f,1.0f);
(我也做了类似于二维代码的事情)。
我做错了什么吗?为什么windows版本和iphone版本看起来不一样?
编辑:为了澄清,我尝试在 glRotatef 调用上方和下方添加 glTranslatef() 调用,但这似乎对我的相机角度有影响。在上述代码之后,相机代码立即应用于我的 modelView 矩阵。看起来像这样:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(mRotate.mZ,0,0,1);
glRotatef(mRotate.mY,0,1,0);
glRotatef(mRotate.mX,1,0,0);
glTranslatef(-mPos.mX,-mPos.mY,-mPos.mZ);
相机的初始值为mRotate.mX = -80,mPos.Z = -8。
I'm working on an iphone project where I need both a horizontal and a vertical layout. I've got it working in 2D perfectly well, using the following setup to my projection matrix:
if(isHoriz()){
glRotatef(90.0f,0.0f,0.0f,1.0f);
SOrtho(0, 480, 0, 320, -1, 1);
}
else
SOrtho(0, 320, 0, 480, -1, 1);
The problem lies when I move to 3D
glViewport(0,0,320,480);
glScissor(0,0,320,480);
gluPerspective(28.8f,320/480,0.1f,1000.0f);
if(isHoriz())
glRotatef(90.0f,0.0f,0.0f,1.0f);
This seems to work, but the view is offset vertically wrong. What I want to happen is that the top half of the vertical display becomes the center of the horizontal display. Instead, it seems like the middle of the vertical display becomes the center of the horizontal display.
Because this is an iphone, the window height (480) and width (320) remain constant regardless of rotation. I've tried building the same code into a windows program, and the code that gets the effect I want for the horizontal rotation is simple. I just change the window shape, so that I have a landscape window, and then remove the call to
if(isHoriz())
glRotatef(90.0f,0.0f,0.0f,1.0f);
(I also do something similar to the 2D code).
Am I doing something wrong? Why don't the windows version and the iphone version look the same?
Edit: To clarify, I've tried adding glTranslatef() calls above and below the glRotatef call, but this seems to have an effect on the angle of my camera. The camera code is applied to my modelView matrix immediately after the above code. It looks like this:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(mRotate.mZ,0,0,1);
glRotatef(mRotate.mY,0,1,0);
glRotatef(mRotate.mX,1,0,0);
glTranslatef(-mPos.mX,-mPos.mY,-mPos.mZ);
The initial values of the camera are mRotate.mX = -80, mPos.Z = -8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我仍然不确定为什么会发生这种情况,但我将旋转从投影移动到模型视图,一切正常。我认为这与 Windows 窗口的形状不同(320x480 或 480x320)与 iPhone 形状(始终为 320x480)不匹配有关。
I'm still not certain why this happened, but I moved my rotation from the projection to the modelview and everything works fine. I think it had to do with the fact that the Windows window had a varying shape (either 320x480 or 480x320) that did not match the iPhone shape (always 320x480).