added rounded rectangle, first iteration works but still needs improved corners

This commit is contained in:
Omar Mohamed Ali Mudhir
2017-01-16 03:58:21 +00:00
parent baaaf213cc
commit 8c217af051
4 changed files with 131 additions and 45 deletions

View File

@@ -59,6 +59,7 @@ void App::init()
circle.create<6>(25, 12);
circle1.create<4>(25, 12);
circle2.create<15>(25, 12);
rounded.create<5>(50, 50, 10);
if (!tex.load("data/image.png"))
printf("error loading image\n");
@@ -86,15 +87,14 @@ void App::init()
void App::update(float dt)
{
static float theta = 0;
theta += M_PI * 0.5f * dt;
float red = fabsf(sinf(theta));
// static float theta = 0;
// theta += M_PI * 0.5f * dt;
// float red = fabsf(sinf(theta));
// glm::mat4 proj = glm::perspective(glm::radians(85.f), 1.f, .1f, 100.f);
// glm::mat4 model = glm::translate(glm::vec3(0, 0, 0));
// glm::mat4 view = glm::lookAt(glm::vec3(sinf(theta), 0, 1), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
float ar = width / height;
glm::mat4 proj = glm::ortho<float>(0, width, height, 0, -1, 1);
Shape* shapes[] = { &circle, &circle1, &circle2, &plane, &longPlane };
@@ -103,42 +103,43 @@ void App::update(float dt)
glClear(GL_COLOR_BUFFER_BIT);
auto s = glm::scale(glm::vec3(2));
for (int i = 0; i < 5; i++)
{
shader.use();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glActiveTexture(GL_TEXTURE0);
tex.bind();
shader.u_int("tex", 0);
shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 1, 75 + 120 * i, 0.f}) * s);
shapes[i]->draw_fill();
tex.unbind();
shader_color.use();
shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 2, 75 + 120 * i, 0.f}) * s);
shader_color.u_vec4("col", {1, 1, 1, 1});
shapes[i]->draw_stroke();
shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 0, 75 + 120 * i, 0.f}) * s);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
shapes[i]->draw_fill();
shader_uv.use();
shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 3, 75 + 120 * i, 0.f}) * s);
shader_uv.u_int("tex", 0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
shapes[i]->draw_fill();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 4, 75 + 120 * i, 0.f}) * s);
shapes[i]->draw_fill();
tex.unbind();
}
// shader.use();
// shader.u_mat4("mvp", proj * glm::translate(glm::vec3{width/2, height/2, 0.f}) * glm::scale(glm::vec3(10)));
// shader.u_int("tex", 0);
// glActiveTexture(GL_TEXTURE0);
// tex.bind();
// circle.draw_fill();
// tex.unbind();
// for (int i = 0; i < 5; i++)
// {
// shader.use();
//
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// glActiveTexture(GL_TEXTURE0);
// tex.bind();
// shader.u_int("tex", 0);
// shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 1, 75 + 120 * i, 0.f}) * s);
// shapes[i]->draw_fill();
// tex.unbind();
//
// shader_color.use();
// shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 2, 75 + 120 * i, 0.f}) * s);
// shader_color.u_vec4("col", {1, 1, 1, 1});
// shapes[i]->draw_stroke();
//
// shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 0, 75 + 120 * i, 0.f}) * s);
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// shapes[i]->draw_fill();
//
// shader_uv.use();
// shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 3, 75 + 120 * i, 0.f}) * s);
// shader_uv.u_int("tex", 0);
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// shapes[i]->draw_fill();
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 4, 75 + 120 * i, 0.f}) * s);
// shapes[i]->draw_fill();
// tex.unbind();
// }
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
shader_uv.use();
shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{width/2, height/2, 0.f}) * glm::scale(glm::vec3(10)));
shader_uv.u_int("tex", 0);
glActiveTexture(GL_TEXTURE0);
tex.bind();
rounded.draw_fill();
tex.unbind();
}

View File

@@ -12,6 +12,7 @@ class App
Plane plane;
Plane longPlane;
Circle circle, circle1, circle2;
Rounded rounded;
Texture2D tex;
public:
static App I;

View File

