添加贴图
This commit is contained in:
@ -7,6 +7,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|||||||
set(BUILD_SHARED_LIBS OFF)
|
set(BUILD_SHARED_LIBS OFF)
|
||||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(MSVC)
|
if(MSVC)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251")
|
||||||
add_compile_options(/utf-8)
|
add_compile_options(/utf-8)
|
||||||
@ -18,6 +20,9 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin-int)
|
|||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
|
||||||
|
|
||||||
|
file(GLOB ASSETS Sandbox/assets/*)
|
||||||
|
file(COPY ${ASSETS} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/assets)
|
||||||
|
|
||||||
|
|
||||||
add_subdirectory(Hazel)
|
add_subdirectory(Hazel)
|
||||||
add_subdirectory(Sandbox)
|
add_subdirectory(Sandbox)
|
||||||
@ -8,14 +8,21 @@ add_subdirectory(vendor/glm)
|
|||||||
|
|
||||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||||
|
|
||||||
|
include_directories(vendor/stb)
|
||||||
|
file(GLOB STB vendor/stb/stb_image.h)
|
||||||
|
|
||||||
|
|
||||||
include_directories(vendor/imgui)
|
include_directories(vendor/imgui)
|
||||||
include_directories(vendor/imgui/backends)
|
include_directories(vendor/imgui/backends)
|
||||||
|
|
||||||
file(GLOB IMGUI vendor/imgui/**.cpp
|
file(GLOB IMGUI vendor/imgui/**.cpp
|
||||||
vendor/imgui/backends/imgui_impl_opengl3.cpp
|
vendor/imgui/backends/imgui_impl_opengl3.cpp
|
||||||
vendor/imgui/backends/imgui_impl_sdl3.cpp)
|
vendor/imgui/backends/imgui_impl_sdl3.cpp)
|
||||||
|
|
||||||
add_library(Hazel SHARED ${SOURCES} ${IMGUI})
|
add_library(Hazel SHARED
|
||||||
|
${SOURCES}
|
||||||
|
${IMGUI}
|
||||||
|
${STB}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -41,7 +48,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
|
|||||||
glm::glm
|
glm::glm
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_definitions(${PROJECT_NAME} PRIVATE HZ_BUILD_DLL)# 编译DLL时定义
|
target_compile_definitions(${PROJECT_NAME} PRIVATE HZ_BUILD_DLL STB_IMAGE_IMPLEMENTATION)# 编译DLL时定义
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
target_compile_definitions(${PROJECT_NAME} PUBLIC HZ_PLATFORM_WINDOWS IMGUI_API=__declspec\(dllexport\))# 编译DLL时定义
|
target_compile_definitions(${PROJECT_NAME} PUBLIC HZ_PLATFORM_WINDOWS IMGUI_API=__declspec\(dllexport\))# 编译DLL时定义
|
||||||
|
|||||||
@ -1,12 +1,29 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
|
||||||
// For use by Hazel Applications
|
// For use by Hazel Applications
|
||||||
#include "Hazel/Core.h"
|
|
||||||
#include "Hazel/Application.h"
|
#include "Hazel/Application.h"
|
||||||
|
#include "Hazel/Core.h"
|
||||||
#include "Hazel/Log.h"
|
#include "Hazel/Log.h"
|
||||||
#include "Hazel/Window.h"
|
#include "Hazel/Window.h"
|
||||||
#include "Hazel/ImGui/ImGuiLayer.h"
|
#include "Hazel/ImGui/ImGuiLayer.h"
|
||||||
#include "Hazel/Renderer/GraphicsContext.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------ Renderer ----------------------------
|
||||||
|
#include "Hazel/Renderer/Renderer.h"
|
||||||
|
#include "Hazel/Renderer/RendererCommand.h"
|
||||||
|
|
||||||
|
#include "Hazel/Renderer/Buffer.h"
|
||||||
|
#include "Hazel/Renderer/Shader.h"
|
||||||
|
#include "Hazel/Renderer/Texture.h"
|
||||||
|
#include "Hazel/Renderer/VertexArray.h"
|
||||||
|
|
||||||
|
#include "Hazel/Renderer/OrthographicCamera.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ------------------------ Entry Point ------------------------
|
// ------------------------ Entry Point ------------------------
|
||||||
|
|||||||
@ -1,16 +1,19 @@
|
|||||||
|
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
|
|
||||||
#include <imgui_impl_opengl3_loader.h>
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3/SDL_opengl_glext.h>
|
|
||||||
|
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
#include "Renderer/Renderer.h"
|
||||||
|
#include "Renderer/RendererCommand.h"
|
||||||
|
|
||||||
|
|
||||||
namespace Hazel {
|
namespace Hazel {
|
||||||
|
|
||||||
Application* Application::s_Instance = nullptr;
|
Application* Application::s_Instance = nullptr;
|
||||||
|
|
||||||
|
|
||||||
Application::Application()
|
Application::Application()
|
||||||
{
|
{
|
||||||
if (s_Instance != nullptr)
|
if (s_Instance != nullptr)
|
||||||
@ -22,40 +25,12 @@ namespace Hazel {
|
|||||||
m_Window = std::unique_ptr<Window>(Window::Create());
|
m_Window = std::unique_ptr<Window>(Window::Create());
|
||||||
m_Window->SetEventCallback(std::bind(&Application::OnEvent, this, std::placeholders::_1));
|
m_Window->SetEventCallback(std::bind(&Application::OnEvent, this, std::placeholders::_1));
|
||||||
|
|
||||||
|
Renderer::Init();
|
||||||
|
|
||||||
m_imguiLayer = new ImGuiLayer();
|
m_imguiLayer = new ImGuiLayer();
|
||||||
PushOverlay(m_imguiLayer);
|
PushOverlay(m_imguiLayer);
|
||||||
|
|
||||||
|
m_Window->SetVSync(true);
|
||||||
// Vertex Array
|
|
||||||
glGenVertexArrays(1, &m_VertexArray);
|
|
||||||
glBindVertexArray(m_VertexArray);
|
|
||||||
|
|
||||||
// Vertex Buffer
|
|
||||||
glGenBuffers(1, &m_VertexBuffer);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
|
|
||||||
|
|
||||||
float vertices[3 * 3] = {
|
|
||||||
-0.5f, -0.5f, 0.0f,
|
|
||||||
0.5f, -0.5f, 0.0f,
|
|
||||||
0.0f, 0.5f, 0.0f,
|
|
||||||
};
|
|
||||||
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
|
||||||
|
|
||||||
// Index Buffer
|
|
||||||
glGenBuffers(1, &m_IndexBuffer);
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer);
|
|
||||||
|
|
||||||
uint32_t indices[3] = { 0, 1, 2 };
|
|
||||||
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Shader
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,14 +39,14 @@ namespace Hazel {
|
|||||||
void Application::Run() {
|
void Application::Run() {
|
||||||
while (m_Running)
|
while (m_Running)
|
||||||
{
|
{
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
float currentTime = (float)SDL_GetTicks();
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
TimeStep timestep = currentTime - m_lastFrameTime;
|
||||||
|
m_lastFrameTime = currentTime;
|
||||||
|
|
||||||
|
|
||||||
glBindVertexArray(m_VertexArray);
|
|
||||||
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
|
|
||||||
for (Layer* layer : m_layerStack)
|
for (Layer* layer : m_layerStack)
|
||||||
{
|
{
|
||||||
layer->OnUpdate();
|
layer->OnUpdate(timestep);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_imguiLayer->Begin();
|
m_imguiLayer->Begin();
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#include "Layer.h"
|
#include "Layer.h"
|
||||||
#include "LayerStack.h"
|
#include "LayerStack.h"
|
||||||
|
#include "Core/TimeStep.h"
|
||||||
#include "ImGui/ImGuiLayer.h"
|
#include "ImGui/ImGuiLayer.h"
|
||||||
|
|
||||||
namespace Hazel {
|
namespace Hazel {
|
||||||
@ -30,7 +31,8 @@ namespace Hazel {
|
|||||||
|
|
||||||
LayerStack m_layerStack;
|
LayerStack m_layerStack;
|
||||||
|
|
||||||
unsigned m_VertexArray, m_VertexBuffer, m_IndexBuffer;
|
TimeStep m_TimeStep;
|
||||||
|
float m_lastFrameTime = 0.0f;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Application* s_Instance;
|
static Application* s_Instance;
|
||||||
|
|||||||
@ -10,5 +10,14 @@
|
|||||||
#else
|
#else
|
||||||
#error Hazel only support Windows!
|
#error Hazel only support Windows!
|
||||||
#endif // HZ_PLATFORM_WINDOWS
|
#endif // HZ_PLATFORM_WINDOWS
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
using Scope = std::unique_ptr<T>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using Ref = std::shared_ptr<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
5
Hazel/src/Hazel/Core/TimeStep.cpp
Normal file
5
Hazel/src/Hazel/Core/TimeStep.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "TimeStep.h"
|
||||||
27
Hazel/src/Hazel/Core/TimeStep.h
Normal file
27
Hazel/src/Hazel/Core/TimeStep.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef TIMESTEP_H
|
||||||
|
#define TIMESTEP_H
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class TimeStep
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TimeStep(float time = 0.0f) : m_Time(time) {}
|
||||||
|
|
||||||
|
|
||||||
|
operator float() const { return m_Time / 1000.0f; }
|
||||||
|
|
||||||
|
float GetSeconds() const { return m_Time / 1000.0f; }
|
||||||
|
float GetMilliseconds() const { return m_Time; }
|
||||||
|
private:
|
||||||
|
float m_Time; // ms
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //TIMESTEP_H
|
||||||
@ -5,7 +5,8 @@
|
|||||||
|
|
||||||
extern Hazel::Application* Hazel::CreateApplication();
|
extern Hazel::Application* Hazel::CreateApplication();
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
|
||||||
|
int main(int, char**) {
|
||||||
Hazel::Log::init();
|
Hazel::Log::init();
|
||||||
|
|
||||||
auto app = Hazel::CreateApplication();
|
auto app = Hazel::CreateApplication();
|
||||||
|
|||||||
@ -10,6 +10,8 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#include "Core/TimeStep.h"
|
||||||
|
|
||||||
namespace Hazel {
|
namespace Hazel {
|
||||||
|
|
||||||
class HAZEL_API Layer
|
class HAZEL_API Layer
|
||||||
@ -20,7 +22,7 @@ namespace Hazel {
|
|||||||
|
|
||||||
virtual void OnAttach() {}
|
virtual void OnAttach() {}
|
||||||
virtual void OnDetech() {}
|
virtual void OnDetech() {}
|
||||||
virtual void OnUpdate() {}
|
virtual void OnUpdate(TimeStep& ts) {}
|
||||||
virtual void OnImGuiRender() {}
|
virtual void OnImGuiRender() {}
|
||||||
virtual void OnEvent(SDL_Event& e) {}
|
virtual void OnEvent(SDL_Event& e) {}
|
||||||
|
|
||||||
|
|||||||
51
Hazel/src/Hazel/Renderer/Buffer.cpp
Normal file
51
Hazel/src/Hazel/Renderer/Buffer.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Buffer.h"
|
||||||
|
|
||||||
|
#include <Hazel/Log.h>
|
||||||
|
|
||||||
|
#include "Renderer.h"
|
||||||
|
#include "Platform/OpenGL/OpenGLBuffer.h"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
// std::shared_ptr<VertexBuffer> VertexBuffer::Create(float* vertices, uint32_t size)
|
||||||
|
VertexBuffer* VertexBuffer::Create(float* vertices, uint32_t size)
|
||||||
|
{
|
||||||
|
switch (Renderer::GetAPI())
|
||||||
|
{
|
||||||
|
case RendererAPI::API::NONE:
|
||||||
|
HZ_CORE_ERROR("NONE is not Support!");
|
||||||
|
return nullptr;
|
||||||
|
case RendererAPI::API::OPENGL:
|
||||||
|
// return std::make_shared<VertexBuffer>(OpenGLVertexBuffer(vertices, size));
|
||||||
|
return new OpenGLVertexBuffer(vertices, size);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("Unknown RendererAPI!");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
IndexBuffer* IndexBuffer::Create(uint32_t* indices, uint32_t count)
|
||||||
|
{
|
||||||
|
switch (Renderer::GetAPI())
|
||||||
|
{
|
||||||
|
case RendererAPI::API::NONE:
|
||||||
|
HZ_CORE_ERROR("NONE is not Support!");
|
||||||
|
return nullptr;
|
||||||
|
case RendererAPI::API::OPENGL:
|
||||||
|
return new OpenGLIndexBuffer(indices, count);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("Unknown RendererAPI!");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
154
Hazel/src/Hazel/Renderer/Buffer.h
Normal file
154
Hazel/src/Hazel/Renderer/Buffer.h
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BUFFER_H
|
||||||
|
#define BUFFER_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <Hazel/Log.h>
|
||||||
|
|
||||||
|
#include "Hazel/Core.h"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
enum class ShaderDataType : uint8_t
|
||||||
|
{
|
||||||
|
None = 0,
|
||||||
|
Float, Float2, Float3, Float4, Mat3, Mat4, Int, Int2, Int3, Int4, Bool
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t ShaderDataTypeSize(ShaderDataType type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case ShaderDataType::Float: return 4;
|
||||||
|
case ShaderDataType::Float2: return 4 * 2;
|
||||||
|
case ShaderDataType::Float3: return 4 * 3;
|
||||||
|
case ShaderDataType::Float4: return 4 * 4;
|
||||||
|
case ShaderDataType::Mat3: return 4 * 3 * 3;
|
||||||
|
case ShaderDataType::Mat4: return 4 * 4 * 4;
|
||||||
|
case ShaderDataType::Int: return 4;
|
||||||
|
case ShaderDataType::Int2: return 4 * 2;
|
||||||
|
case ShaderDataType::Int3: return 4 * 3;
|
||||||
|
case ShaderDataType::Int4: return 4 * 4;
|
||||||
|
case ShaderDataType::Bool: return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("Unknown ShaderDataType!");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BufferElement
|
||||||
|
{
|
||||||
|
std::string name;
|
||||||
|
ShaderDataType Type;
|
||||||
|
uint32_t Size;
|
||||||
|
uint32_t Offset;
|
||||||
|
bool Normalized;
|
||||||
|
|
||||||
|
BufferElement() {}
|
||||||
|
|
||||||
|
BufferElement(ShaderDataType type, const std::string& name, bool normalized = false) : name(name), Type(type), Size(ShaderDataTypeSize(type)),Offset(0), Normalized(normalized)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t GetComponentCount() const
|
||||||
|
{
|
||||||
|
switch (Type)
|
||||||
|
{
|
||||||
|
case ShaderDataType::Float: return 1;
|
||||||
|
case ShaderDataType::Float2: return 2;
|
||||||
|
case ShaderDataType::Float3: return 3;
|
||||||
|
case ShaderDataType::Float4: return 4;
|
||||||
|
case ShaderDataType::Mat3: return 3 * 3;
|
||||||
|
case ShaderDataType::Mat4: return 4 * 4;
|
||||||
|
case ShaderDataType::Int: return 1;
|
||||||
|
case ShaderDataType::Int2: return 2;
|
||||||
|
case ShaderDataType::Int3: return 3;
|
||||||
|
case ShaderDataType::Int4: return 4;
|
||||||
|
case ShaderDataType::Bool: return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("Unknown ShaderDataType!");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// BufferLayout
|
||||||
|
class HAZEL_API BufferLayout
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BufferLayout() {}
|
||||||
|
BufferLayout(const std::initializer_list<BufferElement>& elements) : m_Elements(elements)
|
||||||
|
{
|
||||||
|
CalcOffsetAndStride();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const std::vector<BufferElement>& GetElements() const { return m_Elements; }
|
||||||
|
inline uint32_t GetStride() const { return m_Stride; }
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<BufferElement>::iterator begin() { return m_Elements.begin(); }
|
||||||
|
std::vector<BufferElement>::iterator end() { return m_Elements.end(); }
|
||||||
|
|
||||||
|
std::vector<BufferElement>::const_iterator begin() const { return m_Elements.begin(); }
|
||||||
|
std::vector<BufferElement>::const_iterator end() const { return m_Elements.end(); }
|
||||||
|
private:
|
||||||
|
void CalcOffsetAndStride()
|
||||||
|
{
|
||||||
|
uint32_t offset = 0;
|
||||||
|
m_Stride = 0;
|
||||||
|
for (auto& element : m_Elements)
|
||||||
|
{
|
||||||
|
element.Offset = offset;
|
||||||
|
offset += element.Size;
|
||||||
|
m_Stride += element.Size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<BufferElement> m_Elements;
|
||||||
|
uint32_t m_Stride;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// VertexBuffer
|
||||||
|
class HAZEL_API VertexBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~VertexBuffer() {}
|
||||||
|
|
||||||
|
virtual void Bind() const = 0;
|
||||||
|
virtual void Unbind() const = 0;
|
||||||
|
virtual void SetLayout(const BufferLayout& layout) = 0;
|
||||||
|
virtual const BufferLayout& GetLayout() const = 0;
|
||||||
|
|
||||||
|
// static std::shared_ptr<VertexBuffer> Create(float* vertices, uint32_t size);
|
||||||
|
static VertexBuffer* Create(float* vertices, uint32_t size);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// IndexBuffer
|
||||||
|
class HAZEL_API IndexBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~IndexBuffer() {}
|
||||||
|
|
||||||
|
virtual void Bind() const = 0;
|
||||||
|
virtual void Unbind() const = 0;
|
||||||
|
|
||||||
|
virtual uint32_t GetCount() const = 0;
|
||||||
|
|
||||||
|
static IndexBuffer* Create(uint32_t* indices, uint32_t count);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //BUFFER_H
|
||||||
26
Hazel/src/Hazel/Renderer/OrthographicCamera.cpp
Normal file
26
Hazel/src/Hazel/Renderer/OrthographicCamera.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "OrthographicCamera.h"
|
||||||
|
|
||||||
|
#include <glm/ext/matrix_clip_space.hpp>
|
||||||
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
OrthographicCamera::OrthographicCamera(float left, float right, float bottom, float top)
|
||||||
|
: m_ProjectionMatrix(glm::ortho(left, right, bottom, top, -1.0f, 1.0f)), m_ViewMatrix(glm::mat4(1.0f))
|
||||||
|
{
|
||||||
|
m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void OrthographicCamera::RecalculateViewMatrix()
|
||||||
|
{
|
||||||
|
glm::mat4 transform = glm::translate(glm::mat4(1.0f), m_Position) * glm::rotate(glm::mat4(1.0f), glm::radians(m_Rotation), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||||
|
|
||||||
|
m_ViewMatrix = glm::inverse(transform);
|
||||||
|
m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
|
||||||
|
}
|
||||||
|
}
|
||||||
46
Hazel/src/Hazel/Renderer/OrthographicCamera.h
Normal file
46
Hazel/src/Hazel/Renderer/OrthographicCamera.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef ORTHOGRAPHICCAMERA_H
|
||||||
|
#define ORTHOGRAPHICCAMERA_H
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include "Hazel/Core.h"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class HAZEL_API OrthographicCamera
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
OrthographicCamera(float left, float right, float bottom, float top);
|
||||||
|
|
||||||
|
|
||||||
|
const glm::vec3& GetPosition() const { return m_Position; }
|
||||||
|
|
||||||
|
void SetPosition(const glm::vec3& prosition) { m_Position = prosition; RecalculateViewMatrix(); }
|
||||||
|
float GetRotation() const { return m_Rotation; }
|
||||||
|
void SetRotation(float rotation) { m_Rotation = rotation; RecalculateViewMatrix(); }
|
||||||
|
|
||||||
|
const glm::mat4& GetProjectionMatrix() const { return m_ProjectionMatrix; }
|
||||||
|
const glm::mat4& GetViewMatrix() const { return m_ViewMatrix; }
|
||||||
|
const glm::mat4& GetViewProjectionMatrix() const { return m_ViewProjectionMatrix; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void RecalculateViewMatrix();
|
||||||
|
|
||||||
|
private:
|
||||||
|
glm::mat4 m_ProjectionMatrix;
|
||||||
|
glm::mat4 m_ViewMatrix;
|
||||||
|
glm::mat4 m_ViewProjectionMatrix;
|
||||||
|
|
||||||
|
|
||||||
|
glm::vec3 m_Position = {0.0f, 0.0f, 0.0f};
|
||||||
|
float m_Rotation = 0.0f;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //ORTHOGRAPHICCAMERA_H
|
||||||
40
Hazel/src/Hazel/Renderer/Renderer.cpp
Normal file
40
Hazel/src/Hazel/Renderer/Renderer.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Renderer.h"
|
||||||
|
|
||||||
|
#include <SDL3/SDL_opengl.h>
|
||||||
|
|
||||||
|
#include "Platform/OpenGL/OpenGLShader.h"
|
||||||
|
#include "RendererCommand.h"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
|
||||||
|
Renderer::SceneData* Renderer::m_SceneData = new Renderer::SceneData;
|
||||||
|
|
||||||
|
void Renderer::Init()
|
||||||
|
{
|
||||||
|
RendererCommand::Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::BeginScene(OrthographicCamera& camera)
|
||||||
|
{
|
||||||
|
m_SceneData->ViewProjectionMatrix = camera.GetViewProjectionMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::EndScene()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::Submit(const std::shared_ptr<Shader>& shader, const std::shared_ptr<VertexArray>& vertexArray, const glm::mat4& transform)
|
||||||
|
{
|
||||||
|
shader->Bind();
|
||||||
|
std::dynamic_pointer_cast<OpenGLShader>(shader)->UploadUniformMat4("u_ViewProjection", m_SceneData->ViewProjectionMatrix);
|
||||||
|
std::dynamic_pointer_cast<OpenGLShader>(shader)->UploadUniformMat4("u_Transform", transform);
|
||||||
|
|
||||||
|
vertexArray->Bind();
|
||||||
|
RendererCommand::DrawIndexed(vertexArray);
|
||||||
|
}
|
||||||
|
}
|
||||||
42
Hazel/src/Hazel/Renderer/Renderer.h
Normal file
42
Hazel/src/Hazel/Renderer/Renderer.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef RENDERER_H
|
||||||
|
#define RENDERER_H
|
||||||
|
|
||||||
|
#include <Hazel/Renderer/OrthographicCamera.h>
|
||||||
|
|
||||||
|
#include "RendererAPI.h"
|
||||||
|
#include "Shader.h"
|
||||||
|
#include "Hazel/Core.h"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
class HAZEL_API Renderer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static void Init();
|
||||||
|
|
||||||
|
static void BeginScene(OrthographicCamera& camera);
|
||||||
|
static void EndScene();
|
||||||
|
|
||||||
|
static void Submit(const std::shared_ptr<Shader>& shader, const std::shared_ptr<VertexArray>& vertexArray, const glm::mat4& transform = glm::mat4(1.0f));
|
||||||
|
|
||||||
|
|
||||||
|
inline static RendererAPI::API GetAPI() { return RendererAPI::GetAPI(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct SceneData
|
||||||
|
{
|
||||||
|
glm::mat4 ViewProjectionMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
static SceneData* m_SceneData;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //RENDERER_H
|
||||||
12
Hazel/src/Hazel/Renderer/RendererAPI.cpp
Normal file
12
Hazel/src/Hazel/Renderer/RendererAPI.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "RendererAPI.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
RendererAPI::API RendererAPI::s_API = API::OPENGL;
|
||||||
|
|
||||||
|
}
|
||||||
39
Hazel/src/Hazel/Renderer/RendererAPI.h
Normal file
39
Hazel/src/Hazel/Renderer/RendererAPI.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef RENDERERAPI_H
|
||||||
|
#define RENDERERAPI_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "VertexArray.h"
|
||||||
|
#include "glm/vec4.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class HAZEL_API RendererAPI
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum class API
|
||||||
|
{
|
||||||
|
NONE = 0,
|
||||||
|
OPENGL = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual void Init() = 0;
|
||||||
|
virtual void SetClearColor(const glm::vec4& color) = 0;
|
||||||
|
virtual void Clear() = 0;
|
||||||
|
|
||||||
|
virtual void DrawIndexed(const std::shared_ptr<Hazel::VertexArray>& vertexArray) = 0;
|
||||||
|
|
||||||
|
inline static API GetAPI() {return s_API; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
static API s_API;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //RENDERERAPI_H
|
||||||
12
Hazel/src/Hazel/Renderer/RendererCommand.cpp
Normal file
12
Hazel/src/Hazel/Renderer/RendererCommand.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "RendererCommand.h"
|
||||||
|
#include "Platform/OpenGL/OpenGLRendererAPI.h"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
RendererAPI* RendererCommand::s_RendererAPI = new OpenGLRendererAPI;
|
||||||
|
|
||||||
|
}
|
||||||
38
Hazel/src/Hazel/Renderer/RendererCommand.h
Normal file
38
Hazel/src/Hazel/Renderer/RendererCommand.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef RENDERERCOMMAND_H
|
||||||
|
#define RENDERERCOMMAND_H
|
||||||
|
|
||||||
|
#include "RendererAPI.h"
|
||||||
|
|
||||||
|
namespace Hazel {
|
||||||
|
class HAZEL_API RendererCommand {
|
||||||
|
public:
|
||||||
|
|
||||||
|
inline static void Init()
|
||||||
|
{
|
||||||
|
s_RendererAPI->Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline static void SetClearColor(const glm::vec4& color)
|
||||||
|
{
|
||||||
|
s_RendererAPI->SetClearColor(color);
|
||||||
|
}
|
||||||
|
inline static void Clear()
|
||||||
|
{
|
||||||
|
s_RendererAPI->Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline static void DrawIndexed(const std::shared_ptr<VertexArray>& vertexArray) {
|
||||||
|
s_RendererAPI->DrawIndexed(vertexArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static RendererAPI* s_RendererAPI;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //RENDERERCOMMAND_H
|
||||||
24
Hazel/src/Hazel/Renderer/Shader.cpp
Normal file
24
Hazel/src/Hazel/Renderer/Shader.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Shader.h"
|
||||||
|
#include "Platform/OpenGL/OpenGLShader.h"
|
||||||
|
#include "Renderer.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
Shader* Shader::Create(const std::string& vertexSrc, const std::string& fragmentSrc)
|
||||||
|
{
|
||||||
|
switch (Renderer::GetAPI())
|
||||||
|
{
|
||||||
|
case RendererAPI::API::NONE: return nullptr;
|
||||||
|
case RendererAPI::API::OPENGL: return new OpenGLShader(vertexSrc, fragmentSrc);
|
||||||
|
}
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("Unknown RendererAPI");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
28
Hazel/src/Hazel/Renderer/Shader.h
Normal file
28
Hazel/src/Hazel/Renderer/Shader.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SHADER_H
|
||||||
|
#define SHADER_H
|
||||||
|
#include <string>
|
||||||
|
#include <Hazel/Core.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class HAZEL_API Shader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Shader() = default;
|
||||||
|
|
||||||
|
virtual void Bind() const = 0;
|
||||||
|
virtual void Unbind() const = 0;
|
||||||
|
|
||||||
|
static Shader* Create(const std::string& vertexSrc, const std::string& fragmentSrc);
|
||||||
|
private:
|
||||||
|
uint32_t m_RendererID;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //SHADER_H
|
||||||
30
Hazel/src/Hazel/Renderer/Texture.cpp
Normal file
30
Hazel/src/Hazel/Renderer/Texture.cpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Hazel/Renderer/Texture.h"
|
||||||
|
#include "Platform/OpenGL/OpenGLTexture.h"
|
||||||
|
#include "Renderer.h"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
|
||||||
|
Ref<Texture2D> Texture2D::Create(const std::string& path)
|
||||||
|
{
|
||||||
|
switch (Renderer::GetAPI())
|
||||||
|
{
|
||||||
|
case RendererAPI::API::NONE:
|
||||||
|
HZ_CORE_ERROR("NONE is not Support!");
|
||||||
|
return nullptr;
|
||||||
|
case RendererAPI::API::OPENGL:
|
||||||
|
// return std::make_shared<VertexBuffer>(OpenGLVertexBuffer(vertices, size));
|
||||||
|
return std::make_shared<OpenGLTexture2D>(path);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("Unknown RendererAPI!");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
41
Hazel/src/Hazel/Renderer/Texture.h
Normal file
41
Hazel/src/Hazel/Renderer/Texture.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef TEXTURE_H
|
||||||
|
#define TEXTURE_H
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <Hazel\Core.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class HAZEL_API Texture
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Texture() = default;
|
||||||
|
virtual const uint32_t GetWidth() const = 0;
|
||||||
|
virtual const uint32_t GetHeight() const = 0;
|
||||||
|
|
||||||
|
virtual void Bind(uint32_t slot = 0) const = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class HAZEL_API Texture2D : public Texture
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Texture2D() = default;
|
||||||
|
|
||||||
|
static Ref<Texture2D> Create(const std::string& path);
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //TEXTURE_H
|
||||||
34
Hazel/src/Hazel/Renderer/VertexArray.cpp
Normal file
34
Hazel/src/Hazel/Renderer/VertexArray.cpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "VertexArray.h"
|
||||||
|
|
||||||
|
#include <Platform/OpenGL/OpenGLVertexArray.h>
|
||||||
|
|
||||||
|
#include "Renderer.h"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
VertexArray::~VertexArray()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexArray* VertexArray::Create()
|
||||||
|
{
|
||||||
|
switch (Renderer::GetAPI())
|
||||||
|
{
|
||||||
|
case RendererAPI::API::NONE:
|
||||||
|
HZ_CORE_ERROR("NONE is not Support!");
|
||||||
|
return nullptr;
|
||||||
|
case RendererAPI::API::OPENGL:
|
||||||
|
return new OpenGLVertexArray();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("Unknown RendererAPI!");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
35
Hazel/src/Hazel/Renderer/VertexArray.h
Normal file
35
Hazel/src/Hazel/Renderer/VertexArray.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef VERTEXARRAY_H
|
||||||
|
#define VERTEXARRAY_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "Buffer.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class HAZEL_API VertexArray
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~VertexArray();
|
||||||
|
|
||||||
|
virtual void Bind() const = 0;
|
||||||
|
virtual void Unbind() const = 0;
|
||||||
|
|
||||||
|
virtual void AddVertexBuffer(const std::shared_ptr<VertexBuffer>& vertex_buffer) = 0;
|
||||||
|
virtual void SetIndexBuffer(const std::shared_ptr<IndexBuffer>& index_buffer) = 0;
|
||||||
|
|
||||||
|
virtual const std::vector<std::shared_ptr<VertexBuffer>>& GetVertexBuffer() const = 0;
|
||||||
|
virtual const std::shared_ptr<IndexBuffer>& GetIndexBuffer() const = 0;
|
||||||
|
|
||||||
|
static VertexArray* Create();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //VERTEXARRAY_H
|
||||||
63
Hazel/src/Platform/OpenGL/OpenGLBuffer.cpp
Normal file
63
Hazel/src/Platform/OpenGL/OpenGLBuffer.cpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "OpenGLBuffer.h"
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
////////////////////// VertexBuffer //////////////////////////
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
OpenGLVertexBuffer::OpenGLVertexBuffer(float* vertices, uint32_t size)
|
||||||
|
{
|
||||||
|
glCreateBuffers(1, &m_RendererID);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenGLVertexBuffer::~OpenGLVertexBuffer()
|
||||||
|
{
|
||||||
|
glDeleteBuffers(1, &m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLVertexBuffer::Bind() const
|
||||||
|
{
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLVertexBuffer::Unbind() const
|
||||||
|
{
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
////////////////////// IndexBuffer ///////////////////////////
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t* indices, uint32_t count) : m_Count(count)
|
||||||
|
{
|
||||||
|
glCreateBuffers(1, &m_RendererID);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(uint32_t), indices, GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenGLIndexBuffer::~OpenGLIndexBuffer()
|
||||||
|
{
|
||||||
|
glDeleteBuffers(1, &m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLIndexBuffer::Bind() const
|
||||||
|
{
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLIndexBuffer::Unbind() const
|
||||||
|
{
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
42
Hazel/src/Platform/OpenGL/OpenGLBuffer.h
Normal file
42
Hazel/src/Platform/OpenGL/OpenGLBuffer.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef OPENGLBUFFER_H
|
||||||
|
#define OPENGLBUFFER_H
|
||||||
|
#include <Hazel/Renderer/Buffer.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class OpenGLVertexBuffer : public VertexBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
OpenGLVertexBuffer(float* vertices, uint32_t size);
|
||||||
|
virtual ~OpenGLVertexBuffer();
|
||||||
|
virtual void Bind() const override;
|
||||||
|
virtual void Unbind() const override;
|
||||||
|
virtual void SetLayout(const BufferLayout& layout) override {m_Layout = layout;}
|
||||||
|
virtual const BufferLayout& GetLayout() const override {return m_Layout;}
|
||||||
|
private:
|
||||||
|
uint32_t m_RendererID;
|
||||||
|
BufferLayout m_Layout;
|
||||||
|
};
|
||||||
|
|
||||||
|
class OpenGLIndexBuffer : public IndexBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
OpenGLIndexBuffer(uint32_t* indices, uint32_t count);
|
||||||
|
virtual ~OpenGLIndexBuffer();
|
||||||
|
virtual void Bind() const override;
|
||||||
|
virtual void Unbind() const override;
|
||||||
|
virtual uint32_t GetCount() const override {return m_Count;}
|
||||||
|
private:
|
||||||
|
uint32_t m_RendererID;
|
||||||
|
uint32_t m_Count;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //OPENGLBUFFER_H
|
||||||
32
Hazel/src/Platform/OpenGL/OpenGLRendererAPI.cpp
Normal file
32
Hazel/src/Platform/OpenGL/OpenGLRendererAPI.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "OpenGLRendererAPI.h"
|
||||||
|
|
||||||
|
#include <SDL3/SDL_opengl.h>
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
void OpenGLRendererAPI::Init()
|
||||||
|
{
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLRendererAPI::SetClearColor(const glm::vec4& color)
|
||||||
|
{
|
||||||
|
glClearColor(color.x, color.y, color.z, color.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLRendererAPI::Clear()
|
||||||
|
{
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLRendererAPI::DrawIndexed(const std::shared_ptr<Hazel::VertexArray>& vertexArray)
|
||||||
|
{
|
||||||
|
glDrawElements(GL_TRIANGLES, vertexArray->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
Hazel/src/Platform/OpenGL/OpenGLRendererAPI.h
Normal file
23
Hazel/src/Platform/OpenGL/OpenGLRendererAPI.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef OPENGLRENDERERAPI_H
|
||||||
|
#define OPENGLRENDERERAPI_H
|
||||||
|
#include <Hazel/Renderer/RendererAPI.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class OpenGLRendererAPI : public RendererAPI
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void Init() override;
|
||||||
|
virtual void SetClearColor(const glm::vec4& color) override;
|
||||||
|
virtual void Clear() override;
|
||||||
|
virtual void DrawIndexed(const std::shared_ptr<Hazel::VertexArray>& vertexArray) override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //OPENGLRENDERERAPI_H
|
||||||
122
Hazel/src/Platform/OpenGL/OpenGLShader.cpp
Normal file
122
Hazel/src/Platform/OpenGL/OpenGLShader.cpp
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "OpenGLShader.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <Hazel/Log.h>
|
||||||
|
|
||||||
|
#include "glm/gtc/type_ptr.hpp"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
OpenGLShader::OpenGLShader(const std::string& vertexSrc, const std::string& fragmentSrc)
|
||||||
|
{
|
||||||
|
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
|
||||||
|
const char* vertexShaderSrc = vertexSrc.c_str();
|
||||||
|
glShaderSource(vertexShader, 1, &vertexShaderSrc, 0);
|
||||||
|
|
||||||
|
glCompileShader(vertexShader);
|
||||||
|
|
||||||
|
GLint compileSuccess = 0;
|
||||||
|
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compileSuccess);
|
||||||
|
if (compileSuccess == GL_FALSE)
|
||||||
|
{
|
||||||
|
int length = 0;
|
||||||
|
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &length);
|
||||||
|
|
||||||
|
std::vector<GLchar> infoLog(length);
|
||||||
|
glGetShaderInfoLog(vertexShader, length, &length, &infoLog[0]);
|
||||||
|
|
||||||
|
glDeleteShader(vertexShader);
|
||||||
|
HZ_CORE_ERROR("Vertex shader compilation failed!");
|
||||||
|
HZ_CORE_ERROR("{0}", infoLog.data());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
const char* fragmentShaderSrc = fragmentSrc.c_str();
|
||||||
|
glShaderSource(fragmentShader, 1, &fragmentShaderSrc, 0);
|
||||||
|
glCompileShader(fragmentShader);
|
||||||
|
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compileSuccess);
|
||||||
|
if (compileSuccess == GL_FALSE)
|
||||||
|
{
|
||||||
|
int length = 0;
|
||||||
|
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &length);
|
||||||
|
std::vector<GLchar> infoLog(length);
|
||||||
|
glGetShaderInfoLog(fragmentShader, length, &length, &infoLog[0]);
|
||||||
|
glDeleteShader(fragmentShader);
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("[{0}:{1}]: Fragment shader compilation failed!", __FILE__, __LINE__);
|
||||||
|
HZ_CORE_ERROR("{0}", infoLog.data());
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_RendererID = glCreateProgram();
|
||||||
|
glAttachShader(m_RendererID, vertexShader);
|
||||||
|
glAttachShader(m_RendererID, fragmentShader);
|
||||||
|
glLinkProgram(m_RendererID);
|
||||||
|
glGetProgramiv(m_RendererID, GL_LINK_STATUS, &compileSuccess);
|
||||||
|
if (compileSuccess == GL_FALSE)
|
||||||
|
{
|
||||||
|
int length = 0;
|
||||||
|
glGetProgramiv(m_RendererID, GL_INFO_LOG_LENGTH, &length);
|
||||||
|
std::vector<GLchar> infoLog(length);
|
||||||
|
glGetProgramInfoLog(m_RendererID, length, &length, &infoLog[0]);
|
||||||
|
glDeleteShader(vertexShader);
|
||||||
|
glDeleteShader(fragmentShader);
|
||||||
|
glDeleteProgram(m_RendererID);
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("shaderProgram link failed!");
|
||||||
|
HZ_CORE_ERROR("{0}", infoLog.data());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
glDeleteShader(vertexShader);
|
||||||
|
glDeleteShader(fragmentShader);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
OpenGLShader::~OpenGLShader()
|
||||||
|
{
|
||||||
|
glDeleteProgram(m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLShader::Bind() const
|
||||||
|
{
|
||||||
|
glUseProgram(m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLShader::Unbind() const
|
||||||
|
{
|
||||||
|
glUseProgram(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLShader::UploadUniformFloat3(const std::string& name, const glm::vec3& value) const
|
||||||
|
{
|
||||||
|
GLint location = glGetUniformLocation(m_RendererID, name.c_str());
|
||||||
|
glUniform4fv(location, 1, glm::value_ptr(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLShader::UploadUniformFloat4(const std::string& name, const glm::vec4& value) const
|
||||||
|
{
|
||||||
|
GLint location = glGetUniformLocation(m_RendererID, name.c_str());
|
||||||
|
glUniform4fv(location, 1, glm::value_ptr(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLShader::UploadUniformMat4(const std::string& name, const glm::mat4& matrix) const
|
||||||
|
{
|
||||||
|
GLint location = glGetUniformLocation(m_RendererID, name.c_str());
|
||||||
|
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLShader::UploadUniformInt(const char* str, const int value) const
|
||||||
|
{
|
||||||
|
const GLint location = glGetUniformLocation(m_RendererID, str);
|
||||||
|
glUniform1i(location, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
36
Hazel/src/Platform/OpenGL/OpenGLShader.h
Normal file
36
Hazel/src/Platform/OpenGL/OpenGLShader.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef OPENGLSHADER_H
|
||||||
|
#define OPENGLSHADER_H
|
||||||
|
#include <Hazel/Renderer/Shader.h>
|
||||||
|
|
||||||
|
#include "glm/fwd.hpp"
|
||||||
|
#include "glm/vec4.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class HAZEL_API OpenGLShader : public Shader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
OpenGLShader(const std::string& vertexSrc, const std::string& fragmentSrc);
|
||||||
|
virtual ~OpenGLShader() override;
|
||||||
|
|
||||||
|
void Bind() const override;
|
||||||
|
void Unbind() const override;
|
||||||
|
|
||||||
|
void UploadUniformInt(const char* str, int value) const;
|
||||||
|
|
||||||
|
void UploadUniformFloat3(const std::string& name, const glm::vec3& value) const;
|
||||||
|
void UploadUniformFloat4(const std::string& name, const glm::vec4& value) const;
|
||||||
|
void UploadUniformMat4(const std::string& name, const glm::mat4& matrix) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint32_t m_RendererID;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //OPENGLSHADER_H
|
||||||
62
Hazel/src/Platform/OpenGL/OpenGLTexture.cpp
Normal file
62
Hazel/src/Platform/OpenGL/OpenGLTexture.cpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "OpenGLTexture.h"
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <Hazel/Log.h>
|
||||||
|
|
||||||
|
#include "stb_image.h"
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
OpenGLTexture2D::OpenGLTexture2D(const std::string& path) : m_Path(path)
|
||||||
|
{
|
||||||
|
int width, height, channels;
|
||||||
|
stbi_set_flip_vertically_on_load(1);
|
||||||
|
|
||||||
|
stbi_uc* data = stbi_load(path.c_str(), &width, &height, &channels, 0);
|
||||||
|
if (!data)
|
||||||
|
{
|
||||||
|
HZ_CORE_ERROR("Failed to load texture {0}", path);
|
||||||
|
}
|
||||||
|
m_Width = width;
|
||||||
|
m_Height = height;
|
||||||
|
|
||||||
|
GLenum internalFormat = 0, dataFormat = 0;
|
||||||
|
if (channels == 4) {
|
||||||
|
internalFormat = GL_RGBA8;
|
||||||
|
dataFormat = GL_RGBA;
|
||||||
|
} else if (channels == 3) {
|
||||||
|
internalFormat = GL_RGB8;
|
||||||
|
dataFormat = GL_RGB;
|
||||||
|
}
|
||||||
|
|
||||||
|
HZ_CORE_TRACE("Texture Info: {0}", path);
|
||||||
|
HZ_CORE_TRACE(" Width: {0}, Height: {1}, Channel: {2}", m_Width, m_Height, channels);
|
||||||
|
|
||||||
|
|
||||||
|
glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);
|
||||||
|
glTextureStorage2D(m_RendererID, 1, internalFormat, m_Width, m_Height);
|
||||||
|
|
||||||
|
glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
|
|
||||||
|
glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, dataFormat, GL_UNSIGNED_BYTE, data);
|
||||||
|
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenGLTexture2D::~OpenGLTexture2D()
|
||||||
|
{
|
||||||
|
glDeleteTextures(1, &m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLTexture2D::Bind(uint32_t slot) const
|
||||||
|
{
|
||||||
|
glBindTextureUnit(slot, m_RendererID);
|
||||||
|
}
|
||||||
|
}
|
||||||
36
Hazel/src/Platform/OpenGL/OpenGLTexture.h
Normal file
36
Hazel/src/Platform/OpenGL/OpenGLTexture.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef OPENGLTEXTURE_H
|
||||||
|
#define OPENGLTEXTURE_H
|
||||||
|
#include <Hazel/Renderer/Texture.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class OpenGLTexture2D : public Hazel::Texture2D
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
OpenGLTexture2D(const std::string& path);
|
||||||
|
virtual ~OpenGLTexture2D();
|
||||||
|
|
||||||
|
virtual const uint32_t GetWidth() const override {return m_Width; }
|
||||||
|
virtual const uint32_t GetHeight() const override { return m_Height; }
|
||||||
|
|
||||||
|
virtual void Bind(uint32_t slot) const override;
|
||||||
|
|
||||||
|
uint32_t GetRendererID() const { return m_RendererID; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_Path;
|
||||||
|
uint32_t m_Width, m_Height;
|
||||||
|
uint32_t m_RendererID;
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //OPENGLTEXTURE_H
|
||||||
87
Hazel/src/Platform/OpenGL/OpenGLVertexArray.cpp
Normal file
87
Hazel/src/Platform/OpenGL/OpenGLVertexArray.cpp
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "OpenGLVertexArray.h"
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
static GLenum ShaderDataTypeToOpenGLBaseType(ShaderDataType type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case ShaderDataType::Float: return GL_FLOAT;
|
||||||
|
case ShaderDataType::Float2: return GL_FLOAT;
|
||||||
|
case ShaderDataType::Float3: return GL_FLOAT;
|
||||||
|
case ShaderDataType::Float4: return GL_FLOAT;
|
||||||
|
case ShaderDataType::Mat3: return GL_FLOAT;
|
||||||
|
case ShaderDataType::Mat4: return GL_FLOAT;
|
||||||
|
case ShaderDataType::Int: return GL_INT;
|
||||||
|
case ShaderDataType::Int2: return GL_INT;
|
||||||
|
case ShaderDataType::Int3: return GL_INT;
|
||||||
|
case ShaderDataType::Int4: return GL_INT;
|
||||||
|
case ShaderDataType::Bool: return GL_BOOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
HZ_CORE_ERROR("Unknown ShaderDataType!");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenGLVertexArray::OpenGLVertexArray()
|
||||||
|
{
|
||||||
|
glCreateVertexArrays(1, &m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenGLVertexArray::~OpenGLVertexArray()
|
||||||
|
{
|
||||||
|
glDeleteVertexArrays(1, &m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLVertexArray::Bind() const
|
||||||
|
{
|
||||||
|
glBindVertexArray(m_RendererID);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLVertexArray::Unbind() const
|
||||||
|
{
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLVertexArray::AddVertexBuffer(const std::shared_ptr<VertexBuffer>& vertexBuffer)
|
||||||
|
{
|
||||||
|
glBindVertexArray(m_RendererID);
|
||||||
|
vertexBuffer->Bind();
|
||||||
|
|
||||||
|
|
||||||
|
if (auto size = vertexBuffer->GetLayout().GetElements().size() )
|
||||||
|
{
|
||||||
|
HZ_CORE_INFO("Added Vertex Buffer {0}", size);
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
HZ_CORE_ERROR("Vertex Buffer has no layout!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t index = 0;
|
||||||
|
for (auto& element : vertexBuffer->GetLayout())
|
||||||
|
{
|
||||||
|
glEnableVertexAttribArray(index);
|
||||||
|
glVertexAttribPointer(index, element.GetComponentCount(), ShaderDataTypeToOpenGLBaseType(element.Type),
|
||||||
|
element.Normalized ? GL_TRUE : GL_FALSE, vertexBuffer->GetLayout().GetStride(),
|
||||||
|
reinterpret_cast<void*>(element.Offset));
|
||||||
|
index ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_VertexBuffers.push_back(vertexBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGLVertexArray::SetIndexBuffer(const std::shared_ptr<IndexBuffer>& indexBuffer)
|
||||||
|
{
|
||||||
|
glBindVertexArray(m_RendererID);
|
||||||
|
indexBuffer->Bind();
|
||||||
|
m_IndexBuffers = indexBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
34
Hazel/src/Platform/OpenGL/OpenGLVertexArray.h
Normal file
34
Hazel/src/Platform/OpenGL/OpenGLVertexArray.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
//
|
||||||
|
// Created by sfd on 25-4-22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef OPENGLVERTEXARRAY_H
|
||||||
|
#define OPENGLVERTEXARRAY_H
|
||||||
|
#include <Hazel/Renderer/VertexArray.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace Hazel
|
||||||
|
{
|
||||||
|
class OpenGLVertexArray : public VertexArray
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
OpenGLVertexArray();
|
||||||
|
virtual ~OpenGLVertexArray();
|
||||||
|
virtual void Bind() const override;
|
||||||
|
virtual void Unbind() const override;
|
||||||
|
|
||||||
|
virtual void AddVertexBuffer(const std::shared_ptr<VertexBuffer>& vertexBuffer) override;
|
||||||
|
virtual void SetIndexBuffer(const std::shared_ptr<IndexBuffer>& indexBuffer) override;
|
||||||
|
|
||||||
|
virtual const std::vector<std::shared_ptr<VertexBuffer>>& GetVertexBuffer() const override { return m_VertexBuffers; }
|
||||||
|
virtual const std::shared_ptr<IndexBuffer>& GetIndexBuffer() const override { return m_IndexBuffers; }
|
||||||
|
private:
|
||||||
|
uint32_t m_RendererID;
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<VertexBuffer>> m_VertexBuffers;
|
||||||
|
std::shared_ptr<IndexBuffer> m_IndexBuffers;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //OPENGLVERTEXARRAY_H
|
||||||
@ -69,6 +69,7 @@ namespace Hazel
|
|||||||
{
|
{
|
||||||
HZ_CORE_ERROR("Could not initialize GLAD context!");
|
HZ_CORE_ERROR("Could not initialize GLAD context!");
|
||||||
}
|
}
|
||||||
|
HZ_CORE_INFO("Init GLAD OK!");
|
||||||
|
|
||||||
|
|
||||||
SetVSync(true);
|
SetVSync(true);
|
||||||
|
|||||||
7988
Hazel/vendor/stb/stb_image.h
vendored
Normal file
7988
Hazel/vendor/stb/stb_image.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
BIN
Sandbox/assets/textures/Checkerboard.png
Normal file
BIN
Sandbox/assets/textures/Checkerboard.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
BIN
Sandbox/assets/textures/container2.png
Normal file
BIN
Sandbox/assets/textures/container2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 435 KiB |
BIN
Sandbox/assets/textures/iceLogo.png
Normal file
BIN
Sandbox/assets/textures/iceLogo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 216 KiB |
@ -1,39 +1,298 @@
|
|||||||
#include <Hazel.h>
|
#include <Hazel.h>
|
||||||
|
|
||||||
#include "imgui.h"
|
#include "imgui.h"
|
||||||
#include "imgui_impl_sdl2.h"
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
#include "imgui_impl_opengl3.h"
|
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <Platform/OpenGL/OpenGLShader.h>
|
||||||
|
#include <Platform/OpenGL/OpenGLTexture.h>
|
||||||
|
|
||||||
|
#include "glm/gtc/type_ptr.hpp"
|
||||||
|
#include "glm/gtx/transform.hpp"
|
||||||
|
|
||||||
|
|
||||||
class ExampleLayer : public Hazel::Layer
|
class ExampleLayer : public Hazel::Layer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ExampleLayer() : Layer("ExampleLayer")
|
// ExampleLayer() : Layer("ExampleLayer"), m_Camera(-1.0f, 1.0f, -1.0f, 1.0f), m_CameraPosition(0.0f)
|
||||||
|
|
||||||
|
ExampleLayer() : Layer("ExampleLayer"), m_Camera(-1.6f, 1.6f, -0.9f, 0.9f), m_CameraPosition(0.0f), m_SquarePosition(glm::vec3(0.0f))
|
||||||
{
|
{
|
||||||
|
// ------------------------------------------------------------test------------------------------------------------------------
|
||||||
|
// Vertex Array
|
||||||
|
m_VertexArray.reset(Hazel::VertexArray::Create());
|
||||||
|
|
||||||
|
// Vertex Buffer
|
||||||
|
float vertices[3 * 7] = {
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f,
|
||||||
|
0.5f, -0.5f, 0.0f, 0.2f, 0.3f, 0.8f, 1.0f,
|
||||||
|
0.0f, 0.5f, 0.0f, 0.8f, 0.8f, 0.2f, 1.0f
|
||||||
|
};
|
||||||
|
std::shared_ptr<Hazel::VertexBuffer> m_VertexBuffer;
|
||||||
|
m_VertexBuffer.reset(Hazel::VertexBuffer::Create(vertices, sizeof(vertices)));
|
||||||
|
Hazel::BufferLayout layout = {
|
||||||
|
{Hazel::ShaderDataType::Float3, "a_Postion"},
|
||||||
|
{Hazel::ShaderDataType::Float4, "a_Color"},
|
||||||
|
};
|
||||||
|
m_VertexBuffer->SetLayout(layout);
|
||||||
|
m_VertexArray->AddVertexBuffer(m_VertexBuffer);
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t indices[6] = {0, 1, 2};
|
||||||
|
std::shared_ptr<Hazel::IndexBuffer> m_IndexBuffer;
|
||||||
|
m_IndexBuffer.reset(Hazel::IndexBuffer::Create(indices, sizeof(indices) / sizeof(indices[0])));
|
||||||
|
m_VertexArray->SetIndexBuffer(m_IndexBuffer);
|
||||||
|
|
||||||
|
// Shader
|
||||||
|
std::string vertexSrc = R"(
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 a_Position;
|
||||||
|
layout(location = 1) in vec3 a_Color;
|
||||||
|
|
||||||
|
uniform mat4 u_ViewProjection;
|
||||||
|
uniform mat4 u_Transform;
|
||||||
|
|
||||||
|
out vec3 v_Position;
|
||||||
|
out vec3 v_Color;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
v_Position = a_Position;
|
||||||
|
v_Color = a_Color;
|
||||||
|
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
)";
|
||||||
|
std::string fragmentSrc = R"(
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
layout(location = 0) out vec4 color;
|
||||||
|
|
||||||
|
in vec3 v_Position;
|
||||||
|
in vec3 v_Color;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
color = vec4(v_Color, 1.0f);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
m_Shader.reset(Hazel::Shader::Create(vertexSrc, fragmentSrc));
|
||||||
|
|
||||||
|
|
||||||
|
m_SquareVA.reset(Hazel::VertexArray::Create());
|
||||||
|
|
||||||
|
float squareVertices[5 * 4] = {
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.0f,1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.0f,1.0f, 1.0f,
|
||||||
|
-0.5f, 0.5f, 0.0f,0.0f, 1.0f
|
||||||
|
};
|
||||||
|
std::shared_ptr<Hazel::VertexBuffer> squareVB; //= VertexBuffer::Create(squareVertices, sizeof(squareVertices));
|
||||||
|
squareVB.reset(Hazel::VertexBuffer::Create(squareVertices, sizeof(squareVertices)));
|
||||||
|
squareVB->SetLayout(
|
||||||
|
{
|
||||||
|
{Hazel::ShaderDataType::Float3, "a_Postion"},
|
||||||
|
{Hazel::ShaderDataType::Float2, "a_TexCoord"}
|
||||||
|
});
|
||||||
|
m_SquareVA->AddVertexBuffer(squareVB);
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t squareIndice[6] = {0, 1, 2, 2, 3, 0};
|
||||||
|
std::shared_ptr<Hazel::IndexBuffer> squareIB;
|
||||||
|
// = std::make_shared<IndexBuffer>(IndexBuffer::Create(squareIndice, sizeof(squareIndice) / sizeof(uint32_t)));
|
||||||
|
squareIB.reset(Hazel::IndexBuffer::Create(squareIndice, sizeof(squareIndice) / sizeof(uint32_t)));
|
||||||
|
m_SquareVA->SetIndexBuffer(squareIB);
|
||||||
|
|
||||||
|
std::string bluevertexSrc = R"(
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 a_Position;
|
||||||
|
layout(location = 1) in vec2 a_TexCoord;
|
||||||
|
|
||||||
|
uniform mat4 u_ViewProjection;
|
||||||
|
uniform mat4 u_Transform;
|
||||||
|
|
||||||
|
out vec2 v_TexCoord;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
v_TexCoord = a_TexCoord;
|
||||||
|
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
std::string bluefragmentSrc = R"(
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
layout(location = 0) out vec4 color;
|
||||||
|
|
||||||
|
in vec3 v_Position;
|
||||||
|
uniform vec4 u_Color;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
color = u_Color;
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
m_colorShader.reset(Hazel::Shader::Create(bluevertexSrc, bluefragmentSrc));
|
||||||
|
|
||||||
|
|
||||||
|
std::string textureVertexSrc = R"(
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 a_Position;
|
||||||
|
layout(location = 1) in vec2 a_TexCoord;
|
||||||
|
|
||||||
|
uniform mat4 u_ViewProjection;
|
||||||
|
uniform mat4 u_Transform;
|
||||||
|
|
||||||
|
out vec2 v_TexCoord;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
v_TexCoord = a_TexCoord;
|
||||||
|
gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0f);
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
std::string textureFragmentSrc = R"(
|
||||||
|
#version 460 core
|
||||||
|
|
||||||
|
layout(location = 0) out vec4 color;
|
||||||
|
|
||||||
|
in vec2 v_TexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D u_Texture;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
vec4 tmpColor = texture(u_Texture, v_TexCoord);
|
||||||
|
if(gl_FragCoord.x < 635){
|
||||||
|
color = tmpColor;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if(tmpColor.a < 0.1){
|
||||||
|
discard;
|
||||||
|
}else{
|
||||||
|
color = vec4(v_TexCoord, 0.0f, 1.0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
m_TextureShader.reset(Hazel::Shader::Create(textureVertexSrc, textureFragmentSrc));
|
||||||
|
|
||||||
|
|
||||||
|
// Texture
|
||||||
|
m_Texture = Hazel::Texture2D::Create("assets/textures/Checkerboard.png");
|
||||||
|
m_logoTexture = Hazel::Texture2D::Create("assets/textures/iceLogo.png");
|
||||||
|
|
||||||
|
std::dynamic_pointer_cast<Hazel::OpenGLShader>(m_TextureShader)->Bind();
|
||||||
|
std::dynamic_pointer_cast<Hazel::OpenGLShader>(m_TextureShader)->UploadUniformInt("u_Texture", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnUpdate() override
|
void OnUpdate(Hazel::TimeStep& ts) override
|
||||||
{
|
{
|
||||||
|
// key event
|
||||||
|
{
|
||||||
|
float time = ts;
|
||||||
|
|
||||||
|
const bool* state = SDL_GetKeyboardState(NULL);
|
||||||
|
if (state[SDL_SCANCODE_A])
|
||||||
|
{
|
||||||
|
m_CameraPosition.x -= m_CameraSpeed * time;
|
||||||
|
}
|
||||||
|
else if (state[SDL_SCANCODE_D])
|
||||||
|
{
|
||||||
|
m_CameraPosition.x += m_CameraSpeed * time;
|
||||||
|
}
|
||||||
|
if (state[SDL_SCANCODE_W])
|
||||||
|
{
|
||||||
|
m_CameraPosition.y += m_CameraSpeed * time;
|
||||||
|
}
|
||||||
|
else if (state[SDL_SCANCODE_S])
|
||||||
|
{
|
||||||
|
m_CameraPosition.y -= m_CameraSpeed * time;
|
||||||
|
}
|
||||||
|
if (state[SDL_SCANCODE_Q])
|
||||||
|
{
|
||||||
|
m_CameraRotation += m_CameraRotationSpeed * time;
|
||||||
|
}
|
||||||
|
else if (state[SDL_SCANCODE_E])
|
||||||
|
{
|
||||||
|
m_CameraRotation -= m_CameraRotationSpeed * time;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state[SDL_SCANCODE_I])
|
||||||
|
{
|
||||||
|
m_SquarePosition.y += m_CameraSpeed * time;
|
||||||
|
}
|
||||||
|
else if (state[SDL_SCANCODE_K])
|
||||||
|
{
|
||||||
|
m_SquarePosition.y -= m_CameraSpeed * time;
|
||||||
|
}
|
||||||
|
if (state[SDL_SCANCODE_J])
|
||||||
|
{
|
||||||
|
m_SquarePosition.x -= m_CameraSpeed * time;
|
||||||
|
}
|
||||||
|
else if (state[SDL_SCANCODE_L])
|
||||||
|
{
|
||||||
|
m_SquarePosition.x += m_CameraSpeed * time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Hazel::RendererCommand::SetClearColor({0.1f, 0.1f, 0.1f, 1.0f});
|
||||||
|
Hazel::RendererCommand::Clear();
|
||||||
|
|
||||||
|
m_Camera.SetPosition(m_CameraPosition);
|
||||||
|
m_Camera.SetRotation(m_CameraRotation);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Hazel::Renderer::BeginScene(m_Camera);
|
||||||
|
|
||||||
|
auto transform = glm::translate(glm::mat4(1.0f), m_SquarePosition);
|
||||||
|
|
||||||
|
m_Texture->Bind();
|
||||||
|
Hazel::Renderer::Submit(m_TextureShader, m_SquareVA, transform);
|
||||||
|
|
||||||
|
m_logoTexture->Bind();
|
||||||
|
Hazel::Renderer::Submit(m_TextureShader, m_SquareVA);
|
||||||
|
|
||||||
|
Hazel::Renderer::EndScene();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnEvent(SDL_Event& event) override
|
void OnEvent(SDL_Event& event) override
|
||||||
{
|
{
|
||||||
if (event.type == SDL_EVENT_KEY_DOWN)
|
|
||||||
{
|
|
||||||
HZ_CORE_TRACE("{}", event.key.key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnImGuiRender() override
|
void OnImGuiRender() override
|
||||||
{
|
{
|
||||||
ImGui::Begin("Hazel Layer");
|
ImGui::Begin("Hazel Layer");
|
||||||
ImGui::Text("Hello World");
|
ImGui::Text("Rotation: %f", m_CameraRotation);
|
||||||
|
ImGui::Text("Position: ( %.2f, %.2f, %.2f)", m_CameraPosition.x, m_CameraPosition.y, m_CameraPosition.z);
|
||||||
|
ImGui::Text("frame: %.3f", ImGui::GetIO().Framerate);
|
||||||
|
ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor));
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Hazel::Ref<Hazel::Shader> m_Shader;
|
||||||
|
Hazel::Ref<Hazel::VertexArray> m_VertexArray;
|
||||||
|
|
||||||
|
Hazel::Ref<Hazel::VertexArray> m_SquareVA;
|
||||||
|
Hazel::Ref<Hazel::Shader> m_colorShader, m_TextureShader;
|
||||||
|
|
||||||
|
Hazel::Ref<Hazel::Texture2D> m_Texture, m_logoTexture;
|
||||||
|
|
||||||
|
Hazel::OrthographicCamera m_Camera;
|
||||||
|
|
||||||
|
glm::vec3 m_CameraPosition;
|
||||||
|
float m_CameraSpeed = 1.0f;
|
||||||
|
|
||||||
|
float m_CameraRotation = 0.0f;
|
||||||
|
float m_CameraRotationSpeed = 30.f;
|
||||||
|
|
||||||
|
glm::vec3 m_SquarePosition;
|
||||||
|
float m_SquareMoveSpeed = 1.0f;
|
||||||
|
|
||||||
|
glm::vec3 m_SquareColor = {0.2f, 0.3f, 0.8f};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -44,15 +303,16 @@ public:
|
|||||||
{
|
{
|
||||||
PushLayer(new ExampleLayer());
|
PushLayer(new ExampleLayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
~Sandbox();
|
~Sandbox();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Sandbox::~Sandbox() = default;
|
Sandbox::~Sandbox() = default;
|
||||||
|
|
||||||
Hazel::Application* Hazel::CreateApplication() {
|
Hazel::Application* Hazel::CreateApplication()
|
||||||
|
{
|
||||||
return new Sandbox();
|
return new Sandbox();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user