Skip to content

Commit

Permalink
Updated model update function
Browse files Browse the repository at this point in the history
  • Loading branch information
Pipe-Runner committed Dec 19, 2023
1 parent d9665ec commit 85737aa
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 109 deletions.
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"cmake.configureArgs": [
"-DVCPKG_APPLOCAL_DEPS=ON",
"-DX_VCPKG_APPLOCAL_DEPS_INSTALL=ON",
"-DVCPKG_MANIFEST_MODE=ON",
"-DVCPKG_TARGET_TRIPLET=x64-linux"
"-DVCPKG_TARGET_TRIPLET=x64-linux",
"-DVCPKG_MANIFEST_MODE=ON"
],
"vcpkg.general.enable": true,
"vcpkg.target.hostTriplet": "",
"vcpkg.target.hostTriplet": "x64-linux",
// use x64-linx / arm64-osx
"vcpkg.target.defaultTriplet": "x64-linux",
"vcpkg.target.useStaticLib": false,
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ brew install pkg-config
- [ ] Remove VCPKG extension and use directly in CMake
- [ ] Figure out how to add new cpp files correctly without needing to delete the build folder.
- [ ] Lift mouse settings on app level instead of per-scene level
- [ ] Scene Graph
- [ ] Find optimised way of doing transforms
115 changes: 71 additions & 44 deletions src/engine/app/base_scene.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
#include "base_scene.h"

