forked from luboslenco/kha3d_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Empty.hx
147 lines (129 loc) · 3.73 KB
/
Empty.hx
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
140
141
142
143
144
145
146
147
package ;
import kha.Framebuffer;
import kha.Color;
import kha.math.FastMatrix4;
import kha.math.FastVector3;
import khage.g4.Buffer;
using Khage;
class Empty{
// An array of vertices to form a cube
static var vertices:Array<Float> = [
-1.0,-1.0,-1.0,
-1.0,-1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, 1.0,-1.0,
-1.0,-1.0,-1.0,
-1.0, 1.0,-1.0,
1.0,-1.0, 1.0,
-1.0,-1.0,-1.0,
1.0,-1.0,-1.0,
1.0, 1.0,-1.0,
1.0,-1.0,-1.0,
-1.0,-1.0,-1.0,
-1.0,-1.0,-1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0,-1.0,
1.0,-1.0, 1.0,
-1.0,-1.0, 1.0,
-1.0,-1.0,-1.0,
-1.0, 1.0, 1.0,
-1.0,-1.0, 1.0,
1.0,-1.0, 1.0,
1.0, 1.0, 1.0,
1.0,-1.0,-1.0,
1.0, 1.0,-1.0,
1.0,-1.0,-1.0,
1.0, 1.0, 1.0,
1.0,-1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0,-1.0,
-1.0, 1.0,-1.0,
1.0, 1.0, 1.0,
-1.0, 1.0,-1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
1.0,-1.0, 1.0
];
// Array of colors for each cube vertex
static var colors:Array<Float> = [
0.583, 0.771, 0.014,
0.609, 0.115, 0.436,
0.327, 0.483, 0.844,
0.822, 0.569, 0.201,
0.435, 0.602, 0.223,
0.310, 0.747, 0.185,
0.597, 0.770, 0.761,
0.559, 0.436, 0.730,
0.359, 0.583, 0.152,
0.483, 0.596, 0.789,
0.559, 0.861, 0.639,
0.195, 0.548, 0.859,
0.014, 0.184, 0.576,
0.771, 0.328, 0.970,
0.406, 0.615, 0.116,
0.676, 0.977, 0.133,
0.971, 0.572, 0.833,
0.140, 0.616, 0.489,
0.997, 0.513, 0.064,
0.945, 0.719, 0.592,
0.543, 0.021, 0.978,
0.279, 0.317, 0.505,
0.167, 0.620, 0.077,
0.347, 0.857, 0.137,
0.055, 0.953, 0.042,
0.714, 0.505, 0.345,
0.783, 0.290, 0.734,
0.722, 0.645, 0.174,
0.302, 0.455, 0.848,
0.225, 0.587, 0.040,
0.517, 0.713, 0.338,
0.053, 0.959, 0.120,
0.393, 0.621, 0.362,
0.673, 0.211, 0.457,
0.820, 0.883, 0.371,
0.982, 0.099, 0.879
];
var buffer:Buffer<{pos:Vec3,col:Vec3}>;
var mvp:FastMatrix4;
public function new() {
// Projection matrix: 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
var projection = FastMatrix4.perspectiveProjection(45.0, 4.0 / 3.0, 0.1, 100.0);
// Or, for an ortho camera
//var projection = Matrix4.orthogonalProjection(-10.0, 10.0, -10.0, 10.0, 0.0, 100.0); // In world coordinates
// Camera matrix
var view = FastMatrix4.lookAt(new FastVector3(4, 3, 3), // Camera is at (4, 3, 3), in World Space
new FastVector3(0, 0, 0), // and looks at the origin
new FastVector3(0, 1, 0) // Head is up (set to (0, -1, 0) to look upside-down)
);
// Model matrix: an identity matrix (model will be at the origin)
var model = FastMatrix4.identity();
// Our ModelViewProjection: multiplication of our 3 matrices
// Remember, matrix multiplication is the other way around
mvp = FastMatrix4.identity();
mvp = mvp.multmat(projection);
mvp = mvp.multmat(view);
mvp = mvp.multmat(model);
var numVertices : Int = Std.int(vertices.length/3);
buffer = new Buffer<{pos:Vec3,col:Vec3}>(numVertices,numVertices,StaticUsage);
buffer.rewind();
for (i in 0...numVertices) {
buffer.write_pos(vertices[i*3+0],vertices[i*3+1],vertices[i*3+2]);
buffer.write_col(colors[i*3+0],colors[i*3+1],colors[i*3+2]);
}
for (i in 0...numVertices) {
buffer.writeIndex(i);
}
}
public function render(frame:Framebuffer) {
// A graphics object which lets us perform 3D operations
frame.usingG4({
// Clear screen
g4.clear(Color.fromFloats(0.0, 0.0, 0.3), 1.0);
g4.usingPipeline("simple.vert","simple.frag",{depth:{write:true, mode: CompareMode.Less}},{
pipeline.set_MVP(mvp);
pipeline.draw(buffer);
});
});
}
}