Small rendering improvements

- Make view and projection matrix part of the camera
- Add a getter function for the camera in renderer
- Take shader uniform values by const ref
main
Nigel Barink 2022-12-24 02:04:51 +01:00
parent fd68c5dde3
commit ad79aa2865
6 changed files with 49 additions and 29 deletions

View File

@ -1,6 +1,7 @@
#include <YoggieEngine.h> #include <YoggieEngine.h>
#include "Camera.h" #include "Camera.h"
namespace YoggieEngine { namespace YoggieEngine {
Camera::Camera(glm::vec3 position, glm::vec3 rotation, float zoom) Camera::Camera(glm::vec3 position, glm::vec3 rotation, float zoom)
: Position(position), Rotation(rotation), Zoom(zoom) { : Position(position), Rotation(rotation), Zoom(zoom) {
@ -8,17 +9,28 @@ namespace YoggieEngine {
Right = glm::vec3(0.0f, 0.0f, 1.0f); Right = glm::vec3(0.0f, 0.0f, 1.0f);
Up = glm::vec3(0.0f, 1.0f, 0.0f); Up = glm::vec3(0.0f, 1.0f, 0.0f);
auto rotated_position = glm::rotate(glm::mat4(1.0f), Rotation.x, glm::vec3(1.0f, 0.0f, 0.0f)) * glm::vec4(Position, 1.0f);
ViewMatrix = glm::lookAt(
Position,
glm::vec3{ rotated_position.x, rotated_position.y , rotated_position.z } + Front,
Up);
ProjectionMatrix = glm::perspective(glm::radians(Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
} }
Camera::~Camera() { Camera::~Camera() {
} }
glm::mat4 Camera::GetViewMatrix() {
return glm::lookAt(
void Camera::Update() {
ViewMatrix = glm::lookAt(
Position, Position,
Position + Front, Position + Front,
Up Up);
);
} }
} }

View File

@ -2,20 +2,24 @@
namespace YoggieEngine { namespace YoggieEngine {
class Camera { class Camera {
public: public:
Camera(glm::vec3 position, glm::vec3 rotation, float zoom);
~Camera();
void Update();
glm::vec3 Position; glm::vec3 Position;
glm::vec3 Rotation; glm::vec3 Rotation;
float Zoom; float Zoom;
Camera(glm::vec3 position, glm::vec3 rotation, float zoom); glm::mat4 ViewMatrix;
~Camera(); glm::mat4 ProjectionMatrix;
glm::mat4 GetViewMatrix();
private: private:
glm::vec3 Front; glm::vec3 Front;
glm::vec3 Right; glm::vec3 Right;
glm::vec3 Up; glm::vec3 Up;
}; };
} }

View File

@ -101,29 +101,29 @@ namespace YoggieEngine {
} }
void Shader::setUniformMat4(std::string uniformName, glm::mat4 matrix4) const void Shader::setUniformMat4(std::string uniformName, const glm::mat4& matrix4) const
{ {
glUniformMatrix4fv(glGetUniformLocation(id, uniformName.c_str()), 1, GL_FALSE, glm::value_ptr(matrix4)); glUniformMatrix4fv(glGetUniformLocation(id, uniformName.c_str()), 1, GL_FALSE, glm::value_ptr(matrix4));
} }
void Shader::setUniformVec4(std::string uniformName, glm::vec4 vector4) const void Shader::setUniformVec4(std::string uniformName, const glm::vec4& vector4) const
{ {
glUniform4fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector4)); glUniform4fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector4));
} }
void Shader::setUniformVec3(std::string uniformName, glm::vec3 vector3) const void Shader::setUniformVec3(std::string uniformName, const glm::vec3& vector3) const
{ {
glUniform3fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector3)); glUniform3fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector3));
} }
void Shader::setUniformVec2(std::string uniformName, glm::vec2 vector2) const void Shader::setUniformVec2(std::string uniformName,const glm::vec2& vector2) const
{ {
glUniform2fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector2)); glUniform2fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector2));
} }
void Shader::setUniformFloat(std::string uniformName, float value) const void Shader::setUniformFloat(std::string uniformName, const float& value) const
{ {
glUniform1f(glGetUniformLocation(id, uniformName.c_str()), value); glUniform1f(glGetUniformLocation(id, uniformName.c_str()), value);
} }
void Shader::setUniformInt(std::string uniformName, int value) const void Shader::setUniformInt(std::string uniformName, const int& value) const
{ {
glUniform1i(glGetUniformLocation(id, uniformName.c_str()), value); glUniform1i(glGetUniformLocation(id, uniformName.c_str()), value);
} }

