Skip to content

Commit

Permalink
Merge branch 'dev' into vulkan
Browse files Browse the repository at this point in the history
  • Loading branch information
brenocq committed Dec 30, 2023
2 parents 3e453b3 + 458906d commit 77a0e20
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/atta/component/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(ATTA_COMPONENT_MODULE_SOURCE
components/mesh.cpp
components/name.cpp
components/pointLight.cpp
components/polygonCollider2D.cpp
components/prismaticJoint.cpp
components/prototype.cpp
components/relationship.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/atta/component/components/boxCollider2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace atta::component {

/// %Component to create a box collider
/** This collider can be used both for 2D physics.
/** This collider can be used for 2D physics.
*
* Transform and RigidBody2D components are necessary
* for the entity to participate in the physics iteration.
Expand Down
2 changes: 1 addition & 1 deletion src/atta/component/components/circleCollider2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace atta::component {

/// %Component to create sphere collider
/** This collider can be used with 2D 3D physics.
/** This collider can be used with 2D physics.
*
* Transform and RigidBody2D components are necessary for the
* entity to participate in the physics iteration.
Expand Down
1 change: 1 addition & 0 deletions src/atta/component/components/components.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <atta/component/components/mesh.h>
#include <atta/component/components/name.h>
#include <atta/component/components/pointLight.h>
#include <atta/component/components/polygonCollider2D.h>
#include <atta/component/components/prismaticJoint.h>
#include <atta/component/components/prototype.h>
#include <atta/component/components/relationship.h>
Expand Down
94 changes: 94 additions & 0 deletions src/atta/component/components/polygonCollider2D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//--------------------------------------------------
// Atta Component Module
// polygonCollider2D.cpp
// Date: 2022-10-16
// By Breno Cunha Queiroz
//--------------------------------------------------
#include <atta/component/components/polygonCollider2D.h>
#include <atta/file/serializer/serializer.h>
#include <imgui.h>

namespace atta::component {

template <>
ComponentDescription& TypedComponentRegistry<PolygonCollider2D>::getDescription() {
static ComponentDescription desc = {
"Polygon Collider 2D",
{
{AttributeType::FLOAT32, offsetof(PolygonCollider2D, offset), "offset", -2000.0f, 2000.0f, 0.01f},
{AttributeType::CUSTOM, offsetof(PolygonCollider2D, points), "points"},
},
// Max instances
1024,
// Serialize
{{"points",
[](std::ostream& os, void* data) {
std::vector<vec2>* points = static_cast<std::vector<vec2>*>(data);
file::write<uint32_t>(os, points->size());
for (vec2 p : *points) {
file::write(os, p.x);
file::write(os, p.y);
}
}}},
// Deserialize
{{"points",
[](std::istream& is, void* data) {
std::vector<vec2>* points = static_cast<std::vector<vec2>*>(data);
uint32_t size;
file::read(is, size);
for (int i = 0; i < size; i++) {
vec2 p;
file::read(is, p.x);
file::read(is, p.y);
points->push_back(p);
}
}}},
// renderUI
{{"",
[=](void* data, std::string imguiId) // Define how the component will be rendered
{
// Render offset
const std::vector<AttributeDescription> aDescs = TypedComponentRegistry<PolygonCollider2D>::getDescription().attributeDescriptions;
float size = aDescs[1].offset - aDescs[0].offset;
void* attribData = (void*)((uint8_t*)data + aDescs[0].offset);
ComponentRegistry::renderUIAttribute(aDescs[0], attribData, size, imguiId + aDescs[0].name);

ImGui::Text("Points");
std::vector<vec2>* points = (std::vector<vec2>*)((uint8_t*)data + aDescs[1].offset);
for (int i = 0; i < points->size(); i++) {
ImGui::PushID(i);
{
// Edit point
ImGui::DragFloat2("##PolygonCollider2DPoint", (float*)&((*points)[i]));

// Delete point
ImGui::SameLine();
if (ImGui::Button("-##PolygonCollider2DDeletePoint"))
points->erase(points->begin() + i);

// Rearrange point
if (i > 0) {
ImGui::SameLine();
if (ImGui::Button("^")) {
std::swap((*points)[i], (*points)[i - 1]);
}
}
if (i < points->size() - 1) {
ImGui::SameLine();
if (ImGui::Button("v")) {
std::swap((*points)[i], (*points)[i + 1]);
}
}
}
ImGui::PopID();
}
// Add point
if (ImGui::Button("+##PolygonCollider2DAddPoint"))
points->push_back({});
}}},
};

return desc;
}

} // namespace atta::component
34 changes: 34 additions & 0 deletions src/atta/component/components/polygonCollider2D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//--------------------------------------------------
// Atta Component Module
// polygonCollider2D.h
// Date: 2022-10-16
// By Breno Cunha Queiroz
//--------------------------------------------------
#ifndef ATTA_COMPONENT_COMPONENTS_POLYGON_COLLIDER_2D_H
#define ATTA_COMPONENT_COMPONENTS_POLYGON_COLLIDER_2D_H

#include <atta/component/interface.h>

namespace atta::component {

/// %Component to create a polygon collider
/** This collider can be used only for 2D physics.
*
* Transform and RigidBody2D components are necessary
* for the entity to participate in the physics iteration.
*
* This collider allows to define the collision shape from a number of points
*
* The polygon will also be scaled by the Transform.
*/
struct PolygonCollider2D final : public Component {
vec2 offset = {0.0f, 0.0f}; ///< Offset
std::vector<vec2> points; ///< Polygon points
};
ATTA_REGISTER_COMPONENT(PolygonCollider2D)
template <>
ComponentDescription& TypedComponentRegistry<PolygonCollider2D>::getDescription();

} // namespace atta::component

#endif // ATTA_COMPONENT_COMPONENTS_POLYGON_COLLIDER_2D_H
30 changes: 17 additions & 13 deletions src/atta/component/components/rigidBody2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
// By Breno Cunha Queiroz
//--------------------------------------------------
#include <atta/component/components/rigidBody2D.h>
#include <atta/physics/interface.h>
#include <atta/physics/engines/box2DEngine.h>
#include <atta/physics/interface.h>

namespace atta::component {

template <>
ComponentDescription& TypedComponentRegistry<RigidBody2D>::getDescription() {
static ComponentDescription desc = {"Rigid Body 2D",
{{AttributeType::UINT32, offsetof(RigidBody2D, type), "type", {}, {}, {}, {"Dynamic", "Kinematic", "Static"}},
{AttributeType::FLOAT32, offsetof(RigidBody2D, linearVelocity), "linearVelocity", {}, {}, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, angularVelocity), "angularVelocity", {}, {}, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, mass), "mass", 0.0f, 100.0f, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, friction), "friction", 0.0f, 1.0f, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, restitution), "restitution", 0.0f, 1.0f, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, linearDamping), "linearDamping", {}, {}, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, angularDamping), "angularDamping", {}, {}, 0.001f},
{AttributeType::BOOL, offsetof(RigidBody2D, allowSleep), "allowSleep"},
{AttributeType::BOOL, offsetof(RigidBody2D, awake), "awake"},
{AttributeType::BOOL, offsetof(RigidBody2D, fixedRotation), "fixedRotation"}}};
static ComponentDescription desc = {
"Rigid Body 2D",
{
{AttributeType::UINT32, offsetof(RigidBody2D, type), "type", {}, {}, {}, {"Dynamic", "Kinematic", "Static"}},
{AttributeType::FLOAT32, offsetof(RigidBody2D, linearVelocity), "linearVelocity", {}, {}, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, angularVelocity), "angularVelocity", {}, {}, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, mass), "mass", 0.0f, 100.0f, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, friction), "friction", 0.0f, 1.0f, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, restitution), "restitution", 0.0f, 1.0f, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, linearDamping), "linearDamping", {}, {}, 0.001f},
{AttributeType::FLOAT32, offsetof(RigidBody2D, angularDamping), "angularDamping", {}, {}, 0.001f},
{AttributeType::BOOL, offsetof(RigidBody2D, allowSleep), "allowSleep"},
{AttributeType::BOOL, offsetof(RigidBody2D, awake), "awake"},
{AttributeType::BOOL, offsetof(RigidBody2D, fixedRotation), "fixedRotation"},
{AttributeType::BOOL, offsetof(RigidBody2D, groundFriction), "groundFriction"},
}};

