xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
path.cc
浏览该文件的文档.
5//
6
7#define ROLE OpenGLObject
8#define LABEL Path
11#include <xcmath/utils/show.hpp>
12constexpr static auto STROKE_SHADER_ID = 0;
13constexpr static auto FILL_SHADER_ID = 1;
15 return GL::ShaderProgram::from_file(SHADER_FILE("path/stroke.vs"),
16 SHADER_FILE("path/stroke.fs"));
17}
19 return GL::ShaderProgram::from_file(SHADER_FILE("path/fill.vs"),
20 SHADER_FILE("path/fill.fs"));
21}
22
24 : mobject_(mobject),
25 position_vbo_(_gl GL_ARRAY_BUFFER),
26 stroke_vbo_(_gl GL_ARRAY_BUFFER),
27 fill_vbo_(_gl GL_ARRAY_BUFFER) {
28 _I("Create Path: " << this << " from mobject: " << mobject_.mobject());
29}
31 auto count = mobject_->points().count() + 1;
32 std::vector<xcmath::vec3<_gl GLfloat>> vertices(count);
33 std::vector<xcmath::vec4<_gl GLfloat>> strokes_colors(count);
34 std::vector<xcmath::vec4<_gl GLfloat>> fill_colors(count);
35 for (size_t i = 0; i < mobject_->points().count(); ++i) {
36 vertices[i] = xcmath::vec3<_gl GLfloat>(
37 mobject_->points()[i].x(), mobject_->points()[i].y(), 0.0f);
38 strokes_colors[i] = xcmath::vec4<_gl GLfloat>(mobject_->stroke_color());
39 fill_colors[i] = xcmath::vec4<_gl GLfloat>(mobject_->fill_color());
40 }
41 vertices[count - 1] = xcmath::vec3<_gl GLfloat>(
42 mobject_->points()[0].x(), mobject_->points()[0].y(), 0.0f);
43 strokes_colors[count - 1] =
44 xcmath::vec4<_gl GLfloat>(mobject_->stroke_color());
45 fill_colors[count - 1] = xcmath::vec4<_gl GLfloat>(mobject_->fill_color());
46 vao().bind();
47 position_vbo_ = GL::Buffer(_gl GL_ARRAY_BUFFER);
48 position_vbo_.bind();
49 position_vbo_.buffer_data(vertices, _gl GL_STATIC_DRAW);
50 stroke_vbo_ = GL::Buffer(_gl GL_ARRAY_BUFFER);
51 stroke_vbo_.bind();
52 stroke_vbo_.buffer_data(strokes_colors, _gl GL_STATIC_DRAW);
53 fill_vbo_ = GL::Buffer(_gl GL_ARRAY_BUFFER);
54 fill_vbo_.bind();
55 fill_vbo_.buffer_data(fill_colors, _gl GL_STATIC_DRAW);
56 _gl glVertexAttribFormat(0, 3, _gl GL_FLOAT, _gl GL_FALSE, 0);
57 _gl glVertexAttribFormat(1, 4, _gl GL_FLOAT, _gl GL_FALSE, 0);
58 _gl glVertexAttribFormat(2, 4, _gl GL_FLOAT, _gl GL_FALSE, 0);
59 _gl glVertexAttribBinding(0, 0);
60 _gl glVertexAttribBinding(1, 1);
61 _gl glVertexAttribBinding(2, 2);
62 _gl glBindVertexBuffer(0, position_vbo_.id(), 0,
64 _gl glBindVertexBuffer(1, stroke_vbo_.id(), 0,
66 _gl glBindVertexBuffer(2, fill_vbo_.id(), 0,
68 _gl glEnableVertexAttribArray(0);
69 _gl glEnableVertexAttribArray(1);
70 _gl glEnableVertexAttribArray(2);
71 stroke_shader_program_ =
73 fill_shader_program_ =
75}
76
78 _I("Destroy Path: " _SELF);
79 stroke_shader_program_.reset();
80 fill_shader_program_.reset();
81 stroke_vbo_.destroy();
82 fill_vbo_.destroy();
83 position_vbo_.destroy();
84 vao().unbind();
85}
87 vao().bind();
88 if (mobject_->stroke_color().a() > 0.0f) {
89 stroke_shader_program_->use();
90 stroke_shader_program_->uniform("model", mobject_.model_matrix());
91 _gl glDrawArrays(_gl GL_LINE_STRIP, 0,
92 mobject_->points().closed()
93 ? (mobject_->points().count() + 1)
94 : mobject_->points().count());
95 }
96 if (mobject_->fill_color().a() > 0.0f) {
97 fill_shader_program_->use();
98 fill_shader_program_->uniform("model", mobject_.model_matrix());
99 _gl glDrawArrays(_gl GL_TRIANGLE_FAN, 0,
100 mobject_->points().count() + 1);
101 }
102};
104 const xcmath::mat4<float_t> &projection_view) {
105 stroke_shader_program_->uniform("projection_view", projection_view);
106 fill_shader_program_->uniform("projection_view", projection_view);
107}
108
virtual void update_projection_view(const xcmath::mat4< float_t > &projection_view) override
Definition path.cc:103
Path(mobject::Path *mobject)
Definition path.cc:23
void render() const override
Definition path.cc:86
Matrix class template
Definition mat.hpp:27
Vector class template
Definition vec.hpp:206
#define XCAL_OPENGL_REGIST_OBJECT_IMPL(class_, type)
Definition object.hpp:50
#define XCAL_SHADER_INSTANCE(T, _id)
#define SHADER_FILE(name)