我的目标是能够创建一个所谓的占用网格,它类似于一个瓷砖/网格游戏,看起来像附件中的图像。这是一个小机器人项目,我正在工作。所有正在做的C/C++。
因此,我希望能够:
1.改变每个网格单元的分辨率或大小。例如1x 1cm或5x 5cm等。
1.根据某些条件更改每个单元格的颜色。障碍==黑色,自由==白色等。可能会添加用户单击单元格并更改颜色。例如,如果robot从0x 0开始,则单元格为红色,然后移动到1x 1。现在0x 0应为白色,1x 1应为红色。
1.添加某种形式的滚动或跟随(可能通过MVP摄像机完成)。
我应该采用哪种OpenGL方法?我目前正在考虑:
1.有一个带有颜色属性的正方形着色器(两个三角形顶点和索引缓冲区)。然后有一个顶点数组对象/缓冲区,其中包含所有的索引和颜色,但不知道如何在运行时处理颜色的变化。
1.使每个栅格具有所有真实的世界的中心或角坐标(0x 0、0x 1、...1x1等),并使着色器绘制各个正方形(如果可能)。
1.使用NxN的纹理/图像并更新纹理像素颜色。
我只是不确定什么是最好的可伸缩性或性能方法。如果我想绘制一个10000 X10000的网格单元(例如拉小很远),或者颜色变化很大,该怎么办?
网格最动态的方面是填充颜色。例如,我可能有一个100 x100的网格。在一个示例中,所有的单元格都是白色的,然后随着机器人的移动,我改变相应单元格的颜色。或者,如果它检测到单元格中的障碍物,改变单元格的颜色。
1条答案
按热度按时间lmvvr0a81#
I came up with something that you should use for your project. It is a zoomable grid that can be resized to whatever dimensions you need. It should be relatively fast, lines are batched and quads are instanced. It has basic interaction with left and right click and the scroll wheel click and scroll, but you should be able to adapt this interface to your needs. The API should be fairly easy to use
createCell
,removeCell
, etc...A few things:
This was fun to make and I hope you can make use of it, if you have any questions let me know!
flatten
section was a bit unstructured. That flatten method was originally just to flatten a 2D models vector down to a 1D vector to be sent to the GPU (the internal_model
data). The ternary operations are determining a 2D slice of this list (think a box section within the main grid), which is how the frustum cull occurs. bottomLeft and topRight is used to calculate the bounding box of the camera frustum which gets intersected with the whole grid to get indexes into an (ordered) vector of models and colors that gets sent to the GPU, its abit hacky but it ensures operations that interact with the grid (add, remove, change color,etc...) are O(1). There was a small bug causing some cells to freeze, and it was caused by resetting the cell to uninitialized in the QuadRenderer'sremove()
method omitted it from being sent to the GPU (a performance saving optimization), and it was causing the memory to be offset incorrectly, and causing the rendering to desynchronize. So it could have been solved two ways, remove the check to send only initialized cells to the GPU (slower) or just set squares to white instead (hackier). So I chose to set removed cells to white, which means they still get rendered (even though they are camouflage with the clear color). You might be able to change so white cells don't get rendered either, but it shouldn't be too much lost performance, considering you keep cells uninitialized to start (don't for god sake initialize them to white! :) )The remove() function can be changed to:
Here is the code: