【问题标题】:How to draw 3D terrain with triangles?如何用三角形绘制 3D 地形?
【发布时间】:2013-11-01 16:09:48
【问题描述】:

我有一个代表一些 3D 地形的 3D 布尔数组。目前我可以通过在数组中x y和z指定的位置绘制一个点来绘制它,它看起来像这样。

我不知道如何使用三角形绘制它,所以它看起来像真实的地形。我也不想把每个都画成一个立方体。

是否有任何算法可以获取要绘制的点(请记住,为了提高效率,只应绘制陆地外部的点)?

【问题讨论】:

  • 只要假设陆地是一对一的(也就是说,对于每个平面点,都只有一个高度相关联,例如,对于每个 (x,y) 坐标对,有恰好一个 z 关联),那么您可以将最接近的 n 点连接到每个点,并用线作为伪三角化。可能还有其他(更好的)方法可以做到这一点。
  • @abiessu no 对于每个 (x, y) 坐标都有 HEIGHT 多个 z。
  • Isosurface 提取,可能通过marching cubes
  • @genpfault 谢谢你很有帮助:)

标签: java opengl 3d rendering lwjgl


【解决方案1】:

绝对惊人的问题!我忍不住要玩盒子,因为盒子很性感。制作省略隐藏面的盒子实际上相当容易。

以下算法在您的网格中获取true 的3D 位置列表,只需扫描网格并填充数组即可轻松获得。同样使用这种数据格式,您可以存储更大的网格,前提是地形相当稀疏。首先,为我的斯巴达式 for-each 循环道歉,我只想将代码放在适当的位置,避免编写数十个函数对象或使用 lambdas。也很抱歉使用 C++ 而不是 Java,家里没有 javac,反正我对它也不是很好。如下:

#include <vector>
#include <map>
#include <set>
#include <utility>
#include <assert.h>
#include <math.h>

struct Pos3 {
    int x, y, z;

    Pos3(int _x = 0, int _y = 0, int _z = 0);
    bool operator <(const Pos3 &other) const;
};

std::vector<int> index_buffer;
std::vector<float> vertex_buffer;

void onInitialize()
{
    const int N = 32;
    std::vector<Pos3> points;
    GeneratePoints(points, N);
    // input: bunch of points in NxNxN box (easy to get from a boolean array,
    // can have much larger terrains if stored like this)

    std::set<Pos3> point_set;
    point_set.insert(points.begin(), points.end());
    // put all the points to a set to be able to lookup neighbors (not needed with an array)

    std::vector<std::vector<int> > polygons;
    polygons.reserve(3 * points.size()); // guess
    std::map<Pos3, int> vertex_map;
    for(size_t i = 0, n = points.size(); i < n; ++ i) {
        Pos3 p = points[i], corners[8] = {
            p, Pos3(p.x + 1, p.y, p.z), Pos3(p.x + 1, p.y + 1, p.z), Pos3(p.x, p.y + 1, p.z),
            Pos3(p.x, p.y, p.z + 1), Pos3(p.x + 1, p.y, p.z + 1), Pos3(p.x + 1, p.y + 1, p.z + 1),
            Pos3(p.x, p.y + 1, p.z + 1)
        };
        // get corners of a cube

        static const int sides[][3 + 4] = {
            0, -1,  0,  4, 5, 1, 0,          1, 0, 0,   5, 6, 2, 1,
            0,  1,  0,  6, 7, 3, 2,         -1, 0, 0,   7, 4, 0, 3,
            0,  0, -1,  0, 1, 2, 3,          0, 0, 1,   7, 6, 5, 4
        };
        // directions and side quad indices

        for(int j = 0; j < 6; ++ j) {
            Pos3 n(p.x + sides[j][0], p.y + sides[j][1], p.z + sides[j][2]); // position of a neighbor
            if(point_set.find(n) != point_set.end())
                continue; // have a neighbor, will not triangulate this side

            polygons.resize(polygons.size() + 1);
            std::vector<int> &poly = polygons.back(); // or use emplace_back() in c++11
            poly.resize(4); // form quads
            for(int v = 0; v < 4; ++ v) {
                Pos3 vert = corners[sides[j][3 + v]];
                std::map<Pos3, int>::iterator it; // use map to reuse vertices
                if((it = vertex_map.find(vert)) == vertex_map.end())
                    vertex_map[vert] = poly[v] = vertex_map.size(); // new vertex
                else
                    poly[v] = (*it).second; // existing vertex
            }
        }
        // generate sides, skip invisible sides
        // note that this still triangulates cavities, would have to flood-fill
        // outside area and then set all that is not outside to opaque (did not
        // solve that as this is also a valid behavior)
    }

    vertex_buffer.resize(vertex_map.size() * 3);
    for(std::map<Pos3, int>::const_iterator it = vertex_map.begin(), e = vertex_map.end(); it != e; ++ it) {
        size_t i = (*it).second * 3;
        vertex_buffer[i + 0] = ((*it).first.x + .5f) / (N + 1) * 2 - 1;
        vertex_buffer[i + 1] = ((*it).first.y + .5f) / (N + 1) * 2 - 1;
        vertex_buffer[i + 2] = ((*it).first.z + .5f) / (N + 1) * 2 - 1;
    }
    // convert points from the discrete domain
    // to a unit 3D cube centered around the origin

    index_buffer.reserve(polygons.size() * 2 * 3); // approximate number of triangles
    for(size_t i = 0, n = polygons.size(); i < n; ++ i) {
        const std::vector<int> &poly = polygons[i];
        for(size_t j = 2, n = poly.size(); j < n; ++ j) {
            index_buffer.push_back(poly[0]);
            index_buffer.push_back(poly[j]);
            index_buffer.push_back(poly[j - 1]);
        }
    }
    // convert polygons (those are actually quads) to triangles
}

