feat: Create buffer, vertex array and window classes

This commit is contained in:
Huseyn Ismayilov 2024-08-02 03:32:51 +04:00
parent 5d536e7b71
commit 1b4ce73811
23 changed files with 620 additions and 269 deletions

View File

@ -1,6 +1,7 @@
#ifndef EDITOR_H #ifndef EDITOR_H
#define EDITOR_H #define EDITOR_H
#define GLFW_INCLUDE_NONE
#include "Engine.h" #include "Engine.h"
#include "UI.h" #include "UI.h"
@ -10,21 +11,11 @@ public:
Editor(); Editor();
~Editor(); ~Editor();
void Initialize(); static void Init();
void Run(); static void Run();
void Shutdown(); static void Shutdown();
GLFWwindow* GetWindow() const;
private: private:
GLFWwindow* window;
Engine engine;
UI ui;
int screenWidth = 800;
int screenHeight = 600;
void InitializeWindow();
void InitializeImGui(); void InitializeImGui();
}; };

View File

@ -1,34 +0,0 @@
#ifndef UI_H
#define UI_H
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <string>
class UI
{
public:
UI();
~UI();
void Initialize(GLFWwindow* window);
void Run();
void Render();
void Shutdown();
void Print(const std::string& message);
void ShowMenu();
void ShowHierarchy();
void ShowProject();
void ShowConsole();
void ShowScene();
void ShowInspector();
private:
ImVec4 clearColor = ImVec4(0.0f, 0.0f, 0.0f, 1.00f);
ImVec4* styleColors;
std::string log;
};
#endif

View File

@ -1,6 +1,6 @@
[Window][DockSpaceViewport_11111111] [Window][DockSpaceViewport_11111111]
Pos=0,19 Pos=0,19
Size=800,581 Size=900,581
Collapsed=0 Collapsed=0
[Window][Debug##Default] [Window][Debug##Default]
@ -9,41 +9,41 @@ Size=400,400
Collapsed=0 Collapsed=0
[Window][Hierarchy] [Window][Hierarchy]
Pos=602,19 Pos=710,19
Size=198,581 Size=190,581
Collapsed=0 Collapsed=0
DockId=0x00000006,0 DockId=0x00000006,0
[Window][Scene] [Window][Scene]
Pos=192,19 Pos=199,19
Size=408,428 Size=509,420
Collapsed=0 Collapsed=0
DockId=0x00000003,0 DockId=0x00000003,0
[Window][Project] [Window][Project]
Pos=192,449 Pos=199,441
Size=408,151 Size=509,159
Collapsed=0 Collapsed=0
DockId=0x00000004,0 DockId=0x00000004,0
[Window][Console] [Window][Console]
Pos=192,449 Pos=199,441
Size=408,151 Size=509,159
Collapsed=0 Collapsed=0
DockId=0x00000004,1 DockId=0x00000004,1
[Window][Inspector] [Window][Inspector]
Pos=0,19 Pos=0,19
Size=190,581 Size=197,581
Collapsed=0 Collapsed=0
DockId=0x00000001,0 DockId=0x00000001,0
[Docking][Data] [Docking][Data]
DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,19 Size=800,581 Split=X DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,19 Size=900,581 Split=X
DockNode ID=0x00000005 Parent=0x8B93E3BD SizeRef=600,581 Split=X DockNode ID=0x00000005 Parent=0x8B93E3BD SizeRef=708,581 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=190,581 Selected=0xE7039252 DockNode ID=0x00000001 Parent=0x00000005 SizeRef=197,581 Selected=0xE7039252
DockNode ID=0x00000002 Parent=0x00000005 SizeRef=408,581 Split=Y DockNode ID=0x00000002 Parent=0x00000005 SizeRef=509,581 Split=Y
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=608,428 CentralNode=1 Selected=0xE192E354 DockNode ID=0x00000003 Parent=0x00000002 SizeRef=608,420 CentralNode=1 Selected=0xE192E354
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=608,151 Selected=0xD04A4B96 DockNode ID=0x00000004 Parent=0x00000002 SizeRef=608,159 Selected=0xD04A4B96
DockNode ID=0x00000006 Parent=0x8B93E3BD SizeRef=198,581 Selected=0x29EABFBD DockNode ID=0x00000006 Parent=0x8B93E3BD SizeRef=190,581 Selected=0x29EABFBD

View File

@ -1,60 +1,22 @@
#include <iostream>
#include "Editor.h" #include "Editor.h"
#include "UI.h"
Editor::Editor() : window(nullptr){} Editor::Editor()= default;
Editor::~Editor() Editor::~Editor()
{ {
Shutdown(); Shutdown();
} }
void Editor::Initialize() void Editor::Init()
{ {
InitializeWindow(); auto engine = Engine();
engine.Initialize(window);
ui.Initialize(window); engine.Run();
Run();
} }
void Editor::Run() { void Editor::Run() {
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
ui.Run();
engine.Render();
ui.Render();
glfwSwapBuffers(window);
}
} }
void Editor::Shutdown() { void Editor::Shutdown() {
engine.Shutdown();
ui.Shutdown();
glfwDestroyWindow(window);
glfwTerminate();
}
GLFWwindow* Editor::GetWindow() const {
return window;
}
void Editor::InitializeWindow() {
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(screenWidth, screenHeight, "Editor", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, Engine::FramebufferSizeCallback);
} }

