vec2; material and mesh initial commit

This commit is contained in:
Vadim Lopatin 2016-01-15 12:07:11 +03:00
parent 39e73e16d2
commit fa19941071
6 changed files with 336 additions and 7 deletions

View File

@ -280,6 +280,8 @@
<Compile Include="src\dlangui\graphics\images.d" /> <Compile Include="src\dlangui\graphics\images.d" />
<Compile Include="src\dlangui\graphics\resources.d" /> <Compile Include="src\dlangui\graphics\resources.d" />
<Compile Include="src\dlangui\graphics\scene\camera.d" /> <Compile Include="src\dlangui\graphics\scene\camera.d" />
<Compile Include="src\dlangui\graphics\scene\material.d" />
<Compile Include="src\dlangui\graphics\scene\mesh.d" />
<Compile Include="src\dlangui\graphics\scene\node.d" /> <Compile Include="src\dlangui\graphics\scene\node.d" />
<Compile Include="src\dlangui\graphics\scene\scene3d.d" /> <Compile Include="src\dlangui\graphics\scene\scene3d.d" />
<Compile Include="src\dlangui\graphics\scene\transform.d" /> <Compile Include="src\dlangui\graphics\scene\transform.d" />

View File

@ -139,6 +139,8 @@
<Compile Include="src\dlangui\graphics\images.d" /> <Compile Include="src\dlangui\graphics\images.d" />
<Compile Include="src\dlangui\graphics\resources.d" /> <Compile Include="src\dlangui\graphics\resources.d" />
<Compile Include="src\dlangui\graphics\scene\camera.d" /> <Compile Include="src\dlangui\graphics\scene\camera.d" />
<Compile Include="src\dlangui\graphics\scene\material.d" />
<Compile Include="src\dlangui\graphics\scene\mesh.d" />
<Compile Include="src\dlangui\graphics\scene\node.d" /> <Compile Include="src\dlangui\graphics\scene\node.d" />
<Compile Include="src\dlangui\graphics\scene\scene3d.d" /> <Compile Include="src\dlangui\graphics\scene\scene3d.d" />
<Compile Include="src\dlangui\graphics\scene\transform.d" /> <Compile Include="src\dlangui\graphics\scene\transform.d" />

View File

@ -769,6 +769,8 @@
<Folder name="graphics"> <Folder name="graphics">
<Folder name="scene"> <Folder name="scene">
<File path="src\dlangui\graphics\scene\camera.d" /> <File path="src\dlangui\graphics\scene\camera.d" />
<File path="src\dlangui\graphics\scene\material.d" />
<File path="src\dlangui\graphics\scene\mesh.d" />
<File path="src\dlangui\graphics\scene\node.d" /> <File path="src\dlangui\graphics\scene\node.d" />
<File path="src\dlangui\graphics\scene\scene3d.d" /> <File path="src\dlangui\graphics\scene\scene3d.d" />
<File path="src\dlangui\graphics\scene\transform.d" /> <File path="src\dlangui\graphics\scene\transform.d" />

View File

