处理换行情况,修复绘制三角形时没有按照顶点索引绘制

This commit is contained in:
2024-12-23 21:01:07 +08:00
parent c158670a19
commit e6a7254598
15 changed files with 52 additions and 45 deletions

View File

@@ -112,7 +112,7 @@ void dx_window::begin_frame() {
// context.draw_line( { 600, 600 }, { mouse_x, mouse_y }, { 1, 0, 1, 1 }, thickness);
// if (test_texture) context.draw_texture({ 0.f, 0.f }, test_texture->size().cast<float>(), test_texture);
context.draw_string({0, 0}, L"你好,世界!", 32);
context.draw_string({0, 0}, L"你好,世界!全是水群大师\n测试换行", 32);
context.flush();

View File

@@ -12,7 +12,7 @@ void dx_rect_pipeline::use() {
loader.use();
}
void dx_rect_pipeline::draw() {
void dx_rect_pipeline::draw(uint32_t in_index_count) {
const auto d3d_context = aorii::get_renderer<dx_renderer>()->get_d3d_context();
// 设置顶点缓冲区
@@ -26,8 +26,8 @@ void dx_rect_pipeline::draw() {
d3d_context->VSSetConstantBuffers(0, 1, &c_buffer);
// 设置图元拓扑
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// 绘制矩形
d3d_context->Draw(vertex_buffer->get_size(), 0);
d3d_context->DrawIndexed(in_index_count, 0, 0);
}

View File

@@ -8,7 +8,7 @@ class dx_rect_pipeline : public rect_pipeline {
public:
bool init() override;
void use() override;
void draw() override;
void draw(uint32_t in_index_count) override;
private:
dx_pipeline_loader loader;
};

View File

@@ -11,7 +11,7 @@ void dx_rounded_rect_pipeline::use() {
loader.use();
}
void dx_rounded_rect_pipeline::draw() {
void dx_rounded_rect_pipeline::draw(uint32_t in_index_count) {
const auto d3d_context = aorii::get_renderer<dx_renderer>()->get_d3d_context();
// 设置顶点缓冲区
@@ -26,8 +26,8 @@ void dx_rounded_rect_pipeline::draw() {
d3d_context->PSSetConstantBuffers(0, 1, &c_buffer);
// 设置图元拓扑
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// 绘制矩形
d3d_context->Draw(vertex_buffer->get_size(), 0);
d3d_context->DrawIndexed(in_index_count, 0, 0);
}

View File

@@ -8,7 +8,7 @@ class dx_rounded_rect_pipeline : public rounded_rect_pipeline {
public:
bool init() override;
void use() override;
void draw() override;
void draw(uint32_t in_index_count) override;
private:
dx_pipeline_loader loader;
};

View File

@@ -26,7 +26,7 @@ void dx_sdf_text_pipeline::use() {
loader.use();
}
void dx_sdf_text_pipeline::draw() {
void dx_sdf_text_pipeline::draw(uint32_t in_index_count) {
if (!glyph_texture)
return;
const auto d3d_context = aorii::get_renderer<dx_renderer>()->get_d3d_context();
@@ -49,8 +49,8 @@ void dx_sdf_text_pipeline::draw() {
d3d_context->PSSetShaderResources(0, 1, &srv);
// 设置图元拓扑
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// 绘制矩形
d3d_context->Draw(vertex_buffer->get_size(), 0);
d3d_context->DrawIndexed(in_index_count, 0, 0);
}

View File

@@ -8,7 +8,7 @@ public:
bool init() override;
void use() override;
void draw() override;
void draw(uint32_t in_index_count) override;
private:
dx_pipeline_loader loader;

View File

@@ -12,7 +12,7 @@ void dx_segment_pipeline::use() {
loader.use();
}
void dx_segment_pipeline::draw() {
void dx_segment_pipeline::draw(uint32_t in_index_count) {
const auto d3d_context = aorii::get_renderer<dx_renderer>()->get_d3d_context();
// 设置顶点缓冲区
@@ -27,8 +27,8 @@ void dx_segment_pipeline::draw() {
d3d_context->PSSetConstantBuffers(0, 1, &c_buffer);
// 设置图元拓扑
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// 绘制矩形
d3d_context->Draw(vertex_buffer->get_size(), 0);
d3d_context->DrawIndexed(in_index_count, 0, 0);
}

View File

@@ -6,7 +6,7 @@ class dx_segment_pipeline : public segment_pipeline {
public:
bool init() override;
void use() override;
void draw() override;
void draw(uint32_t in_index_count) override;
private:
dx_pipeline_loader loader;
};

View File

@@ -24,7 +24,7 @@ void dx_texture_pipeline::use() {
loader.use();
}
void dx_texture_pipeline::draw() {
void dx_texture_pipeline::draw(uint32_t in_index_count) {
if (!texture) return;
const auto d3d_context = aorii::get_renderer<dx_renderer>()->get_d3d_context();
@@ -42,8 +42,8 @@ void dx_texture_pipeline::draw() {
d3d_context->PSSetShaderResources(0, 1, &srv);
// 设置图元拓扑
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
d3d_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// 绘制矩形
d3d_context->Draw(vertex_buffer->get_size(), 0);
d3d_context->DrawIndexed(in_index_count, 0, 0);
}

View File

@@ -8,7 +8,7 @@ class dx_texture_pipeline : public texture_pipeline {
public:
bool init() override;
void use() override;
void draw() override;
void draw(uint32_t in_index_count) override;
private:
dx_pipeline_loader loader;

View File

@@ -45,7 +45,7 @@ public:
virtual void destroy();
virtual void use() = 0;
virtual void draw() = 0;
virtual void draw(uint32_t in_index_count) = 0;
virtual void set_triangle(const std::span<const aorii_vertex>& in_vertexes, const std::span<const aorii_triangle>& in_triangles);
protected:

View File

@@ -7,8 +7,8 @@
aorii_text* text = nullptr;
void renderer_context::init() {
text = new aorii_text(2048);
text->initialize(LR"(C:\Windows\Fonts\msyh.ttc)", 32);
text->precache_common_characters();
text->initialize(LR"(C:\Windows\Fonts\msyh.ttc)", 64);
// text->precache_common_characters();
}
void renderer_context::draw_rectangle(const Eigen::Vector2f& in_pos, const Eigen::Vector2f& in_size, const linear_color& in_color) {
@@ -43,9 +43,15 @@ void renderer_context::draw_string(const Eigen::Vector2f& in_pos, const std::wst
to_text_pipeline(in_color);
float cursor_x = in_pos.x();
float cursor_y = in_pos.y();
const float scale = in_height / text->get_font_pixel_size();
wchar_t last_char = 0;
for (const auto c : in_str) {
if (c == '\n') {
cursor_x = in_pos.x();
cursor_y += text->get_ascent() * scale;
continue;
}
cursor_x += last_char ? text->get_kerning(last_char, c) : 0;
character_info info{};
@@ -54,7 +60,7 @@ void renderer_context::draw_string(const Eigen::Vector2f& in_pos, const std::wst
// 根据in_height缩放字符大小
const float y_offset = (info.y_offset + text->get_ascent()) * scale;
const Eigen::Vector2f size { info.width * scale, info.height * scale };
const Eigen::Vector2f pos { cursor_x + info.x_offset * scale, in_pos.y() + y_offset };
const Eigen::Vector2f pos { cursor_x + info.x_offset * scale, cursor_y + y_offset };
cursor_x += info.advance * scale;
aorii_vertex_param param{};
@@ -145,31 +151,28 @@ void renderer_context::make_rect(const Eigen::Vector2f& in_pos, const Eigen::Vec
aorii_vertex v4{ { in_pos.x() + in_size.x(), in_pos.y() + in_size.y() }, { 1, 1 }, in_color, in_param }; // 右下角
if (in_angle != 0) {
auto center = in_pos + in_size / 2;
auto center = in_pos + in_size * 0.5f;
const Eigen::Affine2f transform =
Eigen::Translation<float, 2>(center) * // 平移到旋转中心
Eigen::Rotation2D<float>(in_angle) * // 旋转
Eigen::Translation<float, 2>(-center); // 平移回原点
Eigen::Translation2f(center) * // 平移到旋转中心
Eigen::Rotation2Df(in_angle) * // 旋转
Eigen::Translation2f(-center); // 平移回原点
v1.position = transform * v1.position;
v2.position = transform * v2.position;
v3.position = transform * v3.position;
v4.position = transform * v4.position;
v1.position = transform * v1.position.homogeneous();
v2.position = transform * v2.position.homogeneous();
v3.position = transform * v3.position.homogeneous();
v4.position = transform * v4.position.homogeneous();
}
const uint32_t index1 = vertices.size();
const uint32_t base_index = 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(v3);
// vertices.push_back(v2);
vertices.push_back(v4);
const aorii_triangle t1 = { index1, index2, index3 };
const aorii_triangle t2 = { index2, index3, index4 };
triangles.push_back(t1);
triangles.push_back(t2);
triangles.push_back(aorii_triangle{ base_index + 0, base_index + 1, base_index + 2 });
triangles.push_back(aorii_triangle{ base_index + 2, base_index + 1, base_index + 3 });
}
void renderer_context::rotate_vertex(const float in_angle, const Eigen::Vector2f& in_center) {

View File

@@ -77,7 +77,7 @@ public:
if (vertices.empty() || triangles.empty()) return;
if (current_pipeline) {
current_pipeline->set_triangle(vertices, triangles);
current_pipeline->draw();
current_pipeline->draw(triangles.size() * 3);
}
clear();
}

View File

@@ -23,13 +23,16 @@ struct Constants {
ParameterBlock<Constants> param_buffer : register(b0);
PSInput vertex_main(VSInput input) {
float2 altas_uv = input.param_a.xy;
float2 char_size = input.param_b.xy;
PSInput output;
output.position = mul(float4(input.position, 0.0f, 1.0f), param_buffer.transform);
output.uv = input.uv;
output.color = input.color;
output.altas_uv = float2(input.param_a.x, input.param_a.y);
output.altas_uv = altas_uv;
output.altas_index = input.param_a.z;
output.char_size = float2(input.param_b.x, input.param_b.y);
output.char_size = char_size;
return output;
}
@@ -40,7 +43,8 @@ float4 pixel_main(PSInput input) : SV_Target {
float2 uv = input.altas_uv + input.char_size * input.uv;
float distance = atlas_texture.Sample(sampler_state, float3(uv, input.altas_index)).r;
// return float4(distance, distance, distance, 1.0);
float alpha = smoothstep(0.47, 0.53, distance);
float range = 0.07;
float alpha = smoothstep(0.5 - range, 0.5 + range, distance);
float4 color = input.color;
color.a *= alpha;