From 5f6bcda21109810b7dfd4578128983d7f711b0ca Mon Sep 17 00:00:00 2001 From: Omar Mohamed Ali Mudhir Date: Wed, 18 Jan 2017 19:42:34 +0000 Subject: [PATCH] added intersection and uvs correction functions --- engine/shape.hpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/engine/shape.hpp b/engine/shape.hpp index 81bf544..699e4cf 100644 --- a/engine/shape.hpp +++ b/engine/shape.hpp @@ -12,6 +12,28 @@ public: bool create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize); void draw_fill() const; void draw_stroke() const; +protected: + glm::vec2 quad_mid_point(glm::vec2 a, glm::vec2 b, glm::vec2 c, glm::vec2 d) + { + float den = (a.x-c.x)*(b.y-d.y) - (a.y-c.y)*(b.x-d.x); + float numx = (a.x*c.y-a.y*c.x)*(b.x-d.x) - (a.x-c.x)*(b.x*d.y-b.y*d.x); + float numy = (a.x*c.y-a.y*c.x)*(b.y-d.y) - (a.y-c.y)*(b.x*d.y-b.y*d.x); + return glm::vec2(numx, numy) / den; + } + void adjust_quad_uvs(vertex_t& va, vertex_t& vb, vertex_t& vc, vertex_t& vd) + { + static float d[4]; + static vertex_t* v[4]; + v[0] = &va; v[1] = &vb; v[2] = &vc; v[3] = &vc; + auto mid = quad_mid_point(va.pos, vb.pos, vc.pos, vd.pos); + for (int i = 0; i < 4; i++) + d[i] = glm::distance(glm::vec2(v[i]->pos), mid); + for (int i = 0; i < 4; i++) + { + float q = (d[i] + d[(i + 2) % 4]) / d[(i + 2) % 4]; + v[i]->uvs = glm::vec4(glm::vec2(v[i]->uvs), 0, 1) * q; + } + } }; class Plane : public Shape