@ -2,6 +2,238 @@ module dlangui.core.math3d;
import std.math; import std.math;
/// 2 dimensional vector
struct vec2 {
union {
float[2] vec;
struct {
float x;
float y;
}
}
alias u = x;
alias v = y;
/// create with all components filled with specified value
this(float v) {
x = v;
y = v;
}
this(float[2] v) {
vec = v;
}
this(const vec2 v) {
vec = v.vec;
}
this(float x, float y) {
vec[0] = x;
vec[1] = y;
}
ref vec2 opAssign(float[2] v) {
vec = v;
return this;
}
ref vec2 opAssign(vec2 v) {
vec = v.vec;
return this;
}
/// fill all components of vector with specified value
ref vec2 clear(float v) {
vec[0] = vec[1] = v;
return this;
}
/// add value to all components of vector
ref vec2 add(float v) {
vec[0] += v;
vec[1] += v;
return this;
}
/// multiply all components of vector by value
ref vec2 mul(float v) {
vec[0] *= v;
vec[1] *= v;
return this;
}
/// subtract value from all components of vector
ref vec2 sub(float v) {
vec[0] -= v;
vec[1] -= v;
return this;
}
/// divide all components of vector by value
ref vec2 div(float v) {
vec[0] /= v;
vec[1] /= v;
return this;
}
/// add components of another vector to corresponding components of this vector
ref vec2 add(vec2 v) {
vec[0] += v.vec[0];
vec[1] += v.vec[1];
return this;
}
/// multiply components of this vector by corresponding components of another vector
ref vec2 mul(vec2 v) {
vec[0] *= v.vec[0];
vec[1] *= v.vec[1];
return this;
}
/// subtract components of another vector from corresponding components of this vector
ref vec2 sub(vec2 v) {
vec[0] -= v.vec[0];
vec[1] -= v.vec[1];
return this;
}
/// divide components of this vector by corresponding components of another vector
ref vec2 div(vec2 v) {
vec[0] /= v.vec[0];
vec[1] /= v.vec[1];
return this;
}
/// add value to all components of vector
vec2 opBinary(string op : "+")(float v) const {
vec2 res = this;
res.vec[0] += v;
res.vec[1] += v;
return res;
}
/// multiply all components of vector by value
vec2 opBinary(string op : "*")(float v) const {
vec2 res = this;
res.vec[0] *= v;
res.vec[1] *= v;
return res;
}
/// subtract value from all components of vector
vec2 opBinary(string op : "-")(float v) const {
vec2 res = this;
res.vec[0] -= v;
res.vec[1] -= v;
return res;
}
/// divide all components of vector by value
vec3 opBinary(string op : "/")(float v) const {
vec3 res = this;
res.vec[0] /= v;
res.vec[1] /= v;
res.vec[2] /= v;
return res;
}
/// add value to all components of vector
ref vec2 opOpAssign(string op : "+")(float v) {
vec[0] += v;
vec[1] += v;
return this;
}
/// multiply all components of vector by value
ref vec2 opOpAssign(string op : "*")(float v) {
vec[0] *= v;
vec[1] *= v;
return this;
}
/// subtract value from all components of vector
ref vec2 opOpAssign(string op : "-")(float v) {
vec[0] -= v;
vec[1] -= v;
vec[2] -= v;
return this;
}
/// divide all components of vector by value
ref vec2 opOpAssign(string op : "/")(float v) {
vec[0] /= v;
vec[1] /= v;
vec[2] /= v;
return this;
}
/// by component add values of corresponding components of other vector
ref vec2 opOpAssign(string op : "+")(const vec2 v) {
vec[0] += v.vec[0];
vec[1] += v.vec[1];
return this;
}
/// by component multiply values of corresponding components of other vector
ref vec2 opOpAssign(string op : "*")(const vec2 v) {
vec[0] *= v.vec[0];
vec[1] *= v.vec[1];
return this;
}
/// by component subtract values of corresponding components of other vector
ref vec2 opOpAssign(string op : "-")(const vec2 v) {
vec[0] -= v.vec[0];
vec[1] -= v.vec[1];
return this;
}
/// by component divide values of corresponding components of other vector
ref vec2 opOpAssign(string op : "/")(const vec2 v) {
vec[0] /= v.vec[0];
vec[1] /= v.vec[1];
return this;
}
/// add value to all components of vector
vec2 opBinary(string op : "+")(const vec2 v) const {
vec2 res = this;
res.vec[0] += v.vec[0];
res.vec[1] += v.vec[1];
return res;
}
/// subtract value from all components of vector
vec3 opBinary(string op : "-")(const vec2 v) const {
vec2 res = this;
res.vec[0] -= v.vec[0];
res.vec[1] -= v.vec[1];
return res;
}
/// subtract value from all components of vector
float opBinary(string op : "*")(const vec3 v) const {
return dot(v);
}
/// dot product (sum of by-component products of vector components)
float dot(const vec2 v) const {
float res = 0.0f;
res += vec[0] * v.vec[0];
res += vec[1] * v.vec[1];
return res;
}
/// returns vector with all components which are negative of components for this vector
vec2 opUnary(string op : "-")() const {
vec2 ret = this;
ret.vec[0] = -vec[0];
ret.vec[1] = -vec[1];
return ret;
}
/// sum of squares of all vector components
@property float magnitudeSquared() {
return vec[0]*vec[0] + vec[1]*vec[1];
}
/// length of vector
@property float magnitude() {
return sqrt(magnitudeSquared);
}
alias length = magnitude;
/// normalize vector: make its length == 1
void normalize() {
div(length);
}
/// returns normalized copy of this vector
@property vec2 normalized() {
vec2 res = this;
res.normalize();
return res;
}
}
/// 3 dimensional vector /// 3 dimensional vector
struct vec3 { struct vec3 {
union { union {
@ -18,6 +250,10 @@ struct vec3 {
alias r = x; alias r = x;
alias g = y; alias g = y;
alias b = z; alias b = z;
/// create with all components filled with specified value
this(float v) {
x = y = z = v;
}
this(float[3] v) { this(float[3] v) {
vec = v; vec = v;
} }
@ -230,9 +466,9 @@ struct vec3 {
/// returns vector with all components which are negative of components for this vector /// returns vector with all components which are negative of components for this vector
vec3 opUnary(string op : "-")() const { vec3 opUnary(string op : "-")() const {
vec3 ret = this; vec3 ret = this;
ret.vec[0] = vec[0]; ret.vec[0] = -vec[0];
ret.vec[1] = vec[1]; ret.vec[1] = -vec[1];
ret.vec[2] = vec[2]; ret.vec[2] = -vec[2];
return ret; return ret;
} }
@ -311,6 +547,10 @@ struct vec4 {
alias g = y; alias g = y;
alias b = z; alias b = z;
alias a = w; alias a = w;
/// create with all components filled with specified value
this(float v) {
x = y = z = w = v;
}
this(float[4] v) { this(float[4] v) {
vec = v; vec = v;
} }
@ -563,10 +803,10 @@ struct vec4 {
/// returns vector with all components which are negative of components for this vector /// returns vector with all components which are negative of components for this vector
vec4 opUnary(string op : "-")() const { vec4 opUnary(string op : "-")() const {
vec4 ret = this; vec4 ret = this;
ret[0] = vec[0]; ret[0] = -vec[0];
ret[1] = vec[1]; ret[1] = -vec[1];
ret[2] = vec[2]; ret[2] = -vec[2];
ret[3] = vec[3]; ret[3] = -vec[3];
return ret; return ret;
} }

View File

@ -0,0 +1,4 @@
module dlangui.graphics.scene.material;
class Material {
}

View File

@ -0,0 +1,79 @@
module dlangui.graphics.scene.mesh;
import dlangui.graphics.scene.material;
import dlangui.core.math3d;
class Mesh {
protected Submesh[] _submeshes;
}
class Submesh {
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];
}
/// add vertex data, returns index of added vertex
void setVertex(int index, vec3 coord, vec3 normal, vec4 color, vec2 txcoord) {
setVertexCoord(index, coord);
setVertexNormal(index, normal);
setVertexColor(index, color);
setVertexTxCoord(index, txcoord);
}
/// add vertex data, returns index of added vertex
int addVertex(vec3 coord, vec3 normal, vec4 color, vec2 txcoord) {
_coords ~= coord.vec;
_normals ~= normal.vec;
_colors ~= color.vec;
_txcoords ~= txcoord.vec;
_vertexCount++;
return _vertexCount - 1;
}
int addTriangleIndexes(int p1, int p2, int p3) {
_indexes ~= p1;
_indexes ~= p2;
_indexes ~= p3;
_triangleCount++;
return _triangleCount - 1;
}
}