20 #include <glm/gtc/epsilon.hpp>
21 #include <glm/gtc/matrix_transform.hpp>
22 #include <glm/gtc/quaternion.hpp>
23 #include <glm/gtx/quaternion.hpp>
25 static bool equal(
float a,
float b) {
26 return glm::epsilonEqual(a, b, std::numeric_limits<float>::epsilon());
29 static bool equal(glm::vec2 a,
float b) {
33 static bool equal(glm::vec3 a,
float b) {
39 const static float fov = 45.0f;
49 const static glm::vec3
rightUnit(1.0f, 0.0f, 0.0f);
50 const static glm::vec3
upUnit(0.0f, 1.0f, 0.0f);
51 const static glm::vec3
dirUnit(0.0f, 0.0f, -1.0f);
68 pos = glm::vec3(0.0f, 0.0f, 0.0f);
69 rot = glm::vec2(glm::pi<float>(), 0.0f);
70 posSpeed = glm::vec3(0.0f, 0.0f, 0.0f);
74 view = glm::mat4(1.0f);
112 if ((x != 0) || (y != 0))
180 glm::quat quatY = glm::angleAxis(
rot.x, glm::vec3(0.0f, 1.0f, 0.0f));
181 glm::quat quatX = glm::angleAxis(
rot.y, glm::vec3(1.0f, 0.0f, 0.0f));
182 glm::quat quaternion = quatY * quatX;
184 glm::vec3 clampedSpeed;
187 if (glm::length(clampedSpeed) > (
maxSpeed * runFactor)) {
192 if (glm::length(clampedSpeed) >
maxSpeed) {
193 clampedSpeed = glm::normalize(clampedSpeed) *
maxSpeed;
197 pos += quaternion * clampedSpeed * dT;
199 glm::mat4 translate = glm::translate(glm::mat4(1.0f),
pos);
200 glm::mat4 rotate = glm::toMat4(quaternion);
201 view = glm::inverse(translate * rotate);
206 glm::vec3 at(0.0f, 0.0f, -1.0f);
207 glm::vec3 up(0.0f, -1.0f, 0.0f);
231 FrustumPlane() : normal(glm::vec3(0.0f, 0.0f, 0.0f)), d(0.0f) { }
232 void set(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) {
233 normal = glm::normalize(glm::cross(v3 - v2, v1 - v2));
234 d = -glm::dot(normal, v2);
236 float distance(glm::vec3 p) {
237 return d + glm::dot(normal, p);
264 glm::vec3(1.0f, 0.0f, 0.0f),
265 glm::vec3(0.0f, 1.0f, 0.0f),
266 glm::vec3(0.0f, 0.0f, 1.0f),
267 glm::vec3(1.0f, 1.0f, 0.0f),
268 glm::vec3(0.0f, 1.0f, 1.0f),
269 glm::vec3(1.0f, 0.0f, 1.0f)
283 glm::mat4 inverse = glm::inverse(combo);
284 frustumVertices[
NTL] = glm::vec3(1.0f, 1.0f, 0.0f);
285 frustumVertices[
NTR] = glm::vec3(-1.0f, 1.0f, 0.0f);
286 frustumVertices[
NBL] = glm::vec3(1.0f, -1.0f, 0.0f);
287 frustumVertices[
NBR] = glm::vec3(-1.0f, -1.0f, 0.0f);
288 frustumVertices[
FTL] = glm::vec3(1.0f, 1.0f, 1.0f);
289 frustumVertices[
FTR] = glm::vec3(-1.0f, 1.0f, 1.0f);
290 frustumVertices[
FBL] = glm::vec3(1.0f, -1.0f, 1.0f);
291 frustumVertices[
FBR] = glm::vec3(-1.0f, -1.0f, 1.0f);
292 for (
int i = 0; i < 8; i++) {
293 glm::vec4 t = inverse * glm::vec4(frustumVertices[i], 1.0f);
294 frustumVertices[i] = glm::vec3(t) / t.w;
295 frustumVertices[i].y *= -1.0f;
299 planes[
TOP].set(frustumVertices[
NTR], frustumVertices[
NTL], frustumVertices[
FTL]);
300 planes[
BOTTOM].set(frustumVertices[
NBL], frustumVertices[
NBR], frustumVertices[
FBR]);
301 planes[
LEFT].set(frustumVertices[NTL], frustumVertices[NBL], frustumVertices[
FBL]);
302 planes[
RIGHT].set(frustumVertices[NBR], frustumVertices[NTR], frustumVertices[FBR]);
303 planes[
NEAR].set(frustumVertices[NTL], frustumVertices[NTR], frustumVertices[NBR]);
304 planes[
FAR].set(frustumVertices[
FTR], frustumVertices[FTL], frustumVertices[FBL]);
306 std::vector<glm::vec3> verts;
309 verts.push_back(frustumVertices[NTL]);
310 verts.push_back(frustumVertices[NTR]);
311 verts.push_back(frustumVertices[NBR]);
312 verts.push_back(frustumVertices[NBL]);
315 verts.push_back(frustumVertices[FTR]);
316 verts.push_back(frustumVertices[FTL]);
317 verts.push_back(frustumVertices[FBL]);
318 verts.push_back(frustumVertices[FBR]);
321 verts.push_back(frustumVertices[NTR]);
322 verts.push_back(frustumVertices[NTL]);
323 verts.push_back(frustumVertices[FTL]);
324 verts.push_back(frustumVertices[FTR]);
327 verts.push_back(frustumVertices[NBL]);
328 verts.push_back(frustumVertices[NBR]);
329 verts.push_back(frustumVertices[FBR]);
330 verts.push_back(frustumVertices[FBL]);
333 verts.push_back(frustumVertices[NTL]);
334 verts.push_back(frustumVertices[NBL]);
335 verts.push_back(frustumVertices[FBL]);
336 verts.push_back(frustumVertices[FTL]);
339 verts.push_back(frustumVertices[NBR]);
340 verts.push_back(frustumVertices[NTR]);
341 verts.push_back(frustumVertices[FTR]);
342 verts.push_back(frustumVertices[FBR]);
347 std::vector<glm::vec3> cols;
350 cols.push_back(glm::vec3(1.0f, 1.0f, 1.0f));
355 if (colorBuffer.
getSize() == 0) {
357 for (
int i = 0; i < 6; i++) {
358 for (
int j = 0; j < 4; j++) {
359 cols.push_back(frustumColors[i]);
365 if (indexBuffer.
getSize() == 0) {
366 std::vector<unsigned short> inds;
367 for (
int i = 0; i < 6; i++) {
368 inds.push_back(4 * i);
369 inds.push_back((4 * i) + 1);
370 inds.push_back((4 * i) + 2);
371 inds.push_back((4 * i) + 3);
372 inds.push_back((4 * i) + 2);
373 inds.push_back(4 * i);
380 for (
int i = 0; i < 6; i++) {
382 for (
int c = 0; (c < 8) && ((in == 0) || (out == 0)); c++) {
383 if (planes[i].distance(b.
getCorner(c)) < 0)
399 Shader::drawGL(vertexPointBuffer, colorPointBuffer, MVP, GL_POINTS);
static const float controllerViewFactor
static bool boxInFrustum(BoundingBox b)
static ShaderBuffer vertexPointBuffer
static const float rotationAngleClamp
void bufferData(int elem, int size, void *data)
static float rotationDeltaY
static FrustumPlane planes[6]
static ShaderBuffer colorPointBuffer
static void displayFrustum(glm::mat4 MVP)
static glm::mat4 projection
OpenGL Shader Implementation.
static const float runFactor
static glm::vec2 rotSpeed
static glm::vec3 posSpeed
glm::vec3 getCorner(int i)
static void listenAt(glm::vec3 pos, glm::vec3 at, glm::vec3 up)
static glm::vec3 getPosition()
static const glm::vec3 dirUnit(0.0f, 0.0f,-1.0f)
static const glm::vec3 rightUnit(1.0f, 0.0f, 0.0f)
static const float controllerDeadZone
static const glm::vec3 upUnit(0.0f, 1.0f, 0.0f)
static const float farDist
static void handleMouseMotion(int x, int y)
static void calculateFrustumPlanes()
static const float maxSpeed
static void drawGL(ShaderBuffer &vertices, ShaderBuffer &uvs, glm::vec4 color, unsigned int texture, TextureStorage store=TextureStorage::SYSTEM, unsigned int mode=GL_TRIANGLES, ShaderTexture *target=nullptr, Shader &shader=textShader)
static const float nearDist
static const float rotationAngleVertMax
static bool updateViewFrustum
Runtime Configuration Storage.
static ShaderBuffer vertexBuffer
static void handleControllerAxis(float value, KeyboardButton axis)
static ShaderBuffer colorBuffer
static void handleAction(ActionEvents action, bool isFinished)
static ShaderBuffer indexBuffer
static glm::vec3 frustumVertices[8]
static bool equal(float a, float b)
static glm::vec3 frustumColors[6]
static float rotationDeltaX
static float getLastFrameTime()
IMGUI_API bool Begin(const char *name="Debug", bool *p_opened=NULL, const ImVec2 &initial_size=ImVec2(0, 0), float bg_alpha=-1.0f, ImGuiWindowFlags flags=0)
static void set2DState(bool on, bool depth=true)
static void setSize(glm::i32vec2 s)
static glm::i32vec2 getSize()