- Added 'Camera'
- Added 'SetuniformMat4' to the 'Shader' , this is a temporary solution
- Added u_ViewProjection to glsl shaders

- Fixed 'Application's' placeholder delta time
- Fixed glfwKeyEvent returning KeyReleasedEvent on GLFW_REPEAT

- Note: Camera is not supported for DirectX for the time being due to hlsl not
       supporting uniforms like glsl
This commit is contained in:
Light 2021-07-05 01:59:18 +04:30
parent 2ab97d3863
commit b8ca0099cb
21 changed files with 241 additions and 37 deletions

View file

@ -6,11 +6,13 @@ R"(
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec4 a_Color;
uniform mat4 u_ViewProjection;
out vec4 vso_FragmentColor;
void main()
{
gl_Position = vec4(a_Position, 1.0);
gl_Position = u_ViewProjection * vec4(a_Position, 1.0);
vso_FragmentColor = a_Color;
}
-GLSL

View file

@ -6,11 +6,13 @@ R"(
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec2 a_TexCoord;
uniform mat4 u_ViewProjection;
out vec2 vso_TexCoord;
void main()
{
gl_Position = vec4(a_Position, 1.0);
gl_Position = u_ViewProjection * vec4(a_Position, 1.0);
vso_TexCoord = a_TexCoord;
}
-GLSL

View file

@ -0,0 +1,33 @@
#include "ltpch.h"
#include "Camera.h"
#include <glm/matrix.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace Light {
Camera::Camera(const glm::vec2& position, float aspectRatio, float zoomLevel)
: m_Up(0.0f, 1.0f, 0.0f), m_Position(position), m_AspectRatio(aspectRatio), m_ZoomLevel(zoomLevel)
{
}
void Camera::CalculateView()
{
m_View = glm::lookAt(glm::vec3(m_Position, -100.0f), glm::vec3(m_Position, 0.0f), m_Up);
}
void Camera::CalculateProjection()
{
m_Projection = glm::ortho(-m_ZoomLevel * m_AspectRatio,
+m_ZoomLevel * m_AspectRatio,
-m_ZoomLevel,
+m_ZoomLevel,
FLT_MAX, FLT_MIN);
}
void Camera::Move(const glm::vec2& position)
{
m_Position += position;
}
}

View file

@ -0,0 +1,35 @@
#pragma once
#include "Base.h"
#include <glm/glm.hpp>
namespace Light {
class Camera
{
private:
glm::vec2 m_Position;
float m_AspectRatio;
float m_ZoomLevel;
const glm::vec3 m_Up;
glm::mat4 m_Projection;
glm::mat4 m_View;
public:
Camera(const glm::vec2& position, float aspectRatio, float zoomLevel);
// CAMERA //
void CalculateView();
void CalculateProjection();
inline const glm::mat4& GetView() const { return m_View; }
inline const glm::mat4& GetProjection() const { return m_Projection; }
// CAMERA_CONTROLLER //
void Move(const glm::vec2& position);
};
}

View file