View File

@ -1,8 +1,8 @@
#include <iostream>
#include "Editor.h" #include "Editor.h"
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
Editor editor; Editor::Init();
std::cout << argv << std::endl;
editor.Initialize();
} }

View File

@ -1,8 +1,11 @@
#ifndef ENGINE_H #ifndef ENGINE_H
#define ENGINE_H #define ENGINE_H
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "Graphics.h" #include "Renderer.h"
#include "Window.h"
#include "UI.h"
class Engine class Engine
{ {
@ -10,14 +13,17 @@ public:
Engine(); Engine();
~Engine(); ~Engine();
static void FramebufferSizeCallback(GLFWwindow* window, int width, int height); Window& GetWindow() const { return *m_Window; }
void Initialize(GLFWwindow* window); static Engine& Get() { return *s_Instance; }
void Run();
void Render(); void Render();
void Shutdown(); void Shutdown();
private: private:
Graphics graphics; Window* m_Window;
static Engine* s_Instance;
}; };
#endif #endif

View File

@ -0,0 +1,29 @@
#ifndef FRAMEBUFFER_H
#define FRAMEBUFFER_H
#include <glad/glad.h>
#include <iostream>
#include <glm/glm.hpp>
class FrameBuffer
{
public:
FrameBuffer();
FrameBuffer(int width, int height);
~FrameBuffer();
unsigned int GetFrameTexture() const;
static FrameBuffer Create(int width, int height);
void RescaleFrameBuffer(int width, int height) const;
void Bind() const;
static void Unbind();
void Shutdown() const;
private:
unsigned int m_FBO{}, m_RBO{};
unsigned int m_Texture{};
};
#endif

View File

@ -1,28 +0,0 @@
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <GLFW/glfw3.h>
#include "Shader.h"
class Graphics
{
public:
Graphics();
~Graphics();
static void FramebufferSizeCallback(GLFWwindow* window, int width, int height);
void Initialize(GLFWwindow* window);
void Render();
void Shutdown();
private:
GLFWwindow* window;
Shader shader;
unsigned int VAO, VBO;
void LoadShaders();
void SetupBuffers();
};
#endif

39
engine/include/Renderer.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef RENDERER_H
#define RENDERER_H
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <iostream>
#include "VertexArray.h"
#include "VertexBuffer.h"
#include "FrameBuffer.h"
#include "Window.h"
#include "Shader.h"
#include "UI.h"
struct RendererData
{
VertexArray* m_VAO;
VertexBuffer* m_VBO;
FrameBuffer* m_FBO;
Shader* m_Shader;
};
class Renderer
{
public:
Renderer();
~Renderer();
static void Init();
static void Render();
static void Shutdown();
static RendererData s_Data;
private:
static void LoadShaders();
static void SetupBuffers();
};
#endif

View File

