Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better camera projection #102

Merged
merged 1 commit into from
Sep 21, 2021
Merged
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
7 changes: 2 additions & 5 deletions include/cinnabar-render/camera.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <cinnabar-core/transform.hpp>

Expand All @@ -14,12 +13,10 @@ namespace ce {
~Camera();

glm::mat4 getViewMatrix();
glm::mat4 getProjection(double aspectRatio) { return glm::perspective(this->fov, aspectRatio, this->nearClip, this->farClip); };

void limitPitch();
void sendToShader(ce::Shader* shader, double aspectRatio);
void sendToShader(ce::Shader* shader);

ce::Transform* transform;
double fov, nearClip, farClip;
glm::mat4 projection;
};
}
8 changes: 2 additions & 6 deletions include/cinnabar-render/render_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ namespace ce {

void setSize(glm::vec2 size);
void setClearColor(glm::vec4 color);
double getAspectRatio() { return m_aspectRatio; };

void
clear(),
clear(),
render(Mesh* mesh, Material* material, Transform* transform, Camera* camera);

private:
double m_aspectRatio; // TODO: aspect ratio shouldn't be stored, the Camera should store a projection matrix and it should be left to the user (programmer) to recalculate the projection matrix (using Camera functions) when the window size changes

void
bind(Mesh* mesh, Material* material, Transform* transform, Camera* camera);
void bind(Mesh* mesh, Material* material, Transform* transform, Camera* camera);
};
}
6 changes: 4 additions & 2 deletions src/cinnabar-render-demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <cinnabar-core/time.hpp>
#include <cinnabar-core/tpnt_log.h>
Expand Down Expand Up @@ -34,7 +35,7 @@ int main(int argc, char* argv[]) {

double mouseSens = 0.05;
ce::Camera* camera = new ce::Camera();
// TODO: Seperate so i can put in a player class later
camera->projection = glm::perspective(glm::radians(75.0), (double)window->getAspectRatio(), 0.1, 100.0);
glm::vec3 cameraVelocity(0.0f);
camera->transform->setPosition(0.0f, 0.0f, 1.5f);
/*
Expand All @@ -55,7 +56,7 @@ int main(int argc, char* argv[]) {
mouseDelta *= -mouseSens;
camera->transform->yaw(mouseDelta.x);
camera->transform->pitch(mouseDelta.y);
camera->limitPitch();
camera->transform->setPitch(std::clamp(camera->transform->getPitch(), -90.0f, 90.0f));
break;
}
case SDL_MOUSEBUTTONDOWN: {
Expand Down Expand Up @@ -123,6 +124,7 @@ int main(int argc, char* argv[]) {
}
case SDL_WINDOWEVENT: {
renderEngine->setSize(window->getWindowSize());
camera->projection = glm::perspective(glm::radians(75.0), (double)window->getAspectRatio(), 0.1, 100.0);
break;
}
}
Expand Down
19 changes: 3 additions & 16 deletions src/cinnabar-render/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
#include <cinnabar-render/shader.hpp>

ce::Camera::Camera()
: transform(NULL),
fov(glm::radians(75.0)),
nearClip(0.1),
farClip(100.0) {
: transform(NULL) {
this->transform = new Transform();
}

Expand All @@ -35,17 +32,7 @@ glm::mat4 ce::Camera::getViewMatrix() {
return transformMatrix;
}

void ce::Camera::limitPitch() {
// TODO: this shouldn't be part of camera, this is related to movement and should be added to the movement scripts
float cameraPitch = this->transform->getPitch();
if (cameraPitch > 90.0f)
cameraPitch = 90.0f;
if (cameraPitch < -90.0f)
cameraPitch = -90.0f;
this->transform->setPitch(cameraPitch);
}

void ce::Camera::sendToShader(ce::Shader* shader, double aspectRatio) {
void ce::Camera::sendToShader(ce::Shader* shader) {
shader->setUniform("transform.view", getViewMatrix());
shader->setUniform("transform.proj", getProjection(aspectRatio));
shader->setUniform("transform.proj", this->projection);
}
6 changes: 2 additions & 4 deletions src/cinnabar-render/render_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ void ce::RenderEngine::bind(Mesh* mesh, Material* material, Transform* transform
// TODO: get rid of unneccecary binding
mesh->sendToShader(material->shader, true);
material->shader->setUniform("transform.model", transform->getMatrix());
camera->sendToShader(material->shader, m_aspectRatio); // TODO: make aspect ratio need to be sent to camera manually on window resize, remove m_aspectRatio from renderEngine
camera->sendToShader(material->shader);

// Bind Things
mesh->bind();
material->bind();
}

ce::RenderEngine::RenderEngine(glm::vec4 clearColor)
: m_aspectRatio(0) {
ce::RenderEngine::RenderEngine(glm::vec4 clearColor) {
GLenum err = glewInit();
if (GLEW_OK != err) {
LOG_ERROR("GLEW error: %s", (const char*)glewGetErrorString(err));
Expand Down Expand Up @@ -56,7 +55,6 @@ void ce::RenderEngine::setClearColor(glm::vec4 color) {

void ce::RenderEngine::setSize(glm::vec2 size) {
glViewport(0, 0, size.x, size.y);
m_aspectRatio = size.x / size.y;
}
void ce::RenderEngine::render(Mesh* mesh, Material* material, Transform* transform, Camera* camera) {
bind(mesh, material, transform, camera);
Expand Down