diff options
author | rtk0c <[email protected]> | 2022-05-08 00:50:52 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-05-08 00:50:52 -0700 |
commit | a4ccb12fb3062e446d5b6a1dfd6fa91ae1c2fa5f (patch) | |
tree | f2765c869af4f37106a0f5ebc530ea7448655a0c /source | |
parent | daca3ada37d885b197d6fa810b6b793f18f8068e (diff) |
Changeset: 29 Redefine world coordinate to have units in meters
Diffstat (limited to 'source')
-rw-r--r-- | source/AppConfig.hpp | 2 | ||||
-rw-r--r-- | source/Camera.cpp | 16 | ||||
-rw-r--r-- | source/Camera.hpp | 1 | ||||
-rw-r--r-- | source/EditorCorePrivate.cpp | 21 | ||||
-rw-r--r-- | source/EditorCorePrivate.hpp | 2 | ||||
-rw-r--r-- | source/SceneThings.cpp | 2 | ||||
-rw-r--r-- | source/main.cpp | 4 |
7 files changed, 27 insertions, 21 deletions
diff --git a/source/AppConfig.hpp b/source/AppConfig.hpp index d797708..794bee5 100644 --- a/source/AppConfig.hpp +++ b/source/AppConfig.hpp @@ -9,7 +9,7 @@ constexpr std::string_view kAppName = "ProjectBrussel"; // Since kAppName is initialized by a C string literal, we know it's null termianted constexpr const char* kAppNameC = kAppName.data(); -inline float mainWidnowWidth; +inline float mainWindowWidth; inline float mainWindowHeight; inline float mainWindowAspectRatio; diff --git a/source/Camera.cpp b/source/Camera.cpp index 9041d80..c634ded 100644 --- a/source/Camera.cpp +++ b/source/Camera.cpp @@ -6,8 +6,10 @@ Camera::Camera() : eye(0.0f, 0.0f, 0.0f) - , target(0.0, 0.0f, -1.0f) - , perspective{ false } { + , target(0.0, 0.0f, -2.0f) + , pixelsPerMeter{ 50.0f } // Basic default + , perspective{ false } // +{ } void Camera::SetEyePos(glm::vec3 pos) { @@ -36,10 +38,12 @@ glm::mat4 Camera::CalcProjectionMatrix() const { if (perspective) { return glm::perspective(90.0f, AppConfig::mainWindowAspectRatio, 0.1f, 1000.0f); } else { + float widthMeters = AppConfig::mainWindowWidth / pixelsPerMeter; + float heightMeters = AppConfig::mainWindowHeight / pixelsPerMeter; return glm::ortho( - eye.x - AppConfig::mainWidnowWidth / 2, - eye.x + AppConfig::mainWidnowWidth / 2, - eye.y - AppConfig::mainWindowHeight / 2, - eye.y + AppConfig::mainWindowHeight / 2); + eye.x - widthMeters / 2, + eye.x + widthMeters / 2, + eye.y - heightMeters / 2, + eye.y + heightMeters / 2); } } diff --git a/source/Camera.hpp b/source/Camera.hpp index 8cfcd0d..72c16f3 100644 --- a/source/Camera.hpp +++ b/source/Camera.hpp @@ -8,6 +8,7 @@ public: std::string name; glm::vec3 eye; glm::vec3 target; + float pixelsPerMeter; bool perspective; public: diff --git a/source/EditorCorePrivate.cpp b/source/EditorCorePrivate.cpp index 38ba8f6..de48ea2 100644 --- a/source/EditorCorePrivate.cpp +++ b/source/EditorCorePrivate.cpp @@ -318,9 +318,8 @@ EditorInstance::EditorInstance(App* app) : mApp{ app } , mEdContentBrowser(&mEdInspector) { mEditorCamera.name = "Editor Camera"s; - mEditorCamera.SetEyePos(glm::vec3(0, 0, 200)); - mEditorCamera.SetTargetPos(glm::vec3(0, 0, 0)); - mEditorCamera.SetHasPerspective(true); + mEditorCamera.SetEyePos(glm::vec3(0, 0, 1)); + mEditorCamera.SetTargetDirection(glm::vec3(0, 0, -1)); app->BindActiveCamera(&mEditorCamera); } @@ -449,12 +448,10 @@ void EditorInstance::Show() { if (mDragCam_Happening) { auto newPos = ImGui::GetMousePos(); // NOTE: we are emulating as if the mouse is dragging the "canvas", through moving the camera in the opposite direction of the natural position delta - cameraPos.x = mDragCam_CamInitial.x + mDragCam_CursorInitial.x - newPos.x; - cameraPos.y = mDragCam_CamInitial.y + -(mDragCam_CursorInitial.y - newPos.y); // Invert Y delta because ImGui uses top-left origin (mouse moving down translates to positive value, but in our coordinate system down is negative) - - // Draw movement indicator - auto drawList = ImGui::GetForegroundDrawList(); - ImGui::DrawArrow(drawList, ImVec2(mDragCam_CursorInitial.x, mDragCam_CursorInitial.y), newPos, IM_COL32(0, 255, 255, 255)); + float deltaX = mDragCam_CursorInitial.x - newPos.x; + cameraPos.x = mDragCam_CamInitial.x + deltaX / camera.pixelsPerMeter; + float deltaY = -(mDragCam_CursorInitial.y - newPos.y); // Invert Y delta because ImGui uses top-left origin (mouse moving down translates to positive value, but in our coordinate system down is negative) + cameraPos.y = mDragCam_CamInitial.y + deltaY / camera.pixelsPerMeter; } if (ImGui::IsMouseReleased(ImGuiMouseButton_Right)) { mDragCam_Happening = false; @@ -626,7 +623,7 @@ void EditorInstance::ShowWorldProperties() { vec.x = std::cos(60.0f); vec.y = 0.0f; vec.z = std::sin(60.0f); - camera.eye = camera.target + 200.0f * vec; + camera.eye = camera.target + 4.0f * vec; camera.SetHasPerspective(true); } @@ -641,6 +638,10 @@ void EditorInstance::ShowWorldProperties() { ImGui::Checkbox("Move camera with scoll wheel", &mMoveCamScrollWheel); ImGui::SliderFloat("Camera scroll speed", &mMoveCamScrollSpeed, 0.01, 10.0f); + if (ImGui::InputFloat("Pixels per meter", &camera.pixelsPerMeter)) { + camera.pixelsPerMeter = std::max(camera.pixelsPerMeter, 0.0f); + } + ImGui::Unindent(); } diff --git a/source/EditorCorePrivate.hpp b/source/EditorCorePrivate.hpp index 616ca45..884d33c 100644 --- a/source/EditorCorePrivate.hpp +++ b/source/EditorCorePrivate.hpp @@ -88,7 +88,7 @@ private: ImVec2 mDragCam_CursorInitial; int mSpriteView_Frame; float mMoveCamScrollSpeed = 1.0f; - float mMoveCamSlideSpeed = 10.0f; + float mMoveCamSlideSpeed = 0.1f; EditorCameraMode mEcm = ECM_2D; bool mSpriteView_OpenNextFrame = false; bool mWindowVisible_ImGuiDemo = false; diff --git a/source/SceneThings.cpp b/source/SceneThings.cpp index 6e4624b..ca5f60d 100644 --- a/source/SceneThings.cpp +++ b/source/SceneThings.cpp @@ -8,7 +8,7 @@ SimpleGeometryObject::SimpleGeometryObject(GameWorld* world) : GameObject(KD_SimpleGeometry, world) , mRenderObject() - , mSize{ 50.0f, 50.0f, 50 } + , mSize{ 1.0f, 1.0f, 1.0f } , mXFaceColor(kXAxisColor) , mYFaceColor(kYAxisColor) , mZFaceColor(kZAxisColor) diff --git a/source/main.cpp b/source/main.cpp index 592350f..c0668d0 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -36,7 +36,7 @@ void GlfwErrorCallback(int error, const char* description) { } void GlfwFramebufferResizeCallback(GLFWwindow* window, int width, int height) { - AppConfig::mainWidnowWidth = width; + AppConfig::mainWindowWidth = width; AppConfig::mainWindowHeight = height; AppConfig::mainWindowAspectRatio = (float)width / height; } @@ -401,7 +401,7 @@ void main() { } } - int fbWidth = AppConfig::mainWidnowWidth; + int fbWidth = AppConfig::mainWindowWidth; int fbHeight = AppConfig::mainWindowHeight; glfwGetFramebufferSize(window, &fbWidth, &fbHeight); glViewport(0, 0, fbWidth, fbHeight); |