summaryrefslogtreecommitdiff
path: root/2d/softbody/softbody_2/SpringRectangle.h
diff options
context:
space:
mode:
Diffstat (limited to '2d/softbody/softbody_2/SpringRectangle.h')
-rw-r--r--2d/softbody/softbody_2/SpringRectangle.h31
1 files changed, 27 insertions, 4 deletions
diff --git a/2d/softbody/softbody_2/SpringRectangle.h b/2d/softbody/softbody_2/SpringRectangle.h
index b45bd3c..bc8eed8 100644
--- a/2d/softbody/softbody_2/SpringRectangle.h
+++ b/2d/softbody/softbody_2/SpringRectangle.h
@@ -2,24 +2,33 @@
#include "../../../shared_cpp/types.h"
#include "../../../shared_cpp/mathlib.h"
+struct SoftbodyUpdateVertexData {
+ Vector2 position;
+};
struct SoftbodyRectangle {
float32 width = 100;
float32 height = 100;
+ Vector2 position = Vector2(300, 400);
+
// Represents the density of springs that will be in the rectangle.
int32 springDensity = 10;
-
- int32 numSubdivisions = 0;
+ SoftbodyUpdateVertexData* updateData = NULL;
Mesh2d mesh;
+ Mesh2d pointsMesh;
+
+ Vertex2d* pointsVertices = NULL;
void load(Renderer2d* renderer) {
int32 numVertices = springDensity * springDensity; // Each subdivision is a square.
int32 numIndices = 6 * ((springDensity - 1) * (springDensity - 1));
auto vertices = new Vertex2d[numVertices];
+ updateData = new SoftbodyUpdateVertexData[numVertices];
auto indices = new GLuint[numIndices];
+ // Load a square with the desired density
int32 vIdx = 0;
int32 iIdx = 0;
float32 inverseDensity = 1.f / springDensity;
@@ -28,6 +37,7 @@ struct SoftbodyRectangle {
for (int32 x = 0; x < springDensity; x++) { // Columns
Vector2 position = Vector2(x * inverseDensity - halfInv, y * inverseDensity- halfInv);
vertices[vIdx] = { position, Vector4(1, 0, 0, 1) };
+ updateData[vIdx].position = position;
if (y != springDensity - 1 && x != springDensity - 1) {
indices[iIdx++] = vIdx;
@@ -42,22 +52,35 @@ struct SoftbodyRectangle {
}
}
- mesh.load(vertices, numVertices, indices, numIndices, renderer);
+ mesh.load(vertices, numVertices, indices, numIndices, renderer, GL_DYNAMIC_DRAW);
mesh.model = Mat4x4().scale(Vector3(width, height, 0)).translateByVec2(Vector2(300, 400));
+ pointsVertices = new Vertex2d[numVertices];
+ for (int32 v = 0; v < numVertices; v++) {
+ pointsVertices[v].position = vertices[v].position;
+ pointsVertices[v].color = Vector4(1, 1, 0, 1);
+ }
+ pointsMesh.load(pointsVertices, numVertices, renderer, GL_DYNAMIC_DRAW);
+
delete [] vertices;
delete [] indices;
}
void update(float32 dtSeconds) {
-
+ for (int32 v = 0; v < pointsMesh.numVertices; v++) {
+ pointsVertices[v].position = Vector2(updateData[v].position.x * width, updateData[v].position.y * height) + position;
+ }
+ pointsMesh.updateVertices(pointsVertices);
}
void render(Renderer2d* renderer) {
mesh.render(renderer);
+ pointsMesh.render(renderer, GL_POINTS);
}
void unload() {
mesh.unload();
+ pointsMesh.unload();
+ delete [] pointsVertices;
}
};