summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-06-17 20:44:55 -0400
committerMatthew Kosarek <mattkae@protonmail.com>2021-06-17 20:44:55 -0400
commite68d14c4d5b4cced62c2148323c38e7f49292bd7 (patch)
tree9df84f2f9b4264cfcd74f0ab4738b5fd39f49e67
parent4079e69fe4107d2a50b122dbd58710bbeb10ace2 (diff)
Rotating 3d square
-rwxr-xr-x3d/rigidbody/dist/output.wasmbin37871 -> 43282 bytes
-rw-r--r--3d/rigidbody/main.cpp64
-rw-r--r--shared_cpp/RenderShared.cpp6
-rw-r--r--shared_cpp/RenderShared.h7
-rw-r--r--shared_cpp/Renderer3d.cpp5
-rw-r--r--shared_cpp/mathlib.h69
6 files changed, 128 insertions, 23 deletions
diff --git a/3d/rigidbody/dist/output.wasm b/3d/rigidbody/dist/output.wasm
index bd15618..9365ee9 100755
--- a/3d/rigidbody/dist/output.wasm
+++ b/3d/rigidbody/dist/output.wasm
Binary files differ
diff --git a/3d/rigidbody/main.cpp b/3d/rigidbody/main.cpp
index 8fe0691..1dfc593 100644
--- a/3d/rigidbody/main.cpp
+++ b/3d/rigidbody/main.cpp
@@ -1,4 +1,5 @@
#include "../../shared_cpp/Renderer3d.h"
+#include "../../shared_cpp/RenderShared.h"
#include "../../shared_cpp/Camera3d.h"
#include "../../shared_cpp/types.h"
#include "../../shared_cpp/WebglContext.h"
@@ -18,50 +19,71 @@ struct Cube {
void load(Renderer3d* renderer) {
Vertex3d cubeVertices[] = {
{
- Vector3 { -1.0, -1.0, 1.0 },
- Vector3()
+ Vector3 { 1.f, 1.f, 1.f },
+ Vector3(),
+ colorFromHex(255.f, 0.f, 0.f, 255.f)
},
{
- Vector3 { 1.0, -1.0, 1.0 },
- Vector3()
+ Vector3 { -1.f, 1.f, 1.f },
+ Vector3(),
+ colorFromHex(0.f, 255.f, 0.f, 255.f)
},
{
- Vector3 { -1.0, 1.0, 1.0 },
- Vector3()
+ Vector3 { -1.f, 1.f, -1.f },
+ Vector3(),
+ colorFromHex(0.f, 0.f, 255.f, 255.f)
},
{
- Vector3 { 1.0, 1.0, 1.0 },
- Vector3()
+ Vector3 { 1.f, 1.f, -1.f },
+ Vector3(),
+ colorFromHex(255.f, 0.f, 255.f, 255.f)
},
{
- Vector3 { -1.0, -1.0, -1.0 },
- Vector3()
+ Vector3 { 1.f, -1.f, 1.f },
+ Vector3(),
+ colorFromHex(255.f, 255.f, 0.f, 255.f)
},
{
- Vector3 { 1.0, -1.0, -1.0 },
- Vector3()
+ Vector3 { -1.f, -1.f, 1.f },
+ Vector3(),
+ colorFromHex(255.f, 0.f, 0.f, 255.f)
},
{
- Vector3 { -1.0, 1.0, -1.0 },
- Vector3()
+ Vector3 { -1.f, -1.f, -1.f },
+ Vector3(),
+ colorFromHex(0.f, 0.f, 255.f, 255.f)
},
{
- Vector3 { 1.0, 1.0, -1.0 },
- Vector3()
+ Vector3 { 1.f, -1.f, -1.f },
+ Vector3(),
+ colorFromHex(255.f, 255.f, 0.f, 255.f)
}
};
uint32 cubeIndices[] = {
- 0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1
+ 0, 1, 3, //top 1
+ 3, 1, 2, //top 2
+ 2, 6, 7, //front 1
+ 7, 3, 2, //front 2
+ 7, 6, 5, //bottom 1
+ 5, 4, 7, //bottom 2
+ 5, 1, 4, //back 1
+ 4, 1, 0, //back 2
+ 4, 3, 7, //right 1
+ 3, 4, 0, //right 2
+ 5, 6, 2, //left 1
+ 5, 1, 2 //left 2
};
- mesh.load(&cubeVertices[0], 8, cubeIndices, 14, renderer);
+ mesh.load(&cubeVertices[0], 8, cubeIndices, 36, renderer);
}
void update(float32 dtSeconds) {
- mesh.model = mesh.model.rotate2D((PI / 8.f) * dtSeconds);
+ float32 multiplier = (PI / 8.f) * dtSeconds;
+ Vector3 currentRotation = Vector3 { multiplier, multiplier, multiplier };
+ mesh.model = mesh.model.rotate(currentRotation.x, currentRotation.y, currentRotation.z);
}
void render(Renderer3d* renderer) {
@@ -97,8 +119,8 @@ int main() {
void load() {
renderer.load(&context);
cube.load(&renderer);
- camera.projection = camera.projection.getPerspectiveProjection(0.1f, 100.f, PI / 2.f, 800.f / 600.f);
- camera.view = camera.view.translate({ 0, 0, -10 });
+ camera.projection = Mat4x4().getPerspectiveProjection(0.1f, 100.f, PI / 2.f, 800.f / 600.f);
+ camera.view = Mat4x4().translate({ 0, 0, -10 });
mainLoop.run(update);
}
diff --git a/shared_cpp/RenderShared.cpp b/shared_cpp/RenderShared.cpp
new file mode 100644
index 0000000..8b51c90
--- /dev/null
+++ b/shared_cpp/RenderShared.cpp
@@ -0,0 +1,6 @@
+#include "RenderShared.h"
+
+Color colorFromHex(float32 r, float32 g, float32 b, float32 a) {
+ float32 scale = 1.f / 255.f;
+ return { r * scale, g * scale, b * scale, a * scale };
+}
diff --git a/shared_cpp/RenderShared.h b/shared_cpp/RenderShared.h
new file mode 100644
index 0000000..c50627f
--- /dev/null
+++ b/shared_cpp/RenderShared.h
@@ -0,0 +1,7 @@
+#pragma once
+#include "mathlib.h"
+#include "types.h"
+
+typedef Vector4 Color;
+
+Color colorFromHex(float32 r, float32 g, float32 b, float32 a);
diff --git a/shared_cpp/Renderer3d.cpp b/shared_cpp/Renderer3d.cpp
index 7afaea0..4ec5504 100644
--- a/shared_cpp/Renderer3d.cpp
+++ b/shared_cpp/Renderer3d.cpp
@@ -80,8 +80,9 @@ void Mesh3d::load(Vertex3d* inVertices, uint32 inNumVertices, uint32* inIndices,
glEnableVertexAttribArray(renderer->attributes.position);
glVertexAttribPointer(renderer->attributes.position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)0);
- glEnableVertexAttribArray(renderer->attributes.normal);
- glVertexAttribPointer(renderer->attributes.normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, normal));
+ // @TODO: Show normal once it's actually implemented
+ //glEnableVertexAttribArray(renderer->attributes.normal);
+ //glVertexAttribPointer(renderer->attributes.normal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, normal));
glEnableVertexAttribArray(renderer->attributes.color);
glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex3d), (GLvoid *)offsetof(Vertex3d, color));
diff --git a/shared_cpp/mathlib.h b/shared_cpp/mathlib.h
index ec0ae87..c922032 100644
--- a/shared_cpp/mathlib.h
+++ b/shared_cpp/mathlib.h
@@ -91,6 +91,10 @@ struct Vector3 {
float length() {
return sqrtf(x * x + y * y + z * z);
}
+
+ Vector3 operator+(const Vector3& other) {
+ return { x + other.x, y + other.y, z + other.z };
+ }
};
struct Vector4 {
@@ -166,6 +170,55 @@ struct Mat4x4 {
return result;
}
+ Mat4x4 getXRotationMatrix(float angleRadians) {
+ return {
+ { 1, 0, 0, 0,
+ 0, cos(angleRadians), -sin(angleRadians), 0,
+ 0, sin(angleRadians), cos(angleRadians), 0,
+ 0, 0, 0, 1 }
+ };
+ }
+
+ Mat4x4 getYRotationMatrix(float angleRadians) {
+ return {
+ { cos(angleRadians), 0, sin(angleRadians), 0,
+ 0, 1, 0, 0,
+ -sin(angleRadians), 0, cos(angleRadians), 0,
+ 0, 0, 0, 1 }
+ };
+ }
+
+ Mat4x4 getZRotationMatrix(float angleRadians) {
+ return {
+ { cos(angleRadians), -sin(angleRadians), 0, 0,
+ sin(angleRadians), cos(angleRadians), 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1 }
+ };
+ }
+
+ Mat4x4 rotate(float xRadians, float yRadians, float zRadians) {
+ Mat4x4 result = copy();
+
+ Mat4x4 rotationMatrix;
+ if (xRadians != 0) {
+ rotationMatrix = getXRotationMatrix(xRadians);
+ result = result * rotationMatrix;
+ }
+
+ if (yRadians != 0) {
+ rotationMatrix = getYRotationMatrix(yRadians);
+ result = result * rotationMatrix;
+ }
+
+ if (zRadians != 0) {
+ rotationMatrix = getZRotationMatrix(zRadians);
+ result = result * rotationMatrix;
+ }
+
+ return result;
+ }
+
Vector2 multByVec2(Vector2 v) {
Vector4 vec4 = { v.x, v.y, 0.0, 1.0 };
return {
@@ -178,6 +231,22 @@ struct Mat4x4 {
return multByVec2(v);
}
+ Mat4x4 multMat4x4(const Mat4x4& other) {
+ Mat4x4 result;
+ for (int i = 0; i < 4; ++i) {
+ for (int j = 0; j < 4; ++j) {
+ int row = i * 4;
+ result.m[row + j] = m[row + 0] * other.m[0 + j] + m[row + 1] * other.m[4 + j] + m[row + 2] * other.m[8 + j] + m[row + 3] * other.m[12 + j];
+ }
+ }
+
+ return result;
+ }
+
+ Mat4x4 operator*(const Mat4x4& other) {
+ return multMat4x4(other);
+ }
+
Mat4x4 getOrthographicMatrix(float left, float right, float bottom, float top) {
Mat4x4 result;
result.m[0] = 2.0 / (right - left);