feat: Add properties, stb library, and Texture class

This commit is contained in:
Huseyn Ismayilov 2024-08-22 19:55:30 +04:00
parent c31afc2818
commit 5fceafd553
19 changed files with 8271 additions and 76 deletions

View File

@ -15,6 +15,7 @@ add_subdirectory(${THIRDPARTY_DIR}/glad)
add_subdirectory(${THIRDPARTY_DIR}/glfw)
add_subdirectory(${THIRDPARTY_DIR}/glm)
add_subdirectory(${THIRDPARTY_DIR}/imgui)
add_subdirectory(${THIRDPARTY_DIR}/stb)
# Add engine and editor
add_subdirectory(engine)

View File

@ -9,41 +9,41 @@ Size=400,400
Collapsed=0
[Window][Hierarchy]
Pos=710,19
Size=190,581
Pos=0,19
Size=189,581
Collapsed=0
DockId=0x00000006,0
DockId=0x00000005,0
[Window][Scene]
Pos=199,19
Size=509,420
Pos=191,19
Size=510,420
Collapsed=0
DockId=0x00000003,0
[Window][Project]
Pos=199,441
Size=509,159
Pos=191,441
Size=510,159
Collapsed=0
DockId=0x00000004,0
[Window][Console]
Pos=199,441
Size=509,159
Pos=191,441
Size=510,159
Collapsed=0
DockId=0x00000004,1
[Window][Inspector]
Pos=0,19
Pos=703,19
Size=197,581
Collapsed=0
DockId=0x00000001,0
DockId=0x00000002,0
[Docking][Data]
DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,19 Size=900,581 Split=X
DockNode ID=0x00000005 Parent=0x8B93E3BD SizeRef=708,581 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=197,581 Selected=0xE7039252
DockNode ID=0x00000002 Parent=0x00000005 SizeRef=509,581 Split=Y
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=608,420 CentralNode=1 Selected=0xE192E354
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=608,159 Selected=0xD04A4B96
DockNode ID=0x00000006 Parent=0x8B93E3BD SizeRef=190,581 Selected=0x29EABFBD
DockNode ID=0x00000005 Parent=0x8B93E3BD SizeRef=189,581 Selected=0x29EABFBD
DockNode ID=0x00000006 Parent=0x8B93E3BD SizeRef=709,581 Split=X
DockNode ID=0x00000001 Parent=0x00000006 SizeRef=701,581 Split=Y
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=608,420 CentralNode=1 Selected=0xE192E354
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=608,159 Selected=0xD04A4B96
DockNode ID=0x00000002 Parent=0x00000006 SizeRef=197,581 Selected=0xE7039252

View File

@ -0,0 +1,10 @@
#version 330 core
out vec4 fragColor;
in vec3 ourColor;
uniform vec3 color;
void main()
{
fragColor = vec4(color, 1.0f) * vec4(ourColor, 1.0f);
}

View File

@ -2,10 +2,12 @@
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
uniform mat4 transform;
out vec3 ourColor;
void main()
{
gl_Position = vec4(aPos, 1.0f);
gl_Position = transform * vec4(aPos, 1.0f);
ourColor = aColor;
}

View File

@ -10,10 +10,10 @@ add_library(${PROJECT_NAME})
if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_compile_definitions(${PROJECT_NAME} PUBLIC RESOURCES_PATH="./resources/")
else()
target_compile_definitions(${PROJECT_NAME} PUBLIC RESOURCES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/resources/")
target_compile_definitions(${PROJECT_NAME} PUBLIC RESOURCES_PATH="${CMAKE_SOURCE_DIR}/editor/resources/")
endif()
target_sources(${PROJECT_NAME} PRIVATE ${ENGINE_SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} glfw glad glm imgui)
target_link_libraries(${PROJECT_NAME} glfw glad glm imgui stb)

View File

@ -3,27 +3,28 @@
#include <glad/glad.h>
#include <iostream>
#include <Texture.h>
#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 AttachTexture(int width, int height);
void Bind() const;
static void Unbind();
void Shutdown() const;
static FrameBuffer Create();
Texture* GetFrameTexture() const;
private:
unsigned int m_FBO{}, m_RBO{};
unsigned int m_Texture{};
Texture* m_Texture;
};
#endif

