OpenGL 2D 中的可变形地形 [Worms like]
我已经搜索了一段时间,并且听说过不同的方法来做到这一点,所以我想我应该来这里看看我应该做什么,
根据我收集的信息,我应该使用.. glBitmap 和 0s 和 0xFF数组中的值来制作地形。对此有何意见? 我尝试将其切换为四边形,但我不确定这是否有效以及其实现方式。
我希望地形能够有隧道,比如蠕虫。 2 维。
这是我到目前为止所尝试的,
我尝试制作一个 glBitmap,所以..
pixels = pow(2 * radius, 2);
ras = new GLubyte[pixels];
然后将它们全部设置为 0xFF,并使用 glBitmap(x, y, 0, 0, ras); 绘制它; 然后可以检查是否发生爆炸,以及是否发生爆炸,以及可以将任何像素设置为零。这是一个合理的方法吗?我不太擅长 opengl,我可以在 glBitmap 上放置纹理吗?从我所看到的来看,我认为你做不到。
I've searched for a while and I've heard of different ways to do this, so I thought I'd come here and see what I should do,
From what I've gathered I should use.. glBitmap and 0s and 0xFF values in the array to make the terrain. Any input on this?
I tried switching it to quads, but I'm not sure that is efficient and the way its meant to be done.
I want the terrain to be able to have tunnels, such as worms. 2 Dimensional.
Here is what I've tried so far,
I've tried to make a glBitmap, so..
pixels = pow(2 * radius, 2);
ras = new GLubyte[pixels];
and then set them all to 0xFF, and drew it using glBitmap(x, y, 0, 0, ras);
This could be then checked for explosions and what not and whatever pixels could be set to zero. Is this a plausible approach? I'm not too good with opengl, can I put a texture on a glBitmap? From what I've seen it I don't think you can.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用模板缓冲区。您可以在模板缓冲区中标记地形的被破坏部分,然后使用简单的四边形启用模板测试来绘制地形,而无需手动测试每个像素。
I would suggest you to use the stencil buffer. You mark destroyed parts of the terrain in the stencil buffer and then draw your terrain with stencil testing enabled with a simple quad without manually testing each pixel.
好的,这是一个高级概述,我假设您已经熟悉缓冲区对象等 OpenGL 基础知识。如果有什么不明白或者您想了解更多详细信息,请告诉我。
在计算机图形学中表示地形的最常见方法是高度场:在 X 轴和 Y 轴上规则间隔的点网格,但其 Z(高度)可以变化。高度场的每个 (X,Y) 网格点只能有一个 Z 值,因此地形中不能有“悬垂”,但通常已经足够了。
绘制高度场地形的一种简单方法是使用三角形带(或四边形,但它们已被弃用)。为简单起见,从一个角开始,沿列以之字形顺序发出顶点,然后返回到顶部并执行下一列,依此类推。有一些优化可以实现更好的性能,也有更复杂的方法来构建几何体以获得更好的外观,但这只是入门。
(我在这里假设一个矩形地形,因为这就是通常的做法;如果你真的想要一个圆形,你可以用
OK, this is a high-level overview, and I'm assuming you're familiar with OpenGL basics like buffer objects already. Let me know if something doesn't make sense or if you'd like more details.
The most common way to represent terrain in computer graphics is a heightfield: a grid of points that are spaced regularly on the X and Y axes, but whose Z (height) can vary. A heightfield can only have one Z value per (X,Y) grid point, so you can't have "overhangs" in the terrain, but it's usually sufficient anyway.
A simple way to draw a heightfield terrain is with a triangle strip (or quads, but they're deprecated). For simplicity, start in one corner and issue vertices in a zig-zag order down the column, then go back to the top and do the next column, and so on. There are optimizations that can be done for better performance, and more sophisticated ways of constructing the geometry for better appearance, but that'll get you started.
(I'm assuming a rectangular terrain here since that's how it's commonly done; if you really want a circle, you can substitute ???? and ???? for X and Y so you have a polar grid.)
The coordinates for each vertex will need to be stored in a buffer object, as usual. When you call
glBufferData()
to load the vertex data into the GPU, specify a usage parameter of eitherGL_STREAM_DRAW
if the terrain will usually change from one frame to the next, orGL_DYNAMIC_DRAW
if it will change often but not (close to) every frame. To change the terrain, callglBufferData()
again to copy a different set of vertex data to the GPU.For the vertex data itself, you can specify all three coordinates (X, Y, and Z) for each vertex; that's the simplest thing to do. Or, if you're using a recent enough GL version and you want to be sophisticated, you should be able to calculate the X and Y coordinates in the vertex shader using
gl_VertexID
and the dimensions of the grid (passed to the shader as a uniform value). That way, you only have to store the Z values in the buffer, which means less GPU memory and bandwidth consumed.