Skip to content

Commit c538c9d

Browse files
committed
Created a solver class which will house the cloth simulation, modified and refactored the existing classes in preparation for this, now need to modify the solver to accept multiple input meshes.
1 parent 5539ee8 commit c538c9d

File tree

12 files changed

+489
-77
lines changed

12 files changed

+489
-77
lines changed

Project.pro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ HEADERS += \
4747
include/CSBmesh.h \
4848
include/MaterialEnvMap.h \
4949
include/CSBconstraint.h \
50-
include/csbpoint.h \
51-
include/MaterialCSBpbr.h
50+
include/MaterialCSBpbr.h \
51+
include/CSBsolver.h \
52+
include/CSBparticle.h
5253

5354
SOURCES += \
5455
src/main.cpp \
@@ -69,7 +70,8 @@ SOURCES += \
6970
src/CSBmesh.cpp \
7071
src/MaterialEnvMap.cpp \
7172
src/CSBconstraint.cpp \
72-
src/MaterialCSBpbr.cpp
73+
src/MaterialCSBpbr.cpp \
74+
src/CSBsolver.cpp
7375

7476
OTHER_FILES += \
7577
$$files(shaders/*, true) \

include/CSBconstraint.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <vector>
55
#include <array>
66
#include "vec3.hpp"
7-
#include "csbpoint.h"
7+
#include "CSBparticle.h"
88

99
class CSBconstraint
1010
{
@@ -15,7 +15,7 @@ class CSBconstraint
1515
CSBconstraint(CSBconstraint&&) = default;
1616
CSBconstraint& operator=(CSBconstraint&&) = default;
1717
virtual ~CSBconstraint();
18-
virtual void project(std::vector<CSBpoint> &_positions) = 0;
18+
virtual void project(std::vector<CSBparticle> &_positions) = 0;
1919
};
2020

2121
class DistanceConstraint : public CSBconstraint
@@ -31,7 +31,7 @@ class DistanceConstraint : public CSBconstraint
3131
DistanceConstraint(DistanceConstraint&&) = default;
3232
DistanceConstraint& operator=(DistanceConstraint&&) = default;
3333
virtual ~DistanceConstraint() override = default;
34-
virtual void project(std::vector<CSBpoint> &_positions) override;
34+
virtual void project(std::vector<CSBparticle> &_positions) override;
3535

3636
private:
3737
float m_rest;
@@ -43,7 +43,7 @@ class DistanceConstraint : public CSBconstraint
4343
class BendingConstraint : public CSBconstraint
4444
{
4545
public:
46-
BendingConstraint(const size_t _p1, const size_t _p2, const size_t _p3, float _rest, const std::vector<CSBpoint>&_points) :
46+
BendingConstraint(const size_t _p1, const size_t _p2, const size_t _p3, float _rest, const std::vector<CSBparticle>&_points) :
4747
m_p({{_p1, _p2, _p3}}),
4848
m_rest(_rest)
4949
{
@@ -57,7 +57,7 @@ class BendingConstraint : public CSBconstraint
5757
BendingConstraint(BendingConstraint&&) = default;
5858
BendingConstraint& operator=(BendingConstraint&&) = default;
5959
virtual ~BendingConstraint() override = default;
60-
virtual void project(std::vector<CSBpoint> &_positions) override;
60+
virtual void project(std::vector<CSBparticle> &_positions) override;
6161

6262
private:
6363
std::array<size_t, 3> m_p;
@@ -77,7 +77,7 @@ class PinConstraint : public CSBconstraint
7777
PinConstraint(PinConstraint&&) = default;
7878
PinConstraint& operator=(PinConstraint&&) = default;
7979
virtual ~PinConstraint() override = default;
80-
virtual void project(std::vector<CSBpoint> &_positions) override;
80+
virtual void project(std::vector<CSBparticle> &_positions) override;
8181

8282
private:
8383
glm::vec3 m_pin;
@@ -88,7 +88,7 @@ class PinConstraint : public CSBconstraint
8888
class SelfCollisionConstraint : public CSBconstraint
8989
{
9090
public:
91-
SelfCollisionConstraint(const glm::vec3 &_intersectionP, const size_t &_p, const size_t &_t0, const size_t &_t1, const size_t &_t2, const std::vector<CSBpoint>&_points) :
91+
SelfCollisionConstraint(const glm::vec3 &_intersectionP, const size_t &_p, const size_t &_t0, const size_t &_t1, const size_t &_t2, const std::vector<CSBparticle>&_points) :
9292
m_t({{_t0, _t1, _t2}}),
9393
m_intersectionP(_intersectionP),
9494
m_p(_p)
@@ -104,7 +104,7 @@ class SelfCollisionConstraint : public CSBconstraint
104104
SelfCollisionConstraint(SelfCollisionConstraint&&) = default;
105105
SelfCollisionConstraint& operator=(SelfCollisionConstraint&&) = default;
106106
virtual ~SelfCollisionConstraint() override = default;
107-
virtual void project(std::vector<CSBpoint> &_positions) override;
107+
virtual void project(std::vector<CSBparticle> &_positions) override;
108108

109109
private:
110110
std::array<size_t, 3> m_t;

include/CSBmesh.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <tuple>
77
#include <unordered_map>
88
#include <unordered_set>
9-
#include "csbpoint.h"
9+
#include "CSBparticle.h"
1010
#include "CSBconstraint.h"
1111

1212
class CSBmesh : public Mesh
@@ -34,12 +34,12 @@ class CSBmesh : public Mesh
3434
std::unordered_set<EdgePair> getEdges();
3535
std::vector<GLushort> getConnectedVertices(const GLushort _vert);
3636

37-
std::vector<CSBpoint> m_points;
37+
std::vector<CSBparticle> m_particles;
3838
std::vector<std::unique_ptr<CSBconstraint>> m_constraints;
3939

4040
glm::ivec3 calcCell(const glm::vec3& _coord) const;
4141
size_t hashCell (const glm::ivec3& _cell) const;
42-
size_t hashPoint(const glm::vec3& _coord) const;
42+
size_t hashParticle(const glm::vec3& _coord) const;
4343
void resolveSelfCollision_rays();
4444

4545
void resolveSelfCollision_spheres();

include/csbpoint.h renamed to include/CSBparticle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
#include "vec3.hpp"
55

6-
struct CSBpoint
6+
struct CSBparticle
77
{
8-
CSBpoint(glm::vec3 &_pos, const float &_invMass) :
8+
CSBparticle(glm::vec3 &_pos, const float &_invMass) :
99
m_pos(_pos),
1010
m_prevPos(_pos),
1111
m_invMass(_invMass)

include/CSBscene.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "Material.h"
66
#include "ShaderLib.h"
77
#include "CSBmesh.h"
8+
#include "CSBsolver.h"
89

910

1011
class CSBscene : public Scene
@@ -95,11 +96,11 @@ public slots:
9596
virtual void renderScene() override;
9697

9798
private:
98-
99+
CSBsolver m_solver;
99100
//-----------------------------------------------------------------------------------------------------
100101
/// @brief Holds our test meshes.
101102
//-----------------------------------------------------------------------------------------------------
102-
std::array<CSBmesh, 1> m_meshes;
103+
std::array<Mesh, 1> m_meshes;
103104
//-----------------------------------------------------------------------------------------------------
104105
/// @brief Wraps up our OpenGL buffers and VAO.
105106
//-----------------------------------------------------------------------------------------------------

include/CSBsolver.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef CSBSOLVER_H
2+
#define CSBSOLVER_H
3+
4+
#include <QOpenGLFunctions>
5+
#include "glm/common.hpp"
6+
#include <tuple>
7+
#include <unordered_map>
8+
#include <unordered_set>
9+
#include <memory>
10+
#include "CSBparticle.h"
11+
#include "CSBconstraint.h"
12+
#include "Mesh.h"
13+
14+
class CSBsolver
15+
{
16+
public:
17+
void addTriangleMesh(Mesh& _mesh);
18+
void update(const float _time);
19+
20+
private:
21+
struct EdgePair
22+
{
23+
EdgePair(const GLushort _a, const GLushort _b) :
24+
p(std::min(_a, _b), std::max(_a, _b))
25+
{}
26+
friend bool operator==(const EdgePair &_a, const EdgePair &_b)
27+
{
28+
return _a.p == _b.p;
29+
}
30+
std::pair<GLushort, GLushort> p;
31+
};
32+
friend struct std::hash<CSBsolver::EdgePair>;
33+
34+
void hashVerts();
35+
void hashTris();
36+
std::unordered_set<EdgePair> getEdges(Mesh* _meshRef);
37+
std::vector<GLushort> getConnectedVertices(Mesh* _meshRef, const GLushort _vert);
38+
39+
std::vector<CSBparticle> m_particles;
40+
std::vector<std::unique_ptr<CSBconstraint>> m_constraints;
41+
std::vector<Mesh*> m_referencedMeshes;
42+
43+
glm::ivec3 calcCell(const glm::vec3& _coord) const;
44+
size_t hashCell (const glm::ivec3& _cell) const;
45+
size_t hashParticle(const glm::vec3& _coord) const;
46+
void resolveSelfCollision_rays();
47+
48+
void resolveSelfCollision_spheres();
49+
50+
std::vector<std::vector<GLushort>> m_hashTable;
51+
std::vector<std::vector<size_t>> m_triangleVertHash;
52+
std::vector<GLushort> m_indices;
53+
54+
float m_shortestEdgeDist = 0.0f;
55+
float m_avgEdgeLength = 0.0f;
56+
};
57+
58+
59+
namespace std
60+
{
61+
template <>
62+
struct hash<CSBsolver::EdgePair>
63+
{
64+
size_t operator()(const CSBsolver::EdgePair &_key) const
65+
{
66+
return std::hash<size_t>()(std::hash<GLushort>()(_key.p.first)) ^ std::hash<GLushort>()(_key.p.second);
67+
}
68+
};
69+
}
70+
71+
72+
#endif // CSBSOLVER_H

include/Mesh.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,42 @@ class Mesh
5252
//-----------------------------------------------------------------------------------------------------
5353
const GLfloat* getAttribData(const MeshAttributes::Attribute _attrib) const noexcept;
5454
//-----------------------------------------------------------------------------------------------------
55-
/// @brief Used to the the amount of face indices.
55+
/// @brief Used to the number of vertices.
56+
/// @return The size of our vertex array.
57+
//-----------------------------------------------------------------------------------------------------
58+
size_t getNVerts() const noexcept;
59+
//-----------------------------------------------------------------------------------------------------
60+
/// @brief Used to the amount of face indices.
5661
/// @return The size of our indices array.
5762
//-----------------------------------------------------------------------------------------------------
5863
int getNIndicesData() const noexcept;
5964
//-----------------------------------------------------------------------------------------------------
60-
/// @brief Used to the the amount of vertex data elements.
65+
/// @brief Used to the amount of vertex data elements.
6166
/// @return The size of our vertex array.
6267
//-----------------------------------------------------------------------------------------------------
6368
int getNVertData() const noexcept;
6469
//-----------------------------------------------------------------------------------------------------
65-
/// @brief Used to the the amount of normal data elements.
70+
/// @brief Used to the amount of normal data elements.
6671
/// @return The size of our normal array.
6772
//-----------------------------------------------------------------------------------------------------
6873
int getNNormData() const noexcept;
6974
//-----------------------------------------------------------------------------------------------------
70-
/// @brief Used to the the amount of UV data elements.
75+
/// @brief Used to the amount of UV data elements.
7176
/// @return The size of our UV array.
7277
//-----------------------------------------------------------------------------------------------------
7378
int getNUVData() const noexcept;
7479
//-----------------------------------------------------------------------------------------------------
75-
/// @brief Used to the the amount of data elements across all arrays.
80+
/// @brief Used to the amount of data elements across all arrays.
7681
/// @return The size of our data arrays combined.
7782
//-----------------------------------------------------------------------------------------------------
7883
int getNData() const noexcept;
7984

85+
std::vector<glm::vec3>& getVertices() noexcept;
86+
87+
const std::vector<GLushort>& getIndices() const noexcept;
88+
89+
const std::vector<std::vector<GLushort>>& getAdjacencyInfo() const noexcept;
90+
8091
protected:
8192
//-----------------------------------------------------------------------------------------------------
8293
/// @brief m_vertices contains the vertices

src/CSBconstraint.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
CSBconstraint::~CSBconstraint() = default;
77

8-
void DistanceConstraint::project(std::vector<CSBpoint> &_positions)
8+
void DistanceConstraint::project(std::vector<CSBparticle> &_positions)
99
{
1010
auto& p1 = _positions[m_p1];
1111
auto& p2 = _positions[m_p2];
@@ -18,7 +18,7 @@ void DistanceConstraint::project(std::vector<CSBpoint> &_positions)
1818
p2.m_pos -= (delta * p2.m_invMass);
1919
}
2020

21-
void BendingConstraint::project(std::vector<CSBpoint> &_positions)
21+
void BendingConstraint::project(std::vector<CSBparticle> &_positions)
2222
{
2323
auto& p1 = _positions[m_p[0]];
2424
auto& p2 = _positions[m_p[1]];
@@ -42,12 +42,12 @@ void BendingConstraint::project(std::vector<CSBpoint> &_positions)
4242
p3.m_pos += (k * m_w[2] * -4.f * force);
4343
}
4444

45-
void PinConstraint::project(std::vector<CSBpoint> &_positions)
45+
void PinConstraint::project(std::vector<CSBparticle> &_positions)
4646
{
4747
_positions[m_p].m_pos = m_pin;
4848
}
4949

50-
void SelfCollisionConstraint::project(std::vector<CSBpoint> &_positions)
50+
void SelfCollisionConstraint::project(std::vector<CSBparticle> &_positions)
5151
{
5252
auto& T0 = _positions[m_t[0]].m_pos;
5353
auto& T1 = _positions[m_t[1]].m_pos;

0 commit comments

Comments
 (0)