-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsketch.js
189 lines (154 loc) · 5.4 KB
/
sketch.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
let looping = true;
let keysActive = true;
let socket, cnvs, ctx, canvasDOM;
let fileName = "./frames/sketch";
let maxFrames = 20;
let gl, shaderProgram;
let vertices = [];
let colors = [];
let indices = [];
let amountOfLines = 0;
let r, r2;
let amplitude;
function setup() {
socket = io.connect('http://localhost:8080');
socket.on('receiveOSC', function(data) {
console.log(data.args);
// console.log(data.args[0].value);
amplitude = data.args[0].value;
});
pixelDensity(1);
// cnvs = createCanvas(windowWidth, windowWidth / 16 * 9, WEBGL);
noCanvas();
cnvs = document.getElementById('my_Canvas');
gl = cnvs.getContext('webgl', { preserveDrawingBuffer: true });
r = new Ribbon(2, 25, 2, "left");
r2 = new Ribbon(2, 50, 2, "left");
// canvasDOM = document.getElementById('my_Canvas');
// canvasDOM = document.getElementById('defaultCanvas0');
// gl = canvasDOM.getContext('webgl');
// gl = cnvs.drawingContext;
// gl = canvasDOM.getContext('webgl', { premultipliedAlpha: false });
// gl.colorMask(false, false, false, true);
// gl.colorMask(false, false, false, true);
// Clear the canvas
gl.clearColor(0.0, 0.0, 0.0, 1.0);
// Enable the depth test
gl.enable(gl.DEPTH_TEST);
gl.depthMask(false);
// Clear the color buffer bit
gl.clear(gl.COLOR_BUFFER_BIT);
// gl.colorMask(true, true, true, true);
// gl.clear(gl.COLOR_BUFFER_BIT);
gl.enable(gl.BLEND);
// gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
// gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
// gl.blendFunc(gl.SRC_ALPHA, gl.DST_ALPHA);
// gl.blendFunc(gl.SRC_ALPHA, gl.DST_ALPHA);
// Set the view port
gl.viewport(0, 0, cnvs.width, cnvs.height);
frameRate(30);
background(0);
fill(255, 50);
noStroke();
if (!looping) {
noLoop();
}
}
let ww;
function draw() {
ww = map(sin(frameCount * 0.05), -1, 1, 0.05, 1);
rectangles = 0;
let t = frameCount * 5;
let osc = 0.1;
vertices = [];
colors = [];
indices = [];
let rectangle;
rectangle = makeQuad({
c: [0.0, 0.0, 0.0, 1.0],
v: [
[-2 + (Math.sin(t * 0.05) * osc), -2 + (Math.cos(t * 0.05) * osc)],
[2 + (Math.sin(t * 0.015) * osc), -2 + (Math.cos(t * 0.015) * osc)],
[2 + (Math.sin(t * 0.015) * osc), 2 + (Math.cos(t * 0.015) * osc)],
[-2 + (Math.sin(t * 0.05) * osc), 2 + (Math.cos(t * 0.05) * osc)]
]
});
addRectangleToBuffers(rectangle);
// makeLine(0, 0, 0, 1);
// makeLine(1, 0, 1, 1);
r.upgrade();
r.show();
r2.upgrade();
r2.show();
// Create an empty buffer object and store vertex data
var vertex_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
// Create an empty buffer object and store Index data
var Index_Buffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
// Create an empty buffer object and store color data
var color_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, color_buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
setShaders();
/* ======== Associating shaders to buffer objects =======*/
// Bind vertex buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
// Bind index buffer object
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);
// Get the attribute location
var coord = gl.getAttribLocation(shaderProgram, "coordinates");
// point an attribute to the currently bound VBO
gl.vertexAttribPointer(coord, 3, gl.FLOAT, false, 0, 0);
// Enable the attribute
gl.enableVertexAttribArray(coord);
// bind the color buffer
gl.bindBuffer(gl.ARRAY_BUFFER, color_buffer);
// get the attribute location
var color = gl.getAttribLocation(shaderProgram, "color");
// point attribute to the volor buffer object
gl.vertexAttribPointer(color, 4, gl.FLOAT, false, 0, 0);
// enable the color attribute
gl.enableVertexAttribArray(color);
/*============Drawing the Quad====================*/
// gl.clear(gl.COLOR_BUFFER_BIT);
// gl.colorMask(false, false, false, true);
// gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// gl.colorMask(true, true, true, true);
// gl.enable(gl.BLEND);
// gl.blendFunc(gl.SRC_ALPHA, gl.DST_ALPHA);
//Draw the triangle
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
if (exporting && frameCount < maxFrames) {
frameExport();
}
}
function keyPressed() {
if (keysActive) {
if (keyCode === 32) {
if (looping) {
noLoop();
looping = false;
} else {
loop();
looping = true;
}
}
if (key == 'p' || key == 'P') {
frameExport();
}
if (key == 'r' || key == 'R') {
window.location.reload();
}
if (key == 'm' || key == 'M') {
redraw();
}
}
}