Euphoria
bufferbuilder2d.cc
Go to the documentation of this file.
1 #include "core/bufferbuilder2d.h"
2 
3 #include <fstream>
4 
5 #include "base/vec3.h"
6 #include "base/cint.h"
7 
8 namespace eu::core
9 {
10  BufferPoint2::BufferPoint2(float x, float y, float u, float v)
11  : pos(x, y)
12  , uv(u, v)
13  {
14  }
15 
16 
18  : pos(p)
19  , uv(u)
20  {
21  }
22 
23 
25 
26 
27  void
29  {
30  data.push_back(p.pos.x);
31  data.push_back(p.pos.y);
32  data.push_back(p.uv.x);
33  data.push_back(p.uv.y);
34  }
35 
36 
37  namespace
38  {
39  bool
40  is_ccw
41  (
42  const std::vector<float>& data,
43  std::size_t a,
44  std::size_t b,
45  std::size_t c
46  )
47  {
48  const auto va = vec3f{data[a*4], data[a*4+1], 0};
49  const auto vb = vec3f{data[b*4], data[b*4+1], 0};
50  const auto vc = vec3f{data[c*4], data[c*4+1], 0};
51  const auto cr = vec3f::from_to(va, vb).cross(vec3f::from_to(va, vc));
52  return cr.z < 0;
53  }
54  }
55 
56 
57  void
59  (
60  unsigned int a,
61  unsigned int b,
62  unsigned int c
63  )
64  {
65  ASSERTX
66  (
67  a < (data.size() / 4) &&
68  b < (data.size() / 4) &&
69  c < (data.size() / 4),
70  a, b, c, data.size()/4
71  );
72  ASSERTX(is_ccw(data, a, b, c), a, b, c);
73  tris.push_back(a);
74  tris.push_back(b);
75  tris.push_back(c);
76  }
77 
78 
79  void
81  (
82  const BufferPoint2& a,
83  const BufferPoint2& b,
84  const BufferPoint2& c,
85  const BufferPoint2& d
86  )
87  {
88  const unsigned int ai = c_sizet_to_int(data.size());
89  const unsigned int bi = ai + 1;
90  const unsigned int ci = ai + 2;
91  const unsigned int di = ai + 3;
92 
93  add_vertex(a);
94  add_vertex(b);
95  add_vertex(c);
96  add_vertex(d);
97 
98  add_triangle(ci, bi, ai);
99  add_triangle(ci, di, bi);
100  }
101 
102 
103  void
104  BufferBuilder2::dump(const std::string& filename) const
105  {
106  std::ofstream f(filename.c_str());
107 
108  for(size_t vertex_index=0; vertex_index<data.size(); vertex_index+=4)
109  {
110  f << "v " << data[vertex_index+0] << " " << data[vertex_index+1] << "\n";
111  }
112 
113  for(size_t tex_index=0; tex_index<data.size(); tex_index+=4)
114  {
115  f << "vt " << data[tex_index+2] << " " << data[tex_index+3] << "\n";
116  }
117 
118  for(size_t tri_index=0; tri_index<tris.size(); tri_index+=3)
119  {
120  const auto a = tris[tri_index+0];
121  const auto b = tris[tri_index+1];
122  const auto c = tris[tri_index+2];
123  f << "f "
124  << a << "/" << a << " "
125  << b << "/" << b << " "
126  << c << "/" << c << " "
127  << "\n";
128  }
129  }
130 }
131 
#define ASSERTX(x,...)
Definition: assert.h:48
int c_sizet_to_int(size_t t)
Definition: cint.cc:11
void add_vertex(const BufferPoint2 &p)
void add_quad(const BufferPoint2 &a, const BufferPoint2 &b, const BufferPoint2 &c, const BufferPoint2 &d)
std::vector< unsigned int > tris
void add_triangle(unsigned int a, unsigned int b, unsigned int c)
std::vector< float > data
void dump(const std::string &filename) const
BufferPoint2(float x, float y, float u, float v)
Definition: vec2.h:33
Definition: vec3.h:48
vec3f cross(const vec3f &u) const
Definition: vec3.cc:283
static vec3f from_to(const vec3f &from, const vec3f &to)
Definition: vec3.cc:127
float z
Definition: vec3.h:51