mesh drawing

This commit is contained in:
Vadim Lopatin 2016-02-19 14:07:38 +03:00
parent 887cd27e2f
commit 11593f91db
2 changed files with 24 additions and 0 deletions

View File

@ -303,6 +303,18 @@ class GLProgram : GraphicsEffect {
override void setUniform(string uniformName, vec4 vec) { override void setUniform(string uniformName, vec4 vec) {
checkgl!glUniform4fv(getAttribLocation(uniformName), 1, vec.vec.ptr); checkgl!glUniform4fv(getAttribLocation(uniformName), 1, vec.vec.ptr);
} }
/// draw mesh using this program (program should be bound by this time and all uniforms should be set)
override void draw(Mesh mesh) {
VertexBuffer vb = mesh.vertexBuffer;
if (!vb) {
vb = new GLVertexBuffer();
mesh.vertexBuffer = vb;
}
vb.bind();
vb.draw(this);
vb.unbind();
}
} }
class SolidFillProgram : GLProgram { class SolidFillProgram : GLProgram {

View File

@ -56,6 +56,8 @@ abstract class GraphicsEffect {
void setUniform(string uniformName, vec3 vec); void setUniform(string uniformName, vec3 vec);
void setUniform(string uniformName, vec4 vec); void setUniform(string uniformName, vec4 vec);
void draw(Mesh mesh);
} }
/// vertex attribute properties /// vertex attribute properties
@ -162,6 +164,7 @@ class Mesh {
protected float[] _vertexData; protected float[] _vertexData;
protected MeshPart[] _parts; protected MeshPart[] _parts;
protected VertexBuffer _vertexBuffer; protected VertexBuffer _vertexBuffer;
protected bool _dirtyVertexBuffer = true;
@property ref const(VertexFormat) vertexFormat() const { return _vertexFormat; } @property ref const(VertexFormat) vertexFormat() const { return _vertexFormat; }
@ -170,6 +173,7 @@ class Mesh {
@property void vertexFormat(VertexFormat format) { @property void vertexFormat(VertexFormat format) {
assert(_vertexCount == 0); assert(_vertexCount == 0);
_vertexFormat = format; _vertexFormat = format;
_dirtyVertexBuffer = true;
} }
/// returns vertex count /// returns vertex count
@ -210,6 +214,10 @@ class Mesh {
/// get vertex buffer object /// get vertex buffer object
@property VertexBuffer vertexBuffer() { @property VertexBuffer vertexBuffer() {
if (_dirtyVertexBuffer && _vertexBuffer) {
_vertexBuffer.setData(this);
_dirtyVertexBuffer = false;
}
return _vertexBuffer; return _vertexBuffer;
} }
@ -222,6 +230,7 @@ class Mesh {
_vertexBuffer = buffer; _vertexBuffer = buffer;
if (_vertexBuffer) { if (_vertexBuffer) {
_vertexBuffer.setData(this); _vertexBuffer.setData(this);
_dirtyVertexBuffer = false;
} }
} }
@ -232,6 +241,7 @@ class Mesh {
MeshPart addPart(MeshPart meshPart) { MeshPart addPart(MeshPart meshPart) {
_parts ~= meshPart; _parts ~= meshPart;
_dirtyVertexBuffer = true;
return meshPart; return meshPart;
} }
@ -246,6 +256,7 @@ class Mesh {
_vertexData.assumeSafeAppend(); _vertexData.assumeSafeAppend();
_vertexData ~= data; _vertexData ~= data;
_vertexCount++; _vertexCount++;
_dirtyVertexBuffer = true;
return res; return res;
} }
@ -256,6 +267,7 @@ class Mesh {
_vertexData.assumeSafeAppend(); _vertexData.assumeSafeAppend();
_vertexData ~= data; _vertexData ~= data;
_vertexCount += cast(int)(data.length / _vertexFormat.vertexFloats); _vertexCount += cast(int)(data.length / _vertexFormat.vertexFloats);
_dirtyVertexBuffer = true;
return res; return res;
} }