@ -1,28 +1,36 @@
#ifndef SHADER_H #ifndef SHADER_H
#define SHADER_H #define SHADER_H
#define GLFW_INCLUDE_NONE
#include <glad/glad.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
class Shader class Shader
{ {
public: public:
Shader(); Shader() = default;
Shader(const char* vPath, const char* fPath); Shader(const std::string& vPath, const std::string& fPath);
~Shader(); ~Shader();
void Use(); static Shader Create(const std::string& vPath, const std::string& fPath);
unsigned int GetID(); unsigned int GetID() const;
void Use() const;
void Shutdown() const;
void SetBool(const char* name, bool value) const; void SetBool(const char* name, bool value) const;
void SetInt(const char* name, int value) const; void SetInt(const char* name, int value) const;
void SetFloat(const char* name, float value) const; void SetFloat(const char* name, float value) const;
void SetMat4(const char* name, glm::mat4 value) const; void SetMat4(const char* name, glm::mat4 value) const;
private: private:
unsigned int ID; unsigned int m_ID;
}; };
#endif #endif

31
engine/include/UI.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef UI_H
#define UI_H
#define GLFW_INCLUDE_NONE
#include "FrameBuffer.h"
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <string>
class UI
{
public:
UI();
~UI();
static void Init(GLFWwindow* window);
static void Run();
static void Render(const FrameBuffer& sceneBuffer);
static void Shutdown();
static void Print(const std::string& message);
static void ShowMenu();
static void ShowHierarchy();
static void ShowProject();
static void ShowConsole();
static void ShowScene(const FrameBuffer& sceneBuffer);
static void ShowInspector();
};
#endif

View File

@ -0,0 +1,22 @@
#ifndef VERTEXARRAY_H
#define VERTEXARRAY_H
#include <glad/glad.h>
class VertexArray
{
public:
VertexArray();
~VertexArray();
static VertexArray Create();
void Bind() const;
static void Unbind();
void Shutdown() const;
private:
unsigned int m_VAO{};
};
#endif

View File

@ -0,0 +1,23 @@
#ifndef VERTEXBUFFER_H
#define VERTEXBUFFER_H
#include <glad/glad.h>
class VertexBuffer
{
public:
VertexBuffer();
~VertexBuffer();
static VertexBuffer Create();
void Bind() const;
static void Unbind();
void SetData(int size, const void* data) const;
void Shutdown() const;
private:
unsigned int m_VBO{};
};
#endif

43
engine/include/Window.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef WINDOW_H
#define WINDOW_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <string>
#include <iostream>
struct WindowData
{
std::string Title;
int Width, Height;
WindowData(): Title("Ferx Engine"), Width(900), Height(600){}
WindowData(const std::string& title, int width, int height): Title(title), Width(width), Height(height){};
};
class Window
{
public:
Window();
Window(const std::string& title, int width, int height);
~Window();
static Window Create();
static Window Create(const std::string& title, int width, int height);
static void FramebufferSizeCallback(GLFWwindow* window, int width, int height);
void Init();
GLFWwindow* GetWindow() const;
const std::string& GetTitle() const;
int GetWidth() const;
int GetHeight() const;
void Shutdown();
private:
GLFWwindow* m_Window{};
WindowData m_Data;
};
#endif

View File

@ -1,30 +1,38 @@
#include "Engine.h" #include "Engine.h"
Engine::Engine(){} Engine* Engine::s_Instance = nullptr;
Engine::Engine()
{
s_Instance = this;
m_Window = new Window();
Renderer::Init();
UI::Init(m_Window->GetWindow());
}
Engine::~Engine() Engine::~Engine()
{ {
Shutdown(); Shutdown();
} }
void Engine::FramebufferSizeCallback(GLFWwindow* window, int width, int height) void Engine::Run()
{ {
Graphics::FramebufferSizeCallback(window, width, height); while (!glfwWindowShouldClose(m_Window->GetWindow())) {
} glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
void Engine::Initialize(GLFWwindow* window) UI::Run();
{ Renderer::Render();
graphics.Initialize(window); UI::Render(*Renderer::s_Data.m_FBO);
} glfwSwapBuffers(m_Window->GetWindow());
glfwPollEvents();
void Engine::Render() }
{
graphics.Render();
} }
void Engine::Shutdown() void Engine::Shutdown()
{ {
graphics.Shutdown(); Renderer::Shutdown();
} }