还有一些生成法线的代码(为清楚起见省略),输出如下所示:

形状是在离散格上生成的 Julia 集,当你把它转过来时,你可能会认出它。

这实际上与通过 Delaunay 三角剖分得到的结果非常相似,如果您可以轻松移除内部点的话。生成的形状是空心的。形状中可能有一些“气泡”,以防布尔值也包含气泡(Julia 不会出现)。这很容易通过对布尔值进行泛洪填充以填充它们来解决。

接下来,我们可以应用 Catmull-Clark 细分以获得更平滑的网格:

typedef std::map<std::pair<int, int>, std::pair<size_t, int> > EdgeMap;

static bool Get_EdgeID(size_t &eid, int a, int b, EdgeMap &edges)
{
    std::pair<int, int> e(std::min(a, b), std::max(a, b));
    EdgeMap::iterator it = edges.find(e);
    if(it == edges.end()) {
        edges[e] = std::make_pair(eid = edges.size(), 1); // id, count
        return true; // new edge
    } else {
        eid = (*it).second.first; // get id
        ++ (*it).second.second; // increase count
        return false; // no new edge
    }
}

void CatClark(std::vector<std::vector<int> > &src_quads, std::vector<float> &src_verts)
{
    const static float vpw[4] = {9.0f, 3.0f, 1.0f, 3.0f};
    const static float epw[4] = {3.0f, 3.0f, 1.0f, 1.0f};

    std::vector<std::vector<int> > dst_quads(src_quads.size() * 4, std::vector<int>(4)); // will produce quads
    std::vector<float> dst_verts(src_verts.size() + src_quads.size() * 3, 0); // alloc s¨pace for vertices

    EdgeMap edges;
    std::vector<int> face_valences(src_verts.size() / 3, 0);
    const size_t off_vp = src_quads.size(), off_ep = off_vp + src_verts.size() / 3;
    for(size_t j = 0; j < off_vp; ++ j) {
        assert(src_quads[j].size() == 4); // otherwise won't work
        size_t eid[4];
        for(int k = 0; k < 4; ++ k) {
            int quad[4];
            for(int i = 0; i < 4; ++ i)
                quad[i] = src_quads[j][(i + k) & 3]; // get the 4 vertices (but rotate them each k iteration)
            if(Get_EdgeID(eid[k], quad[0], quad[1], edges)) // create edges
                dst_verts.insert(dst_verts.end(), 3, .0f); // must add new vertex to accomodate subdivided edge point
            ++ face_valences[quad[0]]; // update face-valence
            for(int n = 0; n < 3; ++ n)
                dst_verts[j * 3 + n] += 0.25f * src_verts[quad[0] * 3 + n]; // increment face point 
            for(int i = 0; i < 4; ++ i) {
                for(int n = 0; n < 3; ++ n) {
                    dst_verts[(off_vp + quad[0]) * 3 + n] += vpw[i] * src_verts[quad[i] * 3 + n]; // incremente vertex point
                    dst_verts[(off_ep + eid[k]) * 3 + n] += epw[i] * src_verts[quad[i] * 3 + n]; // increment edge point
                }
            }
        }
        for(int k = 0; k < 4; ++ k) { // make child faces
            dst_quads[4 * j + k][0] = j;
            dst_quads[4 * j + k][4] = off_ep + eid[(3 + k) & 3]; 
            dst_quads[4 * j + k][5] = off_ep + eid[(0 + k) & 3]; 
            dst_quads[4 * j + k][6] = off_vp + src_quads[j][k];
        }
    }
    for(size_t j = 0, n = src_verts.size() / 3; j < n; ++ j) {
        for(int n = 0; n < 3; ++ n)
            dst_verts[(off_vp + j) * 3 + n] *= 0.0625f / float(face_valences[j]);
    }
    for(EdgeMap::const_iterator it = edges.begin(), e = edges.end(); it != e; ++ it) {
        size_t j = (*it).second.first;
        float rvalence = 0.1250f / float((*it).second.second);
        for(int n = 0; n < 3; ++ n)
            dst_verts[(off_ep + j) * 3 + n] *= rvalence;
    }
    dst_quads.swap(src_quads);
    dst_verts.swap(src_verts);
}