@@ -47,7 +47,7 @@ void Plane::create_impl(float w, float h, int div, GLushort *idx, Shape::vertex_
{
count[0] = div * div * 6;
count[1] = 8;
ioff[0] = (GLvoid*)(8 * sizeof(GLushort));
ioff[0] = (GLvoid*)(count[1] * sizeof(GLushort));
ioff[1] = (GLvoid*)0;
const float dx = w / div;
@@ -98,7 +98,7 @@ void Circle::create_impl(float radius, int div, GLushort* idx, Shape::vertex_t*
count[0] = div * 3;
count[1] = div * 2;
ioff[0] = (GLvoid*)0;
ioff[1] = (GLvoid*)(div * 3 * sizeof(GLushort));
ioff[1] = (GLvoid*)(count[0] * sizeof(GLushort));
auto pidx = idx;
auto pidx2 = idx + div * 3;
@@ -127,7 +127,7 @@ void Circle::create_impl(float radius_out, float radius_in, int div, GLushort* i
count[0] = div * 6;
count[1] = div * 4;
ioff[0] = (GLvoid*)0;
ioff[1] = (GLvoid*)(div * 6 * sizeof(GLushort));
ioff[1] = (GLvoid*)(count[0] * sizeof(GLushort));
auto pidx = idx;
auto pidx2 = idx + div * 6;
@@ -155,3 +155,72 @@ void Circle::create_impl(float radius_out, float radius_in, int div, GLushort* i
*pidx2++ = ((i+1)*2+1) % (div*2);// C
}
}
void Rounded::create_impl(float w, float h, float r, int div, GLushort* idx, GLushort* idx_tmp, Shape::vertex_t* vertices)
{
count[0] = (10 + div * 4) * 3;
count[1] = (4 + div * 4) * 2;
ioff[0] = (GLvoid*)0;
ioff[1] = (GLvoid*)(count[0] * sizeof(GLushort));
float X[] = { -w/2, -w/2+r, w/2-r, w/2 };
float Y[] = { -h/2, -h/2+r, h/2-r, h/2 };
auto V = [&](int x, int y) -> Shape::vertex_t {
return { glm::vec4(X[x], Y[y], 0, 1), glm::vec2(X[x]/w, Y[y]/h) + 0.5f };
};
*vertices++ = V(1,0);
*vertices++ = V(2,0);
*vertices++ = V(0,1);
*vertices++ = V(1,1);
*vertices++ = V(2,1);
*vertices++ = V(3,1);
*vertices++ = V(0,2);
*vertices++ = V(1,2);
*vertices++ = V(2,2);
*vertices++ = V(3,2);
*vertices++ = V(1,3);
*vertices++ = V(2,3);
auto Q = [&](int a, int b, int c, int d) {
*idx++ = a;
*idx++ = b;
*idx++ = c;
*idx++ = a;
*idx++ = c;
*idx++ = d;
};
Q(0, 3, 4, 1);
Q(2, 6, 7, 3);
Q(3, 7, 8, 4);
Q(4, 8, 9, 5);
Q(7,10,11, 8);
auto corner = [&](int c, int a, int b, int n) {
idx_tmp[0] = a;
idx_tmp[div] = b;
auto v = vertices-12;
for (int i = 1; i < div; i++)
{
float t = (float)(i) / div;
auto p = glm::normalize(glm::mix(v[a].pos.xyz(), v[b].pos.xyz(), t));
v[n].pos = glm::vec4(p * r + v[c].pos.xyz(), 1);
v[n].uvs = glm::normalize(glm::mix(v[a].uvs, v[b].uvs, t)) * glm::vec2(r/w, r/h) + v[c].uvs;
idx_tmp[i] = n;
n++;
}
for (int i = 0; i < div; i++)
{
*idx++ = c;
*idx++ = idx_tmp[i];
*idx++ = idx_tmp[i+1];
}
};
corner(3, 0, 2, 12 + (div-1)*0);
corner(7, 6,10, 12 + (div-1)*1);
corner(8,11, 9, 12 + (div-1)*2);
corner(4, 5, 1, 12 + (div-1)*3);
}

View File

@@ -50,3 +50,18 @@ public:
return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
}
};
class Rounded : public Shape
{
void create_impl(float w, float h, float r, int div, GLushort* idx, GLushort* idx_tmp, vertex_t* vertices);
public:
template<int div>
bool create(float w, float h, float r)
{
static GLushort idx[(10 + div * 4) * 3];
static GLushort idx_tmp[div+1];
static vertex_t vertices[12 + (div-1) * 4];
create_impl(w, h, r, div, idx, idx_tmp, vertices);
return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
}
};