View File

@ -8,12 +8,12 @@ namespace YoggieEngine {
public: public:
Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath); Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath);
void Use() const; void Use() const;
void setUniformMat4(std::string uniformName, glm::mat4 matrix4)const; void setUniformMat4(std::string uniformName, const glm::mat4& matrix4)const;
void setUniformVec4(std::string uniformName, glm::vec4 vector4)const; void setUniformVec4(std::string uniformName, const glm::vec4& vector4)const;
void setUniformVec3(std::string uniformName, glm::vec3 vector3)const; void setUniformVec3(std::string uniformName, const glm::vec3& vector3)const;
void setUniformVec2(std::string uniformName, glm::vec2 vector2)const; void setUniformVec2(std::string uniformName, const glm::vec2& vector2)const;
void setUniformFloat(std::string uniformName, float value)const; void setUniformFloat(std::string uniformName, const float& value)const;
void setUniformInt(std::string uniformName, int value) const; void setUniformInt(std::string uniformName, const int& value) const;
int id; int id;

View File

@ -7,9 +7,7 @@
#include "../Graphics/Primitives/DrawCommand.h" #include "../Graphics/Primitives/DrawCommand.h"
namespace YoggieEngine { namespace YoggieEngine {
float Angle = 0.0; Camera cam = Camera(glm::vec3(12.0f, 0.0f, 0.0f), glm::vec3(45.0f, 0.0f, 0.0f), 90.0f);
Camera cam = Camera(glm::vec3(12.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
Renderer::Renderer(RendererConfig& config) Renderer::Renderer(RendererConfig& config)
: m_framebuffer(Framebuffer(config.ScreenWidth, config.ScreenHeight)) : m_framebuffer(Framebuffer(config.ScreenWidth, config.ScreenHeight))
@ -20,6 +18,10 @@ Renderer::Renderer(RendererConfig& config)
Renderer::~Renderer(){} Renderer::~Renderer(){}
Camera& Renderer::getCamera() {
return cam;
}
void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& transform) { void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& transform) {
if (renderComponent.VAO == 0 || renderComponent.IBO == 0) if (renderComponent.VAO == 0 || renderComponent.IBO == 0)
@ -63,7 +65,7 @@ void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& t
} }
void Renderer::Render() void Renderer::Render()
{ {
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId()); glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId());
@ -89,10 +91,10 @@ void Renderer::Render()
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), command.transform.Position) * glm::scale(glm::mat4(1.0f), command.transform.Scale) * rotation; glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), command.transform.Position) * glm::scale(glm::mat4(1.0f), command.transform.Scale) * rotation;
command.shader.setUniformVec3("Color", glm::vec3(1.0f, 0.0f, 0.0f)); command.shader.setUniformVec3("Color", glm::vec3(0.3f, 0.3f, 0.3f));
command.shader.setUniformMat4("M", modelMatrix); command.shader.setUniformMat4("M", modelMatrix);
command.shader.setUniformMat4("V", cam.GetViewMatrix()); command.shader.setUniformMat4("V", cam.ViewMatrix);
command.shader.setUniformMat4("P", projection); command.shader.setUniformMat4("P", cam.ProjectionMatrix);
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(command.num_elements), glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(command.num_elements),
GL_UNSIGNED_INT, NULL); GL_UNSIGNED_INT, NULL);

View File

@ -24,11 +24,13 @@ namespace YoggieEngine {
void Submit(Render3DComponent& renderComponent, TransformComponent& transform); // Collects DrawCommands void Submit(Render3DComponent& renderComponent, TransformComponent& transform); // Collects DrawCommands
void Render(); // Draw to screen (usingthe drawCalls void Render(); // Draw to screen (using drawCall structs)
void setCurrentFrameBuffer(const Framebuffer& fb); void setCurrentFrameBuffer(const Framebuffer& fb);
void setClearColor(const glm::vec3& ClearColor); void setClearColor(const glm::vec3& ClearColor);
Camera& getCamera();
private: private:
Framebuffer m_framebuffer; Framebuffer m_framebuffer;
glm::vec3 m_clearColor; glm::vec3 m_clearColor;