Symphony Of Empires
primitive.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 // primitive.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include <memory>
26 #include <type_traits>
27 #include <glm/gtc/matrix_transform.hpp>
28 #include <glm/gtx/intersect.hpp>
29 
30 #include "eng3d/primitive.hpp"
31 #include "eng3d/rectangle.hpp"
32 #include "eng3d/utils.hpp"
33 
34 // Eng3D expects OpenGL to run on a single thread
35 
36 Eng3D::Line::Line(glm::vec2 start, glm::vec2 end)
37  : Eng3D::MeshStatic<2, 0, glm::vec2, glm::vec2>(Eng3D::MeshMode::LINES)
38 {
39  buffer[0] = Eng3D::MeshData(glm::vec2(start.x, start.y), glm::vec2(0.f, 0.f));
40  buffer[1] = Eng3D::MeshData(glm::vec2(end.x, end.y), glm::vec2(1.f, 1.f));
41  upload();
42 }
43 
44 Eng3D::Quad::Quad(glm::vec3 c1, glm::vec3 c2, glm::vec3 c3, glm::vec3 c4)
45  : Eng3D::MeshStatic<6, 0, glm::vec3, glm::vec2>(Eng3D::MeshMode::TRIANGLES)
46 {
47  buffer[0] = Eng3D::MeshData(c1, glm::vec2(0.f, 0.f));
48  buffer[1] = Eng3D::MeshData(c2, glm::vec2(0.f, 1.f));
49  buffer[2] = Eng3D::MeshData(c3, glm::vec2(1.f, 1.f));
50  buffer[3] = Eng3D::MeshData(c3, glm::vec2(1.f, 1.f));
51  buffer[4] = Eng3D::MeshData(c4, glm::vec2(1.f, 0.f));
52  buffer[5] = Eng3D::MeshData(c1, glm::vec2(0.f, 0.f));
53  upload();
54 }
55 
57  : Eng3D::MeshStatic<6, 0, glm::vec2, glm::vec2>(Eng3D::MeshMode::TRIANGLES)
58 {
59  buffer[0] = Eng3D::MeshData(glm::vec2(-1.f, -1.f), glm::vec2(0.f, 0.f));
60  buffer[1] = Eng3D::MeshData(glm::vec2(-1.f, 1.f), glm::vec2(0.f, 1.f));
61  buffer[2] = Eng3D::MeshData(glm::vec2(1.f, -1.f), glm::vec2(1.f, 0.f));
62  buffer[3] = Eng3D::MeshData(glm::vec2(1.f, -1.f), glm::vec2(1.f, 0.f));
63  buffer[4] = Eng3D::MeshData(glm::vec2(-1.f, 1.f), glm::vec2(0.f, 1.f));
64  buffer[5] = Eng3D::MeshData(glm::vec2(1.f, 1.f), glm::vec2(1.f, 1.f));
65  upload();
66 }
67 
68 Eng3D::Square::Square(float start_x, float start_y, float end_x, float end_y)
69  : Eng3D::MeshStatic<6, 0, glm::vec2, glm::vec2>(Eng3D::MeshMode::TRIANGLES)
70 {
71  this->buffer[0] = Eng3D::MeshData(glm::vec2(start_x, start_y), glm::vec2(0.f, 0.f));
72  this->buffer[1] = Eng3D::MeshData(glm::vec2(end_x, start_y), glm::vec2(1.f, 0.f));
73  this->buffer[2] = Eng3D::MeshData(glm::vec2(start_x, end_y), glm::vec2(0.f, 1.f));
74  this->buffer[3] = Eng3D::MeshData(glm::vec2(start_x, end_y), glm::vec2(0.f, 1.f));
75  this->buffer[4] = Eng3D::MeshData(glm::vec2(end_x, start_y), glm::vec2(1.f, 0.f));
76  this->buffer[5] = Eng3D::MeshData(glm::vec2(end_x, end_y), glm::vec2(1.f, 1.f));
77  this->upload();
78 }
79 
81  : Eng3D::MeshStatic<6, 0, glm::vec2, glm::vec2>(Eng3D::MeshMode::TRIANGLES)
82 {
83  this->buffer[0] = Eng3D::MeshData(glm::vec2(pos.left, pos.top), glm::vec2(texcoord.left, texcoord.top));
84  this->buffer[1] = Eng3D::MeshData(glm::vec2(pos.right, pos.top), glm::vec2(texcoord.right, texcoord.top));
85  this->buffer[2] = Eng3D::MeshData(glm::vec2(pos.left, pos.bottom), glm::vec2(texcoord.left, texcoord.bottom));
86  this->buffer[3] = Eng3D::MeshData(glm::vec2(pos.left, pos.bottom), glm::vec2(texcoord.left, texcoord.bottom));
87  this->buffer[4] = Eng3D::MeshData(glm::vec2(pos.right, pos.top), glm::vec2(texcoord.right, texcoord.top));
88  this->buffer[5] = Eng3D::MeshData(glm::vec2(pos.right, pos.bottom), glm::vec2(texcoord.right, texcoord.bottom));
89  this->upload();
90 }
91 
92 Eng3D::TriangleList::TriangleList(std::vector<glm::vec3>& positions, std::vector<glm::vec2>& tex_coords)
93  : Eng3D::Mesh<glm::vec3, glm::vec2>(Eng3D::MeshMode::TRIANGLES)
94 {
95  buffer.resize(positions.size());
96  for(size_t i = 0; i < positions.size(); i++)
97  buffer[i] = Eng3D::MeshData<glm::vec3, glm::vec2>(positions[i], tex_coords[i]);
98  this->upload();
99 }
100 
101 Eng3D::Sphere::Sphere(float center_x, float center_y, float center_z, float _radius, int _resolution, bool cw_winding)
102  : Eng3D::Mesh<glm::vec3, glm::vec2>(Eng3D::MeshMode::TRIANGLES),
103  resolution{ _resolution },
104  radius{ _radius }
105 {
106  buffer.resize(6 * resolution * resolution);
107  glm::vec3 center_pos(center_x, center_y, center_z);
108  // Switch the order of the vertices if not clockwise winding
109  for(int latitude = 0; latitude < resolution; latitude++) {
110  for(int longitude = 0; longitude < resolution; longitude++) {
111  buffer[(longitude + latitude * resolution) * 6 + 0] = calc_pos(center_pos, longitude + 0, latitude + 0);
112  buffer[(longitude + latitude * resolution) * 6 + 1] = calc_pos(center_pos, longitude + cw_winding, latitude + !cw_winding);
113  buffer[(longitude + latitude * resolution) * 6 + 2] = calc_pos(center_pos, longitude + !cw_winding, latitude + cw_winding);
114  buffer[(longitude + latitude * resolution) * 6 + 3] = calc_pos(center_pos, longitude + !cw_winding, latitude + cw_winding);
115  buffer[(longitude + latitude * resolution) * 6 + 4] = calc_pos(center_pos, longitude + cw_winding, latitude + !cw_winding);
116  buffer[(longitude + latitude * resolution) * 6 + 5] = calc_pos(center_pos, longitude + 1, latitude + 1);
117  }
118  }
119  this->upload();
120 }
121 
122 Eng3D::MeshData<glm::vec3, glm::vec2> Eng3D::Sphere::calc_pos(glm::vec3 center_pos, float longitude, float latitude) {
123  const float longitude_ratio = ((float)longitude) / resolution;
124  const float longitude_rad = longitude_ratio * 2 * glm::pi<float>();
125  const float latitude_ratio = ((float)latitude) / resolution;
126  const float latitude_rad = latitude_ratio * glm::pi<float>();
127 
128  const float x = radius * glm::cos(longitude_rad) * glm::sin(latitude_rad);
129  const float y = radius * glm::sin(longitude_rad) * glm::sin(latitude_rad);
130  const float z = radius * glm::cos(latitude_rad);
131 
132  glm::vec3 pos{ x, y, z };
133  pos += center_pos;
134  glm::vec2 tex_coord(longitude_ratio, latitude_ratio);
135  return Eng3D::MeshData<glm::vec3, glm::vec2>(pos, tex_coord);
136 }
Sphere(float x, float y, float z, float radius, int resolution, bool cw_winding=true)
Definition: primitive.cpp:101
MeshMode
Definition: mesh.hpp:81
Line(glm::vec2 start, glm::vec2 end)
Definition: primitive.cpp:36
Packed model - packs both vertices and texcoords into the same buffer.
Definition: mesh.hpp:144
virtual void upload() const
Definition: mesh.hpp:161
std::vector< DataType > buffer
Definition: mesh.hpp:175
A static mesh with fixed number of elements.
Definition: mesh.hpp:192
std::array< DataType, vtc_size > buffer
Definition: mesh.hpp:223
Quad(glm::vec3 c1, glm::vec3 c2, glm::vec3 c3, glm::vec3 c4)
Definition: primitive.cpp:44
Square(float start_x, float start_y, float end_x, float end_y)
Definition: primitive.cpp:68
TriangleList(std::vector< glm::vec3 > &positions, std::vector< glm::vec2 > &tex_coords)
Definition: primitive.cpp:92