Skip to content

Commit

Permalink
De-virtualising drawables and making Sphere3D more flexible.
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffyfreak committed Feb 21, 2016
1 parent b4cff2e commit 5034f76
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
39 changes: 26 additions & 13 deletions src/graphics/Drawables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,11 @@ static const int icosahedron_faces[20][3] = {
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
};

Sphere3D::Sphere3D(Renderer *renderer, RefCountedPtr<Material> mat, Graphics::RenderState *state, int subdivs, float scale)
Sphere3D::Sphere3D(Renderer *renderer, RefCountedPtr<Material> mat, Graphics::RenderState *state, int subdivs, float scale, const Uint32 attribs)
{
PROFILE_SCOPED()
assert(attribs & ATTRIB_POSITION);

m_material = mat;
m_renderState = state;

Expand All @@ -532,9 +534,8 @@ Sphere3D::Sphere3D(Renderer *renderer, RefCountedPtr<Material> mat, Graphics::Re
matrix4x4f trans = matrix4x4f::Identity();
trans.Scale(scale, scale, scale);

//m_surface.reset(new Surface(TRIANGLES, new VertexArray(ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0), mat));
//reserve some data
VertexArray vts(ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0, 256);
//reserve some data - ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0
VertexArray vts(attribs, (subdivs * subdivs) * 20 * 3);
std::vector<Uint32> indices;

//initial vertices
Expand All @@ -557,12 +558,20 @@ Sphere3D::Sphere3D(Renderer *renderer, RefCountedPtr<Material> mat, Graphics::Re

//Create vtx & index buffers and copy data
VertexBufferDesc vbd;
vbd.attrib[0].semantic = ATTRIB_POSITION;
vbd.attrib[0].format = ATTRIB_FORMAT_FLOAT3;
vbd.attrib[1].semantic = ATTRIB_NORMAL;
vbd.attrib[1].format = ATTRIB_FORMAT_FLOAT3;
vbd.attrib[2].semantic = ATTRIB_UV0;
vbd.attrib[2].format = ATTRIB_FORMAT_FLOAT2;
Uint32 attIdx = 0;
vbd.attrib[attIdx].semantic = ATTRIB_POSITION;
vbd.attrib[attIdx].format = ATTRIB_FORMAT_FLOAT3;
++attIdx;
if (attribs & ATTRIB_NORMAL) {
vbd.attrib[attIdx].semantic = ATTRIB_NORMAL;
vbd.attrib[attIdx].format = ATTRIB_FORMAT_FLOAT3;
++attIdx;
}
if (attribs & ATTRIB_UV0) {
vbd.attrib[attIdx].semantic = ATTRIB_UV0;
vbd.attrib[attIdx].format = ATTRIB_FORMAT_FLOAT2;
++attIdx;
}
vbd.numVertices = vts.GetNumVerts();
vbd.usage = BUFFER_USAGE_STATIC;
m_vertexBuffer.reset(renderer->CreateVertexBuffer(vbd));
Expand All @@ -587,9 +596,13 @@ int Sphere3D::AddVertex(VertexArray &vts, const vector3f &v, const vector3f &n)
{
PROFILE_SCOPED()
vts.position.push_back(v);
vts.normal.push_back(n);
//http://www.mvps.org/directx/articles/spheremap.htm
vts.uv0.push_back(vector2f(asinf(n.x)/M_PI+0.5f, asinf(n.y)/M_PI+0.5f));
if (vts.HasAttrib(ATTRIB_NORMAL)) {
vts.normal.push_back(n);
}
if (vts.HasAttrib(ATTRIB_UV0)) {
//http://www.mvps.org/directx/articles/spheremap.htm
vts.uv0.push_back(vector2f(asinf(n.x) / M_PI + 0.5f, asinf(n.y) / M_PI + 0.5f));
}
return vts.GetNumVerts() - 1;
}

Expand Down
22 changes: 11 additions & 11 deletions src/graphics/Drawables.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Circle {
Circle(Renderer *renderer, const float radius, const Color &c, RenderState *state);
Circle(Renderer *renderer, const float radius, const float x, const float y, const float z, const Color &c, RenderState *state);
Circle(Renderer *renderer, const float radius, const vector3f &center, const Color &c, RenderState *state);
virtual void Draw(Renderer *renderer);
void Draw(Renderer *renderer);

private:
void SetupVertexBuffer(const Graphics::VertexArray&, Graphics::Renderer *);
Expand All @@ -40,7 +40,7 @@ class Disk {
public:
Disk(Graphics::Renderer *r, Graphics::RenderState*, const Color &c, float radius);
Disk(Graphics::Renderer *r, RefCountedPtr<Material>, Graphics::RenderState*, const int edges=72, const float radius=1.0f);
virtual void Draw(Graphics::Renderer *r);
void Draw(Graphics::Renderer *r);

void SetColor(const Color&);

Expand All @@ -57,11 +57,11 @@ class Line3D {
public:
Line3D();
Line3D(const Line3D& b); // this needs an explicit copy constructor due to the std::unique_ptr below
virtual ~Line3D() {}
~Line3D() {}
void SetStart(const vector3f &);
void SetEnd(const vector3f &);
void SetColor(const Color &);
virtual void Draw(Renderer*, RenderState*);
void Draw(Renderer*, RenderState*);
private:
void CreateVertexBuffer(Graphics::Renderer *r, const Uint32 size);
void Dirty();
Expand Down Expand Up @@ -128,8 +128,8 @@ class Points {
class Sphere3D {
public:
//subdivisions must be 0-4
Sphere3D(Renderer*, RefCountedPtr<Material> material, Graphics::RenderState*, int subdivisions=0, float scale=1.f);
virtual void Draw(Renderer *r);
Sphere3D(Renderer*, RefCountedPtr<Material> material, Graphics::RenderState*, int subdivisions=0, float scale=1.f, const Uint32 attribs=(ATTRIB_POSITION | ATTRIB_NORMAL | ATTRIB_UV0));
void Draw(Renderer *r);

RefCountedPtr<Material> GetMaterial() const { return m_material; }

Expand Down Expand Up @@ -165,8 +165,8 @@ class TexturedQuad {
TexturedQuad(Graphics::Renderer *r, Graphics::Texture *texture, const vector2f &pos, const vector2f &size, RenderState *state);
TexturedQuad(Graphics::Renderer *r, RefCountedPtr<Graphics::Material> &material, const Graphics::VertexArray &va, RenderState *state);

virtual void Draw(Graphics::Renderer *r);
virtual void Draw(Graphics::Renderer *r, const Color4ub &tint);
void Draw(Graphics::Renderer *r);
void Draw(Graphics::Renderer *r, const Color4ub &tint);
const Graphics::Texture* GetTexture() const { return m_texture.Get(); }
private:
RefCountedPtr<Graphics::Texture> m_texture;
Expand All @@ -181,7 +181,7 @@ class Rect {
public:
Rect(Graphics::Renderer *r, const vector2f &pos, const vector2f &size, const Color &c, RenderState *state, const bool bIsStatic = true);
void Update(const vector2f &pos, const vector2f &size, const Color &c);
virtual void Draw(Graphics::Renderer *r);
void Draw(Graphics::Renderer *r);
private:
RefCountedPtr<Graphics::Material> m_material;
RefCountedPtr<VertexBuffer> m_vertexBuffer;
Expand All @@ -194,7 +194,7 @@ class RoundEdgedRect {
public:
RoundEdgedRect(Graphics::Renderer *r, const vector2f &size, const float rad, const Color &c, RenderState *state, const bool bIsStatic = true);
void Update(const vector2f &size, float rad, const Color &c);
virtual void Draw(Graphics::Renderer *r);
void Draw(Graphics::Renderer *r);
private:
static const int STEPS = 6;
RefCountedPtr<Graphics::Material> m_material;
Expand All @@ -207,7 +207,7 @@ class RoundEdgedRect {
class Axes3D {
public:
Axes3D(Graphics::Renderer *r, Graphics::RenderState *state = nullptr);
virtual void Draw(Graphics::Renderer *r);
void Draw(Graphics::Renderer *r);
private:
RefCountedPtr<Graphics::Material> m_material;
RefCountedPtr<VertexBuffer> m_vertexBuffer;
Expand Down

0 comments on commit 5034f76

Please sign in to comment.