Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic ArchMath #100

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/ArchMath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "math/Math.h"
#include <math.h>

namespace arch {

using namespace arch::math;

} // namespace arch
39 changes: 19 additions & 20 deletions include/Transform.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <utility>

#include "ArchMath.h"

namespace arch {

using Quaternion = glm::vec4;
using Quaternion = float4;

class Transform {
public:
Expand All @@ -18,54 +18,53 @@ class Transform {
* @param z z component of the vector generating rotation axis
* @return Quaternions structure with (q, q^-1)
*/
static std::pair<Quaternion, Quaternion> rotate(float angle, float x, float y, float z);
static std::pair<Quaternion, Quaternion> rotate(f32 angle, f32 x, f32 y, f32 z);

/**
* Calculates quaternions needed for rotation relative to given vector components
* @param angle rotation angle in degrees
* @param rotation_axis vector generating rotation axis
* @return std::pair<glm::vec4, glm::vec4> - (q, q^-1)
* @param rotationAxis vector generating rotation axis
* @return Quaternions structure with (q, q^-1)
*/
static std::pair<Quaternion, Quaternion> rotate(float angle, glm::vec3 rotation_axis);
static std::pair<Quaternion, Quaternion> rotate(f32 angle, float3 rotationAxis);

/**
* Creates matrix needed to shift point by given vector.
* @param translation_vector vector used to translate point
* @return glm::mat4 needed for translating point by given vector
* @param translationVector vector used to translate point
* @return arch::math::Mat4x4 needed for translating point by given vector
*/
static glm::mat4 translate(glm::vec3 translation_vector);
static Mat4x4 translate(float3 translationVector);

/**
* Creates matrix needed to shift point by given vector.
* @param x x component of translation vector
* @param y y component of translation vector
* @param z z component of translation vector
* @return glm::mat4 needed for translating point by given vector
* @return arch::math::Mat4x4 needed for translating point by given vector
*/
static glm::mat4 translate(float x, float y, float z);

static Mat4x4 translate(f32 x, f32 y, f32 z);
/**
* Creates matrix ideal for scaling objects by given values
* @param scaling_vector Vector with scaling factors.
* @return glm::mat4 matrix needed for scaling object by given values for each direction
* @param scalingVector Vector with scaling factors.
* @return arch::math::Mat4x4 matrix needed for scaling object by given values for each direction
*/
static glm::mat4 scale(glm::vec3 scaling_vector);
static Mat4x4 scale(float3 scalingVector);

/**
* Creates matrix ideal for scaling objects by given values
* @param x x factor
* @param y y factor
* @param z z factor
* @return glm::mat4 matrix needed for scaling object by given values for each direction
* @return arch::math::Mat4x4 matrix needed for scaling object by given values for each direction
*/
static glm::mat4 scale(float x, float y, float z);
static Mat4x4 scale(f32 x, f32 y, f32 z);

/**
* Creates matrix ideal for scaling objects in x,y,z directions by given value
* @param a scaling factor
* @return glm::mat4 matrix needed for scaling object by given value for each direction
* @return arch::math::Mat4x4 matrix needed for scaling object by given value for each direction
*/
static glm::mat4 scale(float a);
static Mat4x4 scale(f32 a);
};

} // namespace arch
4 changes: 2 additions & 2 deletions include/Window.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <glm/vec4.hpp>
#include <string>

#include <ArchMath.h>
#include <GLFW/glfw3.h>

namespace arch {
Expand Down Expand Up @@ -33,7 +33,7 @@ class Window {
*/
GLFWwindow* get() const;

void clear(glm::vec4 color) const;
void clear(Color color) const;
void clear(float r, float g, float b, float a) const;
void swapBuffers() const;
void resize(int width, int height) const;
Expand Down
45 changes: 45 additions & 0 deletions include/math/Math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <glm/mat2x2.hpp>
#include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>

namespace arch::math {

using Byte = std::byte;

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;

using f32 = float_t;
using f64 = double_t;
Twarug marked this conversation as resolved.
Show resolved Hide resolved

using float2 = glm::vec2;
using float3 = glm::vec3;
using float4 = glm::vec4;

using int2 = glm::ivec2;
using int3 = glm::ivec3;
using int4 = glm::ivec4;

using uint2 = glm::uvec2;
using uint3 = glm::uvec3;
using uint4 = glm::uvec4;

using Mat2x2 = glm::mat2;
using Mat3x3 = glm::mat3;
using Mat4x4 = glm::mat4;

using Color = glm::vec4;

} // namespace arch::math
19 changes: 9 additions & 10 deletions src/Engine.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "Engine.h"

#include <glm/glm.hpp>

#include "ArchMath.h"
#include "InputHandler.h"
#include "Logger.h"
#include "exceptions/GLFWException.h"
Expand Down Expand Up @@ -52,18 +51,18 @@ void Engine::_mainLoop() {
// };
// 2D square
struct Vertex {
glm::vec3 position;
glm::vec3 color;
glm::vec2 tex_coords;
float3 position;
float3 color;
float2 tex_coords;
};

std::vector<Vertex> vertices{
{ glm::vec3(0.5f, 0.5f, 0.0f), {}, glm::vec2(1.0f, 1.0f)},
{ glm::vec3(0.5f, -0.5f, 0.0f), {}, glm::vec2(1.0f, 0.0f)},
{glm::vec3(-0.5f, -0.5f, 0.0f), {}, glm::vec2(0.0f, 0.0f)},
{glm::vec3(-0.5f, 0.5f, 0.0f), {}, glm::vec2(0.0f, 1.0f)}
{ float3(0.5f, 0.5f, 0.0f), {}, float2(1.0f, 1.0f) },
{ float3(0.5f, -0.5f, 0.0f), {}, float2(1.0f, 0.0f) },
{ float3(-0.5f, -0.5f, 0.0f), {}, float2(0.0f, 0.0f) },
{ float3(-0.5f, 0.5f, 0.0f), {}, float2(0.0f, 1.0f) }
};
std::vector<uint32_t> indices{0, 1, 3, 1, 2, 3};
std::vector<u32> indices{ 0, 1, 3, 1, 2, 3 };
// Model model { { { vertices, indices } } };
// TextureLoader texture_loader;
// Renderer3D renderer {};
Expand Down
48 changes: 25 additions & 23 deletions src/Transform.cpp
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
#include "Transform.h"

#include <glm/gtc/matrix_transform.hpp>

namespace arch {

std::pair<Quaternion, Quaternion> Transform::rotate(float angle, float x, float y, float z) {
std::pair<Quaternion, Quaternion> Transform::rotate(f32 angle, f32 x, f32 y, f32 z) {
// normalize x,y,z
auto norm = static_cast<float>(sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)));
auto norm = static_cast<f32>(sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)));
x /= norm;
y /= norm;
z /= norm;

// fix angle
angle /= 2;
const double pi = std::acos(-1);
angle *= static_cast<float>(pi / 180.0);
const f64 pi = std::acos(-1);
Twarug marked this conversation as resolved.
Show resolved Hide resolved
angle *= static_cast<f32>(pi / 180.0);

// create rotation quaternions
float a = x * std::sin(angle);
float b = y * std::sin(angle);
float c = z * std::sin(angle);
glm::vec3 imaginaryPart = glm::vec3(a, b, c);
glm::vec4 q = glm::vec4(imaginaryPart, std::cos(angle));
glm::vec4 q1 = glm::vec4(-imaginaryPart, std::cos(angle));

return {q, q1};
f32 a = x * std::sin(angle);
f32 b = y * std::sin(angle);
f32 c = z * std::sin(angle);
float3 imaginaryPart = float3(a, b, c);
Twarug marked this conversation as resolved.
Show resolved Hide resolved
float4 q = float4(imaginaryPart, std::cos(angle));
Twarug marked this conversation as resolved.
Show resolved Hide resolved
float4 q1 = float4(-imaginaryPart, std::cos(angle));

return { q, q1 };
}

std::pair<Quaternion, Quaternion> Transform::rotate(float angle, glm::vec3 rotationAxis) {
std::pair<Quaternion, Quaternion> Transform::rotate(f32 angle, float3 rotationAxis) {
return rotate(angle, rotationAxis.x, rotationAxis.y, rotationAxis.z);
}

glm::mat4 Transform::translate(glm::vec3 translationVector) {
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), translationVector);
Mat4x4 Transform::translate(float3 translationVector) {
Mat4x4 translationMatrix = glm::translate(Mat4x4(1.0f), translationVector);
return translationMatrix;
}

glm::mat4 Transform::translate(float x, float y, float z) {
glm::vec3 translationVector = glm::vec3(x, y, z);
Mat4x4 Transform::translate(f32 x, f32 y, f32 z) {
float3 translationVector = float3(x, y, z);

return translate(translationVector);
}

glm::mat4 Transform::scale(glm::vec3 scalingVector) {
glm::mat4 scalingMatrix = glm::scale(glm::mat4(1.0f), scalingVector);
Mat4x4 Transform::scale(float3 scalingVector) {
Mat4x4 scalingMatrix = glm::scale(Mat4x4(1.0f), scalingVector);

return scalingMatrix;
}

glm::mat4 Transform::scale(float x, float y, float z) {
glm::vec3 scalingVector = glm::vec3(x, y, z);
Mat4x4 Transform::scale(f32 x, f32 y, f32 z) {
float3 scalingVector = float3(x, y, z);

return scale(scalingVector);
}

glm::mat4 Transform::scale(float a) {
auto scalingVector = glm::vec3(a);
Mat4x4 Transform::scale(f32 a) {
auto scalingVector = float3(a);

return scale(scalingVector);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ bool Window::shouldClose() const {
return glfwWindowShouldClose(_window);
}

void Window::clear(glm::vec4 color) const {
glClearColor(color.x, color.y, color.z, color.w);
void Window::clear(Color color) const {
glClearColor(color.r, color.g, color.b, color.a);
glClear(GL_COLOR_BUFFER_BIT);
}

void Window::clear(float r, float g, float b, float a) const {
glm::vec4 color(r, g, b, a);
Color color(r, g, b, a);
clear(color);
}

Expand Down
Loading