parent
650e4584bd
commit
554bbe6567
59 changed files with 1091 additions and 360 deletions
@ -1,20 +0,0 @@ |
||||
#pragma once |
||||
|
||||
/*! \file bkpch.h |
||||
Precompiled headers communly used in bakara. |
||||
*/ |
||||
|
||||
#include <iostream> |
||||
#include <ostream> |
||||
#include <utility> |
||||
#include <memory> |
||||
#include <functional> |
||||
#include <string> |
||||
#include <sstream> |
||||
#include <array> |
||||
#include <vector> |
||||
#include <deque> |
||||
#include <exception> |
||||
#include <bakatools.h> |
||||
|
||||
#include <bakara/math/types.h> |
@ -0,0 +1,17 @@ |
||||
#pragma once |
||||
|
||||
namespace Bk |
||||
{ |
||||
class DeltaTime |
||||
{ |
||||
public: |
||||
DeltaTime(float time = 0.0f) : time(time) |
||||
{} |
||||
operator float() { return time; } |
||||
float GetSeconds() { return time; } |
||||
float GetMilliseconds() { return time *1000.0f; } |
||||
|
||||
private: |
||||
float time; |
||||
};
|
||||
} |
@ -0,0 +1,18 @@ |
||||
#pragma once |
||||
|
||||
#include "controller_codes.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class Controller { |
||||
public: |
||||
virtual void Update() = 0; |
||||
virtual float Axes(Code axis) = 0; |
||||
virtual unsigned char Button(Code button) = 0; |
||||
virtual int GetAxesCount() = 0; |
||||
virtual int GetButtonCount() = 0; |
||||
virtual bool IsPresent() = 0; |
||||
virtual const char* GetName() = 0; |
||||
}; |
||||
|
||||
} |
@ -1,39 +0,0 @@ |
||||
#include "keyboard.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
std::vector<bool> Keyboard::keys; |
||||
std::vector<bool> Keyboard::keysChanged; |
||||
|
||||
bool Keyboard::KeyCallback(KeyEvent& e)
|
||||
{ |
||||
//If the button is pressed or down then key is true
|
||||
if (e.get_name() == "KeyPress" || e.get_name() == "KeyRelease") keys[e.get_key()] = !keys[e.get_key()]; |
||||
else keys[e.get_key()] = false; |
||||
//If the button is not down then keyChanged is true
|
||||
keysChanged[e.get_key()] = e.get_name() == "KeyPress" || e.get_name() == "KeyRelease"; |
||||
return false; |
||||
} |
||||
|
||||
bool Keyboard::KeyDown(KeyCode key)
|
||||
{ |
||||
return keys[key]; |
||||
} |
||||
|
||||
bool Keyboard::KeyWentUp(KeyCode key)
|
||||
{ |
||||
return !keys[key] && keyChanged(key); |
||||
} |
||||
|
||||
bool Keyboard::KeyWentDown(KeyCode key)
|
||||
{ |
||||
return keys[key] && keyChanged(key); |
||||
} |
||||
|
||||
bool Keyboard::keyChanged(KeyCode key)
|
||||
{ |
||||
bool ret = keysChanged[key]; |
||||
keysChanged[key] = false; |
||||
return ret; |
||||
} |
||||
} |
@ -1,25 +1,14 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara.pch" |
||||
#include "key_codes.h" |
||||
#include "bakara/events/key_event.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class Keyboard { |
||||
class Keyboard
|
||||
{ |
||||
public: |
||||
|
||||
static bool KeyCallback(KeyEvent& e); |
||||
//Assesors
|
||||
static bool KeyDown(KeyCode key); |
||||
static bool KeyWentUp(KeyCode key); |
||||
static bool KeyWentDown(KeyCode key); |
||||
|
||||
private: |
||||
//Keys state
|
||||
static std::vector<bool> keys; |
||||
static std::vector<bool> keysChanged; |
||||
//Assesor
|
||||
static bool keyChanged(KeyCode key); |
||||
static bool KeyRepeat(Code key); |
||||
static bool KeyUp(Code key); |
||||
static bool KeyDown(Code key); |
||||
}; |
||||
} |
@ -1,101 +0,0 @@ |
||||
#include "mouse.h" |
||||
namespace Bk |
||||
{ |
||||
double Mouse::x = 0; |
||||
double Mouse::y = 0; |
||||
|
||||
double Mouse::lastX = 0; |
||||
double Mouse::lastY = 0; |
||||
|
||||
double Mouse::dX = 0; |
||||
double Mouse::dY = 0; |
||||
|
||||
double Mouse::scrollDX = 0; |
||||
double Mouse::scrollDY = 0; |
||||
|
||||
bool Mouse::firstMouse; |
||||
|
||||
std::vector<bool> Mouse::buttons = { 0 }; |
||||
std::vector<bool> Mouse::buttonsChanged = { 0 }; |
||||
|
||||
bool Mouse::CursorCallback(MouseMoveEvent& e) { |
||||
x = e.get_x(); |
||||
y = e.get_y(); |
||||
|
||||
if(firstMouse) { |
||||
lastX = x; |
||||
lastY = y; |
||||
firstMouse = false; |
||||
} |
||||
dX = x - lastX; |
||||
dY = y -lastY; |
||||
lastX = x; |
||||
lastY = y; |
||||
return false; |
||||
} |
||||
|
||||
bool Mouse::ButtonCallback(MouseButtonEvent& e) { |
||||
if (e.get_name() != "MouseButtonRelease") { |
||||
if(!buttons[e.get_btn()]) { |
||||
buttons[e.get_btn()] = true; |
||||
} |
||||
} else { |
||||
buttons[e.get_btn()] = false; |
||||
} |
||||
|
||||
buttonsChanged[e.get_btn()] = true; |
||||
return false; |
||||
} |
||||
|
||||
bool Mouse::WheelCallback(MouseScrollEvent& e) { |
||||
scrollDX = e.get_dx(); |
||||
scrollDY = e.get_dy(); |
||||
return false; |
||||
}
|
||||
|
||||
Vec2 Mouse::GetPosition() { |
||||
return Vec2(x, y); |
||||
} |
||||
|
||||
double Mouse::GetDX() { |
||||
double _dX = dX; |
||||
dX = 0; |
||||
return _dX; |
||||
} |
||||
|
||||
double Mouse::GetDY() { |
||||
double _dY = dY; |
||||
dY = 0; |
||||
return _dY; |
||||
} |
||||
|
||||
double Mouse::GetScrollDX() { |
||||
double _scrollDX = scrollDX; |
||||
scrollDX = 0; |
||||
return _scrollDX; |
||||
} |
||||
|
||||
double Mouse::GetScrollDY() { |
||||
double _scrollDY = scrollDY; |
||||
scrollDY = 0; |
||||
return _scrollDY; |
||||
}
|
||||
|
||||
bool Mouse::Button(MouseCode button) { |
||||
return buttons[button]; |
||||
} |
||||
|
||||
bool Mouse::ButtonUp(MouseCode button) { |
||||
return !buttons[button] && buttonChanged(button); |
||||
} |
||||
|
||||
bool Mouse::ButtonDown(MouseCode button) { |
||||
return buttons[button] && buttonChanged(button); |
||||
} |
||||
|
||||
bool Mouse::buttonChanged(MouseCode button) { |
||||
bool ret = buttonsChanged[button]; |
||||
buttons[button] = false; |
||||
return ret; |
||||
} |
||||
} |
@ -1,49 +1,15 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara.pch" |
||||
#include "bakara/math/types.h" |
||||
#include "mouse_codes.h" |
||||
#include "bakara/events/mouse_event.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class Mouse { |
||||
public: |
||||
//GLFW callback function
|
||||
static bool CursorCallback(MouseMoveEvent& e); |
||||
static bool ButtonCallback(MouseButtonEvent& e); |
||||
static bool WheelCallback(MouseScrollEvent& e); |
||||
|
||||
//Mouse position assesors
|
||||
static Vec2 GetPosition(); |
||||
|
||||
//Mouse scroll assesors
|
||||
static double GetDX(); |
||||
static double GetDY(); |
||||
static double GetScrollDX(); |
||||
static double GetScrollDY(); |
||||
|
||||
//Mouse buttons assesors
|
||||
static bool Button(MouseCode button); |
||||
static bool ButtonUp(MouseCode button); |
||||
static bool ButtonDown(MouseCode button); |
||||
|
||||
private: |
||||
static double x; |
||||
static double y; |
||||
|
||||
static double lastX; |
||||
static double lastY; |
||||
|
||||
static double dX; |
||||
static double dY; |
||||
|
||||
static double scrollDX; |
||||
static double scrollDY; |
||||
|
||||
static bool firstMouse; |
||||
|
||||
static std::vector<bool> buttons; |
||||
static std::vector<bool> buttonsChanged; |
||||
|
||||
static bool buttonChanged(MouseCode button); |
||||
static bool ButtonUp(Code button); |
||||
static bool ButtonDown(Code button); |
||||
static bool ButtonRepeat(Code button); |
||||
}; |
||||
} |
@ -0,0 +1,25 @@ |
||||
#include "buffer.h" |
||||
#include "bakara/renderer/renderer.h" |
||||
#include "bakatools/logging/assert.h" |
||||
#include "platforms/opengl/opengl_buffer.h" |
||||
|
||||
namespace Bk |
||||
{ |
||||
VertexBuffer* VertexBuffer::Create(float *vertices, u32 size) |
||||
{ |
||||
switch (Renderer::GetAPI())
|
||||
{ |
||||
case Renderer::API::None: BK_MSG_ASSERT(false, "API not supported"); return nullptr; |
||||
case Renderer::API::Opengl: return new Platform::OpenglVertexBuffer(vertices, size); |
||||
} |
||||
} |
||||
|
||||
IndexBuffer* IndexBuffer::Create(u32* indices, u32 count) |
||||
{ |
||||
switch (Renderer::GetAPI())
|
||||
{ |
||||
case Renderer::API::None: BK_MSG_ASSERT(false, "API not supported"); return nullptr; |
||||
case Renderer::API::Opengl: return new Platform::OpenglIndexBuffer(indices, count); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,27 @@ |
||||
#pragma once |
||||
#include "bakara/renderer/buffer_layout.h" |
||||
#include "bakatools/container/types.h" |
||||
|
||||
namespace Bk |
||||
{ |
||||
class VertexBuffer
|
||||
{ |
||||
public: |
||||
virtual ~VertexBuffer() {} |
||||
virtual void Bind() = 0; |
||||
virtual void Unbind() = 0; |
||||
virtual void SetLayout(BufferLayout layout) = 0; |
||||
virtual BufferLayout& GetLayout() = 0; |
||||
static VertexBuffer* Create(float* vertices, u32 size); |
||||
}; |
||||
|
||||
class IndexBuffer
|
||||
{ |
||||
public: |
||||
virtual ~IndexBuffer() {} |
||||
virtual void Bind() = 0; |
||||
virtual void Unbind() = 0; |
||||
virtual u32 GetCount() = 0; |
||||
static IndexBuffer* Create(u32* indices, u32 count); |
||||
}; |
||||
} |
@ -0,0 +1,56 @@ |
||||
#include "buffer_layout.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
u32 ShaderType::Size() |
||||
{ |
||||
switch (type)
|
||||
{ |
||||
case None: return 0; |
||||
case Bool: return 1; |
||||
case Int: return 4; |
||||
case Int2: return 4 * 2; |
||||
case Int3: return 4 * 3; |
||||
case Int4: return 4 * 4; |
||||
case Float: return 4; |
||||
case Float2: return 4 * 2; |
||||
case Float3: return 4 * 3; |
||||
case Float4: return 4 * 4; |
||||
case Mat3: return 4 * 3 * 3; |
||||
case Mat4: return 4 * 4 * 4; |
||||
} |
||||
} |
||||
|
||||
u32 ShaderType::Count() |
||||
{ |
||||
switch (type)
|
||||
{ |
||||
case None: return 0; |
||||
case Bool: return 1; |
||||
case Int: return 1; |
||||
case Int2: return 2; |
||||
case Int3: return 3; |
||||
case Int4: return 4; |
||||
case Float: return 1; |
||||
case Float2: return 2; |
||||
case Float3: return 3; |
||||
case Float4: return 4; |
||||
case Mat3: return 3 * 3; |
||||
case Mat4: return 4 * 4; |
||||
} |
||||
} |
||||
|
||||
void BufferLayout::CalculteOffsetAndStride() |
||||
{ |
||||
u32 offset = 0; |
||||
stride = 0; |
||||
for(auto& element : elements) |
||||
{ |
||||
element.offset = offset; |
||||
offset += element.type.Size(); |
||||
stride += element.type.Size(); |
||||
|
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,70 @@ |
||||
#pragma once |
||||
|
||||
#include "bakatools/container/types.h" |
||||
#include <initializer_list> |
||||
#include <vector> |
||||
#include <string> |
||||
|
||||
namespace Bk |
||||
{ |
||||
class ShaderType |
||||
{ |
||||
public: |
||||
enum Type : u8 |
||||
{ |
||||
None = 0,
|
||||
Bool, Int, Int2, Int3, Int4, |
||||
Float, Float2, Float3, Float4, |
||||
Mat3, Mat4 |
||||
}; |
||||
ShaderType() : type(None) {} |
||||
ShaderType(Type type) : type(type) {} |
||||
|
||||
ShaderType& operator=(Type value) { type = value; return *this; } |
||||
operator Type() const { return type; } |
||||
bool operator==(Type type) { return type == this->type; } |
||||
bool operator!=(Type type) { return type != this->type; } |
||||
bool operator==(ShaderType value) { return value.type == this->type; } |
||||
bool operator!=(ShaderType value) { return value.type != this->type; } |
||||
|
||||
u32 Size(); |
||||
u32 Count(); |
||||
private: |
||||
Type type; |
||||
}; |
||||
|
||||
struct BufferElement |
||||
{
|
||||
std::string name; |
||||
u32 offset; |
||||
ShaderType type; |
||||
bool normelized; |
||||
|
||||
BufferElement() = default; |
||||
|
||||
BufferElement(std::string name, ShaderType type, bool normelized = false) |
||||
: name(name), type(type) {} |
||||
|
||||
BufferElement(ShaderType type, std::string name, bool normelized = false) |
||||
: name(name), type(type), normelized(normelized) {} |
||||
}; |
||||
|
||||
class BufferLayout
|
||||
{ |
||||
public: |
||||
BufferLayout() = default; |
||||
|
||||
BufferLayout(const std::initializer_list<BufferElement>& elements) |
||||
: elements(elements) { CalculteOffsetAndStride(); } |
||||
|
||||
inline u32 GetStride() { return stride; } |
||||
inline std::vector<BufferElement>& GetElements() { return elements; } |
||||
|
||||
std::vector<BufferElement>::iterator begin() { return elements.begin(); } |
||||
std::vector<BufferElement>::iterator end() { return elements.end(); } |
||||
private: |
||||
void CalculteOffsetAndStride(); |
||||
std::vector<BufferElement> elements; |
||||
u32 stride; |
||||
}; |
||||
} |
@ -0,0 +1,23 @@ |
||||
#include "ortho_camera.h" |
||||
#include "bakara/math/types.h" |
||||
#include "glm/ext/matrix_clip_space.hpp" |
||||
#include "glm/ext/matrix_transform.hpp" |
||||
#include "glm/matrix.hpp" |
||||
#include "glm/trigonometric.hpp" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
OrthographicCamera::OrthographicCamera(float left, float right, float bottom, float top) |
||||
: projectionMatrix(Math::ortho(left, right, bottom, top, -1.0f, 1.0f)) |
||||
{ |
||||
RecalculViewMatrix(); |
||||
} |
||||
|
||||
void OrthographicCamera::RecalculViewMatrix() |
||||
{ |
||||
Mat4 transform = Math::translate(Mat4(1.0f), position);
|
||||
transform = Math::rotate(transform, Math::radians(rotation) , Vec3(0.0f, 0.0f, 1.0f)); |
||||
viewMatrix = Math::inverse(transform); |
||||
viewProjectionMatrix = projectionMatrix * viewMatrix; |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/math/types.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class OrthographicCamera |
||||
{ |
||||
public: |
||||
OrthographicCamera(float left, float right, float bottom, float top); |
||||
|
||||
void SetPosition(Vec3 position) { this->position = position; RecalculViewMatrix(); } |
||||
Vec3 GetPosition() { return position; } |
||||
void SetRotation(float rotation) { this->rotation = rotation; RecalculViewMatrix(); } |
||||
float GetRotaion() { return rotation; } |
||||
|
||||
Mat4 GetViewMatrix() { return viewMatrix; } |
||||
Mat4 GetProjectionMatrix() { return projectionMatrix; } |
||||
Mat4 GetViewProjectionMatrix() { return viewProjectionMatrix; } |
||||
|
||||
private: |
||||
void RecalculViewMatrix(); |
||||
Mat4 viewMatrix; |
||||
Mat4 projectionMatrix; |
||||
Mat4 viewProjectionMatrix; |
||||
|
||||
Vec3 position = { 0.0f, 0.0f, 0.0f }; |
||||
float rotation = 0.0f; |
||||
}; |
||||
} |
@ -0,0 +1,8 @@ |
||||
#include "render_command.h" |
||||
#include "bakara/renderer/renderer_api.h" |
||||
#include "platforms/opengl/opengl_renderer_api.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
RendererAPI* RenderCommand::rendererAPI = new OpenglRendererAPI(); |
||||
} |
@ -0,0 +1,18 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/math/types.h" |
||||
#include "bakara/renderer/renderer_api.h" |
||||
#include "bakara/renderer/vertex_array.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class RenderCommand |
||||
{ |
||||
public: |
||||
static inline void Clear(Vec4 color) { rendererAPI->Clear(color.r, color.g, color.b, color.a); } |
||||
static inline void Clear(float red, float green, float blue, float alpha) { rendererAPI->Clear(red, green, blue, alpha); } |
||||
static inline void DrawIndexed(Ref<VertexArray> va) { rendererAPI->DrawIndexed(va); } |
||||
private: |
||||
static RendererAPI* rendererAPI; |
||||
}; |
||||
} |
@ -0,0 +1,27 @@ |
||||
#include "renderer.h" |
||||
#include "bakara/renderer/cameras/ortho_camera.h" |
||||
#include "bakara/renderer/render_command.h" |
||||
namespace Bk
|
||||
{ |
||||
Renderer::API Renderer::s_RenderAPI = Renderer::API::Opengl; |
||||
Renderer::SceneData* Renderer::sceneData = new Renderer::SceneData(); |
||||
|
||||
void Renderer::BeginScene(OrthographicCamera camera) |
||||
{ |
||||
sceneData->VPMatrix = camera.GetViewProjectionMatrix(); |
||||
} |
||||
|
||||
void Renderer::EndScene() |
||||
{ |
||||
|
||||
} |
||||
|
||||
void Renderer::Submit(Ref<VertexArray> va, Ref<Shader> shader, Mat4 transform) |
||||
{ |
||||
va->Bind(); |
||||
shader->Bind(); |
||||
shader->Set("viewProjection", sceneData->VPMatrix); |
||||
shader->Set("transform", transform); |
||||
RenderCommand::DrawIndexed(va); |
||||
}
|
||||
} |
@ -0,0 +1,34 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/math/types.h" |
||||
#include "bakara/renderer/cameras/ortho_camera.h" |
||||
#include "bakara/renderer/shader.h" |
||||
#include "bakara/renderer/vertex_array.h" |
||||
#include "bakatools/container/types.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class Renderer |
||||
{ |
||||
public: |
||||
enum class API
|
||||
{ |
||||
None = 0, |
||||
Opengl = 1, |
||||
}; |
||||
|
||||
static API GetAPI() { return s_RenderAPI; } |
||||
|
||||
static void BeginScene(OrthographicCamera camera); |
||||
static void EndScene(); |
||||
|
||||
static void Submit(Ref<VertexArray> va, Ref<Shader> shader, Mat4 transform = Mat4(1.0f));
|
||||
private: |
||||
struct SceneData
|
||||
{ |
||||
Mat4 VPMatrix; |
||||
}; |
||||
static SceneData* sceneData; |
||||
static API s_RenderAPI; |
||||
}; |
||||
} |
@ -0,0 +1,14 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/renderer/vertex_array.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class RendererAPI |
||||
{ |
||||
public: |
||||
virtual ~RendererAPI() {} |
||||
virtual void Clear(float red, float green, float blue, float alpha) = 0; |
||||
virtual void DrawIndexed(Ref<VertexArray> va) = 0; |
||||
}; |
||||
} |
@ -0,0 +1,16 @@ |
||||
#include "shader.h" |
||||
#include "bakara/renderer/renderer.h" |
||||
#include "bakatools/logging/assert.h" |
||||
#include "platforms/opengl/opengl_shader.h" |
||||
|
||||
namespace Bk |
||||
{ |
||||
Shader* Shader::Create(std::string vertexSrc, std::string fragSrc) |
||||
{ |
||||
switch(Renderer::GetAPI()) |
||||
{ |
||||
case Renderer::API::None: BK_MSG_ASSERT(false, "API not supported"); return nullptr; |
||||
case Renderer::API::Opengl: return new Platform::OpenglShader(vertexSrc, fragSrc); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/math/types.h" |
||||
#include <string> |
||||
|
||||
namespace Bk |
||||
{ |
||||
class Shader
|
||||
{ |
||||
public: |
||||
virtual ~Shader() {} |
||||
|
||||
virtual void Bind() = 0; |
||||
virtual void Unbind() = 0; |
||||
|
||||
virtual void Set(const std::string name, int val) = 0; |
||||
virtual void Set(const std::string name, bool val) = 0; |
||||
virtual void Set(const std::string name, float val) = 0; |
||||
virtual void Set(const std::string name, float v1, float v2, float v3) = 0; |
||||
virtual void Set(const std::string name, Vec3 v) = 0; |
||||
virtual void Set(const std::string name, float v1, float v2, float v3, float v4) = 0; |
||||
virtual void Set(const std::string name, Vec4 v) = 0; |
||||
virtual void Set(const std::string name, Mat3 val) = 0; |
||||
virtual void Set(const std::string name, Mat4 val) = 0; |
||||
|
||||
static Shader* Create(std::string vertexSrc, std::string fragSrc); |
||||
}; |
||||
} |
@ -0,0 +1,16 @@ |
||||
#include "bakara/renderer/renderer.h" |
||||
#include "bakatools/logging/assert.h" |
||||
#include "vertex_array.h" |
||||
#include "platforms/opengl/opengl_vertex_array.h" |
||||
|
||||
namespace Bk |
||||
{ |
||||
VertexArray* VertexArray::Create() |
||||
{ |
||||
switch (Renderer::GetAPI())
|
||||
{ |
||||
case Renderer::API::None: BK_MSG_ASSERT(false, "API not supported"); return nullptr; |
||||
case Renderer::API::Opengl: return new Platform::OpenglVertexArray(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,21 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/renderer/buffer.h" |
||||
#include "bakatools/container/types.h" |
||||
|
||||
namespace Bk |
||||
{ |
||||
class VertexArray
|
||||
{ |
||||
public: |
||||
virtual ~VertexArray() {} |
||||
virtual void Bind() = 0; |
||||
virtual void Unbind() = 0; |
||||
virtual void AddVertexBuffer(Ref<VertexBuffer> buf) = 0; |
||||
virtual void SetIndexbuffer(Ref<IndexBuffer> buf) = 0; |
||||
|
||||
virtual std::vector<Ref<VertexBuffer>> GetVertexBuffers() = 0; |
||||
virtual Ref<IndexBuffer> GetIndexbuffer() = 0; |
||||
static VertexArray* Create(); |
||||
}; |
||||
} |
@ -0,0 +1,4 @@ |
||||
#include "glfw_controller.h" |
||||
|
||||
#include <GLFW/glfw3.h> |
||||
|
@ -0,0 +1,35 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/io/controller.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
namespace Platform
|
||||
{ |
||||
class ControllerGLFW : public Controller
|
||||
{ |
||||
public: |
||||
ControllerGLFW() = delete; |
||||
ControllerGLFW(int i); |
||||
|
||||
void Update() override; |
||||
float Axes(Code axis) override; |
||||
unsigned char Button(Code button) override; |
||||
int GetAxesCount() override; |
||||
int GetButtonCount() override; |
||||
bool IsPresent() override; |
||||
const char* GetName() override; |
||||
|
||||
private: |
||||
int present; |
||||
int id; |
||||
const char* name; |
||||
|
||||
int axesCount; |
||||
const float* axes; |
||||
|
||||
int buttonCount; |
||||
const unsigned char* buttons; |
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,27 @@ |
||||
#include "bakara/io/keyboard.h" |
||||
#include "bakara/core/application.h" |
||||
#include <GLFW/glfw3.h> |
||||
|
||||
namespace Bk
|
||||
{ |
||||
bool Keyboard::KeyDown(Code key) |
||||
{ |
||||
auto* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); |
||||
auto state = glfwGetKey(window, static_cast<int32_t>(key)); |
||||
return state == GLFW_PRESS; |
||||
} |
||||
|
||||
bool Keyboard::KeyUp(Code key) |
||||
{ |
||||
auto* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); |
||||
auto state = glfwGetKey(window, static_cast<int32_t>(key)); |
||||
return state == GLFW_RELEASE; |
||||
} |
||||
|
||||
bool Keyboard::KeyRepeat(Code key) |
||||
{ |
||||
auto* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); |
||||
auto state = glfwGetKey(window, static_cast<int32_t>(key)); |
||||
return state == GLFW_REPEAT; |
||||
} |
||||
} |
@ -0,0 +1,35 @@ |
||||
#include "bakara/io/mouse.h" |
||||
#include "bakara/core/application.h" |
||||
#include <GLFW/glfw3.h> |
||||
|
||||
namespace Bk
|
||||
{ |
||||
Vec2 Mouse::GetPosition() |
||||
{ |
||||
auto* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); |
||||
double xpos, ypos; |
||||
glfwGetCursorPos(window, &xpos, &ypos); |
||||
return { (float)xpos, (float)ypos }; |
||||
} |
||||
|
||||
bool Mouse::ButtonUp(Code button) |
||||
{ |
||||
auto* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); |
||||
auto state = glfwGetMouseButton(window, static_cast<int32_t>(button)); |
||||
return state == GLFW_RELEASE; |
||||
} |
||||
|
||||
bool Mouse::ButtonDown(Code button) |
||||
{ |
||||
auto* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); |
||||
auto state = glfwGetMouseButton(window, static_cast<int32_t>(button)); |
||||
return state == GLFW_PRESS; |
||||
} |
||||
|
||||
bool Mouse::ButtonRepeat(Code button) |
||||
{ |
||||
auto* window = static_cast<GLFWwindow*>(Application::Get().GetWindow().GetNativeWindow()); |
||||
auto state = glfwGetMouseButton(window, static_cast<int32_t>(button)); |
||||
return state == GLFW_REPEAT; |
||||
} |
||||
} |
@ -0,0 +1,56 @@ |
||||
#include "opengl_buffer.h" |
||||
#include <glad/glad.h> |
||||
|
||||
namespace Bk::Platform |
||||
{ |
||||
OpenglVertexBuffer::OpenglVertexBuffer(float* vertices, u32 size) |
||||
{ |
||||
glGenBuffers(1, &id); |
||||
glBindBuffer(GL_ARRAY_BUFFER, id);
|
||||
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
|
||||
} |
||||
|
||||
OpenglVertexBuffer::~OpenglVertexBuffer()
|
||||
{ |
||||
glDeleteBuffers(1, &id); |
||||
} |
||||
|
||||
void OpenglVertexBuffer::Bind() |
||||
{ |
||||
glBindBuffer(GL_ARRAY_BUFFER, id);
|
||||
} |
||||
|
||||
void OpenglVertexBuffer::Unbind()
|
||||
{ |
||||
glBindBuffer(GL_ARRAY_BUFFER, 0); |
||||
} |
||||
|
||||
|
||||
OpenglIndexBuffer::OpenglIndexBuffer(u32* indices, u32 count) |
||||
: count(count) |
||||
{ |
||||
glGenBuffers(1, &id); |
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); |
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(u32), indices, GL_STATIC_DRAW); |
||||
} |
||||
|
||||
OpenglIndexBuffer::~OpenglIndexBuffer()
|
||||
{ |
||||
glDeleteBuffers(1, &id); |
||||
} |
||||
|
||||
void OpenglIndexBuffer::Bind() |
||||
{ |
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
|
||||
} |
||||
|
||||
void OpenglIndexBuffer::Unbind()
|
||||
{ |
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
||||
} |
||||
|
||||
u32 OpenglIndexBuffer::GetCount() |
||||
{ |
||||
return count; |
||||
} |
||||
} |
@ -0,0 +1,35 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/renderer/buffer.h" |
||||
#include "bakara/renderer/buffer_layout.h" |
||||
|
||||
namespace Bk::Platform
|
||||
{ |
||||
class OpenglVertexBuffer : public VertexBuffer |
||||
{ |
||||
public: |
||||
OpenglVertexBuffer(float* vertices, u32 size); |
||||
virtual ~OpenglVertexBuffer(); |
||||
virtual void Bind() override; |
||||
virtual void Unbind() override; |
||||
virtual void SetLayout(BufferLayout layout) override { this->layout = layout; } |
||||
virtual BufferLayout& GetLayout() override { return layout; } |
||||
private: |
||||
BufferLayout layout; |
||||
u32 id; |
||||
};
|
||||
|
||||
class OpenglIndexBuffer : public IndexBuffer |
||||
{ |
||||
public: |
||||
OpenglIndexBuffer(u32* vertices, u32 count); |
||||
virtual ~OpenglIndexBuffer(); |
||||
virtual void Bind() override; |
||||
virtual void Unbind() override; |
||||
virtual u32 GetCount() override; |
||||
|
||||
private: |
||||
u32 id; |
||||
u32 count; |
||||
}; |
||||
} |
@ -0,0 +1,16 @@ |
||||
#include "opengl_renderer_api.h" |
||||
#include <glad/glad.h> |
||||
|
||||
namespace Bk
|
||||
{ |
||||
void OpenglRendererAPI::Clear(float red, float green, float blue, float alpha) |
||||
{ |
||||
glClearColor(red, green, blue, alpha); |
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
||||
} |
||||
|
||||
void OpenglRendererAPI::DrawIndexed(Ref<VertexArray> va) |
||||
{ |
||||
glDrawElements(GL_TRIANGLES, va->GetIndexbuffer()->GetCount(), GL_UNSIGNED_INT, nullptr); |
||||
} |
||||
} |
@ -0,0 +1,14 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/renderer/renderer_api.h" |
||||
#include "bakara/renderer/vertex_array.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class OpenglRendererAPI : public RendererAPI |
||||
{ |
||||
public: |
||||
virtual void Clear(float red, float green, float blue, float alpha) override; |
||||
virtual void DrawIndexed(Ref<VertexArray> va) override; |
||||
}; |
||||
} |
@ -0,0 +1,108 @@ |
||||
#include "opengl_shader.h" |
||||
#include "bakatools/logging/log.h" |
||||
#include "bakara/math/types.h" |
||||
#include <glad/glad.h> |
||||
|
||||
namespace Bk::Platform
|
||||
{ |
||||
OpenglShader::OpenglShader(std::string vertexSrc, std::string fragSrc) |
||||
{ |
||||
int success; |
||||
char infoLog[512]; |
||||
//Creating a shader program
|
||||
id = glCreateProgram(); |
||||
//Compile shader
|
||||
GLuint ret = glCreateShader(GL_VERTEX_SHADER); |
||||
const GLchar* shader = vertexSrc.c_str(); |
||||
glShaderSource(ret, 1, &shader, NULL); |
||||
glCompileShader(ret); |
||||
glGetShaderiv(ret, GL_COMPILE_STATUS, &success); |
||||
if(!success) { |
||||
glGetShaderInfoLog(ret, 512, NULL, infoLog); |
||||
BK_INFO("Error with fragment shader compilating : {0}", infoLog);
|
||||
} |
||||
glAttachShader(id, ret); |
||||
|
||||
GLuint ret1 = glCreateShader(GL_FRAGMENT_SHADER); |
||||
shader = fragSrc.c_str(); |
||||
glShaderSource(ret1, 1, &shader, NULL); |
||||
glCompileShader(ret1); |
||||
glGetShaderiv(ret1, GL_COMPILE_STATUS, &success); |
||||
if(!success) { |
||||
glGetShaderInfoLog(ret1, 512, NULL, infoLog); |
||||
BK_INFO("Error with fragment shader compilating : {0}", infoLog);
|
||||
} |
||||
glAttachShader(id, ret1); |
||||
glLinkProgram(id); |
||||
|
||||
glGetProgramiv(id, GL_LINK_STATUS, &success); |
||||
if(!success) { |
||||
glGetProgramInfoLog(id, 512, NULL, infoLog); |
||||
BK_INFO("Error when link sharder in a program : {0}", infoLog);
|
||||
} |
||||
|
||||
glDeleteShader(ret); |
||||
glDeleteShader(ret1); |
||||
} |
||||
|
||||
OpenglShader::~OpenglShader() |
||||
{ |
||||
glDeleteProgram(id); |
||||
} |
||||
|
||||
void OpenglShader::Bind()
|
||||
{ |
||||
glUseProgram(id); |
||||
} |
||||
|
||||
void OpenglShader::Unbind()
|
||||
{ |
||||
glUseProgram(0); |
||||
} |
||||
|
||||
void OpenglShader::Set(const std::string name, int val)
|
||||
{ |
||||
glUniform1i(glGetUniformLocation(id, name.c_str()), val); |
||||
} |
||||
|
||||
void OpenglShader::Set(const std::string name, bool value)
|
||||
{ |
||||
glUniform1i(glGetUniformLocation(id, name.c_str()), (int)value); |
||||
} |
||||
|
||||
void OpenglShader::Set(const std::string name, float val)
|
||||
{ |
||||
glUniform1f(glGetUniformLocation(this->id, name.c_str()), val); |
||||
} |
||||
|
||||
void OpenglShader::Set(const std::string name, float v1, float v2, float v3)
|
||||
{ |
||||
glUniform3f(glGetUniformLocation(id, name.c_str()), v1, v2, v3); |
||||
} |
||||
|
||||
void OpenglShader::Set(const std::string name, Vec3 v)
|
||||
{ |
||||
this->Set(name, v.x, v.y, v.z); |
||||
} |
||||
|
||||
void OpenglShader::Set(const std::string name, float v1, float v2, float v3, float v4)
|
||||
{ |
||||
glUniform4f(glGetUniformLocation(id, name.c_str()), v1, v2, v3, v4); |
||||
} |
||||
|
||||
void OpenglShader::Set(const std::string name, Vec4 v)
|
||||
{ |
||||
glUniformMatrix3fv(glGetUniformLocation(this->id, name.c_str()), 1, GL_FALSE, Math::value_ptr(v)); |
||||
} |
||||
|
||||
void OpenglShader::Set(const std::string name, Mat3 val)
|
||||
{ |
||||
glUniformMatrix3fv(glGetUniformLocation(this->id, name.c_str()), 1, GL_FALSE, glm::value_ptr(val)); |
||||
} |
||||
|
||||
void OpenglShader::Set(const std::string name, Mat4 val)
|
||||
{ |
||||
glUniformMatrix4fv(glGetUniformLocation(this->id, name.c_str()), 1, GL_FALSE, glm::value_ptr(val)); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,29 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/renderer/shader.h" |
||||
|
||||
namespace Bk::Platform
|
||||
{ |
||||
class OpenglShader : public Shader |
||||
{ |
||||
public: |
||||
OpenglShader(std::string vertexSrc, std::string fragSrc); |
||||
virtual ~OpenglShader(); |
||||
|
||||
virtual void Bind() override; |
||||
virtual void Unbind() override; |
||||
|
||||
virtual void Set(const std::string name, int val) override; |
||||
virtual void Set(const std::string name, bool val) override; |
||||
virtual void Set(const std::string name, float val) override; |
||||
virtual void Set(const std::string name, float v1, float v2, float v3) override; |
||||
virtual void Set(const std::string name, Vec3 v) override; |
||||
virtual void Set(const std::string name, float v1, float v2, float v3, float v4) override; |
||||
virtual void Set(const std::string name, Vec4 v) override; |
||||
virtual void Set(const std::string name, Mat3 val) override; |
||||
virtual void Set(const std::string name, Mat4 val) override; |
||||
|
||||
private: |
||||
unsigned int id; |
||||
}; |
||||
} |
@ -0,0 +1,76 @@ |
||||
#include "platforms/opengl/opengl_vertex_array.h" |
||||
#include "bakara/renderer/buffer.h" |
||||
#include "bakara/renderer/buffer_layout.h" |
||||
#include "bakatools/logging/assert.h" |
||||
#include <glad/glad.h> |
||||
|
||||
namespace Bk::Platform |
||||
{ |
||||
static GLenum OpenglTypeFromShaderType(ShaderType type) |
||||
{ |
||||
switch(type) |
||||
{ |
||||
case ShaderType::None: BK_MSG_ASSERT(false, "Shader type not supported"); return GL_NONE; |
||||
case ShaderType::Float: return GL_FLOAT; |
||||
case ShaderType::Float2: return GL_FLOAT; |
||||
case ShaderType::Float3: return GL_FLOAT; |
||||
case ShaderType::Float4: return GL_FLOAT; |
||||
case ShaderType::Mat3: return GL_FLOAT; |
||||
case ShaderType::Mat4: return GL_FLOAT; |
||||
case ShaderType::Int: return GL_INT; |
||||
case ShaderType::Int2: return GL_INT; |
||||
case ShaderType::Int3: return GL_INT; |
||||
case ShaderType::Int4: return GL_INT; |
||||
case ShaderType::Bool: return GL_BOOL; |
||||
} |
||||
} |
||||
|
||||
OpenglVertexArray::OpenglVertexArray() |
||||
{ |
||||
glGenVertexArrays(1, &id); |
||||
} |
||||
|
||||
OpenglVertexArray::~OpenglVertexArray() |
||||
{ |
||||
glDeleteVertexArrays(1, &id); |
||||
} |
||||
|
||||
void OpenglVertexArray::Bind() |
||||
{ |
||||
glBindVertexArray(id); |
||||
} |
||||
|
||||
void OpenglVertexArray::Unbind() |
||||
{ |
||||
glBindVertexArray(0); |
||||
} |
||||
|
||||
void OpenglVertexArray::AddVertexBuffer(Ref<VertexBuffer> buf) |
||||
{ |
||||
BK_MSG_ASSERT(!buf->GetLayout().GetElements().empty(), "Vertex Buffer layout empty"); |
||||
glBindVertexArray(id); |
||||
u32 index = 0; |
||||
u32 stride = buf->GetLayout().GetStride(); |
||||
for(auto& element : buf->GetLayout()) |
||||
{ |
||||
glEnableVertexAttribArray(index); |
||||
glVertexAttribPointer(index,
|
||||
element.type.Count(),
|
||||
OpenglTypeFromShaderType(element.type),
|
||||
element.normelized ? GL_TRUE : GL_FALSE,
|
||||
stride,
|
||||
(const void*)(u64)element.offset); |
||||
index++; |
||||
} |
||||
vBuffers.push_back(buf); |
||||
glBindVertexArray(0); |
||||
} |
||||
|
||||
void OpenglVertexArray::SetIndexbuffer(Ref<IndexBuffer> buf) |
||||
{ |
||||
glBindVertexArray(id); |
||||
buf->Bind(); |
||||
iBuffer = buf; |
||||
glBindVertexArray(0); |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara/renderer/buffer.h" |
||||
#include "bakara/renderer/vertex_array.h" |
||||
#include "bakatools/container/types.h" |
||||
#include <vector> |
||||
|
||||
namespace Bk::Platform |
||||
{ |
||||
|
||||
class OpenglVertexArray : public VertexArray
|
||||
{ |
||||
public: |
||||
OpenglVertexArray(); |
||||
virtual ~OpenglVertexArray(); |
||||
virtual void Bind() override; |
||||
virtual void Unbind() override; |
||||
virtual void AddVertexBuffer(Ref<VertexBuffer> buf) override; |
||||
virtual void SetIndexbuffer(Ref<IndexBuffer> buf) override; |
||||
virtual std::vector<Ref<VertexBuffer>> GetVertexBuffers() override { return vBuffers; } |
||||
virtual Ref<IndexBuffer> GetIndexbuffer() override { return iBuffer; } |
||||
|
||||
private: |
||||
std::vector<Ref<VertexBuffer>> vBuffers; |
||||
Ref<IndexBuffer> iBuffer; |
||||
u32 id; |
||||
}; |
||||
} |
Loading…
Reference in New Issue