-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment2.js
167 lines (136 loc) · 5.43 KB
/
assignment2.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"use strict";
window.onload = function init() {
var gl;
var program;
var index = 0;
var mousedown = false;
var strips = [];
var strip = [];
var canvas = document.getElementById("gl-canvas");
var thickness = document.getElementById("thickness").value;
var red = document.getElementById("red").value;
var green = document.getElementById("green").value;
var blue = document.getElementById("blue").value;
var straight = document.getElementById("straight-lines").checked;
updateLineAppearance();
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) { alert("WebGL isn't available"); }
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
var gray = 45 / 255.0;
gl.clearColor(gray, gray, gray, 1.0);
program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
gl.clear(gl.COLOR_BUFFER_BIT);
var vBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
gl.bufferData(gl.ARRAY_BUFFER, 100000, gl.STATIC_DRAW);
var cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
var vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
gl.bufferData(gl.ARRAY_BUFFER, 200000, gl.STATIC_DRAW);
render();
function addPoint(event) {
var point = coord(event);
var color = getColor();
if (straight) {
if (strip.length == 2) {
strip.pop();
index -= 4;
}
}
strip.push(point);
if (strip.length >= 2) {
var first = strip[strip.length - 2];
var normal = normalvec2(point, first);
if (strip.length == 2) {
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 8 * index, flatten(subtract(first, normal)));
gl.bufferSubData(gl.ARRAY_BUFFER, 8 * (index+1), flatten(add(first, normal)));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 16 * index, color);
gl.bufferSubData(gl.ARRAY_BUFFER, 16 * (index+1), color);
index += 2;
}
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 8 * index, flatten(subtract(point, normal)));
gl.bufferSubData(gl.ARRAY_BUFFER, 8 * (index+1), flatten(add(point, normal)));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 16 * index, color);
gl.bufferSubData(gl.ARRAY_BUFFER, 16 * (index+1), color);
index += 2;
}
}
function normalvec2(a, b) {
var sub = subtract(a, b);
return mult(normalize(vec2(-sub[1], sub[0])), vec2(thickness, thickness));
}
function coord(event) {
// TODO: werkt alleen als canvas helemaal linksboven staat.
var rect = canvas.getBoundingClientRect();
return vec2(2 * (event.clientX - rect.left) / canvas.width - 1,
2 * (canvas.height - (event.clientY - rect.top)) / canvas.height - 1);
}
function getColor() {
return flatten(vec4(red / 255.0, green / 255.0, blue / 255.0, 1.0));
}
function updateLineAppearance() {
document.getElementById("line-appearance").style.backgroundColor = "rgb(" + red + "," + green + "," + blue + ")";
document.getElementById("line-appearance").style.height = (thickness * canvas.width) + "px";
}
function render() {
gl.clear(gl.COLOR_BUFFER_BIT);
var first = 0;
for (var i = 0; i < strips.length; i++) {
if (strips[i].length > 1) {
gl.drawArrays(gl.TRIANGLE_STRIP, first, strips[i].length * 2);
}
first += strips[i].length * 2;
}
window.requestAnimFrame(render);
}
canvas.addEventListener("mousedown", function(event) {
mousedown = true;
strip = [];
strips.push(strip);
addPoint(event);
});
canvas.addEventListener("mouseup", function() {
mousedown = false;
if (straight) {
addPoint(event);
}
});
canvas.addEventListener("mousemove", function(event) {
if (mousedown) {
addPoint(event);
}
});
document.getElementById("clear-canvas").addEventListener("click", function() {
strips = [];
index = 0;
});
document.getElementById("thickness").addEventListener("input", function(event) {
thickness = event.target.value;
updateLineAppearance();
});
document.getElementById("red").addEventListener("input", function(event) {
red = event.target.value;
updateLineAppearance();
});
document.getElementById("green").addEventListener("input", function(event) {
green = event.target.value;
updateLineAppearance();
});
document.getElementById("blue").addEventListener("input", function(event) {
blue = event.target.value;
updateLineAppearance();
});
document.getElementById("straight-lines").addEventListener("change", function(event) {
straight = event.target.checked;
});
}