diff --git a/Headers/System/Camera.hpp b/Headers/System/Camera.hpp index d20da9a..eaccda1 100644 --- a/Headers/System/Camera.hpp +++ b/Headers/System/Camera.hpp @@ -59,7 +59,7 @@ class Camera : public System::Component { return new_camera; } - void RenderStart() ; + void RenderStart(int windowWidth, int windowHeight) ; void TakeScreenshot(const std::string& filename); }; } diff --git a/Headers/System/Scene.hpp b/Headers/System/Scene.hpp index fd30188..922277d 100644 --- a/Headers/System/Scene.hpp +++ b/Headers/System/Scene.hpp @@ -18,7 +18,7 @@ namespace System { /// Initializes the scene, setting up audio and physics engines. /// static void Initialize(); - void Run(); + void Run(int windowWidth, int windowHeight); static void Shutdown(); }; } diff --git a/Sources/System/Camera.cpp b/Sources/System/Camera.cpp index 212bee2..71030bb 100644 --- a/Sources/System/Camera.cpp +++ b/Sources/System/Camera.cpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace System { @@ -56,17 +57,26 @@ System::Camera::~Camera() { allCameras.erase(std::remove(allCameras.begin(), allCameras.end(), this), allCameras.end()); } -void System::Camera::RenderStart() { +void System::Camera::RenderStart(int windowWidth, int windowHeight) { if (targetTexture == nullptr) { // Render to the screen System::Graphics::GL::gl_glBindFramebuffer(System::Graphics::GL_FrameBufferTarget::GL_FRAMEBUFFER, 0);// render to the screen (This is done by using 0 as the second parameter of glBindFramebuffer). - System::Graphics::GL::gl_glViewport(viewport.x, viewport.y, viewport.width, viewport.height); + + int pixelX = static_cast(viewport.x * windowWidth); + int pixelY = static_cast(viewport.y * windowHeight); + int pixelWidth = static_cast(viewport.width * windowWidth); + int pixelHeight = static_cast(viewport.height * windowHeight); + + System::Graphics::GL::gl_glViewport(pixelX, pixelY, pixelWidth, pixelHeight); System::Graphics::GL::gl_glClear(System::Graphics::GL_BitField::COLOR_BUFFER_BIT | System::Graphics::GL_BitField::DEPTH_BUFFER_BIT); + + float aspect = static_cast(pixelWidth) / static_cast(pixelHeight); + projectionMatrix = orthographic ? System::Matrix4x4::Ortho(viewport.x, viewport.x + viewport.width, viewport.y, viewport.y + viewport.height, nearClipPlane, farClipPlane) - : System::Matrix4x4::Perspective(Mathf::Radians(60.0f), viewport.width / viewport.height, nearClipPlane, farClipPlane); + : System::Matrix4x4::Perspective(Mathf::Radians(60.0f), aspect, nearClipPlane, farClipPlane); - viewMatrix = Matrix4x4::LookAt(System::Vector3(0, 0, 5), System::Vector3(0, 0, 0), System::Vector3(0, 1, 0)); + viewMatrix = Matrix4x4::LookAt(transform()->GetPosition(), transform()->GetPosition() + transform()->forward(), transform()->up()); }else{ //Render to the target texture @@ -74,6 +84,11 @@ void System::Camera::RenderStart() { System::Graphics::GL::gl_glViewport(0, 0, targetTexture->GetWidth(), targetTexture->GetHeight()); System::Graphics::GL::gl_glClear(System::Graphics::GL_BitField::COLOR_BUFFER_BIT | System::Graphics::GL_BitField::DEPTH_BUFFER_BIT); + float aspect = static_cast(targetTexture->GetWidth()) / static_cast(targetTexture->GetHeight()); + projectionMatrix = orthographic + ? System::Matrix4x4::Ortho(viewport.x, viewport.x + viewport.width, viewport.y, viewport.y + viewport.height, nearClipPlane, farClipPlane) + : System::Matrix4x4::Perspective(Mathf::Radians(60.0f), aspect, nearClipPlane, farClipPlane); + viewMatrix = Matrix4x4::LookAt(transform()->GetPosition(), transform()->GetPosition() + transform()->forward(), transform()->up()); } } void Camera::TakeScreenshot(const std::string& filename) { diff --git a/Sources/System/Scene.cpp b/Sources/System/Scene.cpp index 91d5c5f..083b7f9 100644 --- a/Sources/System/Scene.cpp +++ b/Sources/System/Scene.cpp @@ -60,7 +60,7 @@ physicsEngine = new JoltPhysicsEngine(); } - void Scene::Run() { + void Scene::Run(int windowWidth, int windowHeight) { // Update All GameObjects for (GameObject* go : GameObject::allGameObjects) { for (auto const& [type, components] : go->components) { @@ -71,7 +71,7 @@ physicsEngine = new JoltPhysicsEngine(); } // Render Scene System::Camera* cam = Camera::Getmain(); - cam->RenderStart(); + cam->RenderStart(windowWidth, windowHeight); for (int i = 0; i < GameObject::allGameObjects.size(); i++) { GameObject* go = GameObject::allGameObjects[i]; System::MeshRenderer* renderer = go->GetComponent(); diff --git a/Tests/scene_render/scene_render.cpp b/Tests/scene_render/scene_render.cpp index ea7405c..009f790 100644 --- a/Tests/scene_render/scene_render.cpp +++ b/Tests/scene_render/scene_render.cpp @@ -46,7 +46,7 @@ int main() { renderer->material = material; System::Scene scene; - scene.Run(); + scene.Run(800, 600); delete material; delete shader; diff --git a/Tests/scene_render/scene_render_debug b/Tests/scene_render/scene_render_debug new file mode 100755 index 0000000..46e160d Binary files /dev/null and b/Tests/scene_render/scene_render_debug differ