Skip to content

Commit

Permalink
camera component
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Jan 4, 2024
1 parent 0a0fa70 commit f65ee84
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 55 deletions.
21 changes: 20 additions & 1 deletion editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void EditorApp::update(float dt)
{
game->update(dt);
}
else
else if (isViewportFocused)
{
goop::Camera* cam = scene->getCurrentCamera();

Expand Down Expand Up @@ -149,6 +149,7 @@ void EditorApp::gui()
// Game Viewport
auto r = goop::sys::gRenderer.get();
ImGui::Begin("Viewport");
isViewportFocused = ImGui::IsWindowFocused();
ImVec2 max = ImGui::GetWindowContentRegionMax();
ImVec2 min = ImGui::GetWindowContentRegionMin();
max.x += ImGui::GetWindowPos().x;
Expand Down Expand Up @@ -239,9 +240,11 @@ void EditorApp::gui()
ImGui::Text("Transform");
auto& tc = e.getComponent<goop::TransformComponent>();

ImGui::PushID("Transform");
ImGui::DragFloat3("Position", &tc.position[0], 0.1f);
ImGui::DragFloat3("Rotation", &tc.rotation[0], 0.1f);
ImGui::DragFloat3("Scale", &tc.scale[0], 0.1f);
ImGui::PopID();
}
if (e.hasComponent<goop::RigidbodyComponent>())
{
Expand Down Expand Up @@ -359,6 +362,16 @@ void EditorApp::gui()
ImGui::EndPopup();
}
}
if (e.hasComponent<goop::CameraComponent>())
{
ImGui::Text("Camera");
auto& cam = e.getComponent<goop::CameraComponent>();
ImGui::PushID("Camera");
ImGui::DragFloat3("Position", &cam.position[0], 0.1f);
ImGui::DragFloat3("Rotation", &cam.rotation[0], 0.1f);
ImGui::Checkbox("Active", &cam.active);
ImGui::PopID();
}
guiCustomComponents(scene);
if (ImGui::Button("Add Component"))
{
Expand Down Expand Up @@ -407,6 +420,12 @@ void EditorApp::gui()
ImGui::CloseCurrentPopup();
addComponentPopupOpen = false;
}
if (!e.hasComponent<goop::CameraComponent>() && ImGui::Button("Camera"))
{
e.addComponent<goop::CameraComponent>();
ImGui::CloseCurrentPopup();
addComponentPopupOpen = false;
}
for (const auto& name : customComponentNames)
{
if (ImGui::Button(name.c_str()))
Expand Down
1 change: 1 addition & 0 deletions editor/EditorApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ class EditorApp : public goop::App
char texturePath[256] = "";
std::optional<goop::Entity> selectedEntity = std::nullopt;
std::vector<std::string> customComponentNames;
bool isViewportFocused = false;
};
14 changes: 14 additions & 0 deletions game/GameApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ void GameApp::update(float dt)
{
goop::sys::gPhysics->simulate(dt);
updateCustomComponents(scene, dt);

auto view = scene->view<goop::CameraComponent>();
for (auto entity : view)
{
auto e = goop::Entity(entity, scene);
auto& cc = e.getComponent<goop::CameraComponent>();
if (cc.active)
{
auto& tc = e.getComponent<goop::TransformComponent>();
goop::Camera* cam = scene->getCurrentCamera();
cam->setPosition(tc.position + cc.position);
cam->setRotation(cc.rotation + cc.rotation);
}
}
}
6 changes: 5 additions & 1 deletion goop/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ void Camera::updateRotation()

void Camera::setPosition(const glm::vec3& position) { this->position = position; }

void Camera::setRotation(const glm::vec3& rotation) { this->rotation = rotation; }
void Camera::setRotation(const glm::vec3& rotation)
{
this->rotation = rotation;
updateRotation();
}

const glm::vec3& Camera::getPosition() const { return position; }

Expand Down
12 changes: 12 additions & 0 deletions goop/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ struct RigidbodyComponent
}
};

struct CameraComponent
{
glm::vec3 position;
glm::vec3 rotation;
bool active = false;
CameraComponent() : position(0.0f), rotation(0.0f) {}
CameraComponent(const glm::vec3& position, const glm::vec3& rotation, bool active)
: position(position), rotation(rotation), active(active)
{
}
};

