Files
panopainter/engine/shape.hpp
Omar Mohamed Ali Mudhir 13d8e6e563 base setup for osx in place
2017-01-14 18:30:19 +00:00

155 lines
4.2 KiB
C++

#pragma once
class Shape
{
protected:
GLuint buffers[2];
GLuint arrays[2];
GLuint count[2];
GLvoid* ioff[2];
struct vertex_t { glm::vec4 pos; glm::vec2 uvs; };
public:
void create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize);
void draw_fill() const;
void draw_stroke() const;
};
class Plane : public Shape
{
public:
template<int div>
bool create(float w, float h)
{
static GLshort idx[div * div * 6 + 4];
static vertex_t vertices[(div+1)*(div+1)];
count[0] = div * div * 6;
count[1] = 4;
ioff[0] = (GLvoid*)4;
ioff[1] = (GLvoid*)0;
const float dx = w / div;
const float dy = h / div;
const float ox = -w * 0.5f;
const float oy = -h * 0.5f;
int v_index = 0;
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_index++] = v;
}
}
// generate indices
auto pidx = idx;
*pidx++ = 0;
*pidx++ = div;
*pidx++ = (div+1)*(div);
*pidx++ = (div+1)*(div+1)-1;
for (int y = 0; y < div; y++)
{
int i = y * (div+1);
for (int x = 0; x < div; x++)
{
*pidx++ = i;
*pidx++ = i + div + 1;
*pidx++ = i + div + 2;
*pidx++ = i;
*pidx++ = i + div + 2;
*pidx++ = i + 1;
i++;
}
}
create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
return true;
}
};
class Circle : public Shape
{
public:
template<int div>
bool create(float radius)
{
static GLushort idx[div * 3 + div * 2];
static vertex_t vertices[div + 1];
count[0] = div * 3;
count[1] = div * 2;
ioff[0] = (GLvoid*)0;
ioff[1] = (GLvoid*)(div * 3 * sizeof(idx[0]));
auto pidx = idx;
auto pidx2 = idx + div * 3;
for (int i = 0; i < div; i++)
{
vertex_t v;
float theta = (float)i / div * M_PI * 2.f;
v.pos.x = sinf(theta) * radius;
v.pos.y = cosf(theta) * radius;
v.pos.z = 0;
v.pos.w = 1;
v.uvs = v.pos.xy() * 0.5f + 0.5f;
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 = { 0.5f, 0.5f };
create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
return true;
}
template<int div>
bool create(float radius_out, float radius_in)
{
static GLubyte idx[div*6 + div*2];
static vertex_t vertices[div * 2];
count[0] = div * 6;
count[1] = div * 2;
auto pidx = idx;
auto pidx2 = idx + div * 6;
for (int i = 0; i < div; i++)
{
float theta = (float)i / div * M_PI * 2.f;
vertices[i*2].pos = { sinf(theta) * radius_in, cosf(theta) * radius_in, 0, 1 };
vertices[i*2].uvs = vertices[i*2].pos.xy() * 0.5f + 0.5f;
vertices[i*2+1].pos = { sinf(theta) * radius_out, cosf(theta) * radius_out, 0, 1 };
vertices[i*2+1].uvs = vertices[i*2+1].pos.xy() * 0.5f + 0.5f;
*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++ = 1 + i;
*pidx2++ = 1 + ((i+1) % div);
}
create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
return true;
}
};