-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
87 lines (73 loc) · 2.85 KB
/
main.js
File metadata and controls
87 lines (73 loc) · 2.85 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
const canvas = document.querySelector('#canv');
const ctx = canvas.getContext('2d')
let Vertex = function(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
let Vertex2D = function(x, y) {
this.x = x;
this.y = y;
}
let project = function(M) {
// console.log(M.x);
// console.log(M);
return new Vertex2D(M.x, M.z);
}
let Cube = function(center, size) {
rad = size/2;
this.verticies = [
// Przód
new Vertex(center.x - rad, center.y - rad, center.z - rad), // Lewy dolny 0
new Vertex(center.x - rad, center.y + rad, center.z - rad), // Prawy dolny 1
new Vertex(center.x - rad, center.y + rad, center.z + rad), // Prawy górny 2
new Vertex(center.x - rad, center.y - rad, center.z + rad), // Lewy górny 3
// Tył
new Vertex(center.x + rad, center.y - rad, center.z + rad), // Lewy górny 4
new Vertex(center.x + rad, center.y + rad, center.z + rad), // Prawy górny 5
new Vertex(center.x + rad, center.y + rad, center.z - rad), // Prawy dolny 6
new Vertex(center.x + rad, center.y - rad, center.z - rad), // Lewy dolny 7
];
this.faces = [
// Front
[this.verticies[0], this.verticies[1], this.verticies[3]],
[this.verticies[1], this.verticies[2], this.verticies[3]],
// Right
[this.verticies[1], this.verticies[6], this.verticies[5]],
[this.verticies[1], this.verticies[2], this.verticies[5]],
// Back
[this.verticies[5], this.verticies[6], this.verticies[7]],
[this.verticies[5], this.verticies[7], this.verticies[4]],
// Left
[this.verticies[7], this.verticies[4], this.verticies[3]],
[this.verticies[3], this.verticies[0], this.verticies[7]],
// Bottom
[this.verticies[0], this.verticies[7], this.verticies[1]],
[this.verticies[1], this.verticies[6], this.verticies[7]],
// Top
[this.verticies[3], this.verticies[4], this.verticies[5]],
[this.verticies[3], this.verticies[2], this.verticies[5]],
];
}
let cube = new Cube(new Vertex(0, 0, 0), 200);
let objects = [cube];
let renderOrt = function(objects, ctx) {
const dx = ctx.canvas.clientWidth / 2;
const dy = ctx.canvas.clientHeight / 2;
for(let i = 0; i < objects.length; i++) {
for(let j = 0; j < objects[i].faces.length; j++) {
let face = objects[i].faces[j];
let P = project(face[0]);
ctx.beginPath();
ctx.moveTo(P.x + dx, -P.y + dy);
for(let k = 1; k < face.length; k++) {
let P = project(face[k]);
ctx.lineTo(P.x + dx, -P.y + dy);
}
ctx.closePath();
ctx.stroke();
console.count();
}
}
}
renderOrt(objects, ctx);