This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
forked from ddinan/more-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrinterModel.cs
139 lines (110 loc) · 6.12 KB
/
PrinterModel.cs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using ClassicalSharp;
using ClassicalSharp.Entities;
using ClassicalSharp.GraphicsAPI;
using ClassicalSharp.Model;
using ClassicalSharp.Physics;
using OpenTK;
namespace MoreModels
{
class PrinterModel : IModel
{
public PrinterModel(Game game) : base(game) { }
public override float GetEyeY(Entity e) { return 4f / 16f; }
public override Vector3 CollisionSize { get { return new Vector3(.9375f, 0.75f, .9375f); } }
public override float NameYOffset { get { return 0.75f; } }
public override AABB PickingBounds { get { return new AABB(-0.5f, 0f, -0.5f, 0.5f, 0.5f, 0.5f); } }
public override void CreateParts()
{
vertices = new ModelVertex[boxVertices * boxesBuilt];
bottom = BuildRotatedBox(MakeRotatedBoxBounds(-8, 0, -8, 8, 1, 8) .TexOrigin(0, 0));
left = BuildRotatedBox(MakeRotatedBoxBounds(-7, 1, -7, -6, 6, 7).TexOrigin(34, 14));
right = BuildRotatedBox(MakeRotatedBoxBounds(6, 1, -7, 7, 6, 7) .TexOrigin(34, 14));
center = BuildRotatedBox(MakeRotatedBoxBounds(-6, 2, -6, 6, 7, 5) .TexOrigin(0, 17));
top = BuildRotatedBox(MakeRotatedBoxBounds(-2, 7, -3, 2, 8, 3) .TexOrigin(46, 14));
topLeft = BuildRotatedBox(MakeRotatedBoxBounds(-5, 7, 0, -2, 8, 3) .TexOrigin(56, 14));
topRight = BuildRotatedBox(MakeRotatedBoxBounds(2, 7, 0, 5, 8, 3) .TexOrigin(56, 14));
lineLeft = BuildRotatedBox(MakeRotatedBoxBounds(-4, 7, -7, -3, 8, 0).TexOrigin(60, 0));
lineRight = BuildRotatedBox(MakeRotatedBoxBounds(3, 7, -7, 4, 8, 0) .TexOrigin(60, 0));
back = BuildBox(MakeBoxBounds(-6, 1, 5, 6, 5, 6) .TexOrigin(34, 9));
front = BuildBox(MakeBoxBounds(-4, 2, -7, 4, 6, -6).TexOrigin(46, 21));
tray = BuildBox(MakeBoxBounds(-6, 4, 4, 6, 12, 5).TexOrigin(34, 0).RotOrigin(0, 4, 4));
}
public override void DrawModel(Entity p)
{
vScale = 1f / 64f;
game.Graphics.BindTexture(GetTexture(p));
DrawPart(bottom);
DrawPart(left);
DrawPart(right);
DrawPart(topLeft);
DrawPart(topRight);
DrawTranslate(0f, 0f, 0.75f / 16f, back);
DrawTranslate(0f, 0.5f / 16f, 0f, front);
DrawTranslate(0f, 0f, -0.5f / 16f, center);
DrawTranslate(0f, -0.5f / 16f, 0f, top);
DrawTranslate(0f, -0.5f / 16f, 0f, lineLeft);
DrawTranslate(0f, -0.5f / 16f, 0f, lineRight);
DrawTranslateAndRotate(0f, 0.5f / 16f, 0.75f / 16f, (float)Math.PI / 8f, 0f, 0f, tray);
UpdateVB();
}
private void DrawTranslate(float dispX, float dispY, float dispZ, ModelPart part)
{
VertexP3fT2fC4b vertex = default(VertexP3fT2fC4b);
VertexP3fT2fC4b[] finVertices = game.ModelCache.vertices;
for (int i = 0; i < part.Count; i++)
{
ModelVertex v = vertices[part.Offset + i];
vertex.X = v.X + dispX; vertex.Y = v.Y + dispY; vertex.Z = v.Z + dispZ;
vertex.Col = cols[i >> 2];
vertex.U = (v.U & UVMask) * uScale - (v.U >> UVMaxShift) * 0.01f * uScale;
vertex.V = (v.V & UVMask) * vScale - (v.V >> UVMaxShift) * 0.01f * vScale;
finVertices[index++] = vertex;
}
}
private void DrawTranslateAndRotate(float dispX, float dispY, float dispZ, float rotX, float rotY, float rotZ, ModelPart part)
{
float cosX = (float)Math.Cos(-rotX), sinX = (float)Math.Sin(-rotX);
float cosY = (float)Math.Cos(-rotY), sinY = (float)Math.Sin(-rotY);
float cosZ = (float)Math.Cos(-rotZ), sinZ = (float)Math.Sin(-rotZ);
VertexP3fT2fC4b vertex = default(VertexP3fT2fC4b);
VertexP3fT2fC4b[] finVertices = game.ModelCache.vertices;
for (int i = 0; i < part.Count; i++)
{
ModelVertex v = vertices[part.Offset + i];
// Prepare the vertex coordinates for rotation
v.X -= part.RotX; v.Y -= part.RotY; v.Z -= part.RotZ;
float t = 0;
// Rotate locally.
if (Rotate == RotateOrder.ZYX)
{
t = cosZ * v.X + sinZ * v.Y; v.Y = -sinZ * v.X + cosZ * v.Y; v.X = t; // Inlined RotZ
t = cosY * v.X - sinY * v.Z; v.Z = sinY * v.X + cosY * v.Z; v.X = t; // Inlined RotY
t = cosX * v.Y + sinX * v.Z; v.Z = -sinX * v.Y + cosX * v.Z; v.Y = t; // Inlined RotX
}
else if (Rotate == RotateOrder.XZY)
{
t = cosX * v.Y + sinX * v.Z; v.Z = -sinX * v.Y + cosX * v.Z; v.Y = t; // Inlined RotX
t = cosZ * v.X + sinZ * v.Y; v.Y = -sinZ * v.X + cosZ * v.Y; v.X = t; // Inlined RotZ
t = cosY * v.X - sinY * v.Z; v.Z = sinY * v.X + cosY * v.Z; v.X = t; // Inlined RotY
}
else if (Rotate == RotateOrder.YZX)
{
t = cosY * v.X - sinY * v.Z; v.Z = sinY * v.X + cosY * v.Z; v.X = t; // Inlined RotY
t = cosZ * v.X + sinZ * v.Y; v.Y = -sinZ * v.X + cosZ * v.Y; v.X = t; // Inlined RotZ
t = cosX * v.Y + sinX * v.Z; v.Z = -sinX * v.Y + cosX * v.Z; v.Y = t; // Inlined RotX
}
vertex.X = v.X + part.RotX; vertex.Y = v.Y + part.RotY; vertex.Z = v.Z + part.RotZ;
// Translate part
vertex.X += dispX; vertex.Y += dispY; vertex.Z += dispZ;
vertex.Col = cols[i >> 2];
vertex.U = (v.U & UVMask) * uScale - (v.U >> UVMaxShift) * 0.01f * uScale;
vertex.V = (v.V & UVMask) * vScale - (v.V >> UVMaxShift) * 0.01f * vScale;
finVertices[index++] = vertex;
}
}
protected const int boxesBuilt = 12;
protected ModelPart bottom, back, tray, front, center, topLeft, topRight, lineLeft, lineRight;
protected ModelPart left, right, top;
}
}