Euphoria
compiledlines.cc
Go to the documentation of this file.
1 #include "render/compiledlines.h"
2 
3 #include <numeric>
4 
5 #include "core/lines.h"
6 #include "base/cint.h"
7 
10 #include "render/materialshader.h"
12 
13 namespace eu::render
14 {
15  void
17  (
18  const std::vector<core::Lines::FromToIndices>& lines,
20  )
21  {
22  std::vector<unsigned int> data;
23  data.reserve(lines.size() * 2);
24  for(const auto& line: lines)
25  {
26  const auto [from, to] = line;
27  data.emplace_back(from);
28  data.emplace_back(to);
29  }
30  buffer->set_data(data);
31  }
32 
33 
34  void
36  (
37  const std::vector<core::LinePoint>& points,
38  const std::vector<ShaderAttribute>& attributes,
39  VertexBuffer* vb
40  )
41  {
42  constexpr auto add_float3 = [](std::vector<float>* dst, const vec3f& src)
43  {
44  dst->emplace_back(src.x);
45  dst->emplace_back(src.y);
46  dst->emplace_back(src.z);
47  };
48  std::vector<float> data;
49  const auto total_attributes = std::accumulate
50  (
51  attributes.begin(),
52  attributes.end(),
53  0,
54  [](int count, const ShaderAttribute& att) -> int
55  {
56  return count + att.get_element_count();
57  }
58  );
59  data.reserve(total_attributes * points.size());
60  for(const auto& point: points)
61  {
62  for(const auto& att: attributes)
63  {
64  switch(att.source)
65  {
68  add_float3(&data, point.point);
69  break;
72  add_float3
73  (
74  &data, vec3f
75  {
76  point.color.r,
77  point.color.g,
78  point.color.b
79  }
80  );
81  break;
82  default:
83  DIE("Unhandled case");
84  }
85  }
86  }
87  vb->set_data(data);
88  }
89 
90 
91  void
93  (
94  const mat4f& model_matrix,
95  const mat4f& projection_matrix,
96  const mat4f& view_matrix
97  )
98  {
99  shader->use_shader();
100 
101  // set common constants
102  shader->set_model(model_matrix);
103  shader->set_projection(projection_matrix);
104  shader->set_view(view_matrix);
105 
109  IndexBuffer::bind(nullptr);
110  PointLayout::bind(nullptr);
111  }
112 
113 
114  std::shared_ptr<CompiledLines>
115  compile(MaterialShaderCache* shader_cache, const core::Lines& lines)
116  {
117  std::shared_ptr<CompiledLines> ret {new CompiledLines {}};
118 
119  ret->shader = shader_cache->get(io::FilePath{"~/default_line_shader"});
120 
121  PointLayout::bind(&ret->config);
122  VertexBuffer::bind(&ret->data);
123  IndexBuffer::bind(&ret->lines);
124 
125  const auto attributes = std::vector<ShaderAttribute>
126  {
129  };
130 
131  convert_points_to_vertex_buffer(lines.points, attributes, &ret->data);
132  bind_attributes(attributes, &ret->config);
133 
134  convert_lines_to_index_buffer(lines.indices, &ret->lines);
135  ret->line_count = c_sizet_to_int(lines.indices.size());
136 
137  IndexBuffer::bind(nullptr);
138  VertexBuffer::bind(nullptr);
139  PointLayout::bind(nullptr);
140 
141  return ret;
142  }
143 }
#define ASSERT(x)
Definition: assert.h:29
#define DIE(message)
Definition: assert.h:67
constexpr ShaderAttribute vertex
constexpr ShaderAttribute color
void bind_attributes(const std::vector< ShaderAttribute > &attributes, PointLayout *layout)
std::shared_ptr< CompiledLines > compile(MaterialShaderCache *shader_cache, const core::Lines &lines)
void convert_points_to_vertex_buffer(const std::vector< core::LinePoint > &points, const std::vector< ShaderAttribute > &attributes, VertexBuffer *vb)
void convert_lines_to_index_buffer(const std::vector< core::Lines::FromToIndices > &lines, IndexBuffer *buffer)
int c_sizet_to_int(size_t t)
Definition: cint.cc:11
int line
Definition: nlp_sentence.cc:91
std::string buffer
Definition: nlp_sentence.cc:87
TPoints * points
Definition: polybezier.cc:41
std::shared_ptr< TData > get(const TKey &key)
Definition: cache.h:18
Definition: mat4.h:14
void render(const mat4f &model_matrix, const mat4f &projection_matrix, const mat4f &view_matrix)
std::shared_ptr< MaterialShader > shader
Definition: compiledlines.h:26
Reuses points.
Definition: buffer.h:64
void draw(RenderMode mode, int count) const
Definition: buffer.cc:176
static void bind(const IndexBuffer *ebo)
Definition: buffer.cc:216
static void bind(const PointLayout *vao)
Definition: buffer.cc:130
Represents a shader attribute like vertex, normal or uv coord.
ShaderAttributeType type
the type of the attribute
ShaderAttributeSource source
the source of the shader attribute
Stores vertices, uv, etc.
Definition: buffer.h:13
void set_data(const std::vector< float > &data)
Definition: buffer.cc:29
static void bind(const VertexBuffer *vbo)
Definition: buffer.cc:38
Definition: vec3.h:48