@ -44,12 +44,12 @@ namespace Light {
while (!m_Window->IsClosed())
{
// update layers
m_LayerStack.OnUpdate(1000.0f / 60.0f); // #todo: implement time
m_LayerStack.OnUpdate(60.0f / 10000.0f); // #todo: implement time
// render layers
m_Window->GetGfxContext()->GetRenderer()->BeginScene();
m_Window->GetGfxContext()->GetRenderer()->BeginFrame();
m_LayerStack.OnRender();
m_Window->GetGfxContext()->GetRenderer()->EndScene();
m_Window->GetGfxContext()->GetRenderer()->EndFrame();
// render user interface
m_Window->GetGfxContext()->GetUserInterface()->Begin();

View file

@ -23,16 +23,6 @@ namespace Light {
return new Renderer(windowHandle, sharedContext);
}
void Renderer::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& tint)
{
s_Context->DrawQuadImpl(position, size, tint);
}
void Renderer::DrawQuad(const glm::vec3& position, const glm::vec2& size, std::shared_ptr<Texture> texture)
{
s_Context->DrawQuadImpl(position, size, texture);
}
void Renderer::DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, const glm::vec4& tint)
{
// locals
@ -62,8 +52,8 @@ namespace Light {
// advance
if (!m_QuadRenderer.Advance())
{
EndScene();
BeginScene();
EndFrame();
BeginFrame();
}
}
@ -99,18 +89,31 @@ namespace Light {
// advance
if (!m_TextureRenderer.Advance())
{
EndScene();
BeginScene();
EndFrame();
BeginFrame();
}
}
void Renderer::BeginScene()
void Renderer::BeginFrame()
{
}
void Renderer::EndFrame()
{
}
void Renderer::BeginSceneImpl(const Camera& camera)
{
m_QuadRenderer.Map();
m_TextureRenderer.Map();
m_QuadRenderer.SetCamera(camera);
m_TextureRenderer.SetCamera(camera);
}
void Renderer::EndScene()
void Renderer::EndSceneImpl()
{
//** QUAD_RENDERER **//
if (m_QuadRenderer.GetQuadCount())

View file

@ -15,6 +15,8 @@ namespace Light {
class RenderCommand;
class Texture;
class Camera;
class SharedContext;
class Renderer
@ -30,17 +32,23 @@ namespace Light {
public:
static Renderer* Create(GLFWwindow* windowHandle, std::shared_ptr<SharedContext> sharedContext);
static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& tint);
static void DrawQuad(const glm::vec3& position, const glm::vec2& size, std::shared_ptr<Texture> texture);
static inline void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& tint) { s_Context->DrawQuadImpl(position, size, tint); }
static inline void DrawQuad(const glm::vec3& position, const glm::vec2& size, std::shared_ptr<Texture> texture) { s_Context->DrawQuadImpl(position, size, texture); }
void BeginScene();
void EndScene();
static inline void BeginScene(const Camera& camera) { s_Context->BeginSceneImpl(camera); }
static inline void EndScene() { s_Context->EndSceneImpl(); }
void BeginFrame();
void EndFrame();
private:
Renderer(GLFWwindow* windowHandle, std::shared_ptr<SharedContext> sharedContext);
void DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, const glm::vec4& tint);
void DrawQuadImpl(const glm::vec3& position, const glm::vec2& size, std::shared_ptr<Texture> texture);
void BeginSceneImpl(const Camera& camera);
void EndSceneImpl();
};
}

View file

