Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Headers/System/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
Expand Down
2 changes: 1 addition & 1 deletion Headers/System/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace System {
/// Initializes the scene, setting up audio and physics engines.
/// </summary>
static void Initialize();
void Run();
void Run(int windowWidth, int windowHeight);
static void Shutdown();
};
}
Expand Down
23 changes: 19 additions & 4 deletions Sources/System/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <System/Graphics/GraphicsHelpers.h>
#include <climits>
#include <System/Image.hpp>
#include <System/Transform.hpp>


namespace System {
Expand Down Expand Up @@ -56,24 +57,38 @@ 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<int>(viewport.x * windowWidth);
int pixelY = static_cast<int>(viewport.y * windowHeight);
int pixelWidth = static_cast<int>(viewport.width * windowWidth);
int pixelHeight = static_cast<int>(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<float>(pixelWidth) / static_cast<float>(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
System::Graphics::GL::gl_glBindFramebuffer(System::Graphics::GL_FrameBufferTarget::GL_FRAMEBUFFER, targetTexture->GetNativeTexturePtr());
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<float>(targetTexture->GetWidth()) / static_cast<float>(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) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/System/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<System::MeshRenderer>();
Expand Down
2 changes: 1 addition & 1 deletion Tests/scene_render/scene_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main() {
renderer->material = material;

System::Scene scene;
scene.Run();
scene.Run(800, 600);

delete material;
delete shader;
Expand Down
Binary file added Tests/scene_render/scene_render_debug
Binary file not shown.