Commit before texture adding

main
anulax1225 ago%!(EXTRA string=6 months)
parent 650e4584bd
commit 554bbe6567
  1. 2
      README.md
  2. 18
      src/bakara.h
  3. 20
      src/bakara.pch
  4. 19
      src/bakara/core/application.cpp
  5. 10
      src/bakara/core/application.h
  6. 17
      src/bakara/core/deltatime.h
  7. 6
      src/bakara/core/entry.cpp
  8. 8
      src/bakara/core/layer.h
  9. 2
      src/bakara/core/layer_stack.h
  10. 25
      src/bakara/core/window.h
  11. 3
      src/bakara/events/dispacher.h
  12. 27
      src/bakara/events/event.h
  13. 19
      src/bakara/events/key_event.h
  14. 14
      src/bakara/events/mouse_event.h
  15. 6
      src/bakara/imgui/imgui_layer.cpp
  16. 18
      src/bakara/io/controller.h
  17. 16
      src/bakara/io/controller_codes.h
  18. 8
      src/bakara/io/key_codes.h
  19. 39
      src/bakara/io/keyboard.cpp
  20. 21
      src/bakara/io/keyboard.h
  21. 101
      src/bakara/io/mouse.cpp
  22. 44
      src/bakara/io/mouse.h
  23. 9
      src/bakara/io/mouse_codes.h
  24. 6
      src/bakara/math/types.h
  25. 25
      src/bakara/renderer/buffer.cpp
  26. 27
      src/bakara/renderer/buffer.h
  27. 56
      src/bakara/renderer/buffer_layout.cpp
  28. 70
      src/bakara/renderer/buffer_layout.h
  29. 23
      src/bakara/renderer/cameras/ortho_camera.cpp
  30. 30
      src/bakara/renderer/cameras/ortho_camera.h
  31. 2
      src/bakara/renderer/graphics_context.h
  32. 8
      src/bakara/renderer/render_command.cpp
  33. 18
      src/bakara/renderer/render_command.h
  34. 27
      src/bakara/renderer/renderer.cpp
  35. 34
      src/bakara/renderer/renderer.h
  36. 14
      src/bakara/renderer/renderer_api.h
  37. 16
      src/bakara/renderer/shader.cpp
  38. 28
      src/bakara/renderer/shader.h
  39. 0
      src/bakara/renderer/texture.cpp
  40. 0
      src/bakara/renderer/texture.h
  41. 16
      src/bakara/renderer/vertex_array.cpp
  42. 21
      src/bakara/renderer/vertex_array.h
  43. 4
      src/platforms/glfw/glfw_controller.cpp
  44. 35
      src/platforms/glfw/glfw_controller.h
  45. 0
      src/platforms/glfw/glfw_joystock.cpp
  46. 27
      src/platforms/glfw/glfw_keyboard.cpp
  47. 35
      src/platforms/glfw/glfw_mouse.cpp
  48. 61
      src/platforms/glfw/glfw_window.cpp
  49. 37
      src/platforms/glfw/glfw_window.h
  50. 56
      src/platforms/opengl/opengl_buffer.cpp
  51. 35
      src/platforms/opengl/opengl_buffer.h
  52. 13
      src/platforms/opengl/opengl_context.cpp
  53. 4
      src/platforms/opengl/opengl_context.h
  54. 16
      src/platforms/opengl/opengl_renderer_api.cpp
  55. 14
      src/platforms/opengl/opengl_renderer_api.h
  56. 108
      src/platforms/opengl/opengl_shader.cpp
  57. 29
      src/platforms/opengl/opengl_shader.h
  58. 76
      src/platforms/opengl/opengl_vertex_array.cpp
  59. 28
      src/platforms/opengl/opengl_vertex_array.h

@ -1,4 +1,4 @@
# Baka graphic module
# Baka Rasterazed Artwork
## Prerequiste
* A video/graphics card that supports OpenGL.

