如何为赛车游戏设置 OpenGL 相机
我需要视图来显示道路多边形(矩形 3.f * 100.f),其中道路的消失点位于视口的 3/4 高度,最近的道路边缘作为视口的底侧。请参阅疯狂出租车游戏了解我想做的事情的示例。
我正在使用 iPhone SDK 3.1.2 默认 OpenGL ES 项目模板。
我按如下方式设置投影矩阵:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(-2.25f, 2.25f, -1.5f, 1.5f, 0.1f, 1000.0f);
然后使用 glRotatef 调整横向模式并设置相机。
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glRotatef(-90, 0.0f, 0.0f, 1.0f);
常量浮点相机角度 = 45.0f * M_PI / 180.0f; gluLookAt(0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 100.0f, 0.0f, cos(cameraAngle), sin(cameraAngle));
我的道路多边形三角形带是这样的:
static const GLfloat roadVertices[] = {
-1.5f,0.0f,0.0f, 1.5f、0.0f、0.0f、 -1.5f、0.0f、100.0f、 1.5f、0.0f、100.0f、 };
我似乎找不到 gluLookAt 的正确参数。我的消失点总是在屏幕的中心。
I need the view to show the road polygon (a rectangle 3.f * 100.f) with a vanishing point for a road being at 3/4 height of the viewport and the nearest road edge as a viewport's bottom side. See Crazy Taxi game for an example of what I wish to do.
I'm using iPhone SDK 3.1.2 default OpenGL ES project template.
I setup the projection matrix as follows:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(-2.25f, 2.25f, -1.5f, 1.5f, 0.1f, 1000.0f);
Then I use glRotatef to adjust for landscape mode and setup camera.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-90, 0.0f, 0.0f, 1.0f);
const float cameraAngle = 45.0f * M_PI / 180.0f;
gluLookAt(0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 100.0f, 0.0f, cos(cameraAngle), sin(cameraAngle));
My road polygon triangle strip is like this:
static const GLfloat roadVertices[] = {
-1.5f, 0.0f, 0.0f,
1.5f, 0.0f, 0.0f,
-1.5f, 0.0f, 100.0f,
1.5f, 0.0f, 100.0f,
};
And I can't seem to find the right parameters for gluLookAt. My vanishing point is always at the center of the screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想以真正的 3D 方式做到这一点吗?基本上你的观察点是屏幕的中心。您希望它是地平线以下屏幕的四分之一。
您可以通过投影和视图(本质上是非投影)反转最终位置来解决这个问题。
假设屏幕在 x 方向上从 -1.0f 延伸到 1.0f,在 y 方向上从 -1.0f 延伸到 1.0f,那么您希望地平线达到 (0.0f, 0.5f)。这意味着您需要向下看屏幕的四分之一(即-0.5f)。对于给定的 z(我们将随机选择 0.5f)。因此,如果我们采用您已经定义的视图以及“单位”矩阵以及“投影”矩阵,您可以将其通过 gluUnproject 传递,如下所示:
然后您可以将您的眼睛值插回到 gluLookAt 中,现在您将观察路面下方的某个点。
我确信还有无数其他方法可以做到这一点,但其中一种应该有效......
You want to do this in genuine 3D? basically your look at point is the center of the screen. What you want it to be is a quarter of the screen below the horizon.
You can work it out by inverting a final position by projection and by view (essentially unprojecting).
Assuming the screen stretches from -1.0f to 1.0f in x and -1.0f to 1.0f in y then you want the horizon to go to (0.0f, 0.5f). This means you need to look down by quarter of the screen (ie -0.5f). For a given z (we'll randomly chose 0.5f). So if we take your view as you've already defined and an "identity" matrix as well as your "projection" matrix you can then pass this through gluUnproject as follows:
You can then plug your eye values back into gluLookAt and you will now be looking at a point below the road surface.
I'm sure there are myriad other ways of doing it but that one should work...