Java 如何使用三角形绘制三维地形?

Java 如何使用三角形绘制三维地形?,java,opengl,3d,rendering,lwjgl,Java,Opengl,3d,Rendering,Lwjgl,我有一个三维布尔数组,表示一些三维地形。目前,我可以通过在数组中x、y和z指定的位置绘制一个点来绘制它,看起来是这样的 我搞不懂的是我怎么用三角形画这个,所以它看起来像真实的地形。我也不想把每一个都画成一个立方体 是否有任何算法可以获取要绘制的点请记住,为了提高效率,只应绘制陆地外部的点?如果您有描述地形外壳的点,此软件包非常适合快速计算该点集的Delaunay三角剖分: 稍后可以从三角剖分中绘制每个三角形。我提出了一些方法,可以将所有内容转换回双精度,以便与JOGL一起使用,您可能会发现这些方

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

我搞不懂的是我怎么用三角形画这个,所以它看起来像真实的地形。我也不想把每一个都画成一个立方体


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

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

稍后可以从三角剖分中绘制每个三角形。我提出了一些方法,可以将所有内容转换回双精度,以便与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;}

}

我在代数课上都有,仅供参考。
使用最后一种方法,您将获得一个ArrayList,其中每个条目上有三个双倍树集。每个集合包含每个点的三个坐标,每个条目是一个三角形。

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

稍后可以从三角剖分中绘制每个三角形。我提出了一些方法,可以将所有内容转换回双精度,以便与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;}

}

我在代数课上都有,仅供参考。
使用最后一种方法,您将得到一个ArrayList,其中每个条目上有三个双倍树集,每个集合包含每个点的三个坐标,每个条目是一个三角形。

绝对令人惊讶的问题!我忍不住要玩盒子,因为盒子很性感。实际上,生成省略了隐藏面的长方体是相当容易的

以下算法获取网格中真实的3D位置列表,只需扫描网格并填充数组即可轻松获得。此外,如果地形相当稀疏,使用这种数据格式,您可以存储更大的网格。首先,我为我的斯巴达式的每一个循环道歉,我只是想把代码放在适当的位置,避免编写几十个函数对象或使用lambda。同样,对于使用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容器,rgba过去和未来简介的技巧和技巧,Breakpoint,2007

这使输出与移动立方体/移动四面体/移动三角形的输出非常相似,只是它的分辨率始终高于原始晶格的分辨率。使用上述方法,您可以轻松更改三角剖分分辨率。相同数据的视图略有不同:

或不带线框:

完整的源代码以及VisualStudioWorkspace和win32二进制文件可以在中找到。它使用GLUT和旧的固定函数管道来显示生成的几何体,只是为了简单性和可移植性,否则我必须包括GLEW之类的。我真的很喜欢玩弄你的问题,我希望你会喜欢输出


如果你想使用marching cubes,你可以在网上找到很多演示,或者去看看。

绝对是个令人惊讶的问题!我忍不住要玩盒子,因为盒子很性感。实际上,生成省略了隐藏面的长方体是相当容易的

以下算法获取网格中真实的3D位置列表,只需扫描网格并填充数组即可轻松获得。此外,如果地形相当稀疏,使用这种数据格式,您可以存储更大的网格。首先,我为我的斯巴达式的每一个循环道歉,我只是想把代码放在适当的位置,避免编写几十个函数对象或使用lambda。同样,对于使用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
}
还有一些代码生成法线,为了清晰起见,进行了编辑,输出如下所示:

这个形状是朱丽亚s et在离散晶格上生成,当您将其旋转时,您可能会识别该形状

这实际上非常类似于通过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容器,rgba过去和未来简介的技巧和技巧,Breakpoint,2007

这使输出与移动立方体/移动四面体/移动三角形的输出非常相似,只是它的分辨率始终高于原始晶格的分辨率。使用上述方法,您可以轻松更改三角剖分分辨率。相同数据的视图略有不同:

或不带线框:

完整的源代码以及VisualStudioWorkspace和win32二进制文件可以在中找到。它使用GLUT和旧的固定函数管道来显示生成的几何体,只是为了简单性和可移植性,否则我必须包括GLEW之类的。我真的很喜欢玩弄你的问题,我希望你会喜欢输出


如果你想使用行进立方体,你可以在网上找到许多演示,或者去看看。

只要一个陆块是一对一的,也就是说,对于每个平面点,只有一个高度关联,例如,对于每个x,y坐标对,只有一个z关联,然后,可以使用直线将最接近的n个点连接到每个点,作为伪三角化。也许还有其他更好的方法可以做到这一点。@abiessu不,对于每个x,y坐标,都有许多z的高度。提取,可能是通过。@genpfault谢谢你非常有用:只要假定一个陆块是一对一的,也就是说,对于每个平面点,正好有一个高度关联,例如,对于每个x,y坐标对,正好有一个z关联,然后,可以使用直线将最接近的n个点连接到每个点,作为伪三角化。也许还有其他更好的方法可以做到这一点。@abiessu不,对于每个x,y坐标,都有许多z的高度。提取,也许是通过。@genpfault谢谢你,非常有用:天哪,废人!难以置信的回答,我觉得你应该得到超过100分,因为这是多么的有用:-我希望它很好地符合你的数据。如果没有,也许你可以发布一个样本,我们可以解决一些问题。谢谢你的诱饵:。我的天啊!难以置信的回答,我觉得你应该得到超过100分,因为这是多么的有用:-我希望它很好地符合你的数据。如果没有,也许你可以发布一个样本,我们可以解决一些问题。谢谢你的诱饵:。