3d graphics: mesh, continue development:

This commit is contained in:
Vadim Lopatin 2016-02-16 15:34:58 +03:00
parent 3bd735773f
commit f694bf38eb
2 changed files with 56 additions and 77 deletions

View File

@ -103,8 +103,13 @@ extern (C) int UIAppMain(string[] args) {
scene.activeCamera = cam;
mat4 camMatrix = scene.viewProjectionMatrix;
VertexFormat vfmt = VertexFormat(VertexElementType.POSITION, VertexElementType.COLOR, VertexElementType.TEXCOORD0);
Mesh mesh = new Mesh2(vfmt);
mesh.addVertex([1,2,3, 1,1,1,1, 0,0]);
Mesh mesh = new Mesh(vfmt);
mesh.addVertex([1,2,3, 1,1,1,1, 0,0]);
mesh.addVertex([-1,2,3, 1,1,1,1, 1,0]);
mesh.addVertex([-1,-2,3, 1,1,1,1, 1,1]);
mesh.addVertex([1,-2,3, 1,1,1,1, 0,1]);
mesh.addPart(PrimitiveType.triangles, [0, 1, 2, 2, 3, 0]);
//MeshPart part = new MeshPart();
// show window

View File

@ -97,18 +97,35 @@ struct VertexFormat {
}
}
/// Mesh
class Mesh {
protected VertexFormat _vertexFormat;
protected int _vertexCount;
protected float[] _vertexData;
protected MeshPart[] _parts;
@property ref const(VertexFormat) vertexFormat() const { return _vertexFormat; }
@property void vertexFormat(VertexFormat format) {
assert(_vertexCount == 0);
_vertexFormat = format;
}
/// returns vertex count
@property int vertexCount() const { return _vertexCount; }
/// mesh part count
@property int partCount() const { return _parts.length; }
/// returns mesh part by index
MeshPart part(int index) { return _parts[index]; }
MeshPart addPart(MeshPart meshPart) {
_parts ~= meshPart;
return meshPart;
}
MeshPart addPart(PrimitiveType type, int[] indexes) {
return addPart(new MeshPart(type, indexes));
}
/// adds single vertex
int addVertex(float[] data) {
assert(_vertexFormat.isValid && data.length == _vertexFormat.vertexFloats);
@ -137,82 +154,39 @@ class Mesh {
}
}
class Mesh_bak {
protected MeshPart[] _parts;
protected int _vertexCount;
protected int _triangleCount;
protected float[] _coords; // [x, y, z]
protected float[] _normals; // [x, y, z]
protected float[] _colors; // [r, g, b, a]
protected float[] _txcoords;// [u, v]
protected int[] _indexes; // [v1, v2, v3] -- triangle vertex indexes
protected Material _material;
@property int vertexCount() { return _vertexCount; }
@property int triangleCount() { return _triangleCount; }
float[] arrayElement(ref float[] buf, int elemIndex, int elemLength = 3, float fillWith = 0) {
int startIndex = elemIndex * elemLength;
if (buf.length < startIndex + elemLength) {
if (_vertexCount < elemIndex + 1)
_vertexCount = elemIndex + 1;
int p = cast(int)buf.length;
buf.length = startIndex + elemLength;
for(; p < buf.length; p++)
buf[p] = fillWith;
}
return buf[startIndex .. startIndex + elemLength];
}
void setVertexCoord(int index, vec3 v) {
arrayElement(_coords, index, 3, 0)[0..3] = v.vec[0..3];
}
void setVertexNormal(int index, vec3 v) {
arrayElement(_normals, index, 3, 0)[0..3] = v.vec[0..3];
}
void setVertexColor(int index, vec4 v) {
arrayElement(_colors, index, 4, 1.0f)[0..4] = v.vec[0..4];
}
void setVertexTxCoord(int index, vec2 v) {
arrayElement(_txcoords, index, 2, 0)[0..2] = v.vec[0..2];
}
/// sets vertex data for specified vertex index, returns index of added vertex; pass index -1 to append vertex to end of list
int setVertex(int index, vec3 coord, vec3 normal, vec4 color, vec2 txcoord) {
if (index < 0)
index = _vertexCount;
setVertexCoord(index, coord);
setVertexNormal(index, normal);
setVertexColor(index, color);
setVertexTxCoord(index, txcoord);
return index;
}
/// adds indexes for triangle
int addTriangleIndexes(int p1, int p2, int p3) {
_indexes ~= p1;
_indexes ~= p2;
_indexes ~= p3;
_triangleCount++;
return _triangleCount - 1;
}
/// adds indexes for 2 triangles forming rectangle
int addRectangleIndexes(int p1, int p2, int p3, int p4) {
_indexes ~= p1;
_indexes ~= p2;
_indexes ~= p3;
_indexes ~= p3;
_indexes ~= p4;
_indexes ~= p1;
_triangleCount += 2;
return _triangleCount - 2;
}
/// Graphics primitive type
enum PrimitiveType : int {
triangles,
triangleStripes,
lines,
lineStripes,
points,
}
/// Mesh part - set of vertex indexes with graphics primitive type
class MeshPart {
protected PrimitiveType _type;
protected int[] _indexData;
this(PrimitiveType type, int[] indexes = null) {
_type = type;
_indexData.assumeSafeAppend;
add(indexes);
}
void add(int[] indexes) {
if (indexes.length)
_indexData ~= indexes;
}
/// returns primitive type
@property PrimitiveType type() const { return _type; }
/// change primitive type
@property void type(PrimitiveType t) { _type = t; }
/// index length
@property int length() const { return cast(int)_indexData.length; }
/// index data
@property int[] data() { return _indexData; }
}