forked from interl0per/Console-Graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cs
More file actions
118 lines (106 loc) · 3.91 KB
/
Mesh.cs
File metadata and controls
118 lines (106 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Mesh - all the things that define a mesh. triangles, edges, verticies... later UVs...
*
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleGraphics
{
class Mesh : Program
{
public Mesh(point3[] meshVerts, triangle[] meshFaces, point2[] meshUvs = null)
{
verts = meshVerts;
vertCount = verts.Length;
faces = meshFaces;
faceCount = faces.Length;
uvVerts = meshUvs;
}
public struct triangle
{
public int[] vertIDs;
public int[] uvIds;
public triangle(int v1, int v2, int v3, int vt1, int vt2, int vt3)
{
vertIDs = new int[3];
vertIDs[0] = v1;
vertIDs[1] = v2;
vertIDs[2] = v3;
uvIds = new int[3];
uvIds[0] = vt1;
uvIds[1] = vt2;
uvIds[2] = vt3;
}
};
public struct point3
{
public float x, y, z;
public point3(float newX, float newY, float newZ = 0)
{
x = newX;
y = newY;
z = newZ;
}
};
public struct point2
{
public float x, y;
public point2(float newX, float newY)
{
x = newX;
y = newY;
}
};
public point3[] verts;
public triangle[] faces;
public point2[] uvVerts;
public int vertCount;
public int faceCount;
public void translate(point3 translation)
{
for (int i = 0; i < verts.Length; i++)
{
verts[i] = Matrix.add(verts[i], translation);
}
}
public void rotate(double angle, byte dir)
{
//Rotate about centerpoint of the shape: translate the center of our shape to the origin, rotate, translate back.
float cosAngle = (float)Math.Cos(angle);
float sinAngle = (float)Math.Sin(angle);
Matrix rotMtrx = null;
#region rotations
/* Rotate about X Axis {{1,0,0},
{0, cosAngle,sinAngle*100},
{0, -sinAngle/100, cosAngle}}, 3, 3);
* Rotate about Y Axis {{cosAngle,0,-sinAngle*100},
{0, 1, 0},
{sinAngle, 0, cosAngle}}, 3, 3);
Rotate about Z axis
* {{cosAngle,-sinAngle,0},
{sinAngle, cosAngle,0},
{0, 0, 1}},3,3);
*/
#endregion
if (dir == 0)
rotMtrx = new Matrix(new float[,] {{1,0,0},
{0, cosAngle,sinAngle*100},
{0, -sinAngle/100, cosAngle}}, 3, 3);
else if (dir == 1)
rotMtrx = new Matrix(new float[,] {{cosAngle,0,-sinAngle*100},
{0, 1, 0},
{sinAngle/100, 0, cosAngle}}, 3, 3);
else
rotMtrx = new Matrix(new float[,] {{cosAngle,-sinAngle,0},
{sinAngle, cosAngle,0},
{0, 0, 1}}, 3, 3);
for (int i = 0; i < vertCount; i++)
{
verts[i] = rotMtrx.multiply(verts[i]);
}
}
}
}