Blame projects/asteroid/mesh.h

223ac6
#ifndef MESH_H
223ac6
#define MESH_H
223ac6
223ac6
223ac6
#include <vector></vector>
223ac6
223ac6
#include "geometry.h"
223ac6
223ac6
223ac6
class Mesh {
223ac6
public:
223ac6
    class Vertex {
223ac6
    public:
223ac6
        Vector3 pos;
223ac6
        Vector3 normal;
223ac6
        explicit inline Vertex(const Vector3 &pos = Vector3(), const Vector3 &normal = Vector3()):
223ac6
            pos(pos), normal(normal) { }
223ac6
    };
223ac6
    
223ac6
    class Triangle {
223ac6
    public:
223ac6
        int v[3];
223ac6
        explicit inline Triangle(int v0 = 0, int v1 = 0, int v2 = 0)
223ac6
            { v[0] = v0; v[1] = v1; v[2] = v2; }
223ac6
    };
223ac6
223ac6
    typedef std::vector<vertex> VertexList;</vertex>
223ac6
    typedef std::vector<triangle> TriangleList;</triangle>
223ac6
    
223ac6
    VertexList vertices;
223ac6
    TriangleList triangles;
223ac6
    
223ac6
    mutable unsigned int vbuf, ibuf, elements;
223ac6
223ac6
    inline Mesh(): vbuf(), ibuf() { }
223ac6
    inline ~Mesh() { dirty(); }
223ac6
    
223ac6
    void createbuf() const;
223ac6
    void dirty() const;
223ac6
    void clear();
223ac6
    void draw() const;
223ac6
};
223ac6
223ac6
223ac6
class LinkedMesh {
223ac6
public:
223ac6
    class Vertex {
223ac6
    public:
223ac6
        Vector3 pos;
223ac6
        Real value;
223ac6
        explicit inline Vertex(const Vector3 &pos = Vector3(), Real value = Real()):
223ac6
            pos(pos), value(value) { }
223ac6
    };
223ac6
    
223ac6
    class Edge {
223ac6
    public:
223ac6
        int v[3];
223ac6
        int e[2];
223ac6
        explicit inline Edge(int v0 = 0, int v2 = 0)
223ac6
            { v[0] = v0; v[1] = -1; v[2] = v2; e[0] = 0; e[1] = 0; }
223ac6
    };
223ac6
223ac6
    class Triangle {
223ac6
    public:
223ac6
        int e[3];
223ac6
        bool d[3];
223ac6
        explicit inline Triangle(
223ac6
            int e0 = 0, bool d0 = false,
223ac6
            int e1 = 0, bool d1 = false,
223ac6
            int e2 = 0, bool d2 = false )
223ac6
        {
223ac6
            e[0] = e0; e[1] = e1; e[2] = e2;
223ac6
            d[0] = d0; d[1] = d1; d[2] = d2;
223ac6
        }
223ac6
    };
223ac6
    
223ac6
    class Level {
223ac6
    public:
223ac6
        Real k;
223ac6
        int vertex0, vertex1;
223ac6
        int edge0, edge1;
223ac6
        int triangle0, triangle1;
223ac6
        Level():
223ac6
            k(1),
223ac6
            vertex0(), vertex1(),
223ac6
            edge0(), edge1(),
223ac6
            triangle0(), triangle1() { }
223ac6
    };
223ac6
    
223ac6
    typedef std::vector<vertex> VertexList;</vertex>
223ac6
    typedef std::vector<edge> EdgeList;</edge>
223ac6
    typedef std::vector<triangle> TriangleList;</triangle>
223ac6
    typedef std::vector<level> LevelList;</level>
223ac6
    typedef Vector3 (*VertexFunc)(const Vertex&, Real, Real);
223ac6
    
223ac6
223ac6
    VertexList vertices;
223ac6
    EdgeList edges;
223ac6
    TriangleList triangles;
223ac6
    LevelList levels;
223ac6
223ac6
    
223ac6
    static Vertex average(const Vertex &a, const Vertex &b);
223ac6
    static Vector3 sphere_func(const Vertex &v, Real r, Real kr);
223ac6
    static Vector3 z_func(const Vertex &v, Real r, Real kr);
223ac6
223ac6
    static Real random();
223ac6
    
223ac6
    void check() const;
223ac6
    
223ac6
    void clear();
223ac6
    void next_level(Real k);
223ac6
    void generate_level(Real k = sqrt2);
223ac6
    void generate_levels(int count, Real k = sqrt2);
223ac6
    void smooth();
223ac6
    void smooth(int count);
223ac6
    
223ac6
    void triangle(Real k = 1);
223ac6
    void tetrahedron(Real k = 1);
223ac6
    void icosahedron(Real k = 1);
223ac6
    
223ac6
    void to_mesh(Mesh &mesh, VertexFunc func, Real r = 0, Real kr = 1, int level = -1) const;
223ac6
223ac6
    inline void to_sphere_mesh(Mesh &mesh, Real r = 1, Real kr = 0.5, int level = -1) const
223ac6
        { to_mesh(mesh, &sphere_func, r, kr, level); }
223ac6
    inline void to_z_mesh(Mesh &mesh, Real r = 0, Real kr = 1, int level = -1) const
223ac6
        { to_mesh(mesh, &z_func, r, kr, level); }
223ac6
};
223ac6
223ac6
223ac6
#endif