View File

@ -9,6 +9,7 @@
#include "FrameBuffer.h"
#include "Window.h"
#include "Shader.h"
#include "Texture.h"
#include "UI.h"
struct RendererData
@ -17,6 +18,7 @@ struct RendererData
VertexBuffer* m_VBO;
FrameBuffer* m_FBO;
Shader* m_Shader;
Texture* m_Texture;
};
class Renderer
@ -27,11 +29,14 @@ public:
static void Init();
static void Render();
static void End();
static void Shutdown();
static RendererData s_Data;
static RendererData GetData();
private:
static RendererData s_Data;
static void LoadShaders();
static void SetupBuffers();
};

35
engine/include/Texture.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#include <glad/glad.h>
#include <stb_image.h>
#include <string>
class Texture
{
public:
Texture();
~Texture();
void Init();
void Shutdown() const;
void Bind() const;
void GenerateFromImage(const std::string& path);
static Texture Create();
static void ToImage(int width, int height, const unsigned char* data);
static void GenerateMipmaps();
unsigned int GetID() const;
unsigned char* GetTexture() const;
int GetWidth() const;
int GetHeight() const;
int GetNrChannels() const;
private:
unsigned int m_Texture{};
unsigned char* m_Data{};
int m_Width{}, m_Height{}, m_NrChannels{};
};
#endif

View File

@ -7,6 +7,14 @@
#include <imgui.h>
#include <string>
struct InspectorData
{
float m_Position[3];
float m_Rotation[3];
float m_Scale[3];
float m_Color[3] = {1.0f, 1.0f, 1.0f};
};
class UI
{
public:
@ -19,6 +27,7 @@ public:
static void Shutdown();
static void Print(const std::string& message);
static InspectorData GetData();
static void ShowMenu();
static void ShowHierarchy();
@ -26,6 +35,9 @@ public:
static void ShowConsole();
static void ShowScene(const FrameBuffer& sceneBuffer);
static void ShowInspector();
private:
static InspectorData s_Data;
};
#endif

View File

@ -5,6 +5,7 @@
#include <GLFW/glfw3.h>
#include <string>
#include <iostream>
#include <Renderer.h>
struct WindowData
{

View File

@ -1,9 +0,0 @@
#version 330 core
out vec4 fragColor;
in vec3 ourColor;
void main()
{
fragColor = vec4(ourColor, 1.0f);
}

View File

@ -20,13 +20,10 @@ Engine::~Engine()
void Engine::Run()
{
while (!glfwWindowShouldClose(m_Window->GetWindow())) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
UI::Run();
Renderer::Render();
UI::Render(*Renderer::s_Data.m_FBO);
glfwSwapBuffers(m_Window->GetWindow());
glfwPollEvents();
UI::Render(*Renderer::GetData().m_FBO);
Renderer::End();
}
}

View File