该算法适用于来自 Iñigo 'iq' Quilez / rgba 的 STL 容器,“Tricks and techniques for rgba's past and future intros”,Breakpoint,2007。

这提供的输出与使用行进立方体/行进四面体/行进三角形得到的输出非常相似,只是它的分辨率始终高于原始晶格的分辨率(使用上述方法,您可以轻松更改三角剖分分辨率)。对相同数据的看法略有不同:

或者没有线框:

完整的源代码以及 Visual Studio 工作区和 win32 二进制文件可以在 here 找到。它使用 GLUT 和旧的固定功能管道来显示生成的几何图形(只是为了简单和便携,否则我必须包含 GLEW 等)。我真的很喜欢玩弄你的问题,我希望你会喜欢输出...

如果你想使用行进方块,你可以在网上找到很多演示,或者查看this real-time water simulation I did a couple years ago

【讨论】:

  • 天哪!令人难以置信的答案,我觉得你应该得到超过 100 分的帮助:-D
  • 我希望它很适合您的数据。如果没有,也许您可​​以发布一个示例,我们可以解决一些问题。谢谢你的诱饵:)。
【解决方案2】:

如果您有描述地形外壳的点,则此软件包非常适合(快速)计算该组点的 Delaunay 三角剖分:

http://www.cs.bgu.ac.il/~benmoshe/DT/

您可以稍后从三角剖分中绘制每个三角形。我做了几个或方法将所有内容转换回双精度以供 JOGL 使用,您可能会觉得有帮助:

public static ArrayList<Point_dt[]> DTtoTriListDT(Delaunay_Triangulation DT){
ArrayList<Point_dt[]> triangles = new ArrayList<Point_dt[]>();
Point_dt[] triangle = new Point_dt[3];

Iterator<Triangle_dt> surface = DT.trianglesIterator();
while(surface.hasNext()){
    Triangle_dt tri = surface.next();
    triangle[0] = tri.p1();
    triangle[1] = tri.p2();
    triangle[2] = tri.p3();
    triangles.add(triangle);
}

return triangles;}

public static ArrayList<double[][]> DTtoTriList(Delaunay_Triangulation DT){
ArrayList<Point_dt[]> trianglesdt = Algebra.DTtoTriListDT(DT);
ArrayList<double[][]> triangles = new ArrayList<double[][]>();
double[][] triangle = new double[3][3];

Iterator<Point_dt[]> surface = trianglesdt.iterator();
while(surface.hasNext()){
    Point_dt[] tri = surface.next();

    triangle[0][0] = tri[0].x();
    triangle[0][1] = tri[0].y();
    triangle[0][2] = tri[0].z();

    triangle[1][0] = tri[1].x();
    triangle[1][1] = tri[1].y();
    triangle[1][2] = tri[1].z();

    triangle[2][0] = tri[2].x();
    triangle[2][1] = tri[2].y();
    triangle[2][2] = tri[2].z();

    triangles.add(triangle);
}

return triangles;

}

我在代数课(仅供参考)中掌握了所有内容。 使用最后一种方法,您将获得一个 ArrayList,其中每个条目上包含三个双精度的树集(每个集合包含每个点的三个坐标,每个条目是一个三角形)。

【讨论】:

    猜你喜欢
    • 2020-12-15
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2017-12-15
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    相关资源
    最近更新 更多