@ -14,9 +14,23 @@ Plaform namespace doc
*/
namespace Bk::Plaform {}
namespace Baka = Bk;
#include <bakara/math/types.h>
#include <bakara/io/io_codes.h>
#include <bakara/io/io_codes.h>
#include <bakara/io/keyboard.h>
#include <bakara/io/mouse.h>
#include <bakara/events/events.h>
#include <bakara/core/application.h>
#include <bakara/core/layer.h>
#include <bakara/core/layer.h>
#include <bakara/renderer/cameras/ortho_camera.h>
#include <bakara/renderer/render_command.h>
#include <bakara/renderer/renderer.h>
#include <bakara/renderer/buffer.h>
#include <bakara/renderer/buffer_layout.h>
#include <bakara/renderer/shader.h>
#include <bakara/renderer/vertex_array.h>

@ -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>

@ -1,13 +1,16 @@
#include "application.h"
#include "bakara/core/window.h"
#include "bakatools/logging/assert.h"
#include "bakara/events/dispacher.h"
namespace Bk {
Application* Application::p_instance = nullptr;
Application::Application()
Application::Application(std::string title, u32 width, u32 height)
{
BK_CORE_MSG_ASSERT(p_instance == nullptr, "Application already exists, can not create two application.")
Application::p_instance = this;
h_window = std::unique_ptr<Window>(Window::CreateWindow());
h_window = std::unique_ptr<Window>(Window::CreateWindow(WindowProps(title, width, height)));
imgui_layer = new ImguiLayer();
PushOverlay(imgui_layer);
h_window->SetEventCallback(BK_BIND_EVENT_FN(OnEvent));
@ -19,12 +22,6 @@ namespace Bk {
void Application::OnEvent(Event& e)
{
EventDispatcher dispatcher(e);
dispatcher.dispatch<MouseButtonPressEvent>(BK_BIND_DISPACHER_FN(MouseButtonPressEvent, Mouse::ButtonCallback));
dispatcher.dispatch<MouseButtonReleaseEvent>(BK_BIND_DISPACHER_FN(MouseButtonReleaseEvent, Mouse::ButtonCallback));
dispatcher.dispatch<MouseScrollEvent>(BK_BIND_DISPACHER_FN(MouseScrollEvent, Mouse::WheelCallback));
dispatcher.dispatch<MouseMoveEvent>(BK_BIND_DISPACHER_FN(MouseMoveEvent, Mouse::CursorCallback));
dispatcher.dispatch<KeyPressEvent>(BK_BIND_DISPACHER_FN(KeyPressEvent, Keyboard::KeyCallback));
dispatcher.dispatch<KeyReleaseEvent>(BK_BIND_DISPACHER_FN(KeyReleaseEvent, Keyboard::KeyCallback));
if (!(dispatcher.dispatch<WindowCloseEvent>(BK_BIND_DISPACHER_FN(WindowCloseEvent, OnWindowClose))
|| dispatcher.dispatch<WindowResizeEvent>(BK_BIND_DISPACHER_FN(WindowResizeEvent, OnWindowResize))))
{
@ -57,15 +54,11 @@ namespace Bk {
while (p_running)
{
for (Layer* layer : p_layer_stack)
layer->OnUpdate();
layer->OnUpdate(h_window->GetTime());
imgui_layer->Begin();
for (Layer* layer : p_layer_stack)
layer->ImguiRender();
imgui_layer->End();
h_window->OnUpdate();
}
}

@ -4,11 +4,9 @@
This file contains the main app abstraction.
*/
#include "bakara.pch"
#include "bakara/events/window_event.h"
#include "bakatools/container/types.h"
#include "bakara/core/window.h"
#include "bakara/events/events.h"
#include "bakara/io/mouse.h"
#include "bakara/io/keyboard.h"
#include "bakara/core/layer_stack.h"
#include "bakara/imgui/imgui_layer.h"
@ -23,7 +21,7 @@ namespace Bk {
/*! \fn Bk::Application
Initializes the main window and binds the event callback
*/
Application();
Application(std::string name = "BakaraEngine", u32 width = 1000, u32 height = 800);
/*! \fn Bk::Application::~Application
Virtual destructor enables subclasses to cleanup on termination
*/
@ -93,5 +91,5 @@ namespace Bk {
Must be defined in the user program
@return User defined application
*/
std::unique_ptr<Application> CreateApp();
Application* CreateApp(int argc, char** argv);
}

@ -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;
};
}

@ -1,4 +1,3 @@
#include "bakara.pch"
#include "application.h"
/*! \file entry.cpp
@ -8,14 +7,15 @@
/*! \fn std::unique_ptr<Bk::Application> Bk::CreateApp()
External function implemented client side.
*/
extern std::unique_ptr<Bk::Application> Bk::CreateApp();
extern Bk::Application* Bk::CreateApp(int argc, char** argv);
/*! \fn int main(int argc, char** argv)
Entry of the program.
*/
int main(int argc, char** argv) {
Bk::Log::Init("Bakara");
std::unique_ptr<Bk::Application> app = Bk::CreateApp();
Bk::Application* app = Bk::CreateApp(argc, argv);
app->Run();
delete app;
return 0;
}

@ -1,6 +1,8 @@
#pragma once
#include "bakara.pch"
#include <bakara/events/events.h>
#include "bakara/core/deltatime.h"
#include "bakara/events/event.h"
#include <string>
namespace Bk {
class Layer
@ -12,7 +14,7 @@ namespace Bk {
virtual void OnAttach() {}
virtual void OnDetach() {}
virtual void OnEvent(Event& e) {}
virtual void OnUpdate() {}
virtual void OnUpdate(DeltaTime dt) {}
virtual void ImguiRender() {}
const std::string ToString() const { return name; }
protected:

@ -1,7 +1,7 @@
#pragma once
#include "bakara.pch"
#include "layer.h"
#include <vector>
namespace Bk {
class LayerStack

@ -5,8 +5,11 @@ This file contiens all the interfaces to create a window.
Implementation possible with GLFW, Win32, etc.
*/
#include "bakara.pch"
#include <bakara/events/event.h>
#include "bakara/core/deltatime.h"
#include "bakara/events/event.h"
#include "bakatools/container/types.h"
#include <functional>
#include <string>
namespace Bk {
@ -25,10 +28,16 @@ namespace Bk {
@param width : Width of the window
@param height : Height of the window
*/
WindowProps(std::string title = "Bakara engine", uint width = 1000, uint height = 800)
WindowProps(std::string title, u32 width, u32 height)
: title(title), width(width), height(height) {}
};
/*! \typedef Bk::Window::EventCallback
The EventCallback is a function pointer that takes a Event as parameter
and that reponses to them.
*/
using EventCallback = std::function<void(Event& e)>;
/*! \class Bk::Window
Window is an interface to abstract witch window api is used. This enable compilation with multiple api,
so that it's possible to use the native api of the platform or to implement a graphics api's with more ease.
@ -36,12 +45,6 @@ namespace Bk {
class Window
{
public:
/*! \typedef Bk::Window::EventCallback
The EventCallback is a function pointer that takes a Event as parameter
and that reponses to them.
*/
using EventCallback = std::function<void(Event& e)>;
/*! \fn Bk::Window::~Window
Virtual destructor enables subclasses to cleanup on termination.
*/
@ -93,11 +96,13 @@ namespace Bk {
virtual void* GetNativeWindow() = 0;
virtual DeltaTime GetTime() = 0;
/*! \fn Bk::Window::CreateWindow()
Static function implemented in the api window class to get a window specifique api.
If no parameter is specified, creates the window with the default settings.
@param props : Window information
*/
static Window* CreateWindow(const WindowProps& props = WindowProps());
static Window* CreateWindow(const WindowProps& props);
};
}

@ -1,7 +1,6 @@
#pragma once
#include "bakara.pch"
#include "event.h"
#include "bakara/events/event.h"
namespace Bk {
class EventDispatcher

@ -1,6 +1,8 @@
#pragma once
#include "bakara.pch"
#include "bakatools/base.h"
#include "bakatools/string/format.h"
#include <string>
#define BK_BIND_EVENT_FN(fn) [this](Event& e) { fn(e); }
#define BK_BIND_DISPACHER_FN(event, fn) [this](event& e) -> bool{ return fn(e); }
@ -67,27 +69,4 @@ namespace Bk {
#endif
bool is_in_category(EventCategory category) { return get_category_flags() & category; }
};
class EventDispatcher
{
public:
EventDispatcher(Event& event)
: p_event(event) {}
// F will be deduced by the compiler
template<typename T, typename F>
bool dispatch(const F& func)
{
if (p_event.get_event_type() == T::get_static_type())
{
p_event.handled |= func(static_cast<T&>(p_event));
return true;
}
return false;
}
inline Event& get_event() { return p_event; }
private:
Event& p_event;
};
}

@ -1,35 +1,34 @@
#pragma once
#include <bakara/io/io_codes.h>
#include "bakara/io/key_codes.h"
#include "event.h"
namespace Bk {
class KeyEvent : public Event
{
public:
KeyCode get_key() const { return m_key; }
Code get_key() const { return m_key; }
EVENT_CLASS_CATEGORY(KeyboardCategory | InputCategory)
protected:
KeyEvent(const KeyCode key)
KeyEvent(const Code key)
: m_key(key) {}
KeyCode m_key;
Code m_key;
};
class KeyPressEvent : public KeyEvent
{
public:
KeyPressEvent(KeyCode key, bool is_repeated = false)
: KeyEvent(key), p_is_repeated(is_repeated) {}
KeyPressEvent(Code key, bool IsRepeated = false)
: KeyEvent(key), p_is_repeated(IsRepeated) {}
EVENT_CLASS_TYPE(KeyPress)
EVENT_STRINGIFY("KeyPressEvent: %d repeat : %b", m_key, p_is_repeated)
bool is_repeated() { return p_is_repeated; }
bool IsRepeated() { return p_is_repeated; }
private:
bool p_is_repeated;
@ -38,7 +37,7 @@ namespace Bk {
class KeyReleaseEvent : public KeyEvent
{
public:
KeyReleaseEvent(KeyCode key)
KeyReleaseEvent(Code key)
: KeyEvent(key) {}
EVENT_CLASS_TYPE(KeyRelease)
@ -49,7 +48,7 @@ namespace Bk {
class KeyTypedEvent : public KeyEvent
{
public:
KeyTypedEvent(const KeyCode keycode)
KeyTypedEvent(const Code keycode)
: KeyEvent(keycode) {}
EVENT_CLASS_TYPE(KeyTyped)

@ -1,8 +1,8 @@
#pragma once
#include "bakara/io/mouse_codes.h"
#include "event.h"
#include <bakara/math/types.h>
#include <bakara/io/io_codes.h>
#include "bakara/math/types.h"
namespace Bk
{
@ -48,19 +48,19 @@ namespace Bk
class MouseButtonEvent : public Event
{
public:
MouseCode get_btn() { return m_btn; }
Code get_btn() { return m_btn; }
EVENT_CLASS_CATEGORY(MouseCategory | InputCategory)
protected:
MouseButtonEvent(MouseCode btn)
MouseButtonEvent(Code btn)
: m_btn(btn) {}
MouseCode m_btn;
Code m_btn;
};
class MouseButtonPressEvent : public MouseButtonEvent
{
public:
MouseButtonPressEvent(MouseCode btn)
MouseButtonPressEvent(Code btn)
: MouseButtonEvent(btn) {}
EVENT_STRINGIFY("MouseButtonPressEvent %d", m_btn)
@ -70,7 +70,7 @@ namespace Bk
class MouseButtonReleaseEvent : public MouseButtonEvent
{
public:
MouseButtonReleaseEvent(MouseCode btn)
MouseButtonReleaseEvent(Code btn)
: MouseButtonEvent(btn) {}
EVENT_STRINGIFY("MouseButtonReleaseEvent %d", m_btn)

@ -1,15 +1,9 @@
#include "imgui_layer.h"
#include "bakara.pch"
#include "bakara/core/application.h"
#include <imgui.h>
#include <imgui_internal.h>
#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_opengl3.h>
#include <GLFW/glfw3.h>
#include <glad/glad.h>

@ -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,18 +1,16 @@
#pragma once
#include <cstdint>
namespace Bk {
/*! \typedef Bk::ControllerCode
Unsigned integer representing a unique controller button
*/
using ControllerCode = uint16_t;
/*! \typedef Bk::ControllerAxesCode
Unsigned integer representing a unique controller axe
*/
using ControllerAxes = uint16_t;
using Code = uint16_t;
/*! \enum Bk::ControllerButton
Enumerator made to bind every controller buttons with a Bk::ControllerCode
*/
typedef enum : ControllerCode
typedef enum : Code
{
BTN_DOWN = 0, //!< Cross, A
BTN_LEFT = 1, //!< Square, X
@ -37,7 +35,7 @@ namespace Bk {
/*! \enum Bk::ControllerJoystick
Enum made to bind every controller axes with a Bk::ControllerAxesCode
*/
typedef enum : ControllerAxes
typedef enum : Code
{
AXES_LEFT_STICK_X = 0,
AXES_LEFT_STICK_Y = 1,
@ -45,5 +43,5 @@ namespace Bk {
AXES_LEFT_TRIGGER = 3,
AXES_RIGHT_TRIGGER = 4,
AXES_RIGHT_STICK_Y = 5,
} ControllerJoystick;
} ControllerAxes;
}

@ -1,15 +1,17 @@
#pragma once
#include <cstdint>
namespace Bk {
/*! \typedef Bk::KeyCode
Unsigned integer representing a unique keyboard key
*/
using KeyCode = uint16_t;
using Code = uint16_t;
/*! \enum Bk::KeyCodes
Enumerator made to bind every keyboard keys with a Bk::KeyCode
*/
typedef enum : KeyCode
typedef enum : Code
{
Space = 32, /*!< _ */
Apostrophe = 39, /*!< ' */
@ -139,5 +141,5 @@ namespace Bk {
RightAlt = 346,
RightSuper = 347,
Menu = 348
} KeyCodes;
} KeyCode;
}

@ -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);
};
}

@ -1,14 +1,17 @@
#pragma once
#include <cstdint>
namespace Bk {
/*! \typedef Bk::MouseCode
Unsigned integer representing a unique mouse button
*/
using MouseCode = uint16_t;
using Code = uint16_t;
/*! \enum Bk::MouseCodes
Enumerator made to bind every mouse buttons with a Bk::MouseCode
*/
typedef enum : MouseCode
typedef enum : Code
{
Button0 = 0, /*!< Button left */
Button1 = 1, /*!< Button right */
@ -23,5 +26,5 @@ namespace Bk {
ButtonLeft = Button0,
ButtonRight = Button1,
ButtonMiddle = Button2
} MouseCodes;
} MouseCode;
}

@ -5,11 +5,17 @@ Math types alias file used to abstract math types and not depend on any library.
As long as it's implemented here.
*/
#include "glm/fwd.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
namespace Bk
{
namespace Math = glm;
using Quaterion = glm::quat;
/*! \typedef Bk::Vec2
Wrapper around glm::vec2
*/

@ -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;
};
}

@ -5,7 +5,9 @@ namespace Bk
class GraphicsContext
{
public:
virtual ~GraphicsContext() {}
virtual void Init() = 0;
virtual void SwapBuffers() = 0;
virtual void SetViewport(int width, int height) = 0;
};
}

@ -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;
}
}

@ -1,16 +1,24 @@
#include "glfw_window.h"
#include "bakara/core/deltatime.h"
#include "bakara/events/key_event.h"
#include "bakara/events/mouse_event.h"
#include "bakara/events/window_event.h"
#include "bakatools/logging/assert.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <string.h>
namespace Bk {
Window* Window::CreateWindow(const WindowProps& props)
{
return new Platform::WinGLFW(props);
return new Platform::GlfwWindow(props);
}
namespace Platform {
GlfwWindowData::~GlfwWindowData()
{
delete context;
}
static uint p_glfw_Initialized = 0;
static void glfw_error_callback(int error, const char* description)
@ -18,7 +26,7 @@ namespace Bk {
BK_CORE_CRITICAL("GLFW Error ({0}) {1}", error, description);
}
WinGLFW::WinGLFW(const WindowProps& props)
GlfwWindow::GlfwWindow(const WindowProps& props)
{
p_data.title = props.title;
p_data.width = props.width;
@ -26,13 +34,12 @@ namespace Bk {
Init();
}
WinGLFW::~WinGLFW()
GlfwWindow::~GlfwWindow()
{
delete context;
Close();
}
void WinGLFW::Init()
void GlfwWindow::Init()
{
h_IsOpen = true;
BK_CORE_INFO("Creating window : {0} ({1}, {2})", p_data.title, p_data.width, p_data.height);
@ -43,31 +50,33 @@ namespace Bk {
glfwSetErrorCallback(glfw_error_callback);
}
p_window = glfwCreateWindow((int)p_data.width, (int)p_data.height, p_data.title.c_str(), nullptr, nullptr);
context = new OpenglContext(p_window);
context->Init();
p_data.context = new OpenglContext(p_window);
p_data.context->Init();
p_data.context->SetViewport(p_data.width, p_data.height);
glfwSetWindowUserPointer(p_window, &p_data);
SetVsync(true);
InitEventCallbacks();
}
void WinGLFW::InitEventCallbacks()
void GlfwWindow::InitEventCallbacks()
{
glfwSetFramebufferSizeCallback(p_window, [](GLFWwindow* window, int width, int height)
{
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window);
data.context->SetViewport(width, height);
WindowResizeEvent e(data.width = (uint)width, data.height = (uint)height);
data.callback(e);
});
glfwSetWindowCloseCallback(p_window, [](GLFWwindow* window)
{
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window);
WindowCloseEvent e;
data.callback(e);
});
glfwSetKeyCallback(p_window, [](GLFWwindow* window, int key, int scancode, int action, int mods)
{
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window);
switch(action)
{
case GLFW_PRESS:
@ -92,7 +101,7 @@ namespace Bk {
});
glfwSetMouseButtonCallback(p_window, [](GLFWwindow* window, int button, int action, int mods)
{
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window);
switch(action)
{
case GLFW_PRESS:
@ -111,36 +120,38 @@ namespace Bk {
});
glfwSetScrollCallback(p_window, [](GLFWwindow* window, double offset_x, double offset_y)
{
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window);
MouseScrollEvent e((float)offset_x, (float)offset_y);
data.callback(e);
});
glfwSetCursorPosCallback(p_window, [](GLFWwindow* window, double pos_x, double pos_y)
{
WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window);
MouseMoveEvent e((float)pos_x, (float)pos_y);
data.callback(e);
});
}
void WinGLFW::OnUpdate()
void GlfwWindow::OnUpdate()
{
float time = glfwGetTime();
dt = DeltaTime(time - lastFrameTime);
lastFrameTime = time;
glfwPollEvents();
context->SwapBuffers();
p_data.context->SwapBuffers();
if (h_IsOpen)
{
if (p_Shutdown && h_IsOpen) { Shutdown(); }
}
}
void WinGLFW::SetEventCallback(const EventCallback callback)
void GlfwWindow::SetEventCallback(const EventCallback callback)
{
p_data.callback = callback;
}
void WinGLFW::SetVsync(bool enable)
void GlfwWindow::SetVsync(bool enable)
{
if (h_IsOpen)
{
@ -150,24 +161,24 @@ namespace Bk {
}
}
bool WinGLFW::IsVsync() const
bool GlfwWindow::IsVsync() const
{
return p_data.vsync;
}
void WinGLFW::Shutdown()
void GlfwWindow::Shutdown()
{
h_IsOpen = false;
p_Shutdown = false;
glfwDestroyWindow(p_window);
}
void WinGLFW::Close()
void GlfwWindow::Close()
{
p_Shutdown = true;
}
void WinGLFW::Open()
void GlfwWindow::Open()
{
if (!h_IsOpen)
{

@ -1,17 +1,28 @@
#pragma once
#include "bakara.pch"
#include "bakara/core/deltatime.h"
#include "bakara/core/window.h"
#include "bakara/events/events.h"
#include "platforms/opengl/opengl_context.h"
struct GLFWwindow;
namespace Bk::Platform {
class WinGLFW : public Window
struct GlfwWindowData
{
std::string title;
uint width;
uint height;
bool vsync;
EventCallback callback;
GraphicsContext* context;
~GlfwWindowData();
};
class GlfwWindow : public Window
{
public:
WinGLFW(const WindowProps& props);
virtual ~WinGLFW();
GlfwWindow(const WindowProps& props);
virtual ~GlfwWindow();
inline uint GetWidth() const override { return p_data.width; }
inline uint GetHeight() const override { return p_data.height; }
@ -31,21 +42,17 @@ namespace Bk::Platform {
@return Open flag
*/
bool IsOpen() override { return h_IsOpen; }
virtual DeltaTime GetTime() override { return dt; }
private:
bool h_IsOpen; //!< indicaste if the window is Opened or not
bool p_Shutdown;
GLFWwindow* p_window;
OpenglContext* context;
struct WindowData
{
std::string title;
uint width;
uint height;
bool vsync;
EventCallback callback;
};
WindowData p_data;
GlfwWindowData p_data;
float lastFrameTime = 0.0f;
DeltaTime dt = { 0.0f };
void InitEventCallbacks();
void Init();

@ -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;
};
}

@ -1,3 +1,5 @@
#include "bakatools/logging/assert.h"
#include "bakatools/logging/log.h"
#include "opengl_context.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
@ -16,7 +18,12 @@ namespace Bk::Platform
GLint majVers = 0, minVers = 0;
glGetIntegerv(GL_MAJOR_VERSION, &majVers);
glGetIntegerv(GL_MINOR_VERSION, &minVers);
BK_CORE_INFO("Opengl Version : {0}.{1}", majVers, minVers);
BK_CORE_INFO("Hardware info ");
BK_CORE_INFO(" - Opengl Version {0}.{1}", majVers, minVers);
BK_CORE_INFO(" - GPU Vendor {0}", reinterpret_cast<const char*>(glGetString(GL_VENDOR)));
BK_CORE_INFO(" - Renderer {0}", reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
BK_CORE_INFO(" - Renderer Version {0}", reinterpret_cast<const char*>(glGetString(GL_VERSION)));
BK_CORE_MSG_ASSERT(majVers > 4 || (majVers == 4 && minVers >= 2), "Bakara only support Opengl versions 4.2 or more.")
}
void OpenglContext::SwapBuffers()
@ -24,4 +31,8 @@ namespace Bk::Platform
glfwSwapBuffers(window_handle);
}
void OpenglContext::SetViewport(int width, int height)
{
glViewport(0, 0, width, height);
}
}

@ -1,6 +1,6 @@
#pragma once
#include "bakara.pch"
#include "bakara/renderer/graphics_context.h"
struct GLFWwindow;
@ -11,8 +11,10 @@ namespace Bk::Platform
{
public:
OpenglContext(GLFWwindow* window_handle);
virtual ~OpenglContext() override { }
void Init() override;
void SwapBuffers() override;
void SetViewport(int width, int height) override;
private:
GLFWwindow* window_handle;
};

@ -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…
Cancel
Save