class CustomComponent
{
public:
Expand Down
30 changes: 26 additions & 4 deletions goop/Scene.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Sam Collier 2023

#include "Scene.h"
#include "components/CustomComponents.h"
#include "goop/Components.h"
#include "goop/Entity.h"
#include <glm/common.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include <goop/Core.h>
#include "components/CustomComponents.h"

using namespace goop;

Expand Down Expand Up @@ -81,6 +81,14 @@ void Scene::loadScene(nlohmann::json& startScene)
float size[3] = {box["x"], box["y"], box["z"]};
e.addComponent<goop::RigidbodyComponent>(component["mass"], size);
}
else if (component["type"] == "camera")
{
glm::vec3 position = {component["position"]["x"], component["position"]["y"],
component["position"]["z"]};
glm::vec3 rotation = {component["rotation"]["x"], component["rotation"]["y"],
component["rotation"]["z"]};
e.addComponent<goop::CameraComponent>(position, rotation, component["active"]);
}
else if (customComponentFactoryMap.contains(component["type"]))
{
addCustomComponent(component["type"], e, this);
Expand All @@ -105,7 +113,7 @@ nlohmann::json Scene::saveScene()
{
json transformJson;
transformJson["type"] = "transform";
auto tc = e.getComponent<TransformComponent>();
auto& tc = e.getComponent<TransformComponent>();

transformJson["position"]["x"] = tc.position.x;
transformJson["position"]["y"] = tc.position.y;
Expand All @@ -125,7 +133,7 @@ nlohmann::json Scene::saveScene()
{
json meshJson;
meshJson["type"] = "mesh";
auto mc = e.getComponent<MeshComponent>();
auto& mc = e.getComponent<MeshComponent>();
meshJson["path"] = mc.path;
meshJson["texturePath"] = mc.texturePath;
if (std::holds_alternative<goop::Box>(mc.primitive))
Expand All @@ -138,13 +146,27 @@ nlohmann::json Scene::saveScene()
{
json rbJson;
rbJson["type"] = "rigidBody";
auto rbc = e.getComponent<RigidbodyComponent>();
auto& rbc = e.getComponent<RigidbodyComponent>();
rbJson["mass"] = rbc.mass;
rbJson["box"]["x"] = rbc.box[0];
rbJson["box"]["y"] = rbc.box[1];
rbJson["box"]["z"] = rbc.box[2];
eJson["components"].push_back(rbJson);
}
if (e.hasComponent<CameraComponent>())
{
json camJson;
camJson["type"] = "camera";
auto& cc = e.getComponent<CameraComponent>();
camJson["position"]["x"] = cc.position.x;
camJson["position"]["y"] = cc.position.y;
camJson["position"]["z"] = cc.position.z;
camJson["rotation"]["x"] = cc.rotation.x;
camJson["rotation"]["y"] = cc.rotation.y;
camJson["rotation"]["z"] = cc.rotation.z;
camJson["active"] = cc.active;
eJson["components"].push_back(camJson);
}

saveCustomComponents(this, e, eJson["components"]);

Expand Down
5 changes: 3 additions & 2 deletions goop/sys/platform/glfw/Window_GLFW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ int Window_GLFW::openWindow(uint32_t width, uint32_t height, const char* title,
{
keyDown[key] = false;
}
ImGui_ImplGlfw_KeyCallback(window, key, scancode, action, mods);
});

return 0;
Expand Down Expand Up @@ -93,6 +94,8 @@ void Window_GLFW::pollEvents()
lastMouseX = mouseX;
lastMouseY = mouseY;
glfwPollEvents();
ImGui_ImplGlfw_NewFrame();

if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
bIsLMBDown = true;
Expand All @@ -112,8 +115,6 @@ void Window_GLFW::pollEvents()
}

glfwGetCursorPos(window, &mouseX, &mouseY);

ImGui_ImplGlfw_NewFrame();
}

void Window_GLFW::hideCursor(bool hide)
Expand Down
Loading

0 comments on commit f65ee84

Please sign in to comment.