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
This commit is contained in:
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 "Camera.h"
namespace YoggieEngine {
Camera::Camera(glm::vec3 position, glm::vec3 rotation, float zoom)
: Position(position), Rotation(rotation), Zoom(zoom) {
@ -8,17 +9,28 @@ namespace YoggieEngine {
Right = glm::vec3(0.0f, 0.0f, 1.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() {
}
glm::mat4 Camera::GetViewMatrix() {
return glm::lookAt(
void Camera::Update() {
ViewMatrix = glm::lookAt(
Position,
Position + Front,
Up
);
Position + Front,
Up);
}
}

View File

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

View File

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

View File

@ -7,9 +7,7 @@
#include "../Graphics/Primitives/DrawCommand.h"
namespace YoggieEngine {
float Angle = 0.0;
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);
Camera cam = Camera(glm::vec3(12.0f, 0.0f, 0.0f), glm::vec3(45.0f, 0.0f, 0.0f), 90.0f);
Renderer::Renderer(RendererConfig& config)
: m_framebuffer(Framebuffer(config.ScreenWidth, config.ScreenHeight))
@ -20,6 +18,10 @@ Renderer::Renderer(RendererConfig& config)
Renderer::~Renderer(){}
Camera& Renderer::getCamera() {
return cam;
}
void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& transform) {
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());
@ -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;
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("V", cam.GetViewMatrix());
command.shader.setUniformMat4("P", projection);
command.shader.setUniformMat4("V", cam.ViewMatrix);
command.shader.setUniformMat4("P", cam.ProjectionMatrix);
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(command.num_elements),
GL_UNSIGNED_INT, NULL);

View File

@ -24,11 +24,13 @@ namespace YoggieEngine {
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 setClearColor(const glm::vec3& ClearColor);
Camera& getCamera();
private:
Framebuffer m_framebuffer;
glm::vec3 m_clearColor;