BaseScene::BaseScene(RenderContext &renderContext)
: m_renderContext(renderContext) {
BaseScene::BaseScene(RenderContext &renderContext, std::string sceneTitle)
: m_renderContext(renderContext), m_sceneTitle(sceneTitle) {
m_objectPropertiesEditorPtr =
std::make_shared<ObjectPropertiesEditor>(ObjectPropertiesEditor([&]() {
ImGui::Text(m_sceneTitle.c_str());

if (ImGui::BeginCombo(
"Selected Object",
fmt::format("Object {}", m_activeModelIndex).c_str())) {
for (int itemIdx = 0; itemIdx < m_models.size(); itemIdx++) {
const bool is_selected = (m_activeModelIndex == itemIdx);
if (ImGui::Selectable(
fmt::format("Object {}", itemIdx).c_str(),
is_selected))
m_activeModelIndex = itemIdx;

// TODO: When object is selected, disable animation
if (is_selected) {
// disable animation
} else {
// enable animation
}
}
ImGui::EndCombo();
}
}));

m_cameraPropertiesEditorPtr =
std::make_shared<CameraPropertiesEditor>(CameraPropertiesEditor([&]() {
if (ImGui::BeginCombo(
"Active Camera",
fmt::format("Camera {}", m_activeCameraIndex).c_str())) {
for (int itemIdx = 0; itemIdx < m_cameras.size(); itemIdx++) {
const bool is_selected = (m_activeCameraIndex == itemIdx);
if (ImGui::Selectable(
fmt::format("Camera {}", itemIdx).c_str(),
is_selected))
m_activeCameraIndex = itemIdx;

// Set the initial focus when opening the combo
// (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}

ImGui::SeparatorText("Camera Properties");
}));

m_inputPropertiesEditorPtr =
std::make_shared<InputPropertiesEditor>(InputPropertiesEditor([&]() {
ImGui::SeparatorText("Mouse");
ImGui::SliderFloat("X Sensitivity", &m_xSensitivity, 0.01f, 30.0f);
ImGui::SliderFloat("Y Sensitivity", &m_ySensitivity, 0.01f, 30.0f);
ImGui::SliderFloat("Movement Sensitivity", &m_moveSpeed, 0.01f,
30.0f);
ImGui::Checkbox("Inverted", &isInverted);
}));
}

void BaseScene::OnGUIRender() {
if (ImGui::BeginTabBar("Editor")) {
m_objectPropertiesEditorPtr->Render();
if (m_cameras.size() > 0) {
m_cameraPropertiesEditorPtr->Render();
}
m_inputPropertiesEditorPtr->Render();

ImGui::EndTabBar();
}
}

void BaseScene::OnUpdate() {
Expand Down Expand Up @@ -77,46 +146,4 @@ void BaseScene::OnUpdate() {
activeCamera.UpdatePosition(positionDelta);
}
}
}

void BaseScene::OnGUIRender(
std::shared_ptr<ObjectPropertiesEditor> objectPropertiesEditorPtr) {
if (ImGui::BeginTabBar("Editor")) {
objectPropertiesEditorPtr->Render();

if (m_cameras.size() > 0 && ImGui::BeginTabItem("Camera")) {
if (ImGui::BeginCombo(
"Active Camera",
fmt::format("Camera {}", m_activeCameraIndex).c_str())) {
for (int itemIdx = 0; itemIdx < m_cameras.size(); itemIdx++) {
const bool is_selected = (m_activeCameraIndex == itemIdx);
if (ImGui::Selectable(
fmt::format("Camera {}", itemIdx).c_str(),
is_selected))
m_activeCameraIndex = itemIdx;

// Set the initial focus when opening the combo
// (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}

ImGui::SeparatorText("Camera Properties");
ImGui::EndTabItem();
}

if (ImGui::BeginTabItem("Input")) {
ImGui::SeparatorText("Mouse");
ImGui::SliderFloat("X Sensitivity", &m_xSensitivity, 0.01f, 30.0f);
ImGui::SliderFloat("Y Sensitivity", &m_ySensitivity, 0.01f, 30.0f);
ImGui::SliderFloat("Movement Sensitivity", &m_moveSpeed, 0.01f,
30.0f);
ImGui::Checkbox("Inverted", &isInverted);
ImGui::EndTabItem();
}

ImGui::EndTabBar();
}
}
40 changes: 33 additions & 7 deletions src/engine/app/base_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,43 @@ struct RenderContext {
class BaseScene {
protected:
RenderContext &m_renderContext;
std::vector<Model> m_models;

private:
std::vector<Camera> m_cameras;
int m_activeCameraIndex = 0;

int m_activeModelIndex = 0;
float m_xSensitivity = 8.0f;
float m_ySensitivity = 8.0f;
float m_moveSpeed = 8.0f;
bool isInverted = true;

std::string m_sceneTitle;

std::shared_ptr<ObjectPropertiesEditor> m_objectPropertiesEditorPtr;
std::shared_ptr<CameraPropertiesEditor> m_cameraPropertiesEditorPtr;
std::shared_ptr<InputPropertiesEditor> m_inputPropertiesEditorPtr;

public:
BaseScene(RenderContext &renderContext);
BaseScene(RenderContext &renderContext, std::string sceneTitle);

// * Keep in mind that even if these functions are overriden, the base
// * functions can still be called from children
virtual void OnUpdate();
virtual void OnRender(){};
virtual void OnGUIRender(){};
virtual void OnGUIRender();

protected:
// this has intentionally been made non overrideable with a change in
// signature
void OnGUIRender(
std::shared_ptr<ObjectPropertiesEditor> objectPropertiesEditorPtr);

void AddCamera(Camera camera) {
m_cameras.push_back(std::move(camera));
}

Camera &GetActiveCamera() {
if (m_cameras.size() == 0) {
throw std::runtime_error("No camera has been added to the scene");
}

return m_cameras[m_activeCameraIndex];
}

Expand All @@ -73,4 +81,22 @@ class BaseScene {
m_activeCameraIndex = index;
}
}

void AddModel(Model model) {
m_models.push_back(std::move(model));
}

void SetActiveModelIndex(int index) {
if (index < m_models.size()) {
m_activeModelIndex = index;
}
}

Model &GetActiveModel() {
if (m_models.size() == 0) {
throw std::runtime_error("No model has been added to the scene");
}

return m_models[m_activeModelIndex];
}
};
49 changes: 37 additions & 12 deletions src/engine/app/widgets/properties_editor.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
#include "properties_editor.h"

ObjectPropertiesEditor::ObjectPropertiesEditor(std::function<void()> renderCb)
: m_renderCb(renderCb)
{
}

void ObjectPropertiesEditor::Render()
{
if (ImGui::BeginTabItem("Object Properties"))
{
m_renderCb();
ImGui::EndTabItem();
}
}
: m_renderCb(renderCb) {
}

void ObjectPropertiesEditor::SetAdditionalRenderCallback(
std::function<void()> renderCb) {
m_additionalRenderCb = renderCb;
}

void ObjectPropertiesEditor::Render() {
if (ImGui::BeginTabItem("Object Properties")) {
m_renderCb();
m_additionalRenderCb();
ImGui::EndTabItem();
}
}

CameraPropertiesEditor::CameraPropertiesEditor(std::function<void()> renderCb)
: m_renderCb(renderCb) {
}

void CameraPropertiesEditor::Render() {
if (ImGui::BeginTabItem("Camera")) {
m_renderCb();
ImGui::EndTabItem();
}
}

InputPropertiesEditor::InputPropertiesEditor(std::function<void()> renderCb)
: m_renderCb(renderCb) {
}

void InputPropertiesEditor::Render() {
if (ImGui::BeginTabItem("Input")) {
m_renderCb();
ImGui::EndTabItem();
}
}
42 changes: 27 additions & 15 deletions src/engine/app/widgets/properties_editor.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
#pragma once

#include <functional>
#include <imgui.h>

class BasePropertiesEditor
{
virtual void Render(){};
#include <functional>

class BasePropertiesEditor {
virtual void Render(){};
};

class CameraPropertiesEditor : public BasePropertiesEditor
{
CameraPropertiesEditor();
class CameraPropertiesEditor : public BasePropertiesEditor {
private:
std::function<void()> m_renderCb;

public:
CameraPropertiesEditor(std::function<void()> renderCb);
void Render();
};

class LightPropertiesEditor : public BasePropertiesEditor
{
class LightPropertiesEditor : public BasePropertiesEditor {};

class ObjectPropertiesEditor : public BasePropertiesEditor {
private:
// TODO: Having issues with reference here
std::function<void()> m_renderCb;
std::function<void()> m_additionalRenderCb = []() {};

public:
ObjectPropertiesEditor(std::function<void()> renderCb);
void SetAdditionalRenderCallback(std::function<void()> renderCb);
void Render();
};

class ObjectPropertiesEditor : public BasePropertiesEditor
{
class InputPropertiesEditor : public BasePropertiesEditor {
private:
// TODO: Having issues with reference here
std::function<void()> m_renderCb;
std::function<void()> m_renderCb;

public:
ObjectPropertiesEditor(std::function<void()> renderCb);
void Render();
InputPropertiesEditor(std::function<void()> renderCb);
void Render();
};
9 changes: 7 additions & 2 deletions src/engine/core/gl/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Model {
glm::vec3 scale = glm::vec3(1.0f));
~Model();

/// @brief Applies delta translation to the model
/// @param translation
/// @return the model itself
inline Model &Translate(glm::vec3 translation) {
m_position += translation;

Expand All @@ -40,13 +43,14 @@ class Model {
return *this;
}

/// @brief Sets the position of the model
inline Model &SetPosition(glm::vec3 position) {
InitializeModelMatrix(position, m_rotation, m_scale, true);
return *this;
}

/// @brief Rotates the model along x, y, z axis. Rotation is applied in xyz
/// order
/// @brief Rotates the model by delta along x, y, z axis. Rotation is
/// applied in xyz order
/// @param rotation A vec3 containing angles in degree for each axis
inline Model &Rotate(glm::vec3 rotation) {
m_rotation += rotation;
Expand All @@ -64,6 +68,7 @@ class Model {
return *this;
}

/// @brief Sets the rotation of the model
inline Model &SetOrientation(glm::vec3 rotation) {
InitializeModelMatrix(m_position, rotation, m_scale, true);
return *this;
Expand Down
Loading

0 comments on commit 85737aa

Please sign in to comment.