galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Ellipse.cpp
Go to the documentation of this file.
1
7
8#include <glad/glad.h>
9#include <glm/gtc/constants.hpp>
10#include <glm/trigonometric.hpp>
11
12#include "galaxy/meta/Memory.hpp"
13
14#include "Ellipse.hpp"
15
16namespace galaxy
17{
18 namespace graphics
19 {
21 : m_fragments {12.0f}
22 , m_radii {10.0f, 5.0f}
23
24 {
25 m_mode = GL_LINE_LOOP;
26 }
27
29 : Shape {std::move(e)}
30 {
31 this->m_radii = std::move(e.m_radii);
32 this->m_fragments = e.m_fragments;
33 }
34
36 {
37 if (this != &e)
38 {
39 Shape::operator=(std::move(e));
40
41 this->m_radii = std::move(e.m_radii);
42 this->m_fragments = e.m_fragments;
43 }
44
45 return *this;
46 }
47
49 {
50 }
51
52 void Ellipse::create(const float fragments, const glm::vec2& radii)
53 {
55 m_radii = radii;
56
57 // Thanks to https://stackoverflow.com/a/34735255
58 // For help with maths.
59
60 const auto theta = 2.0f * glm::pi<float>() / m_fragments;
61 const auto cosine = std::cosf(theta);
62 const auto sine = std::sinf(theta);
63
64 auto temp = 0.0f;
65 auto x = 1.0f;
66 auto y = 0.0f;
67
68 meta::vector<unsigned int> indices;
69 meta::vector<graphics::Vertex> vertices;
70
71 auto count = 0u;
72 for (auto i = 0; i < std::floor(m_fragments); i++)
73 {
74 graphics::Vertex vertex;
75 vertex.m_pos.x = (x * m_radii.x) + m_radii.x;
76 vertex.m_pos.y = (y * m_radii.y) + m_radii.y;
77
78 vertices.emplace_back(vertex);
79 indices.push_back(count);
80 count++;
81
82 temp = x;
83 x = cosine * x - sine * y;
84 y = sine * temp + cosine * y;
85 }
86
87 m_width = m_radii.x * 2.0f;
88 m_height = m_radii.y * 2.0f;
90 }
91
92 float Ellipse::fragments() const
93 {
94 return m_fragments;
95 }
96
97 const glm::vec2& Ellipse::radii() const
98 {
99 return m_radii;
100 }
101 } // namespace graphics
102} // namespace galaxy
thread_local const float vertices[]
Video.cpp galaxy.
Definition Video.cpp:19
thread_local const unsigned int indices[]
Definition Video.cpp:21
An elliptical circle.
Definition Ellipse.hpp:21
Ellipse & operator=(Ellipse &&)
Move assignment operator.
Definition Ellipse.cpp:35
float fragments() const
Get fragments.
Definition Ellipse.cpp:92
glm::vec2 m_radii
Radius of ellipse.
Definition Ellipse.hpp:76
virtual ~Ellipse()
Destructor.
Definition Ellipse.cpp:48
Ellipse()
Constructor.
Definition Ellipse.cpp:20
const glm::vec2 & radii() const
Get radii.
Definition Ellipse.cpp:97
float m_fragments
Number of points used to create shape.
Definition Ellipse.hpp:71
void create(const float fragments, const glm::vec2 &radii)
Create the ellipse.
Definition Ellipse.cpp:52
A generic 2D shape.
Definition Shape.hpp:22
VertexArray m_vao
Vertex Array Object.
Definition Shape.hpp:105
Shape & operator=(Shape &&)
Move assignment operator.
Definition Shape.cpp:32
unsigned int m_mode
Type to render i.e. GL_LINES, GL_TRIANGLES, etc.
Definition Shape.hpp:90
float m_height
Cached height.
Definition Shape.hpp:100
float m_width
Cached width.
Definition Shape.hpp:95
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex array object.
Animated.cpp galaxy.
Definition Animated.cpp:16
STL namespace.
Represents a single vertex point.
Definition Vertex.hpp:25
glm::vec2 m_pos
Position..
Definition Vertex.hpp:29