return desc;
}
Expand Down
7 changes: 4 additions & 3 deletions src/atta/component/components/rigidBody2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ struct RigidBody2D final : public Component {
/** A sleeping rigid bodies are not take into account on the physics
* broad phase, making the physics simulation faster.
*/
bool allowSleep = true; ///< If the rigid body can sleep
bool awake = true; ///< If the rigid body start awake or sleeping
bool fixedRotation = false; ///< Disable rigid body rotation
bool allowSleep = true; ///< If the rigid body can sleep
bool awake = true; ///< If the rigid body start awake or sleeping
bool fixedRotation = false; ///< Disable rigid body rotation
bool groundFriction = false; ///< Enable ground friction

/// Set the position of the body's origin and rotation.
/// @param position position of the body's origin
Expand Down
2 changes: 0 additions & 2 deletions src/atta/graphics/renderers/fastRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class FastRenderer final : public Renderer {
void render(std::shared_ptr<Camera> camera) override;
void resize(uint32_t width, uint32_t height) override;

uint32_t getWidth() const override { return _geometryPipeline->getRenderPass()->getFramebuffer()->getWidth(); };
uint32_t getHeight() const override { return _geometryPipeline->getRenderPass()->getFramebuffer()->getHeight(); };
void* getImGuiTexture() const override { return _geometryPipeline->getImGuiTexture(); }
std::shared_ptr<Framebuffer> getFramebuffer() override { return _geometryPipeline->getRenderPass()->getFramebuffer(); }

Expand Down
2 changes: 0 additions & 2 deletions src/atta/graphics/renderers/pbrRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class PbrRenderer final : public Renderer {
void render(std::shared_ptr<Camera> camera) override;
void resize(uint32_t width, uint32_t height) override;

uint32_t getWidth() const override { return _geometryPipeline->getRenderPass()->getFramebuffer()->getWidth(); };
uint32_t getHeight() const override { return _geometryPipeline->getRenderPass()->getFramebuffer()->getHeight(); };
void* getImGuiTexture() const override { return _geometryPipeline->getImGuiTexture(); }
std::shared_ptr<Framebuffer> getFramebuffer() override { return _geometryPipeline->getRenderPass()->getFramebuffer(); }

Expand Down
2 changes: 0 additions & 2 deletions src/atta/graphics/renderers/phongRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class PhongRenderer final : public Renderer {
void render(std::shared_ptr<Camera> camera) override;
void resize(uint32_t width, uint32_t height) override;

uint32_t getWidth() const override { return _geometryPipeline->getRenderPass()->getFramebuffer()->getWidth(); };
uint32_t getHeight() const override { return _geometryPipeline->getRenderPass()->getFramebuffer()->getHeight(); };
void* getImGuiTexture() const override { return _geometryPipeline->getImGuiTexture(); }
std::shared_ptr<Framebuffer> getFramebuffer() override { return _geometryPipeline->getRenderPass()->getFramebuffer(); }

Expand Down
10 changes: 5 additions & 5 deletions src/atta/graphics/renderers/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace atta::graphics {

void Renderer::serialize(std::ostream& os) {
file::write(os, _name);
file::write(os, _width);
file::write(os, _height);
file::write<uint32_t>(os, getWidth());
file::write<uint32_t>(os, getHeight());
}

void Renderer::deserialize(std::istream& is) {
// Name should be file::read before to know which renderer to deserialize
decltype(_width) width, height;
file::read(is, width);
file::read(is, height);
uint32_t width, height;
file::read<uint32_t>(is, width);
file::read<uint32_t>(is, height);
resize(width, height);
}

Expand Down
6 changes: 3 additions & 3 deletions src/atta/graphics/renderers/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class Renderer : public file::Serializable {
virtual void render(std::shared_ptr<Camera> camera) = 0;
virtual void resize(uint32_t width, uint32_t height) = 0;

virtual uint32_t getWidth() const = 0;
virtual uint32_t getHeight() const = 0;
virtual void* getImGuiTexture() const = 0;
virtual std::shared_ptr<Framebuffer> getFramebuffer() = 0;

uint32_t getWidth() const { return _width; }
uint32_t getHeight() const { return _height; }
std::string getName() const { return _name.getString(); }
StringId getSID() const { return _name; }

Expand All @@ -38,9 +38,9 @@ class Renderer : public file::Serializable {
unsigned getSerializedSize() { return Serializable::getSerializedSize(this); }

protected:
StringId _name;
uint32_t _width;
uint32_t _height;
StringId _name;
bool _renderDrawer;
bool _renderSelected;
};
Expand Down
Loading

0 comments on commit 77a0e20

Please sign in to comment.