-
Notifications
You must be signed in to change notification settings - Fork 0
/
10hiddensurface2dz-buffering.html
73 lines (66 loc) · 1.8 KB
/
10hiddensurface2dz-buffering.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2d Z-buffering</title>
<script type="text/javascript" src="utils.js"></script>
</head>
<body>
<canvas id="container" width="800" height="800"></canvas>
<script>
var polygon1 = {
vertex : [[120, 120], [220, 120], [220, 220], [120, 220]],
z : 0,
color : [255, 0, 0, 255]
};
var polygon2 = {
vertex : [[150, 120], [220, 120], [220, 220], [150, 220]],
z : 1,
color : [0, 255, 0, 255]
};
var polygon3 = {
vertex : [[100, 200], [200, 200], [200, 300], [100, 300]],
z : 0,
color : [0, 0, 255, 255]
};
var polyList = [polygon1, polygon2, polygon3];
var xMin = 100, xMax = 300, yMin = 100, yMax = 300; // 200 x 200
var context, width, pixels;
draw();
function draw() {
var canvas = document.getElementById("container");
context = canvas.getContext("2d");
width = canvas.attributes.width.value;
var height = canvas.attributes.height.value;
var imgData = context.getImageData(0, 0, width, height);
pixels = imgData.data;
zBuffering();
context.putImageData(imgData, 0, 0);
// red border rectangle
context.strokeStyle = "rgba(255, 0, 0, 1.0)";
context.strokeRect(xMin, yMin, xMax - xMin, yMax - yMin);
}
function zBuffering() {
var zb, pk, indexP;
for (var x = xMin; x < xMax; x++) {
for (var y = yMin; y < yMax; y++) {
zb = -1;
for (var p = 0; p < polyList.length; p++) {
pk = polyList[p];
if (pointInPolygon(x, y, pk.vertex)) {
if (pk.z > zb) {
zb = pk.z;
indexP = p;
}
}
}
if (zb != -1) {
var color = polyList[indexP].color;
drawPixel(x, y, width, pixels, color);
}
}
}
}
</script>
</body>
</html>