jGL
Loading...
Searching...
No Matches
primitive.h
Go to the documentation of this file.
1#ifndef PRIMITIVE
2#define PRIMITIVE
3
4#include <glm/glm.hpp>
5#include <array>
6#include <iostream>
7
8namespace jGL
9{
10
15 typedef uint64_t RenderPriority;
16
17 struct Primitive
18 {
19 virtual ~Primitive() = default;
20 };
21
27 {
28 TexturedParticle() = default;
29
30 TexturedParticle(glm::vec4 p, glm::vec4 c, glm::vec4 t)
31 : state(p), colour(c), texCoord(t)
32 {}
33
34 glm::vec4 state;
35 glm::vec4 colour;
36 glm::vec4 texCoord;
37 };
38
43 struct Transform : public Primitive
44 {
45
46 Transform(double x, double y, double t, double s)
47 : x(x), y(y), theta(t), scaleX(s), scaleY(s)
48 {}
49
50 Transform(double x, double y, double t, double sx, double sy)
51 : x(x), y(y), theta(t), scaleX(sx), scaleY(sy)
52 {}
53
55 : x(0.0), y(0.0), theta(0.0), scaleX(0.0), scaleY(0.0)
56 {}
57
58 double x;
59 double y;
60 double theta;
61 double scaleX;
62 double scaleY;
63 };
64
76 struct TextureRegion : public Primitive
77 {
78
79 TextureRegion(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
80 : tx(x), ty(y), lx(w), ly(h)
81 {}
82
84 : tx(0), ty(0), lx(0), ly(0)
85 {}
86
87 uint16_t tx;
88 uint16_t ty;
89 uint16_t lx;
90 uint16_t ly;
91 };
92
106 {
107
108 NormalisedTextureRegion(float x, float y, float w, float h)
109 : tx(x), ty(y), lx(w), ly(h)
110 {}
111
113 : tx(0), ty(0), lx(0), ly(0)
114 {}
115
116 float tx;
117 float ty;
118 float lx;
119 float ly;
120 };
121
126 template <class T>
128 {
130 : vertices({glm::tvec2<T>(0),glm::tvec2<T>(0),glm::tvec2<T>(0),glm::tvec2<T>(0)})
131 {}
132
133 BoundingBox(const std::array<glm::tvec2<T>, 4> & v)
134 : vertices(v)
135 {}
136
141 std::array<glm::tvec2<T>, 4> vertices;
142
152 bool in(T x, T y)
153 {
154 auto handedness = [](float x, float y, float x0, float y0, float x1, float y1)
155 {
156 return (y1-y0)*x+(x0-x1)*y+(x1*y0-y1*x0) >= 0;
157 };
158 float px = static_cast<float>(x);
159 float py = static_cast<float>(y);
160 bool h = handedness(px, py, vertices[0].x, vertices[0].y, vertices[1].x, vertices[1].y);
161
162 return (handedness(px, py, vertices[1].x, vertices[1].y, vertices[2].x, vertices[2].y) == h &&
163 handedness(px, py, vertices[2].x, vertices[2].y, vertices[3].x, vertices[3].y) == h &&
164 handedness(px, py, vertices[3].x, vertices[3].y, vertices[0].x, vertices[0].y) == h);
165 }
166 };
167
172 class WorldBoundingBox : public BoundingBox<float>
173 {
174 public:
176 : BoundingBox<float>({glm::tvec2<float>(0),glm::tvec2<float>(0),glm::tvec2<float>(0),glm::tvec2<float>(0)})
177 {}
178
179 WorldBoundingBox(const std::array<glm::tvec2<float>, 4> & v)
180 : BoundingBox<float>(v)
181 {}
182
189 {
190 float lowest = vertices[0].y; uint8_t lowestVertex = 0;
191 glm::vec2 centre = vertices[0];
192 for (uint8_t i = 1; i < vertices.size(); i++)
193 {
194 if (vertices[i].y < lowest) { lowestVertex = i; lowest = vertices[i].y; }
195 centre += vertices[i];
196 }
197 centre *= 0.25;
198
199 uint8_t next = (lowestVertex + 1) % vertices.size();
200 glm::vec2 r = vertices[next] - vertices[lowestVertex];
201 float d = std::sqrt(r.x*r.x+r.y*r.y);
202 glm::vec2 n = r / d;
203 float theta = 0.0;
204 if (n.y != 0.0)
205 {
206 theta = std::atan2(n.y, n.x);
207 }
208
209 r = vertices[(lowestVertex - 1) % vertices.size()] - vertices[lowestVertex];
210
211 return Transform(centre.x, centre.y, -theta, d, std::sqrt(r.x*r.x+r.y*r.y));
212 }
213 };
214
220}
221
229std::ostream & operator <<(std::ostream & out, const jGL::ScreenBoundingBox & sbb);
230
238std::ostream & operator <<(std::ostream & out, const jGL::WorldBoundingBox & wbb);
239#endif /* PRIMITIVE */
A world space bounding box.
Definition primitive.h:173
WorldBoundingBox(const std::array< glm::tvec2< float >, 4 > &v)
Definition primitive.h:179
WorldBoundingBox()
Definition primitive.h:175
Transform toTransform()
Returns a Transform for ShapeRenderer.
Definition primitive.h:188
A drawable graphic.
Definition id.h:10
uint64_t RenderPriority
Higher priority is drawn last.
Definition primitive.h:15
BoundingBox< uint16_t > ScreenBoundingBox
A screen space bounding box.
Definition primitive.h:219
std::ostream & operator<<(std::ostream &out, const jGL::ScreenBoundingBox &sbb)
Prints each vertex on a new line.
Definition primitive.cpp:3
A bounding box template.
Definition primitive.h:128
std::array< glm::tvec2< T >, 4 > vertices
Vertices of bounding box.
Definition primitive.h:141
BoundingBox(const std::array< glm::tvec2< T >, 4 > &v)
Definition primitive.h:133
BoundingBox()
Definition primitive.h:129
bool in(T x, T y)
If the point (x, y) is in the box.
Definition primitive.h:152
Rectangular region of a texture, normalised by the textures dimensions.
Definition primitive.h:106
float lx
Definition primitive.h:118
float ty
Definition primitive.h:117
NormalisedTextureRegion(float x, float y, float w, float h)
Definition primitive.h:108
float tx
Definition primitive.h:116
NormalisedTextureRegion()
Definition primitive.h:112
float ly
Definition primitive.h:119
Definition primitive.h:18
virtual ~Primitive()=default
Rectangular region of a texture in pixels.
Definition primitive.h:77
uint16_t ly
Definition primitive.h:90
TextureRegion(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
Definition primitive.h:79
uint16_t tx
Definition primitive.h:87
uint16_t lx
Definition primitive.h:89
uint16_t ty
Definition primitive.h:88
TextureRegion()
Definition primitive.h:83
Particle with a texture, with a position, orientation, and scale, colour, and texture region.
Definition primitive.h:27
glm::vec4 state
Definition primitive.h:34
glm::vec4 colour
Definition primitive.h:35
glm::vec4 texCoord
Definition primitive.h:36
TexturedParticle(glm::vec4 p, glm::vec4 c, glm::vec4 t)
Definition primitive.h:30
TexturedParticle()=default
Position, rotation, and scale.
Definition primitive.h:44
Transform(double x, double y, double t, double s)
Definition primitive.h:46
Transform(double x, double y, double t, double sx, double sy)
Definition primitive.h:50
double scaleY
Definition primitive.h:62
double theta
Definition primitive.h:60
Transform()
Definition primitive.h:54
double y
Definition primitive.h:59
double scaleX
Definition primitive.h:61
double x
Definition primitive.h:58