@ -1,18 +1,17 @@
#include "FrameBuffer.h"
FrameBuffer::FrameBuffer() = default;
FrameBuffer::FrameBuffer(int width, int height)
FrameBuffer::FrameBuffer()
{
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);
m_Texture = new Texture();
}
void FrameBuffer::AttachTexture(int width, int height)
{
Texture::ToImage(width, height, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture->GetID(), 0);
glGenRenderbuffers(1, &m_RBO);
glBindRenderbuffer(GL_RENDERBUFFER, m_RBO);
@ -29,36 +28,34 @@ FrameBuffer::FrameBuffer(int width, int height)
FrameBuffer::~FrameBuffer()
{
glDeleteFramebuffers(1, &m_FBO);
glDeleteTextures(1, &m_Texture);
glDeleteRenderbuffers(1, &m_RBO);
Shutdown();
}
unsigned int FrameBuffer::GetFrameTexture() const
Texture* FrameBuffer::GetFrameTexture() const
{
return m_Texture;
}
FrameBuffer FrameBuffer::Create(int width, int height)
FrameBuffer FrameBuffer::Create()
{
return FrameBuffer{width, height};
return FrameBuffer{};
}
void FrameBuffer::Shutdown() const
{
glDeleteBuffers(1, &m_FBO);
glDeleteBuffers(1, &m_RBO);
glDeleteTextures(1, &m_Texture);
m_Texture->Shutdown();
}
void FrameBuffer::RescaleFrameBuffer(int width, int height) const
{
glBindTexture(GL_TEXTURE_2D, m_Texture);
m_Texture->Bind();
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);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture->GetID(), 0);
glBindRenderbuffer(GL_RENDERBUFFER, m_RBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);

View File