View File

@ -0,0 +1,76 @@
#include "FrameBuffer.h"
FrameBuffer::FrameBuffer() = default;
FrameBuffer::FrameBuffer(int width, int height)
{
glGenFramebuffers(1, &m_FBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
glGenTextures(1, &m_Texture);
glBindTexture(GL_TEXTURE_2D, m_Texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture, 0);
glGenRenderbuffers(1, &m_RBO);
glBindRenderbuffer(GL_RENDERBUFFER, m_RBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_RBO);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
FrameBuffer::~FrameBuffer()
{
glDeleteFramebuffers(1, &m_FBO);
glDeleteTextures(1, &m_Texture);
glDeleteRenderbuffers(1, &m_RBO);
}
unsigned int FrameBuffer::GetFrameTexture() const
{
return m_Texture;
}
FrameBuffer FrameBuffer::Create(int width, int height)
{
return FrameBuffer{width, height};
}
void FrameBuffer::Shutdown() const
{
glDeleteBuffers(1, &m_FBO);
glDeleteBuffers(1, &m_RBO);
glDeleteTextures(1, &m_Texture);
}
void FrameBuffer::RescaleFrameBuffer(int width, int height) const
{
glBindTexture(GL_TEXTURE_2D, m_Texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture, 0);
glBindRenderbuffer(GL_RENDERBUFFER, m_RBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_RBO);
}
void FrameBuffer::Bind() const
{
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
}
void FrameBuffer::Unbind()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

View File

@ -1,74 +0,0 @@
#include <glad/glad.h>
#include <iostream>
#include "Graphics.h"
#include "Shader.h"
Graphics::Graphics() : window(nullptr), VAO(0), VBO(0) {}
Graphics::~Graphics()
{
Shutdown();
}
void Graphics::Initialize(GLFWwindow* window)
{
this->window = window;
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cerr << "Failed to initialize GLAD" << std::endl;
return;
}
LoadShaders();
SetupBuffers();
}
void Graphics::LoadShaders()
{
shader = Shader(RESOURCES_PATH"shaders/shader.vs", RESOURCES_PATH"shaders/shader.fs");
}
void Graphics::SetupBuffers()
{
float vertices[] = {
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Graphics::FramebufferSizeCallback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void Graphics::Render() {
glClear(GL_COLOR_BUFFER_BIT);
shader.Use();
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void Graphics::Shutdown()
{
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
}

74
engine/src/Renderer.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <glad/glad.h>
#include "Renderer.h"
#include "Engine.h"
RendererData Renderer::s_Data;
Renderer::Renderer() = default;
Renderer::~Renderer()
{
Shutdown();
}
void Renderer::Init()
{
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cerr << "Failed to initialize GLAD" << std::endl;
return;
}
LoadShaders();
SetupBuffers();
}
void Renderer::LoadShaders()
{
s_Data.m_Shader = new Shader(RESOURCES_PATH"shaders/shader.vs", RESOURCES_PATH"shaders/shader.fs");
}
void Renderer::SetupBuffers()
{
float vertices[] = {
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
};
s_Data.m_FBO = new FrameBuffer(FrameBuffer::Create(Engine::Get().GetWindow().GetWidth(), Engine::Get().GetWindow().GetHeight()));
s_Data.m_VAO = new VertexArray();
s_Data.m_VBO = new VertexBuffer();
s_Data.m_VAO->Bind();
s_Data.m_VBO->SetData(sizeof(vertices), vertices);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
VertexArray::Unbind();
VertexBuffer::Unbind();
}
void Renderer::Render() {
glClearColor(0.0f, 0.1f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
s_Data.m_FBO->Bind();
s_Data.m_Shader->Use();
s_Data.m_VAO->Bind();
glDrawArrays(GL_TRIANGLES, 0, 3);
FrameBuffer::Unbind();
}
void Renderer::Shutdown()
{
s_Data.m_VAO->Shutdown();
s_Data.m_VBO->Shutdown();
s_Data.m_FBO->Shutdown();
s_Data.m_Shader->Shutdown();
}

View File

@ -1,12 +1,6 @@
#include <glad/glad.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include "Shader.h" #include "Shader.h"
Shader::Shader(): ID(0){} Shader::Shader(const std::string& vPath, const std::string& fPath)
Shader::Shader(const char* vPath, const char* fPath)
{ {
std::string vCode; std::string vCode;
std::string fCode; std::string fCode;
@ -64,15 +58,15 @@ Shader::Shader(const char* vPath, const char* fPath)
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}; };
ID = glCreateProgram(); m_ID = glCreateProgram();
glAttachShader(ID, vertex); glAttachShader(m_ID, vertex);
glAttachShader(ID, fragment); glAttachShader(m_ID, fragment);
glLinkProgram(ID); glLinkProgram(m_ID);
glGetProgramiv(ID, GL_LINK_STATUS, &success); glGetProgramiv(m_ID, GL_LINK_STATUS, &success);
if(!success) if(!success)
{ {
glGetProgramInfoLog(ID, 512, nullptr, infoLog); glGetProgramInfoLog(m_ID, 512, nullptr, infoLog);
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
} }
@ -80,31 +74,44 @@ Shader::Shader(const char* vPath, const char* fPath)
glDeleteShader(fragment); glDeleteShader(fragment);
} }
Shader::~Shader(){}; Shader::~Shader()
void Shader::Use()
{ {
glUseProgram(ID); glDeleteProgram(m_ID);
} }
unsigned int Shader::GetID() Shader Shader::Create(const std::string& vPath, const std::string& fPath)
{ {
return ID; return Shader{vPath, fPath};
}
void Shader::Use() const
{
glUseProgram(m_ID);
}
void Shader::Shutdown() const
{
glDeleteProgram(m_ID);
}
unsigned int Shader::GetID() const
{
return m_ID;
} }
void Shader::SetBool(const char* name, bool value) const void Shader::SetBool(const char* name, bool value) const
{ {
glUniform1i(glGetUniformLocation(ID, name), (int)value); glUniform1i(glGetUniformLocation(m_ID, name), (int)value);
} }
void Shader::SetInt(const char* name, int value) const void Shader::SetInt(const char* name, int value) const
{ {
glUniform1i(glGetUniformLocation(ID, name), value); glUniform1i(glGetUniformLocation(m_ID, name), value);
} }
void Shader::SetFloat(const char* name, float value) const void Shader::SetFloat(const char* name, float value) const
{ {
glUniform1f(glGetUniformLocation(ID, name), value); glUniform1f(glGetUniformLocation(m_ID, name), value);
} }
void Shader::SetMat4(const char* name, glm::mat4 value) const void Shader::SetMat4(const char* name, glm::mat4 value) const
{ {
glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, glm::value_ptr(value)); glUniformMatrix4fv(glGetUniformLocation(m_ID, name), 1, GL_FALSE, glm::value_ptr(value));
} }

View File

@ -2,20 +2,28 @@
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h> #include <imgui_impl_opengl3.h>
#include "UI.h" #include "UI.h"
#include "FrameBuffer.h"
#include "Window.h"
UI::UI(): styleColors(nullptr){} static ImVec4 m_ClearColor;
static ImVec4* m_StyleColors;
static std::string m_Log;
UI::~UI(){} UI::UI()= default;
void UI::Initialize(GLFWwindow* window) UI::~UI()= default;
void UI::Init(GLFWwindow* window)
{ {
m_ClearColor = ImVec4(0.0f, 0.0f, 0.0f, 1.00f);
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
ImGui::CreateContext(); ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io; ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGuiStyle& style = ImGui::GetStyle(); ImGuiStyle& style = ImGui::GetStyle();
styleColors = style.Colors; m_StyleColors = style.Colors;
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
// Setup Platform/Renderer backends // Setup Platform/Renderer backends
@ -29,17 +37,17 @@ void UI::Run()
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport()); ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
styleColors[ImGuiCol_DockingEmptyBg] = clearColor; m_StyleColors[ImGuiCol_DockingEmptyBg] = m_ClearColor;
} }
void UI::Render() void UI::Render(const FrameBuffer& sceneBuffer)
{ {
ShowConsole(); ShowConsole();
ShowHierarchy(); ShowHierarchy();
ShowInspector(); ShowInspector();
ShowMenu(); ShowMenu();
ShowProject(); ShowProject();
ShowScene(); ShowScene(sceneBuffer);
ImGui::Render(); ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
@ -54,7 +62,7 @@ void UI::Shutdown()
void UI::Print(const std::string& message) void UI::Print(const std::string& message)
{ {
log = message; m_Log = message;
} }
void UI::ShowConsole(){ void UI::ShowConsole(){
@ -72,7 +80,7 @@ void UI::ShowConsole(){
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::Text("%s", log.c_str()); ImGui::Text("%s", m_Log.c_str());
ImGui::End(); ImGui::End();
} }
@ -94,7 +102,7 @@ void UI::ShowInspector()
ImGui::Text("This is some useful text."); ImGui::Text("This is some useful text.");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color", (float*)&clearColor); ImGui::ColorEdit3("clear color", (float*)&m_ClearColor);
if (ImGui::Button("Button")) if (ImGui::Button("Button"))
counter++; counter++;
@ -124,10 +132,20 @@ void UI::ShowProject()
ImGui::End(); ImGui::End();
} }
void UI::ShowScene() void UI::ShowScene(const FrameBuffer& sceneBuffer)
{ {
ImGui::Begin("Scene"); ImGui::Begin("Scene");
{
ImGui::BeginChild("GameRender");
ImGui::Image(
(ImTextureID)sceneBuffer.GetFrameTexture(),
ImGui::GetContentRegionAvail(),
ImVec2(0, 1),
ImVec2(1, 0)
);
}
ImGui::EndChild();
ImGui::End(); ImGui::End();
} }

View File

@ -0,0 +1,31 @@
#include "VertexArray.h"
VertexArray::VertexArray()
{
glGenVertexArrays(1, &m_VAO);
}
VertexArray::~VertexArray()
{
Shutdown();
}
VertexArray VertexArray::Create()
{
return VertexArray{};
}
void VertexArray::Bind() const
{
glBindVertexArray(m_VAO);
}
void VertexArray::Unbind()
{
glBindVertexArray(0);
}
void VertexArray::Shutdown() const
{
glDeleteVertexArrays(1, &m_VAO);
}

View File

@ -0,0 +1,36 @@
#include "VertexBuffer.h"
VertexBuffer::VertexBuffer()
{
glGenBuffers(1, &m_VBO);
}
VertexBuffer::~VertexBuffer()
{
Shutdown();
}
VertexBuffer VertexBuffer::Create()
{
return VertexBuffer{};
}
void VertexBuffer::Bind() const
{
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
}
void VertexBuffer::Unbind()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void VertexBuffer::SetData(int size, const void* data) const
{
Bind();
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
}
void VertexBuffer::Shutdown() const
{
glDeleteBuffers(1, &m_VBO);
}

83
engine/src/Window.cpp Normal file
View File

@ -0,0 +1,83 @@
#include "Window.h"
Window::Window()
{
Init();
}
Window::Window(const std::string& title, int width, int height)
{
m_Data = WindowData{title, width, height};
Init();
}
Window::~Window()
{
Shutdown();
}
void Window::Init()
{
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return;
}
std::cout << "GLFW Initialized" << std::endl;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
m_Window = glfwCreateWindow(m_Data.Width, m_Data.Height, m_Data.Title.c_str(), nullptr, nullptr);
if (!m_Window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent(m_Window);
glfwSetFramebufferSizeCallback(m_Window, FramebufferSizeCallback);
}
Window Window::Create()
{
return Window{};
}
Window Window::Create(const std::string& title, int width, int height)
{
return Window{title, width, height};
}
void Window::FramebufferSizeCallback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
GLFWwindow* Window::GetWindow() const
{
return m_Window;
}
const std::string& Window::GetTitle() const
{
return m_Data.Title;
}
int Window::GetWidth() const
{
return m_Data.Width;
}
int Window::GetHeight() const
{
return m_Data.Height;
}
void Window::Shutdown()
{
glfwDestroyWindow(m_Window);
}