封装renderer_context
This commit is contained in:
parent
3b201e14d1
commit
49f0e7ce47
@ -12,7 +12,7 @@ int main(int argc, char* argv[]) {
|
|||||||
auto glfw_window = window->get_glfw_window();
|
auto glfw_window = window->get_glfw_window();
|
||||||
while (!glfwWindowShouldClose(glfw_window)) {
|
while (!glfwWindowShouldClose(glfw_window)) {
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
aorii::s_renderer->render(0.01f);
|
aorii::update();
|
||||||
}
|
}
|
||||||
|
|
||||||
aorii::destroy_window_manager();
|
aorii::destroy_window_manager();
|
||||||
|
@ -17,7 +17,7 @@ bool dx_renderer::init() {
|
|||||||
|
|
||||||
void dx_renderer::destroy() { glfwTerminate(); }
|
void dx_renderer::destroy() { glfwTerminate(); }
|
||||||
|
|
||||||
bool dx_renderer::render(float delta_time) {
|
bool dx_renderer::render() {
|
||||||
const auto& all_window = aorii::get_all_window();
|
const auto& all_window = aorii::get_all_window();
|
||||||
for (const auto& window : all_window) {
|
for (const auto& window : all_window) {
|
||||||
window->begin_frame();
|
window->begin_frame();
|
||||||
|
@ -9,7 +9,7 @@ class dx_renderer : public renderer {
|
|||||||
public:
|
public:
|
||||||
bool init() override;
|
bool init() override;
|
||||||
void destroy() override;
|
void destroy() override;
|
||||||
bool render(float delta_time) override;
|
bool render() override;
|
||||||
|
|
||||||
renderer_texture* create_texture(const Eigen::Vector2i& size) override;
|
renderer_texture* create_texture(const Eigen::Vector2i& size) override;
|
||||||
void destroy_texture(renderer_texture* texture) override;
|
void destroy_texture(renderer_texture* texture) override;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include "dx_renderer.h"
|
#include "dx_renderer.h"
|
||||||
|
#include "core/renderer/renderer_context.h"
|
||||||
#include "misc/scope_exit.h"
|
#include "misc/scope_exit.h"
|
||||||
|
|
||||||
using namespace aorii;
|
using namespace aorii;
|
||||||
@ -82,6 +83,7 @@ bool dx_window::create_surface(GLFWwindow* in_window) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void dx_window::begin_frame() {
|
void dx_window::begin_frame() {
|
||||||
glfwMakeContextCurrent(get_glfw_window());
|
glfwMakeContextCurrent(get_glfw_window());
|
||||||
const auto render = aorii::get_renderer<dx_renderer>();
|
const auto render = aorii::get_renderer<dx_renderer>();
|
||||||
@ -93,22 +95,29 @@ void dx_window::begin_frame() {
|
|||||||
dx_pipeline_begin_frame(projection_matrix);
|
dx_pipeline_begin_frame(projection_matrix);
|
||||||
|
|
||||||
{
|
{
|
||||||
float random_r = static_cast<float>(rand()) / RAND_MAX;
|
static std::chrono::duration<double> timer = {};
|
||||||
float random_g = static_cast<float>(rand()) / RAND_MAX;
|
static linear_color random_color = { 0, 0, 0, 0 };
|
||||||
float random_b = static_cast<float>(rand()) / RAND_MAX;
|
auto delta_time = get_delta_time();
|
||||||
linear_color random_color = {random_r, random_g, random_b, 1.0f};
|
timer += delta_time;
|
||||||
|
if (timer.count() >= 1) {
|
||||||
|
float random_r = static_cast<float>(rand()) / RAND_MAX;
|
||||||
|
float random_g = static_cast<float>(rand()) / RAND_MAX;
|
||||||
|
float random_b = static_cast<float>(rand()) / RAND_MAX;
|
||||||
|
random_color = { random_r, random_g, random_b, 1.0f };
|
||||||
|
timer -= std::chrono::seconds(1);
|
||||||
|
}
|
||||||
|
static float pos_x = 0;
|
||||||
|
static float pos_y = 0;
|
||||||
|
|
||||||
const aorii_vertex_type v1 {{0.f, 0.f}, random_color }; // 左上角
|
pos_x += delta_time.count() * 1000;
|
||||||
const aorii_vertex_type v2 {{100.f, 0.f}, random_color }; // 右上角
|
if (pos_x >= 1000) {
|
||||||
const aorii_vertex_type v3 {{0.f, 100.f}, random_color }; // 左下角
|
pos_x = 0;
|
||||||
const aorii_vertex_type v4 {{100.f, 100.f}, random_color }; // 右下角
|
pos_y += 1;
|
||||||
const std::array vertices = {
|
}
|
||||||
v1, v2, v3, v4
|
renderer_context context;
|
||||||
};
|
context.draw_rectangle({pos_x, pos_y}, {100, 1000}, random_color);
|
||||||
const aorii_triangle_type t1 = {0, 1, 2};
|
|
||||||
const aorii_triangle_type t2 = {1, 2, 3};
|
pipeline.set_triangle(context.get_vertices(), context.get_triangles());
|
||||||
const std::array indices = { t1, t2 };
|
|
||||||
pipeline.set_triangle({vertices.data(), vertices.size()}, {indices.data(), indices.size()});
|
|
||||||
}
|
}
|
||||||
pipeline.draw();
|
pipeline.draw();
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ protected:
|
|||||||
|
|
||||||
HRESULT build_render_target_view();
|
HRESULT build_render_target_view();
|
||||||
private:
|
private:
|
||||||
|
|
||||||
IDXGISwapChain1* swap_chain = nullptr;
|
IDXGISwapChain1* swap_chain = nullptr;
|
||||||
ID3D11RenderTargetView* render_target_view = nullptr;
|
ID3D11RenderTargetView* render_target_view = nullptr;
|
||||||
Eigen::Matrix4f projection_matrix;
|
Eigen::Matrix4f projection_matrix;
|
||||||
|
@ -8,6 +8,16 @@
|
|||||||
#include "backend/dx/dx_renderer.h"
|
#include "backend/dx/dx_renderer.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::chrono::duration<double> delta_time = {};
|
||||||
|
std::chrono::time_point<std::chrono::system_clock> last_time = {};
|
||||||
|
|
||||||
|
void renderer::tick() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void renderer::destroy_window(renderer_window* window) {
|
||||||
|
delete window;
|
||||||
|
}
|
||||||
|
|
||||||
bool aorii::create_renderer(renderer_api api) {
|
bool aorii::create_renderer(renderer_api api) {
|
||||||
if (s_renderer) return true;
|
if (s_renderer) return true;
|
||||||
switch (api) {
|
switch (api) {
|
||||||
@ -31,6 +41,7 @@ bool aorii::create_renderer(renderer_api api) {
|
|||||||
delete s_renderer;
|
delete s_renderer;
|
||||||
s_renderer = nullptr;
|
s_renderer = nullptr;
|
||||||
}
|
}
|
||||||
|
last_time = std::chrono::high_resolution_clock::now();
|
||||||
return s_renderer != nullptr;
|
return s_renderer != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,6 +53,17 @@ void aorii::destroy_renderer() {
|
|||||||
s_renderer = nullptr;
|
s_renderer = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderer::destroy_window(renderer_window* window) {
|
void aorii::update() {
|
||||||
delete window;
|
const auto current_time = std::chrono::high_resolution_clock::now();
|
||||||
|
delta_time = current_time - last_time;
|
||||||
|
last_time = current_time;
|
||||||
|
|
||||||
|
s_renderer->tick();
|
||||||
|
s_renderer->render();
|
||||||
|
|
||||||
|
std::this_thread::yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::chrono::duration<double>& aorii::get_delta_time() {
|
||||||
|
return delta_time;
|
||||||
}
|
}
|
||||||
|
@ -24,17 +24,19 @@ public:
|
|||||||
|
|
||||||
virtual bool init() = 0;
|
virtual bool init() = 0;
|
||||||
virtual void destroy() = 0;
|
virtual void destroy() = 0;
|
||||||
virtual bool render(float delta_time) = 0;
|
virtual void tick();
|
||||||
|
virtual bool render() = 0;
|
||||||
|
|
||||||
virtual renderer_texture* create_texture(const Eigen::Vector2i& size) = 0;
|
virtual renderer_texture* create_texture(const Eigen::Vector2i& size) = 0;
|
||||||
virtual void destroy_texture(renderer_texture* texture) = 0;
|
virtual void destroy_texture(renderer_texture* texture) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual renderer_window* create_window() = 0;
|
virtual renderer_window* create_window() = 0;
|
||||||
virtual void destroy_window(renderer_window* window);
|
virtual void destroy_window(renderer_window* window);
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace aorii {
|
namespace aorii {
|
||||||
inline renderer* s_renderer = nullptr;
|
inline renderer* s_renderer;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static T* get_renderer() {
|
static T* get_renderer() {
|
||||||
@ -43,6 +45,9 @@ namespace aorii {
|
|||||||
|
|
||||||
bool create_renderer(renderer_api api);
|
bool create_renderer(renderer_api api);
|
||||||
void destroy_renderer();
|
void destroy_renderer();
|
||||||
|
void update();
|
||||||
|
|
||||||
|
const std::chrono::duration<double>& get_delta_time();
|
||||||
|
|
||||||
inline std::filesystem::path s_shader_relative_path = "resource/shader";
|
inline std::filesystem::path s_shader_relative_path = "resource/shader";
|
||||||
inline void set_shader_relative_path(const std::filesystem::path& path) { s_shader_relative_path = path; }
|
inline void set_shader_relative_path(const std::filesystem::path& path) { s_shader_relative_path = path; }
|
||||||
|
50
src/renderer/core/renderer/renderer_context.cpp
Normal file
50
src/renderer/core/renderer/renderer_context.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include "renderer_context.h"
|
||||||
|
|
||||||
|
void renderer_context::draw_rectangle(const Eigen::Vector2i& in_pos, const Eigen::Vector2i& in_size,
|
||||||
|
const linear_color& in_color) {
|
||||||
|
|
||||||
|
const aorii_vertex_type v1 {{in_pos.x(), in_pos.y()}, in_color }; // 左上角
|
||||||
|
const aorii_vertex_type v2 {{in_pos.x() + in_size.x(), in_pos.y()}, in_color }; // 右上角
|
||||||
|
const aorii_vertex_type v3 {{in_pos.x(), in_pos.y() + in_size.y()}, in_color }; // 左下角
|
||||||
|
const aorii_vertex_type v4 {{in_pos.x() + in_size.x(), in_pos.y() + in_size.y()}, in_color }; // 右下角
|
||||||
|
|
||||||
|
const uint32_t index1 = vertices.size();
|
||||||
|
vertices.push_back(v1);
|
||||||
|
const uint32_t index2 = vertices.size();
|
||||||
|
vertices.push_back(v2);
|
||||||
|
const uint32_t index3 = vertices.size();
|
||||||
|
vertices.push_back(v3);
|
||||||
|
const uint32_t index4 = vertices.size();
|
||||||
|
vertices.push_back(v4);
|
||||||
|
|
||||||
|
const aorii_triangle_type t1 = {index1, index2, index3};
|
||||||
|
const aorii_triangle_type t2 = {index2, index3, index4};
|
||||||
|
triangles.push_back(t1);
|
||||||
|
triangles.push_back(t2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void renderer_context::draw_line(const Eigen::Vector2i& in_pos_p1, const Eigen::Vector2i& in_pos_p2,
|
||||||
|
const linear_color& in_color, float in_thickness) {
|
||||||
|
const Eigen::Vector2f direction = (in_pos_p2 - in_pos_p1).cast<float>().normalized();
|
||||||
|
const Eigen::Vector2f normal = {-direction.y(), direction.x()};
|
||||||
|
const Eigen::Vector2f offset = normal * in_thickness;
|
||||||
|
|
||||||
|
const aorii_vertex_type v1 {{in_pos_p1.x() + offset.x(), in_pos_p1.y() + offset.y()}, in_color };
|
||||||
|
const aorii_vertex_type v2 {{in_pos_p1.x() - offset.x(), in_pos_p1.y() - offset.y()}, in_color };
|
||||||
|
const aorii_vertex_type v3 {{in_pos_p2.x() + offset.x(), in_pos_p2.y() + offset.y()}, in_color };
|
||||||
|
const aorii_vertex_type v4 {{in_pos_p2.x() - offset.x(), in_pos_p2.y() - offset.y()}, in_color };
|
||||||
|
|
||||||
|
const uint32_t index1 = vertices.size();
|
||||||
|
vertices.push_back(v1);
|
||||||
|
const uint32_t index2 = vertices.size();
|
||||||
|
vertices.push_back(v2);
|
||||||
|
const uint32_t index3 = vertices.size();
|
||||||
|
vertices.push_back(v3);
|
||||||
|
const uint32_t index4 = vertices.size();
|
||||||
|
vertices.push_back(v4);
|
||||||
|
|
||||||
|
const aorii_triangle_type t1 = {index1, index2, index3};
|
||||||
|
const aorii_triangle_type t2 = {index2, index3, index4};
|
||||||
|
triangles.push_back(t1);
|
||||||
|
triangles.push_back(t2);
|
||||||
|
}
|
26
src/renderer/core/renderer/renderer_context.h
Normal file
26
src/renderer/core/renderer/renderer_context.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Eigen/Eigen>
|
||||||
|
|
||||||
|
#include "core/pipeline/pipeline.h"
|
||||||
|
|
||||||
|
class renderer_context {
|
||||||
|
public:
|
||||||
|
void draw_rectangle(const Eigen::Vector2i& in_pos, const Eigen::Vector2i& in_size, const linear_color& in_color);
|
||||||
|
void draw_line(const Eigen::Vector2i& in_pos_p1, const Eigen::Vector2i& in_pos_p2, const linear_color& in_color, float in_thickness);
|
||||||
|
|
||||||
|
void clear() {
|
||||||
|
vertices.clear();
|
||||||
|
triangles.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] const std::vector<aorii_vertex_type>& get_vertices() const {
|
||||||
|
return vertices;
|
||||||
|
}
|
||||||
|
[[nodiscard]] const std::vector<aorii_triangle_type>& get_triangles() const {
|
||||||
|
return triangles;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::vector<aorii_vertex_type> vertices;
|
||||||
|
std::vector<aorii_triangle_type> triangles;
|
||||||
|
void add_triangle(const Eigen::Vector2i& in_pos_p1, const Eigen::Vector2i& in_pos_p2, const Eigen::Vector2i& in_pos_p3);
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user