parent
b2ebd91bec
commit
7a6c7d58c6
33 changed files with 565 additions and 169 deletions
@ -0,0 +1,76 @@ |
|||||||
|
#include "ortho_camera_controller.h" |
||||||
|
#include "bakara/events/dispacher.h" |
||||||
|
#include "bakara/events/event.h" |
||||||
|
#include "bakara/events/window_event.h" |
||||||
|
#include "bakara/io/keyboard.h" |
||||||
|
#include "glm/trigonometric.hpp" |
||||||
|
#include <cmath> |
||||||
|
|
||||||
|
namespace Bk |
||||||
|
{ |
||||||
|
OrthoCameraController::OrthoCameraController(float aspectRatio, bool rotation, bool zoom) |
||||||
|
: aspectRatio(aspectRatio),
|
||||||
|
camera(-aspectRatio * zoomLevel, aspectRatio * zoomLevel, -zoomLevel, zoomLevel),
|
||||||
|
rotation(rotation), zoom(zoom) {} |
||||||
|
|
||||||
|
void OrthoCameraController::OnEvent(Event& e) |
||||||
|
{ |
||||||
|
EventDispatcher dispatcher(e); |
||||||
|
dispatcher.dispatch<WindowResizeEvent>(BK_BIND_DISPACHER_FN(WindowResizeEvent, OnWindowResize)); |
||||||
|
dispatcher.dispatch<MouseScrollEvent>(BK_BIND_DISPACHER_FN(MouseScrollEvent, OnMouseScroll)); |
||||||
|
} |
||||||
|
|
||||||
|
void OrthoCameraController::OnUpdate(DeltaTime dt) |
||||||
|
{ |
||||||
|
if (rotation)
|
||||||
|
{ |
||||||
|
if(Keyboard::KeyDown(KeyCode::Q)) |
||||||
|
cameraRotation -= rotationSpeed * dt; |
||||||
|
if(Keyboard::KeyDown(KeyCode::E)) |
||||||
|
cameraRotation += rotationSpeed * dt; |
||||||
|
camera.SetRotation(cameraRotation); |
||||||
|
} |
||||||
|
|
||||||
|
if(Keyboard::KeyDown(KeyCode::W)) |
||||||
|
{ |
||||||
|
cameraPosition.x += -std::sin(Math::radians(cameraRotation)) * moveSpeed * dt; |
||||||
|
cameraPosition.y += std::cos(Math::radians(cameraRotation)) * moveSpeed * dt; |
||||||
|
} |
||||||
|
if(Keyboard::KeyDown(KeyCode::S)) |
||||||
|
{ |
||||||
|
cameraPosition.x -= -std::sin(Math::radians(cameraRotation)) * moveSpeed * dt; |
||||||
|
cameraPosition.y -= std::cos(Math::radians(cameraRotation)) * moveSpeed * dt; |
||||||
|
} |
||||||
|
if(Keyboard::KeyDown(KeyCode::D)) |
||||||
|
{ |
||||||
|
cameraPosition.x += std::cos(Math::radians(cameraRotation)) * moveSpeed * dt; |
||||||
|
cameraPosition.y += std::sin(Math::radians(cameraRotation)) * moveSpeed * dt; |
||||||
|
} |
||||||
|
if(Keyboard::KeyDown(KeyCode::A))
|
||||||
|
{ |
||||||
|
cameraPosition.x -= std::cos(Math::radians(cameraRotation)) * moveSpeed * dt; |
||||||
|
cameraPosition.y -= std::sin(Math::radians(cameraRotation)) * moveSpeed * dt; |
||||||
|
} |
||||||
|
camera.SetPosition(cameraPosition); |
||||||
|
} |
||||||
|
|
||||||
|
bool OrthoCameraController::OnMouseScroll(MouseScrollEvent& e) |
||||||
|
{ |
||||||
|
if (zoom)
|
||||||
|
{ |
||||||
|
zoomLevel -= e.GetDY() * 0.01; |
||||||
|
zoomLevel = std::max(zoomLevel, 0.5f); |
||||||
|
zoomLevel = std::min(zoomLevel, 10.0f); |
||||||
|
camera.SetProjection(-aspectRatio * zoomLevel, aspectRatio * zoomLevel, -zoomLevel, zoomLevel); |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
bool OrthoCameraController::OnWindowResize(WindowResizeEvent& e) |
||||||
|
{ |
||||||
|
aspectRatio = (float)e.GetWidth() / (float)e.GetHeight(); |
||||||
|
camera.SetProjection(-aspectRatio * zoomLevel, aspectRatio * zoomLevel, -zoomLevel, zoomLevel); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include "bakara/core/deltatime.h" |
||||||
|
#include "bakara/events/event.h" |
||||||
|
#include "bakara/events/mouse_event.h" |
||||||
|
#include "bakara/events/window_event.h" |
||||||
|
#include "ortho_camera.h" |
||||||
|
|
||||||
|
namespace Bk
|
||||||
|
{ |
||||||
|
class OrthoCameraController |
||||||
|
{ |
||||||
|
public: |
||||||
|
OrthoCameraController(float aspectRatio, bool rotation = false, bool zoom = false); |
||||||
|
|
||||||
|
void OnUpdate(DeltaTime dt); |
||||||
|
void OnEvent(Event& e); |
||||||
|
|
||||||
|
const OrthoCamera& GetCamera() { return camera; } |
||||||
|
|
||||||
|
void SetMoveSpeed(float moveSpeed) { this->moveSpeed = moveSpeed; } |
||||||
|
void SetRotationSpeed(float rotationSpeed) { this->rotationSpeed = rotationSpeed; } |
||||||
|
|
||||||
|
private: |
||||||
|
float aspectRatio; |
||||||
|
float zoomLevel = 1.0f; |
||||||
|
|
||||||
|
bool rotation, zoom; |
||||||
|
float cameraRotation = 0.0f; |
||||||
|
Vec3 cameraPosition = { 0, 0, 0 }; |
||||||
|
float moveSpeed = 1.0f; |
||||||
|
float rotationSpeed = 1.0f; |
||||||
|
|
||||||
|
OrthoCamera camera; |
||||||
|
|
||||||
|
bool OnMouseScroll(MouseScrollEvent& e); |
||||||
|
bool OnWindowResize(WindowResizeEvent& e); |
||||||
|
}; |
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
#include "graphics_context.h" |
||||||
|
#include "bakara/renderer/renderer.h" |
||||||
|
#include "bakatools/logging/assert.h" |
||||||
|
#include "platforms/opengl/opengl_context.h" |
||||||
|
|
||||||
|
namespace Bk
|
||||||
|
{ |
||||||
|
Scope<GraphicsContext> GraphicsContext::Create(void *window_handle) |
||||||
|
{ |
||||||
|
switch (Renderer::GetAPI())
|
||||||
|
{ |
||||||
|
case Renderer::API::None: BK_MSG_ASSERT(false, "API not supported"); return nullptr; |
||||||
|
case Renderer::API::Opengl: return CreateScope<Platform::OpenglContext>(window_handle); |
||||||
|
} |
||||||
|
BK_MSG_ASSERT(false, "API not supported");
|
||||||
|
return nullptr; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,81 @@ |
|||||||
|
#include "renderer2D.h" |
||||||
|
#include "bakara/renderer/buffer.h" |
||||||
|
#include "bakara/renderer/buffer_layout.h" |
||||||
|
#include "bakara/renderer/render_command.h" |
||||||
|
#include "bakara/renderer/texture.h" |
||||||
|
|
||||||
|
namespace Bk
|
||||||
|
{ |
||||||
|
Renderer2D::Storage* Renderer2D::data = new Renderer2D::Storage(); |
||||||
|
|
||||||
|
void Renderer2D::Init() |
||||||
|
{ |
||||||
|
data->shaderLib.Load("assets/shaders/flatColor.glsl"); |
||||||
|
data->shaderLib.Load("assets/shaders/texture.glsl"); |
||||||
|
data->squareVA = VertexArray::Create(); |
||||||
|
|
||||||
|
float squareVertices[5 * 4] = { |
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, |
||||||
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, |
||||||
|
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, |
||||||
|
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f |
||||||
|
}; |
||||||
|
Ref<VertexBuffer> squareVB = VertexBuffer::Create(squareVertices, sizeof(squareVertices)); |
||||||
|
squareVB->SetLayout({ |
||||||
|
{ ShaderType::Float3, "a_Position" }, |
||||||
|
{ ShaderType::Float2, "a_TextCord" } |
||||||
|
}); |
||||||
|
data->squareVA->AddVertexBuffer(squareVB); |
||||||
|
|
||||||
|
u32 squareIndices[6] = { 0, 1, 2, 0, 2, 3 }; |
||||||
|
data->squareVA->SetIndexbuffer(IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(u32)));
|
||||||
|
} |
||||||
|
|
||||||
|
void Renderer2D::Shutdown() |
||||||
|
{ |
||||||
|
delete data; |
||||||
|
} |
||||||
|
|
||||||
|
void Renderer2D::BeginScene(const OrthoCamera &camera) |
||||||
|
{ |
||||||
|
data->viewProjection = camera.GetViewProjection();
|
||||||
|
} |
||||||
|
|
||||||
|
void Renderer2D::EndScene() |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void Renderer2D::DrawQuad(const Vec2& position, const Vec2& size, const Color& color) |
||||||
|
{ |
||||||
|
Renderer2D::DrawQuad(Vec3(position, 0), size, color); |
||||||
|
} |
||||||
|
|
||||||
|
void Renderer2D::DrawQuad(const Vec3& position, const Vec2& size, const Color& color) |
||||||
|
{ |
||||||
|
Ref<Shader> shader = data->shaderLib["flatColor"]; |
||||||
|
data->squareVA->Bind(); |
||||||
|
shader->Bind(); |
||||||
|
shader->Set("u_FlatColor", color); |
||||||
|
shader->Set("u_ViewProjection", data->viewProjection); |
||||||
|
shader->Set("u_Transform", Math::scale(Math::translate(Mat4(1.0f), position), Vec3(size, 0))); |
||||||
|
RenderCommand::DrawIndexed(data->squareVA); |
||||||
|
} |
||||||
|
|
||||||
|
void Renderer2D::DrawQuad(const Vec2& position, const Vec2& size, const Ref<Texture2D>& texture) |
||||||
|
{ |
||||||
|
Renderer2D::DrawQuad(Vec3(position, 0), size, texture); |
||||||
|
} |
||||||
|
|
||||||
|
void Renderer2D::DrawQuad(const Vec3& position, const Vec2& size, const Ref<Texture2D>& texture) |
||||||
|
{ |
||||||
|
Ref<Shader> shader = data->shaderLib["texture"]; |
||||||
|
data->squareVA->Bind(); |
||||||
|
texture->Bind(); |
||||||
|
shader->Bind(); |
||||||
|
shader->Set("u_Texture", 0); |
||||||
|
shader->Set("u_ViewProjection", data->viewProjection); |
||||||
|
shader->Set("u_Transform", Math::scale(Math::translate(Mat4(1.0f), position), Vec3(size, 0))); |
||||||
|
RenderCommand::DrawIndexed(data->squareVA); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include "bakara/renderer/shader.h" |
||||||
|
#include "bakara/renderer/texture.h" |
||||||
|
#include "bakara/renderer/vertex_array.h" |
||||||
|
#include "cameras/ortho_camera.h" |
||||||
|
|
||||||
|
namespace Bk
|
||||||
|
{ |
||||||
|
class Renderer2D
|
||||||
|
{ |
||||||
|
public: |
||||||
|
static void Init(); |
||||||
|
static void Shutdown(); |
||||||
|
|
||||||
|
static void BeginScene(const OrthoCamera& camera); |
||||||
|
static void EndScene(); |
||||||
|
|
||||||
|
static void DrawQuad(const Vec2& position, const Vec2& size, const Color& color); |
||||||
|
static void DrawQuad(const Vec3& position, const Vec2& size, const Color& color); |
||||||
|
static void DrawQuad(const Vec2& position, const Vec2& size, const Ref<Texture2D>& texture); |
||||||
|
static void DrawQuad(const Vec3& position, const Vec2& size, const Ref<Texture2D>& texture); |
||||||
|
private: |
||||||
|
struct Storage |
||||||
|
{ |
||||||
|
Mat4 viewProjection; |
||||||
|
Ref<VertexArray> squareVA; |
||||||
|
ShaderLibrary shaderLib; |
||||||
|
};
|
||||||
|
|
||||||
|
static Storage* data;
|
||||||
|
}; |
||||||
|
} |
Loading…
Reference in New Issue