Symphony Of Empires
curve.cpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // curve.cpp
20 //
21 // Abstract:
22 // Generalized "IO" functions for more abstract access to the OS resources
23 // along with a custom "std::filesystem" implementation to aid and
24 // implement custom paths.
25 // ----------------------------------------------------------------------------
26 
27 #include "eng3d/curve.hpp"
28 #include "eng3d/shader.hpp"
29 #include "eng3d/primitive.hpp"
30 
31 #include <vector>
32 #include <glm/vec2.hpp>
33 #include <glm/mat4x4.hpp>
34 
35 Eng3D::Curve::Curve(std::vector<glm::vec3> points, std::vector<glm::vec3> normals, float width)
36 {
37  this->create_line(points, normals, width);
38 }
39 
40 void Eng3D::Curve::add_line(std::vector<glm::vec3> points, std::vector<glm::vec3> normals, float width)
41 {
42  this->create_line(points, normals, width);
43 }
44 
46 {
47  this->quads = std::make_unique<Eng3D::TriangleList>(positions, tex_coords);
48 }
49 
50 void Eng3D::Curve::add_quad(glm::vec3 c1, glm::vec3 c2, glm::vec3 c3, glm::vec3 c4) {
51  positions.reserve(positions.size() + 6);
52  tex_coords.reserve(tex_coords.size() + 6);
53  positions.push_back(c1);
54  tex_coords.push_back(glm::vec2(0.f, 0.f));
55  positions.push_back(c2);
56  tex_coords.push_back(glm::vec2(0.f, 1.f));
57  positions.push_back(c3);
58  tex_coords.push_back(glm::vec2(1.f, 1.f));
59  positions.push_back(c3);
60  tex_coords.push_back(glm::vec2(1.f, 1.f));
61  positions.push_back(c4);
62  tex_coords.push_back(glm::vec2(1.f, 0.f));
63  positions.push_back(c1);
64  tex_coords.push_back(glm::vec2(0.f, 0.f));
65 }
66 
67 void Eng3D::Curve::create_line(const std::vector<glm::vec3>& points, const std::vector<glm::vec3>& normals, float width) {
68  glm::vec3 prev_c1, prev_c2, prev_c3, prev_c4;
69  for(size_t i = 0; i < points.size() - 1; i++) {
70  auto p1 = points[i];
71  auto p2 = points[i + 1];
72  auto normal = normals[i];
73 
74  // Vec from p1 to p2
75  glm::vec3 p1t2 = p2 - p1;
76  // Orthogonal vector to the line from p1 to p2
77  glm::vec3 ortho = glm::cross(p1t2, normal);//glm::vec2(p1t2.y, -p1t2.x);
78  // ortho = ortho.y > 0 ? ortho : -ortho; // Needed for window order of triangle
79  ortho = glm::normalize(ortho);
80  // Creates the line corners
81  glm::vec3 c1 = p1 - ortho * width * 0.5f;
82  glm::vec3 c2 = p1 + ortho * width * 0.5f;
83  if(i != 0) {
84  c1 = glm::mix(c1, prev_c3, .5f);
85  c2 = glm::mix(c2, prev_c4, .5f);
86  prev_c3 = c1;
87  prev_c4 = c2;
88  this->add_quad(prev_c1, prev_c2, prev_c4, prev_c3);
89  }
90  glm::vec3 c3 = p2 - ortho * width * 0.5f;
91  glm::vec3 c4 = p2 + ortho * width * 0.5f;
92  prev_c1 = c1;
93  prev_c2 = c2;
94  prev_c3 = c3;
95  prev_c4 = c4;
96  }
97  this->add_quad(prev_c1, prev_c2, prev_c4, prev_c3);
98 }
99 
100 #if defined(E3D_BACKEND_OPENGL) || defined(E3D_BACKEND_GLES)
101 void Eng3D::Curve::draw() {
102  if(this->quads.get() == nullptr)
103  this->upload();
104  this->quads->draw();
105 }
106 #elif defined E3D_BACKEND_RGX
107 void Eng3D::Curve::draw() {
108 
109 }
110 #endif
void upload(const Eng3D::OpenGL::VAO &vao, const Eng3D::OpenGL::VBO &vbo, const Eng3D::OpenGL::EBO &ebo, const void *buffer, size_t n_buffer, size_t sz_buffer, size_t sz_buffer_vert, size_t sz_buffer_tex, size_t v_len, size_t t_len, size_t c_len, const void *indices, size_t n_indices, size_t sz_indices, bool has_color)
Definition: mesh.cpp:176
void draw(const Eng3D::OpenGL::VAO &vao, MeshMode mode, const void *indices, size_t n_indices, const void *buffer, size_t n_buffer, int instances)
Definition: mesh.cpp:136
void add_line(std::vector< glm::vec3 > points, std::vector< glm::vec3 > normals, float width)
Definition: curve.cpp:40
void upload()
Definition: curve.cpp:45
Curve()=default