@ -22,9 +22,14 @@ void Renderer::Init()
SetupBuffers();
}
RendererData Renderer::GetData()
{
return s_Data;
}
void Renderer::LoadShaders()
{
s_Data.m_Shader = new Shader(RESOURCES_PATH"shaders/shader.vs", RESOURCES_PATH"shaders/shader.fs");
s_Data.m_Shader = new Shader(RESOURCES_PATH"shaders/vertex.glsl", RESOURCES_PATH"shaders/fragment.glsl");
}
void Renderer::SetupBuffers()
@ -35,7 +40,8 @@ void Renderer::SetupBuffers()
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_FBO = new FrameBuffer();
s_Data.m_FBO->AttachTexture(Engine::Get().GetWindow().GetWidth(), Engine::Get().GetWindow().GetHeight());
s_Data.m_VAO = new VertexArray();
s_Data.m_VBO = new VertexBuffer();
@ -60,11 +66,21 @@ void Renderer::Render() {
s_Data.m_FBO->Bind();
s_Data.m_Shader->Use();
auto transform = glm::mat4(1.0f);
transform = glm::translate(transform, glm::vec3(UI::GetData().m_Position[0], UI::GetData().m_Position[1], UI::GetData().m_Position[2]));
s_Data.m_Shader->SetMat4("transform", transform);
glUniform3fv(glGetUniformLocation(s_Data.m_Shader->GetID(), "color"), 1, UI::GetData().m_Color);
s_Data.m_VAO->Bind();
glDrawArrays(GL_TRIANGLES, 0, 3);
FrameBuffer::Unbind();
}
void Renderer::End()
{
glfwSwapBuffers(Engine::Get().GetWindow().GetWindow());
glfwPollEvents();
}
void Renderer::Shutdown()
{
s_Data.m_VAO->Shutdown();

93
engine/src/Texture.cpp Normal file
View File

@ -0,0 +1,93 @@
#include "UI.h"
#include "Texture.h"
Texture::Texture()
{
Init();
}
Texture::~Texture()
{
Shutdown();
}
void Texture::Init()
{
glGenTextures(1, &m_Texture);
Bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
void Texture::Bind() const
{
glBindTexture(GL_TEXTURE_2D, m_Texture);
}
void Texture::Shutdown() const
{
glDeleteTextures(1, &m_Texture);
}
void Texture::GenerateFromImage(const std::string& path)
{
m_Data = stbi_load(path.c_str(), &m_Width, &m_Height, &m_NrChannels, 0);
if (m_Data)
{
ToImage(m_Width, m_Height, m_Data);
GenerateMipmaps();
}
else
{
UI::Print("Failed to load texture: " + path);
}
stbi_image_free(m_Data);
glBindTexture(GL_TEXTURE_2D, 0);
}
void Texture::ToImage(int width, int height, const unsigned char* data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
}
void Texture::GenerateMipmaps()
{
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
Texture Texture::Create()
{
return Texture{};
}
unsigned int Texture::GetID() const
{
return m_Texture;
}
int Texture::GetWidth() const
{
return m_Width;
}
int Texture::GetHeight() const
{
return m_Height;
}
unsigned char* Texture::GetTexture() const
{
return m_Data;
}
int Texture::GetNrChannels() const
{
return m_NrChannels;
}

View File

@ -8,6 +8,7 @@
static ImVec4 m_ClearColor;
static ImVec4* m_StyleColors;
static std::string m_Log;
InspectorData UI::s_Data;
UI::UI()= default;
@ -23,6 +24,11 @@ void UI::Init(GLFWwindow* window)
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGuiStyle& style = ImGui::GetStyle();
style.WindowBorderSize = 0;
style.WindowMenuButtonPosition = 1;
style.FrameRounding = 4;
style.GrabRounding = 4;
style.WindowRounding = 6;
m_StyleColors = style.Colors;
ImGui::StyleColorsDark();
@ -31,6 +37,11 @@ void UI::Init(GLFWwindow* window)
ImGui_ImplOpenGL3_Init();
}
InspectorData UI::GetData()
{
return s_Data;
}
void UI::Run()
{
ImGui_ImplOpenGL3_NewFrame();
@ -62,14 +73,14 @@ void UI::Shutdown()
void UI::Print(const std::string& message)
{
m_Log = message;
m_Log += message + '\n';
}
void UI::ShowConsole(){
ImGui::Begin("Console");
if(ImGui::Button("Clear")){
Print("");
m_Log.clear();
}
ImGui::SameLine();
@ -89,25 +100,39 @@ void UI::ShowHierarchy()
{
ImGui::Begin("Hierarchy");
ImGui::CollapsingHeader("Triangle");
ImGui::End();
}
void UI::ShowInspector()
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Inspector");
ImGui::Text("This is some useful text.");
ImGui::SeparatorText("Triangle");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color", (float*)&m_ClearColor);
ImGui::BeginGroup();
{
ImGui::Text("Transform");
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(1.0f, 0.0f, 0.0f, 1.0f));
ImGui::DragFloat3("Position", s_Data.m_Position, 0.2f);
ImGui::PopStyleColor();
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0.0f, 1.0f, 0.0f, 1.0f));
ImGui::DragFloat3("Rotation", s_Data.m_Rotation, 0.4f);
ImGui::PopStyleColor();
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0.0f, 0.0f, 1.0f, 1.0f));
ImGui::DragFloat3("Scale", s_Data.m_Scale, 0.1f);
ImGui::PopStyleColor();
}
ImGui::EndGroup();
if (ImGui::Button("Button"))
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::NewLine();
ImGui::BeginGroup();
ImGui::Text("Colors");
ImGui::ColorEdit3("Shader Color", s_Data.m_Color);
ImGui::ColorEdit3("Clear Color", (float*)&m_ClearColor);
ImGui::EndGroup();
ImGui::End();
}
@ -134,19 +159,22 @@ void UI::ShowProject()
void UI::ShowScene(const FrameBuffer& sceneBuffer)
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{0, 0});
ImGui::Begin("Scene");
{
ImGui::BeginChild("GameRender");
ImGui::Image(
(ImTextureID)sceneBuffer.GetFrameTexture(),
(ImTextureID)sceneBuffer.GetFrameTexture()->GetID(),
ImGui::GetContentRegionAvail(),
ImVec2(0, 1),
ImVec2(1, 0)
);
ImGui::EndChild();
}
ImGui::EndChild();
ImGui::End();
ImGui::PopStyleVar();
}

11
thirdparty/stb/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.22)
project(stb)
file(GLOB_RECURSE SOURCES src/*.cpp)
add_library(stb)
target_include_directories(stb PUBLIC include)
target_sources(stb PRIVATE ${SOURCES})

7993
thirdparty/stb/include/stb_image.h vendored Normal file

File diff suppressed because it is too large Load Diff

2
thirdparty/stb/src/stb_image.cpp vendored Normal file
View File

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>