Skip to content

Commit

Permalink
core: using new vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
PottierLoic committed Mar 25, 2024
1 parent 416b518 commit 537e046
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 114 deletions.
2 changes: 1 addition & 1 deletion core/component/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ glm::mat4 Camera::getViewMatrix() {
return glm::mat4(1.0f);
} else {
auto tf = lockedOwner->getComponent<Transform>();
return glm::lookAt(glm::vec3(tf->position.x, tf->position.y, tf->position.z), position + front, up);
return glm::lookAt(glm::vec3(tf->position.x(), tf->position.y(), tf->position.z()), position + front, up);
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/component/light.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "component/component.hpp"
#include "vector/vector3.hpp"
#include "vector/vector.hpp"

namespace SpaceEngine {

Expand All @@ -22,7 +22,7 @@ class Light : public Component {

// Specific attributes
LightType type;
Vector3 direction;
Vec3d direction;
float spotAngle;

/**
Expand Down
4 changes: 2 additions & 2 deletions core/component/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace SpaceEngine {

Transform::Transform(std::weak_ptr<Entity> owner, std::string name) : Component(owner) {
this->name = name;
this->position = Vector3();
this->scale = Vector3(1.0f, 1.0f, 1.0f);
this->position = Vec3d();
this->scale = Vec3d(1.0f, 1.0f, 1.0f);
}

}
14 changes: 7 additions & 7 deletions core/component/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string>

#include "component/component.hpp"
#include "vector/vector3.hpp"
#include "vector/vector.hpp"

namespace SpaceEngine {

Expand All @@ -12,9 +12,9 @@ namespace SpaceEngine {
*
* Properties:
* - name (std::string): The name of the object.
* - position (Vector3): The position of the transform in 3D space.
* - rotation (Vector3): The rotation of the transform in 3D space.
* - scale (Vector3): The scale of the transform in 3D space.
* - position (Vec3d): The position of the transform in 3D space.
* - rotation (Vec3d): The rotation of the transform in 3D space.
* - scale (Vec3d): The scale of the transform in 3D space.
*
* Methods:
* - Transform(std::string name): Default constructor: Initializes a new transform component with default values.
Expand All @@ -23,9 +23,9 @@ namespace SpaceEngine {
class Transform : public Component {
public:
std::string name; /* The name of the object. */
Vector3 position; /* The position of the transform in 3D space. */
Vector3 rotation; /* The rotation of the transform in 3D space. */
Vector3 scale; /* The scale of the transform in 3D space. */
Vec3d position; /* The position of the transform in 3D space. */
Vec3d rotation; /* The rotation of the transform in 3D space. */
Vec3d scale; /* The scale of the transform in 3D space. */

/**
* @brief Default constructor: Initializes a new transform component with default values.
Expand Down
3 changes: 0 additions & 3 deletions core/vector/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ class Vector {
// Equality operators.
constexpr bool operator==(const Vector& vec) const noexcept;
constexpr bool operator!=(const Vector& vec) const noexcept;

// stdout
std::ostream& operator<< <>(std::ostream& stream, const Vector& vec);
};

// Aliases
Expand Down
1 change: 1 addition & 0 deletions core/vector/vector.inl
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,5 @@ std::ostream& operator<<(std::ostream& stream, const Vector<T, N>& vec) {
return stream;
}


}
10 changes: 5 additions & 5 deletions editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ int main() {
if (modelRenderer && modelRenderer->model) {
auto tf = entity->getComponent<Transform>();
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(tf->position.x, tf->position.y, tf->position.z));
model = glm::scale(model, glm::vec3(tf->scale.x, tf->scale.y, tf->scale.z));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.x)), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.y)), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.z)), glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(tf->position.x(), tf->position.y(), tf->position.z()));
model = glm::scale(model, glm::vec3(tf->scale.x(), tf->scale.y(), tf->scale.z()));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.x())), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.y())), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, static_cast<float>(glm::radians(tf->rotation.z())), glm::vec3(0.0f, 0.0f, 1.0f));
shader.setMat4("model", model);
modelRenderer->model->draw(shader);
}
Expand Down
18 changes: 9 additions & 9 deletions editor/menu/component_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ void Menu::initComponentViewers() {
ImGui::PushItemWidth(75);
ImGui::Text("Position");
ImGui::SameLine(100);
if (ImGui::InputDouble("##transform.position.x", &transform->position.x)) {}
if (ImGui::InputDouble("##transform.position.x", &transform->position.x())) {}
ImGui::SameLine(180);
if (ImGui::InputDouble("##transform.position.y", &transform->position.y)) {}
if (ImGui::InputDouble("##transform.position.y", &transform->position.y())) {}
ImGui::SameLine(260);
if (ImGui::InputDouble("##transform.position.z", &transform->position.z)) {}
if (ImGui::InputDouble("##transform.position.z", &transform->position.z())) {}

ImGui::Text("Rotation");
ImGui::SameLine(100);
if (ImGui::InputDouble("##transform.rotation.x", &transform->rotation.x)) {}
if (ImGui::InputDouble("##transform.rotation.x", &transform->rotation.x())) {}
ImGui::SameLine(180);
if (ImGui::InputDouble("##transform.rotation.y", &transform->rotation.y)) {}
if (ImGui::InputDouble("##transform.rotation.y", &transform->rotation.y())) {}
ImGui::SameLine(260);
if (ImGui::InputDouble("##transform.rotation.z", &transform->rotation.z)) {}
if (ImGui::InputDouble("##transform.rotation.z", &transform->rotation.z())) {}

ImGui::Text("Scale");
ImGui::SameLine(100);
if (ImGui::InputDouble("##transform.scale.x", &transform->scale.x)) {}
if (ImGui::InputDouble("##transform.scale.x", &transform->scale.x())) {}
ImGui::SameLine(180);
if (ImGui::InputDouble("##transform.scale.y", &transform->scale.y)) {}
if (ImGui::InputDouble("##transform.scale.y", &transform->scale.y())) {}
ImGui::SameLine(260);
if (ImGui::InputDouble("##transform.scale.z", &transform->scale.z)) {}
if (ImGui::InputDouble("##transform.scale.z", &transform->scale.z())) {}
}
};

Expand Down
81 changes: 45 additions & 36 deletions tests/test_vector2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,50 @@ int testVector2() {
Vec2f vec1(1.0f, 2.0f);
Vec2f vec2(3.0f, 5.0f);

/* Addition check */
Vec2f add_result = vec1 + vec2;
custom_assert((add_result.x() == 4.0f && add_result.y() == 7.0f), "Addition check failed");

/* Subtraction check */
Vec2f sub_result = vec1 - vec2;
custom_assert((sub_result.x() == -2.0f && sub_result.y() == -3.0f), "Subtraction check failed");

/* Addition assignment check */
Vec2f vec3(2.0f, 3.0f);
vec3 += vec1;
custom_assert((vec3.x() == 3.0f && vec3.y() == 5.0f), "Addition assignment check failed");

/* Subtraction assignment check */
Vec2f vec4(5.0f, 7.0f);
vec4 -= vec1;
custom_assert((vec4.x() == 4.0f && vec4.y() == 5.0f), "Subtraction assignment check failed");

/* Multiplication assignment check */
Vec2f vec5(2.0f, 4.0f);
vec5 *= 2.0f; // Assuming scalar multiplication
custom_assert((vec5.x() == 4.0f && vec5.y() == 8.0f), "Multiplication assignment check failed");

// Since the Vector class does not directly support division by another Vector,
// this section will be omitted or you need to implement it in the Vector class.

/* Equality check */
Vec2f vec7(3.0f, 5.0f);
custom_assert((vec2.x() == vec7.x() && vec2.y() == vec7.y()), "Equality check failed");

/* Inequality check */
Vec2f vec8(1.0f, 7.0f);
custom_assert((vec1.x() != vec8.x() || vec1.y() != vec8.y()), "Inequality check failed");

// The distance and glm cast checks are omitted since they would require additional implementation details.

// Test addition
auto add_result = vec1 + vec2;
custom_assert(add_result.x() == 4.0f && add_result.y() == 7.0f, "Vec2f addition failed");

// Test subtraction
auto sub_result = vec1 - vec2;
custom_assert(sub_result.x() == -2.0f && sub_result.y() == -3.0f, "Vec2f subtraction failed");

// Test multiplication by a scalar
auto mul_result_scalar = vec1 * 2.0f;
custom_assert(mul_result_scalar.x() == 2.0f && mul_result_scalar.y() == 4.0f, "Vec2f scalar multiplication failed");

// Test division by a scalar
auto div_result_scalar = vec1 / 2.0f;
custom_assert(div_result_scalar.x() == 0.5f && div_result_scalar.y() == 1.0f, "Vec2f scalar division failed");

// Test += with a vector
Vec2f vec3 = vec1;
vec3 += vec2;
custom_assert(vec3.x() == 4.0f && vec3.y() == 7.0f, "Vec2f += failed");

// Test -= with a vector
vec3 = vec1;
vec3 -= vec2;
custom_assert(vec3.x() == -2.0f && vec3.y() == -3.0f, "Vec2f -= failed");

// Test *= with a scalar
vec3 = vec1;
vec3 *= 2.0f;
custom_assert(vec3.x() == 2.0f && vec3.y() == 4.0f, "Vec2f *= failed");

// Test /= with a scalar
vec3 = vec1;
vec3 /= 2.0f;
custom_assert(vec3.x() == 0.5f && vec3.y() == 1.0f, "Vec2f /= failed");

// Test operator[]
custom_assert(vec1[0] == 1.0f && vec1[1] == 2.0f, "Vec2f operator[] failed");

// Test equality
Vec2f vec4(1.0f, 2.0f);
custom_assert(vec1 == vec4, "Vec2f equality failed");

// Test inequality
custom_assert(vec1 != vec2, "Vec2f inequality failed");
return 0;
}
99 changes: 50 additions & 49 deletions tests/test_vector3.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
#include "test.hpp"
// #include "vector/vector3.hpp"
#include "vector/vector.hpp"

// using SpaceEngine::Vector3;
using SpaceEngine::Vec3f;

int testVector3() {
// Vector3 vec1 = Vector3(1.0, 2.0, 3.0);
// Vector3 vec2 = Vector3(3.0, 5.0, 7.0);

// /* Addition check */
// Vector3 add_result = vec1 + vec2;
// custom_assert(add_result == Vector3(4.0, 7.0, 10.0));

// /* Subtraction check */
// Vector3 sub_result = vec1 - vec2;
// custom_assert(sub_result == Vector3(-2.0, -3.0, -4.0));

// /* Addition assignment check */
// Vector3 vec3(2.0, 3.0, 4.0);
// vec3 += vec1;
// custom_assert(vec3 == Vector3(3.0, 5.0, 7.0));

// /* Subtraction assignment check */
// Vector3 vec4(5.0, 7.0, 9.0);
// vec4 -= vec1;
// custom_assert(vec4 == Vector3(4.0, 5.0, 6.0));

// /* Multiplication assignment check */
// Vector3 vec5(2.0, 4.0, 6.0);
// vec5 *= vec1;
// custom_assert(vec5 == Vector3(2.0, 8.0, 18.0));

// /* Division assignment check */
// Vector3 vec6(6.0, 8.0, 12.0);
// vec6 /= vec2;
// custom_assert(vec6 == Vector3(2.0, 1.6, 1.7142857142857142));

// /* Equality check */
// Vector3 vec7(3.0, 5.0, 7.0);
// custom_assert(vec2 == vec7);

// /* Inequality check */
// Vector3 vec8(1.0, 7.0, 9.0);
// custom_assert(vec1 != vec8);

// /* Distance check */
// double distance_result = vec1.distance(vec2);
// custom_assert(distance_result == sqrt(29.));

// /* glm cast check */
// glm::vec3 glm_vec = vec1;
// custom_assert(glm_vec == glm::vec3(1.0, 2.0, 3.0));

Vec3f vec1(1.0f, 2.0f, 3.0f);
Vec3f vec2(4.0f, 5.0f, 6.0f);

// Test addition
auto add_result = vec1 + vec2;
custom_assert(add_result.x() == 5.0f && add_result.y() == 7.0f && add_result.z() == 9.0f, "Vec3f addition failed");

// Test subtraction
auto sub_result = vec1 - vec2;
custom_assert(sub_result.x() == -3.0f && sub_result.y() == -3.0f && sub_result.z() == -3.0f, "Vec3f subtraction failed");

// Test multiplication by a scalar
auto mul_result_scalar = vec1 * 2.0f;
custom_assert(mul_result_scalar.x() == 2.0f && mul_result_scalar.y() == 4.0f && mul_result_scalar.z() == 6.0f, "Vec3f scalar multiplication failed");

// Test division by a scalar
auto div_result_scalar = vec1 / 2.0f;
custom_assert(div_result_scalar.x() == 0.5f && div_result_scalar.y() == 1.0f && div_result_scalar.z() == 1.5f, "Vec3f scalar division failed");

// Test += with a vector
Vec3f vec3 = vec1;
vec3 += vec2;
custom_assert(vec3.x() == 5.0f && vec3.y() == 7.0f && vec3.z() == 9.0f, "Vec3f += failed");

// Test -= with a vector
vec3 = vec1;
vec3 -= vec2;
custom_assert(vec3.x() == -3.0f && vec3.y() == -3.0f && vec3.z() == -3.0f, "Vec3f -= failed");

// Test *= with a scalar
vec3 = vec1;
vec3 *= 2.0f;
custom_assert(vec3.x() == 2.0f && vec3.y() == 4.0f && vec3.z() == 6.0f, "Vec3f *= failed");

// Test /= with a scalar
vec3 = vec1;
vec3 /= 2.0f;
custom_assert(vec3.x() == 0.5f && vec3.y() == 1.0f && vec3.z() == 1.5f, "Vec3f /= failed");

// Test operator[]
custom_assert(vec1[0] == 1.0f && vec1[1] == 2.0f && vec1[2] == 3.0f, "Vec3f operator[] failed");

// Test equality
Vec3f vec4(1.0f, 2.0f, 3.0f);
custom_assert(vec1 == vec4, "Vec3f equality failed");

// Test inequality
custom_assert(vec1 != vec2, "Vec3f inequality failed");
return 0;
}

0 comments on commit 537e046

Please sign in to comment.