@ -1,6 +1,8 @@
#include "ltpch.h"
#include "QuadRendererProgram.h"
#include "Camera/Camera.h"
#include "Graphics/Shader.h"
#include "Graphics/Buffers.h"
#include "Graphics/VertexLayout.h"
@ -35,6 +37,12 @@ namespace Light {
m_QuadCount++;
}
void QuadRendererProgram::SetCamera(const Camera& camera)
{
m_Shader->Bind();
m_Shader->SetUniformMat4("u_ViewProjection", camera.GetProjection() * camera.GetView());
}
void QuadRendererProgram::Map()
{
m_QuadCount = 0u;

View file

@ -12,6 +12,8 @@ namespace Light {
class IndexBuffer;
class VertexLayout;
class Camera;
class SharedContext;
class QuadRendererProgram : RendererProgram
@ -39,6 +41,8 @@ namespace Light {
QuadRendererProgram(unsigned int maxVertices, std::shared_ptr<SharedContext> sharedContext);
bool Advance();
void SetCamera(const Camera& camera) override;
void Map() override;
void Bind() override;

View file

@ -4,8 +4,12 @@
namespace Light {
class Camera;
class RendererProgram
{
virtual void SetCamera(const Camera& camera) = 0;
virtual void Map() = 0;
virtual void Bind() = 0;
};

View file

@ -1,6 +1,8 @@
#include "ltpch.h"
#include "TextureRendererProgram.h"
#include "Camera/Camera.h"
#include "Graphics/Shader.h"
#include "Graphics/Buffers.h"
#include "Graphics/VertexLayout.h"
@ -35,6 +37,12 @@ namespace Light {
m_QuadCount++;
}
void TextureRendererProgram::SetCamera(const Camera& camera)
{
m_Shader->Bind();
m_Shader->SetUniformMat4("u_ViewProjection", camera.GetProjection() * camera.GetView());
}
void TextureRendererProgram::Map()
{
m_QuadCount = 0u;

View file

@ -12,6 +12,8 @@ namespace Light {
class IndexBuffer;
class VertexLayout;
class Camera;
class SharedContext;
class TextureRendererProgram : RendererProgram
@ -40,6 +42,8 @@ namespace Light {
bool Advance();
void SetCamera(const Camera& camera) override;
void Map() override;
void Bind() override;

View file

@ -2,6 +2,8 @@
#include "Base.h"
#include <glm/glm.hpp>
namespace Light {
class SharedContext;
@ -16,6 +18,10 @@ namespace Light {
virtual void Bind() = 0;
virtual void UnBind() = 0;
//** #TEMP_SHADER_UNIFORMS_TEMP# **//
virtual void SetUniformMat4(const std::string& name, const glm::mat4& value) = 0;
protected:
Shader() = default;

View file

@ -5,6 +5,10 @@
#include "Core/Window.h"
// -----------------------------
// Camera ----------------------
#include "Camera/Camera.h"
// -----------------------------
// Debug
#include "Debug/Logger.h"
// -----------------------------

View file

@ -42,4 +42,9 @@ namespace Light {
m_Context->GetDeviceContext()->PSSetShader(nullptr, nullptr, 0u);
}
void dxShader::SetUniformMat4(const std::string& name, const glm::mat4& value)
{
LT_ENGINE_ERROR("dxShader::SetUniformMat4: NOT_IMPLEMENTED");
}
}

View file

@ -3,6 +3,8 @@
#include "Base.h"
#include "Graphics/Shader.h"
#include <glm/glm.hpp>
#include <d3d11.h>
#include <wrl.h>
@ -27,6 +29,8 @@ namespace Light {
void Bind() override;
void UnBind() override;
void SetUniformMat4(const std::string& name, const glm::mat4& value) override;
Microsoft::WRL::ComPtr<ID3DBlob> GetVertexBlob() { return m_VertexBlob; }
};

View file

@ -3,6 +3,10 @@
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/matrix.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace Light {
glShader::glShader(const std::string& vertexSource, const std::string& fragmentSource)
@ -91,4 +95,14 @@ namespace Light {
glUseProgram(NULL);
}
void glShader::SetUniformMat4(const std::string& name, const glm::mat4& value)
{
int location = glGetUniformLocation(m_ShaderID, name.c_str());
if (location == -1)
LT_ENGINE_ERROR("glShader::SetUniformMat4: failed to find uniform: {}", name);
glUniformMatrix4fv(location, 1, GL_FALSE, &(value[0][0]));
}
}

View file

@ -3,6 +3,8 @@
#include "Base.h"
#include "Graphics/Shader.h"
#include <glm/glm.hpp>
namespace Light {
class glShader : public Shader
@ -16,6 +18,8 @@ namespace Light {
void Bind() override;
void UnBind() override;
void SetUniformMat4(const std::string& name, const glm::mat4& value) override;
};
}

View file

@ -37,7 +37,7 @@ namespace Light {
BindGlfwEvents();
// create graphics context
m_GraphicsContext = std::unique_ptr<GraphicsContext>(GraphicsContext::Create(GraphicsAPI::OpenGL, m_Handle));
m_GraphicsContext = std::unique_ptr<GraphicsContext>(GraphicsContext::Create(GraphicsAPI::DirectX, m_Handle));
LT_ENGINE_ASSERT(m_GraphicsContext, "wWindow::wWindow: failed to create 'GraphicsContext'");
}
@ -149,7 +149,7 @@ namespace Light {
if (action == GLFW_PRESS)
callback(KeyPressedEvent(key));
else
else if(action == GLFW_RELEASE)
callback(KeyReleasedEvent(key));
});

View file

@ -4,8 +4,8 @@ Size=400,400
Collapsed=0
[Window][Dear ImGui Demo]
Pos=556,321
Size=241,281
Pos=-4,0
Size=529,418
Collapsed=0
[Window][Dear ImGui Metrics/Debugger]

View file

@ -5,23 +5,79 @@ class SandboxLayer : public Light::Layer
private:
std::shared_ptr<Light::Texture> m_AwesomefaceTexture;
std::vector<glm::vec3> positions;
std::vector<glm::vec2> sizes;
glm::vec2 m_Direction;
float m_Speed = 1.2f;
Light::Camera m_Camera;
public:
SandboxLayer(const std::string& name) : Light::Layer(name)
SandboxLayer(const std::string& name)
: Light::Layer(name), m_Camera(glm::vec2(0.0f), 800.0f / 600.0f, 1.0f), m_Direction(glm::vec2(0.0f, 0.0f))
{
Light::ResourceManager::LoadTexture("awesomeface", "res/Textures/awesomeface.png");
m_AwesomefaceTexture = Light::ResourceManager::GetTexture("awesomeface");
for (int i = 0; i < 100; i++)
{
glm::vec3 position = glm::vec3(-1.0f + (-100.0f / 400.0f) + ((rand() % 1000) / 400.0f), -1.0f + (-100.0f / 300.0f) + ((rand() % 800) / 300.0f), 0.0f);
glm::vec2 size = glm::vec2(100 / 400.0f, 100 / 300.0f);
positions.push_back(position);
sizes.push_back(size);
}
}
void OnRender() override
{
Light::Renderer::DrawQuad(glm::vec3(-0.75f, -0.75f, 0.0f), glm::vec2(0.7f, 0.1f), glm::vec4(1.f, .2f, .2f, 1.0f));
Light::Renderer::DrawQuad(glm::vec3(0.2f, 0.5f, 0.0f), glm::vec2(0.6f, 0.6f), glm::vec4(.2f, 1.f, .2f, 1.0f));
Light::Renderer::DrawQuad(glm::vec3(-0.3f, 0.2f, 0.0f), glm::vec2(.4f, .4f), glm::vec4(.2f, 2.f, 1.f, 1.0f));
m_Camera.CalculateProjection();
m_Camera.CalculateView();
Light::Renderer::DrawQuad(glm::vec3(-0.3f, -0.5f, 0.0f), glm::vec2(0.1f, 0.1f), m_AwesomefaceTexture);
Light::Renderer::DrawQuad(glm::vec3(-0.5f, +0.5f, 0.0f), glm::vec2(0.1f, 0.1f), m_AwesomefaceTexture);
Light::Renderer::DrawQuad(glm::vec3(-0.1f, -0.5f, 0.0f), glm::vec2(0.1f, 0.1f), m_AwesomefaceTexture);
Light::Renderer::DrawQuad(glm::vec3(+0.5f, -0.5f, 0.0f), glm::vec2(0.1f, 0.1f), m_AwesomefaceTexture);
Light::Renderer::BeginScene(m_Camera);
for (int i = 0; i < 100; i++)
Light::Renderer::DrawQuad(positions[i], sizes[i], m_AwesomefaceTexture);
Light::Renderer::EndScene();
}
bool OnKeyPressed(const Light::KeyPressedEvent& event) override
{
// #todo: add input class
if (event.GetKey() == 65) // GLFW_KEY_A
m_Direction.x += 1.0f;
if(event.GetKey() == 68) // GLFW_KEY_D
m_Direction.x += -1.0f;
if (event.GetKey() == 87) // GLFW_KEY_W
m_Direction.y += 1.0f;
if (event.GetKey() == 83) // GLFW_KEY_S
m_Direction.y += -1.0f;
return true;
}
bool OnKeyReleased(const Light::KeyReleasedEvent& event) override
{
// #todo: add input class
if (event.GetKey() == 65) // GLFW_KEY_A
m_Direction.x -= 1.0f;
if (event.GetKey() == 68) // GLFW_KEY_D
m_Direction.x -= -1.0f;
if (event.GetKey() == 87) // GLFW_KEY_W
m_Direction.y -= 1.0f;
if (event.GetKey() == 83) // GLFW_KEY_S
m_Direction.y -= -1.0f;
return true;
}
void OnUpdate(float deltaTime) override
{
m_Camera.Move(m_Direction * m_Speed * deltaTime);
}
};