#include "pch.h" #include "shape.hpp" bool Shape::create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize) { glGenBuffers(2, buffers); if (!(buffers[0] && buffers[1])) return false; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, isize, idx, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); glBufferData(GL_ARRAY_BUFFER, vsize, vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenVertexArrays(2, arrays); if (!(arrays[0] && arrays[1])) return false; for (int i = 0; i < 2; i++) { glBindVertexArray(arrays[i]); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs)); } glBindVertexArray(0); return true; } void Shape::draw_fill() const { glBindVertexArray(arrays[0]); glDrawElements(GL_TRIANGLES, count[0], GL_UNSIGNED_SHORT, ioff[0]); } void Shape::draw_stroke() const { glBindVertexArray(arrays[1]); glDrawElements(GL_LINES, count[1], GL_UNSIGNED_SHORT, ioff[1]); } void Plane::create_impl(float w, float h, int div, GLushort *idx, Shape::vertex_t *vertices) { count[0] = div * div * 6; count[1] = 8; ioff[0] = (GLvoid*)0; ioff[1] = (GLvoid*)(count[0] * sizeof(GLushort)); const float dx = w / div; const float dy = h / div; const float ox = -w * 0.5f; const float oy = -h * 0.5f; for (int y = 0; y <= div; y++) { for (int x = 0; x <= div; x++) { vertex_t v; v.pos.x = ox + dx * (float)x; v.pos.y = oy + dy * (float)y; v.pos.z = 0; v.pos.w = 1; v.uvs = glm::vec2(x, y) / (float)div; *vertices++ = v; } } // generate indices for (int y = 0; y < div; y++) { int i = y * (div+1); for (int x = 0; x < div; x++) { *idx++ = i; *idx++ = i + div + 1; *idx++ = i + div + 2; *idx++ = i; *idx++ = i + div + 2; *idx++ = i + 1; i++; } } // outline indices *idx++ = 0; // A *idx++ = (div+1)*(div); // B *idx++ = (div+1)*(div); // B *idx++ = (div+1)*(div+1)-1; // C *idx++ = (div+1)*(div+1)-1; // C *idx++ = div; // D *idx++ = div; // D *idx++ = 0; // A } void Circle::create_impl(float radius, int div, GLushort* idx, vertex_t* vertices, kUVMapping map) { count[0] = div * 3; count[1] = div * 2; ioff[0] = (GLvoid*)0; ioff[1] = (GLvoid*)(count[0] * sizeof(GLushort)); auto pidx = idx; auto pidx2 = idx + count[0]; for (int i = 0; i < div; i++) { vertex_t v; float theta = (float)i / div * M_PI * 2.f; glm::vec2 uv = { sinf(theta), cosf(theta) }; v.pos = glm::vec4(uv * radius, 0, 1); v.uvs = (map == kUVMapping::Planar) ? (uv * 0.5f + 0.5f) : glm::vec2((float)i / div, 1.f); vertices[i+1] = v; *pidx++ = 0; *pidx++ = i+1; *pidx++ = ((i+1) % div) + 1; *pidx2++ = 1 + i; *pidx2++ = 1 + ((i+1) % div); } vertices[0].pos = { 0, 0, 0, 1 }; vertices[0].uvs = (map == kUVMapping::Planar) ? glm::vec2(0.5f, 0.5f) : glm::vec2(0.f, 0.f); } void Circle::create_impl(float radius_out, float radius_in, int div, GLushort* idx, vertex_t* vertices, kUVMapping map) { count[0] = div * 6; count[1] = div * 4; ioff[0] = (GLvoid*)0; ioff[1] = (GLvoid*)(count[0] * sizeof(GLushort)); auto pidx = idx; auto pidx2 = idx + count[0]; for (int i = 0; i < div; i++) { float theta = (float)(i%(div-1)) / (div-1) * M_PI * 2.f; glm::vec2 uv = { sinf(theta), cosf(theta) }; if (map == kUVMapping::Planar) { vertices[i*2].uvs = uv * (radius_in/radius_out) * 0.5f + 0.5f; vertices[i*2+1].uvs = uv * 0.5f + 0.5f; } else { vertices[i*2].uvs = { (float)i / div, 0.f }; vertices[i*2+1].uvs = { (float)i / div, 1.f}; } vertices[i*2].pos = glm::vec4(uv * radius_in, 0, 1); vertices[i*2+1].pos = glm::vec4(uv * radius_out, 0, 1); *pidx++ = i*2; // A *pidx++ = i*2+1; // B *pidx++ = ((i+1)*2+1) % (div*2); // C *pidx++ = i*2; // A *pidx++ = ((i+1)*2+1) % (div*2); // C *pidx++ = ((i+1)*2) % (div*2); // D *pidx2++ = i*2; // A *pidx2++ = ((i+1)*2) % (div*2); // D *pidx2++ = i*2+1; // B *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)); auto idx2 = idx + count[0]; 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) { auto v = vertices-12; idx_tmp[0] = a; idx_tmp[div] = b; for (int i = 1; i < div; i++) { float t = (float)(i) / div; auto p = glm::normalize(glm::mix(v[a].pos.xyz()-v[c].pos.xyz(), v[b].pos.xyz()-v[c].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[c].uvs, v[b].uvs-v[c].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++ = *idx2++ = idx_tmp[i]; *idx++ = *idx2++ = 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); *idx2++ = 0; *idx2++ = 1; *idx2++ = 5; *idx2++ = 9; *idx2++ = 11; *idx2++ = 10; *idx2++ = 6; *idx2++ = 2; } void Slice9::create_impl(float w, float h, float r, float tr, GLushort *idx, Shape::vertex_t *vertices) { count[0] = 3 * 3 * 6; count[1] = 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 }; float T[] = { 0, tr, 1-tr, 1 }; auto V = [&](int x, int y) -> Shape::vertex_t { return { glm::vec4(X[x], Y[y], 0, 1), glm::vec2(T[x], T[y]) }; }; for (int y = 0; y < 4; y++) for (int x = 0; x < 4; x++) *vertices++ = V(x,y); for (int y = 0; y < 3; y++) { int i = y * (3+1); for (int x = 0; x < 3; x++) { *idx++ = i; *idx++ = i + 3 + 1; *idx++ = i + 3 + 2; *idx++ = i; *idx++ = i + 3 + 2; *idx++ = i + 1; i++; } } // outline indices *idx++ = 0; // A *idx++ = 3; // B *idx++ = 3; // B *idx++ = 15; // C *idx++ = 15; // C *idx++ = 12; // D *idx++ = 12; // D *idx++ = 0; // A }