diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-17 20:44:55 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-17 20:44:55 -0400 |
commit | e68d14c4d5b4cced62c2148323c38e7f49292bd7 (patch) | |
tree | 9df84f2f9b4264cfcd74f0ab4738b5fd39f49e67 /shared_cpp | |
parent | 4079e69fe4107d2a50b122dbd58710bbeb10ace2 (diff) |
Rotating 3d square
Diffstat (limited to 'shared_cpp')
-rw-r--r-- | shared_cpp/RenderShared.cpp | 6 | ||||
-rw-r--r-- | shared_cpp/RenderShared.h | 7 | ||||
-rw-r--r-- | shared_cpp/Renderer3d.cpp | 5 | ||||
-rw-r--r-- | shared_cpp/mathlib.h | 69 |
4 files changed, 85 insertions, 2 deletions
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); |