创建基于立方体的 3 维游戏 [英] Creating a Cube-based 3-Dimensional Game

查看:35
本文介绍了创建基于立方体的 3 维游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个完全基于完全相同大小的立方体的 3D 游戏.我想学习如何仅使用 2 维游戏库制作我自己的 3 维游戏.目前,我正在做的事情是我有一个数组来存储游戏中每个立方体的所有中心的位置.然后,在绘制单个立方体时,我会计算出需要绘制立方体的哪 3 个面(因为您不需要绘制立方体的所有 3 个面).然后,知道立方体所有角的 3 维点,我使用相机位置、相机角度和我投影的点将这些点投影到 2 维空间.

I am trying to create a 3-dimensional game that is based entirely off of cubes of the exact same size. I wanted to learn how to make my own 3-dimensional game using only 2-dimensional game libraries. Currently, the way I am doing is that I have an array storing the locations of all the centers of each cube in the game. Then, when drawing a single cube, I figure out which 3 sides of the cube I need to draw (since you don't need to draw all 3 sides of the cube). Then, knowing the 3-dimensional points of all the corners of the cube, I project those points onto a 2 dimensional space using the camera position, camera angle, and the point I am projecting.

现在我真正的问题是:现在我可以画一个立方体,我如何画多个立方体,考虑到立方体需要按一定的顺序绘制(即需要先绘制更远的立方体,所以离我们较近的立方体出现在远离我们的立方体的顶部)?给定立方体中心列表及其大小以及相机位置/角度,如何确定首先绘制哪些立方体?

Now my real question is: Now that I can draw a single cube, how do I draw multiple cubes, considering that cubes need to be drawn in a certain order (i.e. the cubes that are further away need to be drawn first so that the cubes that are closer to us appear on top of the cubes far away from us)? How do I determine which cubes to draw first given the list of cube centers and their sizes, and the camera position/angle?

推荐答案

我学习 3D 图形已经好几年了......

its quite a few years when I learned 3D graphics ...

我使用的所有网站现在都消失了(像往常一样在 WEB 上,但有很多新网站).我是 OpenGL 用户,所以我建议使用它.我的许多学生都喜欢 NEHE 教程,因此请查看这里.另请查看 4x4 变换矩阵(齐次坐标).

All of the sites I used are gone now (as usual on the WEB but there are tons of new ones). I am OpenGL user so I recommend to use that. Many of mine students liked NEHE tutorials so look here. Also look at 4x4 transform matrices (homogenous coordinates).

Z-BufferOpenGL 中是自动的,只需使用 Z-Buffer 创建上下文(所有教程都使用它)并调用 glEnable(GL_DEPTH_TEST);

Z-Buffer is automatic in OpenGL just create context with Z-Buffer (all tutorials use it) and call glEnable(GL_DEPTH_TEST);

Face culling(跳过对象的另一面)由glEnable(GL_CULL_FACE);完成,并在同一个多边形缠绕中绘制面(CWCCW)

Face culling (skip the other side of object) is done by glEnable(GL_CULL_FACE); and drawing faces in the same polygon winding (CW or CCW)

这里有几个相关的问题:

Here few related questions:

  • Understanding 4x4 homogenous transform matrices
  • 3D (software) render pipeline computation
  • some transform matrices 3x3 and 4x4 insights
  • how to project points and vectors ... fixed pipeline OpenGL will do it for you
  • code for inverse 4x4 matrix
  • this is mine clean OpenGL app example in borland/embarcadero BDS2006 Turbo C++
  • some projection math

还有一些值得一看的好网站(强烈推荐):

And some good sites to look at (strongly recommend):

抱歉提供了那个链接列表,但我认为它们是相关的,在这里复制它们的内容太多了.现在回到你的问题如何渲染更多的立方体.我看到的选项很少(在 OpenGL 中):

Sorry for that list of links but I think they are relevant and copy their stuff here would be too much. Now back to your question how to render more cubes. I see few options (in OpenGL):

  1. 每个立方体都有自己的变换矩阵

表示它在空间中的位置和方向,然后在渲染每个立方体之前更改 GL_MODELVIEW 矩阵并绘制立方体(每个都具有相同的代码).如果您有太多的对象/立方体,它将消耗大量内存(每个立方体 +16 个浮点数).

Representing its position and orientation in space then just before rendering each cube change GL_MODELVIEW matrix and draw cube (with the same code for each). If you have too much objects/cubes it will consume big chunk of memory (+16 floats per cube).

每个立方体都相互对齐

在这种情况下,您只需要知道 3D 位置(每个立方体 +3 个浮点数),因此只需执行以下操作:

in this case you need to know just the 3D position (+3 floats per cube) so just do something like this:

glMatrixMode(GL_MODELVIEW);    // this store original matrix
glPushMatrix();
glTranslatef(x[i],y[i],z[i]); // position of i-th cube
// here render cube (like glBegin(GL_QUADS); ... or use glDraw... )
glMatrixMode(GL_MODELVIEW);    // this restore original matrix
glPopMatrix();

  • 着色器的使用

    在现代着色器管道中,您可以使用几何着色器在接收点发射立方体,但这对于 OpenGL 初学者来说太多了.但在这种情况下,您将只绘制点,着色器会将它们转换为 GPU 上的立方体,这会更快......

    in modern shader pipeline you can use geometry shader to emit cube on receiving point but this is too much for OpenGL beginner. But in this case you would draw just points and the shader will convert them to cubes on GPU which is much faster ...

    使用 VBO 或 VAO

    • VAO 是顶点数组对象(VBO 的列表)
    • VBO 是顶点缓冲对象
    • VAO is vertex array object (list of VBO's)
    • VBO is vertex buffer object


    VBO 基本上是一组参数,作为一个块复制到 GPU,而不是像 glVertex,glColor,glNormal...这要快得多.这允许您创建空间模型(所有立方体)并以足够的速度一次绘制它,除非您达到 GPU/CPU/MEM 速度限制


    VBO is basically an array of parameters copied to GPU as one chunk and not by individual calls like glVertex,glColor,glNormal... which is much much faster. This allows you to create model of your space (all cubes) and draw it at once with enough speed unless you hit the GPU/CPU/MEM speed limits

    VAO 类似地将更多 VBO 组合在一起,因此您只需要为每个对象绑定一个 VAO 而不是一个 VBOstrong> 为每个参数进一步减少渲染所需的 API 调用次数.

    VAO similarly groups more VBOs together so you need to bind just single VAO per object instead of one VBO for each one parameter further reducing the API calls number needed for rendering.

    这篇关于创建基于立方体的 3 维游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