使用 OpenGL 创建平铺世界
我计划用 OpenGL 创建一个平铺世界,其中的平铺块和房屋会稍微旋转,世界中的建筑物将由模型组成。 谁能建议我应该使用什么投影(正交、透视),以及如何设置视图矩阵(使用 OpenGL)?
如果你无法弄清楚我打算创造什么风格的世界,请看看这个游戏: http://www.youtube. com/watch?v=i6eYtLjFu-Y&feature=PlayList&p=00E63EDCF757EADF&index=2
I'm planning to create a tiled world with OpenGL, with slightly rotated tiles and houses and building in the world will be made of models.
Can anybody suggest me what projection(Orthogonal, Perspective) should I use, and how to setup the View matrix(using OpenGL)?
If you can't figure what style of world I'm planning to create, look at this game:
http://www.youtube.com/watch?v=i6eYtLjFu-Y&feature=PlayList&p=00E63EDCF757EADF&index=2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用正交投影与透视投影完全是一种艺术风格的选择。你所说的 Pokemon 系列是正交的——事实上,它完全是分层的 2D 精灵(不涉及 3D)。
OpenGL 没有 VIEW 矩阵。它有一个模型视图矩阵和一个投影矩阵。对于 Pokemon 风格的关卡,我建议使用简单的 glOrtho 进行投影。
假设您的世界位于 XY 空间(图块、相机和其他对象的坐标采用 [x, y, 0] 形式)。如果单个图块的大小为 1,1,那么像 glOrtho(12, 9, -10, 10) 这样的东西将是一个很好的投影矩阵(12 宽,9 高,Z=0 是地平面)。
对于 MODELVIEW,您可以先加载标识,然后通过图块位置加载 glTranslate(),然后通过相机位置的负值加载 glTranslate(),然后再绘制几何图形。如果您希望能够旋转相机,则可以通过两个 Translate() 之间相机旋转的负值(逆)来旋转 glRotate() 。最后,得到以下矩阵链:
输出 = 投影 × (CameraTranslation-1 × CameraRotation-1 × ModelLocation × ModelRotation) × 输入
括号中的部分是 MODELVIEW,“-1”表示“逆”,这对于平移和转置对于旋转来说实际上是负数。
如果你也想旋转你的模型,你通常首先这样做(在第一个 glTranslate() 之前)。
最后,我建议 OpenGL 论坛 (www.opengl.org) 或 www.gamedev.net 的 OpenGL 子论坛是一个更好的地方来问这个问题:-)
Using Orhtogonal vs Perspective projection is entirely an art style choice. The Pokemon serious you're talking about is orthogonal -- in fact, it's entirely layered 2D sprites (no 3D involved).
OpenGL has no VIEW matrix. It has a MODELVIEW matrix and a PROJECTION matrix. For Pokemon-style levels, I suggest using simple glOrtho for the projection.
Let's assume your world is in XY space (coordinates for tiles, cameras, and other objects are of the form [x, y, 0]). If a single tile is sized 1,1, then something like glOrtho(12, 9, -10, 10) would be a good projection matrix (12 wide, 9 tall, and Z=0 is the ground plane).
For MODELVIEW, you can start by loading identity, glTranslate() by the tile position, and then glTranslate() by the negative of the camera position, before you draw your geometry. If you want to be able to rotate the camera, you glRotate() by the negative (inverse) of the camera rotation between the two Translate()s. In the end, you end up with the following matrix chain:
output = Projection × (CameraTranslation-1 × CameraRotation-1 × ModelLocation × ModelRotation) × input
The parts in parens are MODELVIEW, and the "-1" means "inverse" which really is negative for translation and transpose for rotation.
If you want to rotate your models, too, you generally do that first of all (before the first glTranslate().
Finally, I suggest the OpenGL forums (www.opengl.org) or the OpenGL subforums of www.gamedev.net might be a better place to ask this question :-)
在我看来,该视频游戏使用的投影看起来是倾斜的。有许多不同的投影,不仅仅是透视和正交。请参阅此处以获取最常见的列表: http://en.wikipedia.org /wiki/File:Graphical_projection_comparison.png
您肯定需要透视图,仅围绕 X 轴进行固定旋转。大约45-60度或以上。如果您不关心自己设置投影代码,GLU 库中的 gluPerspective 函数会很方便。
假设 OpenGL 2.1:
gluPerspective 的最后两个参数是到近剪裁平面和远剪裁平面的距离。它们的值取决于您在环境中使用的规模。
The projection used by that video game looks Oblique to me. There are many different projections, not just perspective and orthographic. See here for a list of the most common ones: http://en.wikipedia.org/wiki/File:Graphical_projection_comparison.png
You definitely want perspective, with a fixed rotation around the X-axis only. Around 45-60 degrees or thereof. If you don't care about setting up the projection code yourself, the gluPerspective function from the GLU library is handy.
Assuming OpenGL 2.1:
The last two parameters to gluPerspective is the distance to the near and far clipping planes. Their values depend on the scale you use for the environment.