3D City v-1.0
Computer Graphics Project
core.h
Go to the documentation of this file.
1#pragma once
2
3#define _USE_MATH_DEFINES
4#include "GL/glew.h"
5#include "GLFW/glfw3.h"
6#include "glm/glm.hpp"
7#include "maths.h"
8
9#include <filesystem>
10namespace fs = std::filesystem;
11
12#ifdef _WIN32
13const std::string pathDelemeter(std::string("\\") + "\\");
14#else
15const std::string pathDelemeter("/");
16#endif // _WIN32
17
18#ifdef _MSC_VER
19
20#ifdef _DEBUG
21#define DEBUG_BREAK __debugbreak()
22#define MDEBUG
23#else
24#define DEBUG_BREAK
25#endif
26
27#else
28
29#ifndef NDEBUG
30#define DEBUG_BREAK __builtin_trap()
31#define MDEBUG
32#else
33#define DEBUG_BREAK
34#endif
35
36#endif
37
38#ifdef MDEBUG
39#define GLcall_P(y, x) \
40 GLClearError(); \
41 y = x; \
42 ASSERT(GLLogCall(#x, __FILE__, __LINE__))
43#define GLcall(x) \
44 GLClearError(); \
45 x; \
46 ASSERT(GLLogCall(#x, __FILE__, __LINE__))
47#else
48#define GLcall(x) x
49#define GLcall_P(y, x) y = x
50#endif
51
52#define PHONG_SHADING
53// #define MULTITHREADED
54#define USEBARYCENTRIC
55
56#define vec3 vec3
57
58inline void GLClearError() {
59 while (glGetError() != GL_NO_ERROR)
60 ;
61}
62
63inline bool GLLogCall(const char *function, const char *file, int line) {
64 while (GLenum errorcode = glGetError()) {
65 std::string error;
66 switch (errorcode) {
67 case GL_INVALID_ENUM:
68 error = "INVALID_ENUM";
69 break;
70 case GL_INVALID_VALUE:
71 error = "INVALID_VALUE";
72 break;
73 case GL_INVALID_OPERATION:
74 error = "INVALID_OPERATION";
75 break;
76 case GL_STACK_OVERFLOW:
77 error = "STACK_OVERFLOW";
78 break;
79 case GL_STACK_UNDERFLOW:
80 error = "STACK_UNDERFLOW";
81 break;
82 case GL_OUT_OF_MEMORY:
83 error = "OUT_OF_MEMORY";
84 break;
85 case GL_INVALID_FRAMEBUFFER_OPERATION:
86 error = "INVALID_FRAMEBUFFER_OPERATION";
87 break;
88 }
89 std::cout << "[Opengl Error] ( Error code :" << errorcode << " : " << error << " )" << function << " " << file << " : " << line << std::endl;
90 return false;
91 }
92 return true;
93}
94
95#define ASSERT(x) \
96 if (!(x)) { \
97 DEBUG_BREAK; \
98 assert(false); \
99 }
100
101typedef unsigned int uint;
102inline vec3 operator*(glm::mat4 mat, vec3 vec) {
103 glm::vec4 temp(vec.x, vec.y, vec.z, 1);
104 temp = mat * temp;
105 return vec3(temp.x, temp.y, temp.z);
106}
107
108struct Vertex {
112
113 Vertex(const vec4 &pos, const vec3 &nor = vec3(0), const vec2 &texcoord = vec2(0)) : position(pos), normal(nor), texCoord(texcoord) {}
115
116 Vertex operator*(const float &f) const {
117#ifdef PHONG_SHADING
118 return Vertex(position * f, normal * f, texCoord * f);
119#else
120 return Vertex(position * f, normal, texCoord * f);
121#endif
122 }
123 Vertex operator+(const Vertex &f) const {
124#ifdef PHONG_SHADING
125 return Vertex(f.position + position, f.normal + normal, f.texCoord + texCoord);
126#else
128#endif
129 }
130 Vertex operator-(const Vertex &f) const {
131#ifdef PHONG_SHADING
132 return Vertex(position - f.position, normal - f.normal, texCoord - f.texCoord);
133#else
135#endif
136 }
138 position = per * position;
139 texCoord /= (fabs(position.w) < epsilon ? epsilon : position.w);
140 normal /= (fabs(position.w) < epsilon ? epsilon : position.w);
141 position = vec4(position.x / (fabs(position.w) < epsilon ? epsilon : position.w), position.y / (fabs(position.w) < epsilon ? epsilon : position.w), 1 / position.z, 1);
142 return *this;
143 }
144
145 inline static Vertex perspectiveMul(Vertex in, const mat4f &per) {
146 return in.perspectiveMul(per);
147 }
148
149 void operator*=(const float &f) {
150 position *= f;
151 normal *= f;
152 texCoord *= f;
153 }
154
155 void operator/=(const float &f) {
156 position /= f;
157 normal /= f;
158 texCoord /= f;
159 }
160};
161
165struct color {
166 uint32_t col;
167 color() : color(0) {}
168 explicit color(uint8_t r) : color(r, r, r) {}
169 color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) : col((a << 24u) | (b << 16u) | (g << 8u) | r) {}
170 explicit color(const vec3 &in) : color(fabs(in.x) * 255, fabs(in.y) * 255, fabs(in.z) * 255) {}
171
172 vec4 getcolor() const {
173 auto temp = (uint8_t *)&col;
174 return vec4(temp[0], temp[1], temp[2], temp[3]) / 255;
175 }
176
178 auto temp = (uint8_t *)&col;
179 return vec3(temp[0], temp[1], temp[2]) / 255;
180 }
181
182 [[nodiscard]] inline uint8_t r() const {
183 auto temp = (uint8_t *)&col;
184 return temp[0];
185 }
186 [[nodiscard]] inline uint8_t g() const {
187 auto temp = (uint8_t *)&col;
188 return temp[1];
189 }
190 [[nodiscard]] inline uint8_t b() const {
191 auto temp = (uint8_t *)&col;
192 return temp[2];
193 }
194 [[nodiscard]] inline uint8_t a() const {
195 auto temp = (uint8_t *)&col;
196 return temp[3];
197 }
198
199 [[nodiscard]] inline uint8_t &r() {
200 auto temp = (uint8_t *)&col;
201 return temp[0];
202 }
203 [[nodiscard]] inline uint8_t &g() {
204 auto temp = (uint8_t *)&col;
205 return temp[1];
206 }
207 [[nodiscard]] inline uint8_t &b() {
208 auto temp = (uint8_t *)&col;
209 return temp[2];
210 }
211 [[nodiscard]] inline uint8_t &a() {
212 auto temp = (uint8_t *)&col;
213 return temp[3];
214 }
215
216 color operator*(const vec3 &in) const {
217 auto temp = (uint8_t *)&col;
218 unsigned short r = (unsigned short)temp[0] * in.x;
219 unsigned short g = (unsigned short)temp[1] * in.y;
220 unsigned short b = (unsigned short)temp[2] * in.z;
221 return color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b, a());
222 }
223
224 void operator*=(const vec3 &in) {
225 auto temp = (uint8_t *)&col;
226 unsigned short r = (unsigned short)temp[0] * in.x;
227 unsigned short g = (unsigned short)temp[1] * in.y;
228 unsigned short b = (unsigned short)temp[2] * in.z;
229 col = ((a() << 24u) | (b > 255 ? 255 : b << 16u) | (g > 255 ? 255 : g << 8u) | r > 255 ? 255 : r);
230 }
231
232 color operator*(const color &in) const {
233 auto temp = (uint8_t *)&col;
234 auto temp1 = (uint8_t *)&in.col;
235 auto r = ((unsigned short)temp[0] * temp1[0]) / 255u;
236 auto g = ((unsigned short)temp[1] * temp1[1]) / 255u;
237 auto b = ((unsigned short)temp[2] * temp1[2]) / 255u;
238 auto a = ((unsigned short)temp[3] * temp1[3]) / 255u;
239 color ret(r, g, b, a);
240 return std::move(ret);
241 }
242
243 void operator*=(const color &in) {
244 auto temp = (uint8_t *)&col;
245 auto temp1 = (uint8_t *)&in.col;
246 temp[0] = ((unsigned short)temp[0] * temp1[0]) / 255u;
247 temp[1] = ((unsigned short)temp[1] * temp1[1]) / 255u;
248 temp[2] = ((unsigned short)temp[2] * temp1[2]) / 255u;
249 temp[3] = ((unsigned short)temp[3] * temp1[3]) / 255u;
250 }
251
252 color operator+(const color &in) const {
253 auto temp = (uint8_t *)&col;
254 auto temp1 = (uint8_t *)&in.col;
255 unsigned short r = (unsigned short)temp[0] + temp1[0];
256 unsigned short g = (unsigned short)temp[1] + temp1[1];
257 unsigned short b = (unsigned short)temp[2] + temp1[2];
258 unsigned short a = (unsigned short)temp[3] + temp1[3];
259 return color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b, a > 255 ? 255 : a);
260 }
261
262 void operator+=(const color &in) {
263 auto temp = (uint8_t *)&col;
264 auto temp1 = (uint8_t *)&in.col;
265 unsigned short r = (unsigned short)temp[0] + temp1[0];
266 unsigned short g = (unsigned short)temp[1] + temp1[1];
267 unsigned short b = (unsigned short)temp[2] + temp1[2];
268 unsigned short a = (unsigned short)temp[3] + temp1[3];
269 temp[0] = r > 255 ? 255 : r;
270 temp[1] = g > 255 ? 255 : g;
271 temp[2] = b > 255 ? 255 : b;
272 temp[3] = a > 255 ? 255 : a;
273 }
274
275 color operator*(const float &in) const {
276 auto temp = (uint8_t *)&col;
277 unsigned short r = (unsigned short)temp[0] * in;
278 unsigned short g = (unsigned short)temp[1] * in;
279 unsigned short b = (unsigned short)temp[2] * in;
280 return color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
281 }
282
283 void operator*=(const float &in) {
284 auto temp = (uint8_t *)&col;
285 unsigned short r = (unsigned short)temp[0] * in;
286 unsigned short g = (unsigned short)temp[1] * in;
287 unsigned short b = (unsigned short)temp[2] * in;
288 temp[0] = r > 255 ? 255 : r;
289 temp[1] = g > 255 ? 255 : g;
290 temp[2] = b > 255 ? 255 : b;
291 }
292
293 bool operator==(const color &in) { return col == in.col; }
294};
295
299static std::string searchRes() {
300 auto currentPath = fs::current_path();
301
302 auto orgiPath = currentPath;
303 bool resPathFound = false, appPathFound = false;
304
305 while (true) {
306 if (!resPathFound && !appPathFound && currentPath.has_parent_path()) {
307 fs::current_path(currentPath.parent_path());
308 currentPath = fs::current_path();
309 } else if (appPathFound) {
310 try {
311#ifdef _WIN32
312 auto a = currentPath.string().append("\\res");
313#else
314 auto a = currentPath.string().append("/res");
315#endif
316 fs::current_path(a);
317 fs::current_path(orgiPath);
318 return a;
319 } catch (const std::exception &e) {
320 std::cout << e.what() << "\n \"res\" folder not found inside \"Application\" folder \n";
321 return std::string();
322 }
323 } else
324 break;
325
326 //std::cout << "currently in path: " << currentPath.string() << std::endl;
327
328 for (auto &dirs : fs::directory_iterator(currentPath)) {
329 if (dirs.is_directory() && dirs.path().filename().string() == "Application") {
330 appPathFound = true;
331 break;
332 }
333 }
334 }
335
336 std::cout << "folder \"Application\" not found in 4 back iterations" << std::endl;
338 return std::string();
339}
340
341struct dirLight {
345
346 dirLight(const vec3 &dir = vec3::normalize(vec3(1)), const float &intensity = 1, const color &diffcol = color(255)) : direction(dir), col(diffcol), intensity(intensity) {}
347};
A general matrix class for any size of matrix.
Definition: maths.h:447
bool GLLogCall(const char *function, const char *file, int line)
Definition: core.h:63
const std::string pathDelemeter("/")
#define DEBUG_BREAK
Definition: core.h:30
vec3 operator*(glm::mat4 mat, vec3 vec)
Definition: core.h:102
void GLClearError()
Definition: core.h:58
unsigned int uint
Definition: core.h:101
#define vec3
Definition: core.h:56
vec4_T< float > vec4
Definition: maths.h:439
vec2_T< float > vec2
Definition: maths.h:153
constexpr float epsilon
Definition: maths.h:11
Definition: core.h:108
void operator*=(const float &f)
Definition: core.h:149
static Vertex perspectiveMul(Vertex in, const mat4f &per)
Definition: core.h:145
Vertex perspectiveMul(const mat4f &per)
Definition: core.h:137
Vertex(const vec4 &pos, const vec3 &nor=vec3(0), const vec2 &texcoord=vec2(0))
Definition: core.h:113
vec2 texCoord
Definition: core.h:111
void operator/=(const float &f)
Definition: core.h:155
Vertex()
Definition: core.h:114
Vertex operator+(const Vertex &f) const
Definition: core.h:123
Vertex operator*(const float &f) const
Definition: core.h:116
Vertex operator-(const Vertex &f) const
Definition: core.h:130
vec3 normal
Definition: core.h:110
vec4 position
Definition: core.h:109
class that holds color with 8 bits per channel with 4 channels rgba.
Definition: core.h:165
color operator*(const vec3 &in) const
Definition: core.h:216
uint8_t b() const
Definition: core.h:190
uint8_t & g()
Definition: core.h:203
void operator*=(const color &in)
Definition: core.h:243
color operator*(const color &in) const
Definition: core.h:232
bool operator==(const color &in)
Definition: core.h:293
uint8_t a() const
Definition: core.h:194
color(uint8_t r)
Definition: core.h:168
uint8_t & a()
Definition: core.h:211
uint8_t & b()
Definition: core.h:207
uint8_t & r()
Definition: core.h:199
color()
Definition: core.h:167
void operator+=(const color &in)
Definition: core.h:262
uint8_t r() const
Definition: core.h:182
uint8_t g() const
Definition: core.h:186
vec3 getcolorVec3() const
Definition: core.h:177
color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Definition: core.h:169
color operator+(const color &in) const
Definition: core.h:252
color operator*(const float &in) const
Definition: core.h:275
uint32_t col
Definition: core.h:166
void operator*=(const float &in)
Definition: core.h:283
void operator*=(const vec3 &in)
Definition: core.h:224
vec4 getcolor() const
Definition: core.h:172
color(const vec3 &in)
Definition: core.h:170
Definition: core.h:341
color col
Definition: core.h:344
dirLight(const vec3 &dir=vec3::normalize(vec3(1)), const float &intensity=1, const color &diffcol=color(255))
Definition: core.h:346
vec3 direction
Definition: core.h:342
float intensity
Definition: core.h:343
T z
Definition: maths.h:162
T x
Definition: maths.h:162
vec3_T< float > normalize()
Definition: maths.h:278
T y
Definition: maths.h:162
T z
Definition: maths.h:314
T w
Definition: maths.h:314
T y
Definition: maths.h:314
T x
Definition: maths.h:314