forked from cs4241-19a/a1-gettingstarted
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawMode.js
More file actions
179 lines (150 loc) · 5.33 KB
/
DrawMode.js
File metadata and controls
179 lines (150 loc) · 5.33 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
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
168
169
170
171
172
173
174
175
176
177
178
179
var numberPoints = []; //arry of number of points in each polyline
var numP; //number of points in one polyline
var polyNum = 0; //number of polylines
var drawPoints = []; //points drawn by user
var pointIndex = 0; //keep track of index in point array
var color = []; //color of each point
var currentColor = vec4(0.0, 0.0, 0.0, 1.0); //setting default color, K to represent black
var newline = false; //determines if a new polyline
var gl;
var program;
function drawPolylines(){
var canvas = document.getElementById("webglDraw");
//grabs x,y values of where mouse clicked and converts values to fit on a scale of -1 to 1
// on both the x and y plane with the origin being the center of the canvas
var x = (event.pageX - canvas.offsetLeft) / 250.0 - 1;
var y = -((event.pageY - canvas.offsetTop) /250.0 - 1);
drawPoints.push(vec4(x, y, 0.0, 1.0)); //push x,y value as a point
if(newline || polyNum === 0){ //if nothing is drawn or is a new polyline
polyNum++; //increase number of polyline
numP = 0; //reset number of points to zero for polyline
newline = false;
}
numP++; //increment number of points with each click
numberPoints[polyNum-1] = numP;
renderDrawing();
}
function renderDrawing() {
// Retrieve <canvas> element
var canvas = document.getElementById('webglDraw');
// Get the rendering context for WebGL
gl = WebGLUtils.setupWebGL(canvas, undefined);
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
// Initialize shaders
program = initShaders(gl, "vshader", "fshader");
gl.useProgram(program);
//Set up the viewport
gl.viewport(0, 0, canvas.width, canvas.height);
//set point size to 10
var pointSizeLoc = gl.getUniformLocation(program, "vPointSize");
gl.uniform1f(pointSizeLoc, 10.0);
//clears canvas
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
//loop through to draw each polyline
for (var p = 0; p < numberPoints.length; p++){
renderLine(numberPoints[p]);
}
pointIndex = 0; //reset back to zero after finishing drawing
//handles all key press events
window.onkeypress = keyPressEvent;
}
function renderLine(numPoints) {
var polyPoints = []; //used to store points of one polyline
//retrieves all points of one polyline
for(var p = 0; p < numPoints; p++){
polyPoints.push(drawPoints[pointIndex + p]);
}
//create buffer for points
var vBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(polyPoints), gl.STATIC_DRAW);
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
setColors(currentColor, numPoints); //set each point to the same color
//create buffer for colors
var cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(color), gl.STATIC_DRAW);
var vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
var projMatrix = ortho(-1, 1, -1, 1, -1, 1);
var projMatrixLoc = gl.getUniformLocation(program, "projMatrix");
gl.uniformMatrix4fv(projMatrixLoc, false, flatten(projMatrix));
gl.drawArrays(gl.LINE_STRIP, 0, polyPoints.length); //draws the polyline
pointIndex += numPoints;
}
//setting color of each point
function setColors(curColor, numPoints){
var col = [];
for(var i = 0; i < numPoints; i++) {
col.push(curColor);
}
color = col;
}
//resets canvas to be blank
function resetCanvas(){
polyNum = 0;
drawPoints = [];
numberPoints = [];
currentColor = vec4(0.0, 0.0, 0.0, 1.0);
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
display();
}
//handle key events such as changing color by pressing 'c' or changing
// mode by pressing 'f' or starting a new line by pressing 'b'
function keyPressEvent(event){
var key = event.key;
switch(key){
case 'r': //adds red to color
add(0);
break;
case 'R': //subtract red from color
subtract(0);
break;
case 'g': //adds green to color
add(1);
break;
case 'G': //subtracts green from color
subtract(1);
break;
case 'b': //adds blue to color
add(2);
break;
case 'B': //subtracts blue from color
subtract(2);
break;
case 'n': //sets up for a new polyline
newline = true;
break;
}
}
//adding color
function add(c){
if(currentColor[c] <= 0.9){
currentColor[c] += 0.1
}
renderDrawing();
display();
}
//subtracting color
function subtract(c){
if(currentColor[c] >= 0.1){
currentColor[c] -= 0.1
}
renderDrawing();
display();
}
//display color values
function display(){
let r = currentColor[0].toFixed(1);
let g = currentColor[1].toFixed(1);
let b = currentColor[2].toFixed(1);
document.getElementById("rgb").innerText = "Red: " + r + " | Green: " + g + " | Blue: " + b;
}