summaryrefslogtreecommitdiff
path: root/src/graphics/shape.h
blob: 88fc33e36351a9f2cbb654aae183eee7eddb8561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once

#include <GL/glew.h>
#include <vector>
#include <unordered_set>
#include <QString>
#include <QImage>

#define EIGEN_DONT_VECTORIZE
#define EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT
#include "Eigen/StdVector"
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::Matrix2f)
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::Matrix3f)
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::Matrix3i)
#include "Eigen/Dense"

enum SelectMode
{
    None     = 0,
    Anchor   = 1,
    Unanchor = 2
};

class Shader;

class Shape
{
public:
    Shape();

    void init(const std::vector<Eigen::Vector3f> &vertices, const std::vector<Eigen::Vector3i> &triangles);
    void setVertices(const std::vector<Eigen::Vector3f> &vertices);
    void setVertices_and_Normals(const std::vector<Eigen::Vector3f> &vertices, const std::vector<Eigen::Vector3f> &normals);
    void setFoamInputs(const std::vector<Eigen::Vector3f> &verts, const std::vector<float> &wavelengths,
                       const std::vector<Eigen::Vector3f> &norms, const std::vector<Eigen::Vector2f> &textureCoords);
    void updateFoam(const std::vector<Eigen::Vector3i> &faces,
                           const std::vector<Eigen::Vector3f> &vertices,
                           const std::vector<Eigen::Vector2f> &texCoords,

                           std::vector<Eigen::Vector3f>& verts,
                           std::vector<Eigen::Vector2f>& tex,
                           std::vector<Eigen::Vector3f>& colors);

    void updateMeshFoam(const std::vector<Eigen::Vector3i> &faces,
                               const std::vector<Eigen::Vector3f> &vertices,
                               const std::vector<float> &wavelengths,
                                 const std::vector<Eigen::Vector3f> &waveDirs,

                               std::vector<Eigen::Vector3f>& verts,
                               std::vector<Eigen::Vector3f>& normals,
                               std::vector<Eigen::Vector3f>& colors);



    void setModelMatrix(const Eigen::Affine3f &model);

    void setColor(float r, float g, float b);

    void initGroundPlane(std::string texturePath, float depth, Shader* shader);
    void initSkyPlane(std::string texturePath, float height, Shader* shader);

    void draw(Shader *shader, GLenum mode);
    SelectMode select(Shader *shader, int vertex);
    bool selectWithSpecifiedMode(Shader *shader, int vertex, SelectMode mode);
    int  getClosestVertex(Eigen::Vector3f start, Eigen::Vector3f ray, float threshold);
    bool getAnchorPos(int lastSelected, Eigen::Vector3f& pos, Eigen::Vector3f ray, Eigen::Vector3f start);

    const std::vector<Eigen::Vector3f>& getVertices();
    const std::vector<Eigen::Vector3i>& getFaces();
    const std::unordered_set<int>& getAnchors();

private:
    GLuint m_surfaceVao;
    GLuint m_surfaceVbo;
    GLuint m_surfaceIbo;
    GLuint m_ground_texture;
    QImage m_ground_image;

    GLuint m_sky_texture;
    QImage m_sky_image;

    unsigned int m_numSurfaceVertices;
    unsigned int m_verticesSize;
    float m_red;
    float m_blue;
    float m_green;
    float m_alpha;

    std::vector<Eigen::Vector3i> m_faces;
    std::vector<Eigen::Vector3f> m_vertices;
    std::unordered_set<int>      m_anchors;

    Eigen::Matrix4f m_modelMatrix;
    int lastSelected = -1;

    // Helpers

    void selectHelper();
    Eigen::Vector3f getNormal(const Eigen::Vector3i& face);
    void updateMesh(const std::vector<Eigen::Vector3i> &triangles,
                    const std::vector<Eigen::Vector3f> &vertices,
                           std::vector<Eigen::Vector3f>& verts,
                           std::vector<Eigen::Vector3f>& normals,
                           std::vector<Eigen::Vector3f>& colors);
    void updateMesh_withNormals(const std::vector<Eigen::Vector3i> &faces,
                           const std::vector<Eigen::Vector3f> &vertices,
                           const std::vector<Eigen::Vector3f> &calculated_norms,
                           std::vector<Eigen::Vector3f>& verts,
                           std::vector<Eigen::Vector3f>& normals,
                           std::vector<Eigen::Vector3f>& colors);
};