From 90ee10405dbd50fc778ab9d5483effdc5ce489e1 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Wed, 18 Jan 2017 23:01:00 +0000 Subject: [PATCH] added rect shape with corrected uv interpolation --- engine/app.cpp | 15 ++++++++------- engine/shape.cpp | 35 +++++++++++++++++++++++++++++++++++ engine/shape.hpp | 10 ++++++++-- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/engine/app.cpp b/engine/app.cpp index 1bcfffe..68f0302 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -16,26 +16,27 @@ void App::init() "uniform mat4 mvp;" "in vec4 pos;" "in vec2 uvs;" - "out vec2 uv;" + "out vec3 uv;" "void main(){" - " gl_Position = mvp * pos;" - " uv = uvs;" + " uv = vec3(uvs, pos.w);" + " gl_Position = mvp * vec4(pos.xyz, 1.f);" "}"; static const char* shader_f = "#version 150\n" "uniform sampler2D tex;" - "in vec2 uv;" + "in vec3 uv;" "out vec4 frag;" "void main(){" - " frag = texture(tex, uv);" + //" frag = texture(tex, uv.xy/uv.z);" + " frag = texture(tex, uv.xy);" "}"; static const char* shader_uv_f = "#version 150\n" "uniform sampler2D tex;" - "in vec2 uv;" + "in vec3 uv;" "out vec4 frag;" "void main(){" - " frag = vec4(uv,0,1);" + " frag = vec4(uv.xy,0,1);" "}"; static const char* shader_color_v = "#version 150\n" diff --git a/engine/shape.cpp b/engine/shape.cpp index c89b210..2c4d1c9 100644 --- a/engine/shape.cpp +++ b/engine/shape.cpp @@ -43,6 +43,29 @@ void Shape::draw_stroke() const glDrawElements(GL_LINES, count[1], GL_UNSIGNED_SHORT, ioff[1]); } +bool Rect::create(float w, float h) +{ + static GLushort idx[6 + 8] { + 0, 1, 2, + 0, 2, 3, + 0, 1, + 1, 2, + 2, 3, + 3, 0, + }; + static vertex_t vertices[4]; + vertices[0] = { { -w/2, -h/2, 0, 1 }, { 0, 0 } }; // A + vertices[1] = { { -w/2, h/2, 0, 1 }, { 0, 1 } }; // B + vertices[2] = { { w/2, 0, 0, 1 }, { 1, 1 } }; // C + vertices[3] = { { w/2, -h/2, 0, 1 }, { 1, 0 } }; // D + count[0] = 6; + count[1] = 8; + ioff[0] = (GLvoid*)0; + ioff[1] = (GLvoid*)(count[0] * sizeof(GLushort)); + adjust_quad_uvs(vertices[0], vertices[1], vertices[2], vertices[3]); + return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices)); +} + void Plane::create_impl(float w, float h, int div, GLushort *idx, Shape::vertex_t *vertices) { count[0] = div * div * 6; @@ -166,6 +189,18 @@ void Circle::create_impl(float radius_out, float radius_in, int div, GLushort* i *pidx2++ = i*2+1; // B *pidx2++ = ((i+1)*2+1) % (div*2); // C } + + //if (radius_in != 0 && map == kUVMapping::Tube) + //{ + // for (int i = 0; i < div; i++) + // { + // int a = i*2; // A + // int b = i*2+1; // B + // int c = ((i+1)*2+1) % (div*2); // C + // int d = ((i+1)*2) % (div*2); // D + // adjust_quad_uvs(vertices[a], vertices[b], vertices[c], vertices[d]); + // } + //} } void Rounded::create_impl(float w, float h, float r, int div, GLushort* idx, GLushort* idx_tmp, Shape::vertex_t* vertices) diff --git a/engine/shape.hpp b/engine/shape.hpp index 699e4cf..47a940f 100644 --- a/engine/shape.hpp +++ b/engine/shape.hpp @@ -24,14 +24,15 @@ protected: { static float d[4]; static vertex_t* v[4]; - v[0] = &va; v[1] = &vb; v[2] = &vc; v[3] = &vc; + v[0] = &va; v[1] = &vb; v[2] = &vc; v[3] = &vd; 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; + v[i]->uvs = glm::vec2(v[i]->uvs) * q; + v[i]->pos.w = q; } } }; @@ -50,6 +51,11 @@ public: } }; +class Rect : public Shape +{ + bool create(float w, float h); +}; + class Circle : public Shape { public: