summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2d/softbody/softbody_2/SpringRectangle.h31
-rw-r--r--2d/softbody/softbody_2/dist/output.js9
-rwxr-xr-x2d/softbody/softbody_2/dist/output.wasmbin33390 -> 36383 bytes
-rw-r--r--2d/softbody/softbody_2/main.cpp1
-rw-r--r--shared_cpp/Renderer2d.cpp24
-rw-r--r--shared_cpp/Renderer2d.h5
-rw-r--r--shared_cpp/mathlib.cpp28
-rw-r--r--shared_cpp/mathlib.h4
8 files changed, 91 insertions, 11 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;
}
};
diff --git a/2d/softbody/softbody_2/dist/output.js b/2d/softbody/softbody_2/dist/output.js
index 864fed6..2502d5e 100644
--- a/2d/softbody/softbody_2/dist/output.js
+++ b/2d/softbody/softbody_2/dist/output.js
@@ -2573,6 +2573,14 @@ var ASM_CONSTS = {
}
}
+ function _glBufferSubData(target, offset, size, data) {
+ if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
+ GLctx.bufferSubData(target, offset, HEAPU8, data, size);
+ return;
+ }
+ GLctx.bufferSubData(target, offset, HEAPU8.subarray(data, data+size));
+ }
+
function _glClear(x0) { GLctx['clear'](x0) }
function _glClearColor(x0, x1, x2, x3) { GLctx['clearColor'](x0, x1, x2, x3) }
@@ -3062,6 +3070,7 @@ var asmLibraryArg = {
"glBindBuffer": _glBindBuffer,
"glBindVertexArray": _glBindVertexArray,
"glBufferData": _glBufferData,
+ "glBufferSubData": _glBufferSubData,
"glClear": _glClear,
"glClearColor": _glClearColor,
"glClearDepth": _glClearDepth,
diff --git a/2d/softbody/softbody_2/dist/output.wasm b/2d/softbody/softbody_2/dist/output.wasm
index 8f52a3d..4d1241e 100755
--- a/2d/softbody/softbody_2/dist/output.wasm
+++ b/2d/softbody/softbody_2/dist/output.wasm
Binary files differ
diff --git a/2d/softbody/softbody_2/main.cpp b/2d/softbody/softbody_2/main.cpp
index 80686f9..29cecd6 100644
--- a/2d/softbody/softbody_2/main.cpp
+++ b/2d/softbody/softbody_2/main.cpp
@@ -46,6 +46,7 @@ void load() {
void update(float32 deltaTimeSeconds, void* userData) {
// -- Update
+ rectangle.update(deltaTimeSeconds);
// -- Render
renderer.render();
diff --git a/shared_cpp/Renderer2d.cpp b/shared_cpp/Renderer2d.cpp
index adef4de..c050172 100644
--- a/shared_cpp/Renderer2d.cpp
+++ b/shared_cpp/Renderer2d.cpp
@@ -3,15 +3,16 @@
#include "mathlib.h"
const char* orthographicVertex =
-"attribute vec2 position; \n"
+"attribute vec4 position; \n"
"attribute vec4 color; \n"
"uniform mat4 projection; \n"
"uniform mat4 model; \n"
"varying lowp vec4 VertexColor; \n"
"void main() { \n"
-" vec4 fragmentPosition = projection * model * vec4(position, 1, 1); \n"
+" vec4 fragmentPosition = projection * model * position; \n"
" gl_Position = fragmentPosition; \n"
" VertexColor = color; \n"
+" gl_PointSize = 2.0; \n"
"}";
const char* orthographicFragment =
@@ -55,6 +56,7 @@ void Renderer2d::unload() {
void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, Renderer2d* renderer, GLenum loadType) {
mode = MeshMode::Vertex;
numVertices = inNumVertices;
+ isDynamic = loadType == GL_DYNAMIC_DRAW;
useShader(renderer->shader);
glGenVertexArrays(1, &vao);
@@ -65,7 +67,7 @@ void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, Renderer2d* render
glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Vertex2d), &inVertices[0], loadType);
glEnableVertexAttribArray(renderer->attributes.position);
- glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0);
+ glVertexAttribPointer(renderer->attributes.position, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0);
glEnableVertexAttribArray(renderer->attributes.color);
glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)offsetof(Vertex2d, color));
@@ -78,6 +80,7 @@ void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, GLuint* inIndices,
mode = MeshMode::Index;
numVertices = inNumVertices;
numIndices = inNumIndices;
+ isDynamic = loadType == GL_DYNAMIC_DRAW;
useShader(renderer->shader);
glGenVertexArrays(1, &vao);
@@ -88,7 +91,7 @@ void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, GLuint* inIndices,
glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Vertex2d), &inVertices[0], loadType);
glEnableVertexAttribArray(renderer->attributes.position);
- glVertexAttribPointer(renderer->attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0);
+ glVertexAttribPointer(renderer->attributes.position, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0);
glEnableVertexAttribArray(renderer->attributes.color);
glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)offsetof(Vertex2d, color));
@@ -116,8 +119,7 @@ void Mesh2d::render(Renderer2d* renderer, GLenum drawType) {
}
glBindVertexArray(0);
-}
-
+}
void Mesh2d::unload() {
glDeleteVertexArrays(1, &vao);
@@ -128,3 +130,13 @@ void Mesh2d::unload() {
glDeleteBuffers(1, &ebo);
}
}
+
+void Mesh2d::updateVertices(Vertex2d* vertices) {
+ if (!isDynamic) {
+ printf("Cannot update vertices on a non-dynamic mesh.\n");
+ return;
+ }
+
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices * sizeof(Vertex2d), &vertices[0]);
+}
diff --git a/shared_cpp/Renderer2d.h b/shared_cpp/Renderer2d.h
index a942fa6..3ecb410 100644
--- a/shared_cpp/Renderer2d.h
+++ b/shared_cpp/Renderer2d.h
@@ -27,7 +27,7 @@ struct Renderer2d {
};
struct Vertex2d {
- Vector2 position;
+ Vector4 position;
Vector4 color;
};
@@ -44,9 +44,12 @@ struct Mesh2d {
uint32 numVertices = 0;
uint32 numIndices = 0;
Mat4x4 model;
+ bool isDynamic = false;
void load(Vertex2d* vertices, uint32 numVertices, Renderer2d* renderer, GLenum loadType = GL_STATIC_DRAW);
void load(Vertex2d* vertices, uint32 numVertices, GLuint* indices, uint32 numIndices, Renderer2d* renderer, GLenum loadType = GL_STATIC_DRAW);
void render(Renderer2d* renderer, GLenum drawType = GL_TRIANGLES);
void unload();
+
+ void updateVertices(Vertex2d* vertices);
};
diff --git a/shared_cpp/mathlib.cpp b/shared_cpp/mathlib.cpp
index 5996ba3..c9f2e0d 100644
--- a/shared_cpp/mathlib.cpp
+++ b/shared_cpp/mathlib.cpp
@@ -234,6 +234,20 @@ Vector4::Vector4(float inX, float inY, float inZ, float inW) {
w = inW;
}
+Vector4::Vector4(Vector2& v) {
+ x = v.x;
+ y = v.y;
+ z = 0;
+ w = 1;
+}
+
+Vector4::Vector4(Vector3& v) {
+ x = v.x;
+ y = v.y;
+ z = v.z;
+ w = 1;
+}
+
Vector4 Vector4::fromColor(float r, float g, float b, float a) {
float scale = 1.f / 255.f;
return { r * scale, g * scale, b * scale, a * scale };
@@ -302,6 +316,20 @@ Vector4 lerp(Vector4 start, Vector4 end, float t) {
return (end - start) * t + start;
}
+void Vector4::operator=(const Vector2& v2) {
+ x = v2.x;
+ y = v2.y;
+ z = 0;
+ w = 1;
+}
+
+void Vector4::operator=(const Vector3& v2) {
+ x = v2.x;
+ y = v2.y;
+ z = v2.z;
+ w = 1;
+}
+
Vector4 Vector4::operator+(const Vector4& v2) {
return add(v2);
}
diff --git a/shared_cpp/mathlib.h b/shared_cpp/mathlib.h
index fd21ae2..d8a15e7 100644
--- a/shared_cpp/mathlib.h
+++ b/shared_cpp/mathlib.h
@@ -96,6 +96,8 @@ struct Vector4 {
Vector4(float inX, float inY, float inZ, float inW);
Vector4 fromColor(float r, float g, float b, float a);
Vector4 toNormalizedColor();
+ Vector4(Vector2& v);
+ Vector4(Vector3& v);
float length();
Vector4 normalize();
@@ -106,6 +108,8 @@ struct Vector4 {
Vector4 negate();
Vector4 cross(const Vector4& other);
+ void operator=(const Vector2& v2);
+ void operator=(const Vector3& v2);
Vector4 operator+(const Vector4& v2);
Vector4 operator-(const Vector4& v2);
Vector4 operator-();