-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpointrender.js
192 lines (126 loc) · 4.67 KB
/
pointrender.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
190
191
192
function PointRenderer(canvasId){
var canvas = document.getElementById(canvasId);
var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
var shaderProgram;
var size;
var vertexBuffer;
var colorBuffer;
var valueBuffer;
var transform;
var zoomAttr;
var pointSize;
init();
function init(){
/*=========================Shaders========================*/
// Create a vertex shader object
var vertShader = gl.createShader(gl.VERTEX_SHADER);
// Attach vertex shader source code
gl.shaderSource(vertShader, vertexShader);
// Compile the vertex shader
gl.compileShader(vertShader);
// Create fragment shader object
var fragShader = gl.createShader(gl.FRAGMENT_SHADER);
// Attach fragment shader source code
gl.shaderSource(fragShader, fragmentShader);
// Compile the fragmentt shader
gl.compileShader(fragShader);
// Create a shader program object to store
// the combined shader program
shaderProgram = gl.createProgram();
// Attach a vertex shader
gl.attachShader(shaderProgram, vertShader);
// Attach a fragment shader
gl.attachShader(shaderProgram, fragShader);
// Link both programs
gl.linkProgram(shaderProgram);
// Use the combined shader program object
gl.useProgram(shaderProgram);
vertexBuffer = gl.createBuffer();
colorBuffer = gl.createBuffer();
valueBuffer = gl.createBuffer();
}
function addVertices(vertices, colors, values){
/*==========Defining and storing the geometry=======*/
size = ~~(vertices.length/4);
gl.deleteBuffer(vertexBuffer);
gl.deleteBuffer(colorBuffer);
gl.deleteBuffer(valueBuffer);
vertexBuffer = gl.createBuffer();
colorBuffer = gl.createBuffer();
valueBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
// Get the attribute location
var coord = gl.getAttribLocation(shaderProgram, "coordinates");
// Point an attribute to the currently bound VBO
gl.vertexAttribPointer(coord, 4, gl.FLOAT, false, 0, 0);
// Enable the attribute
gl.enableVertexAttribArray(coord);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
var color = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(color);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.vertexAttribPointer(color, 4, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, valueBuffer);
gl.bufferData(gl.ARRAY_BUFFER, values, gl.STATIC_DRAW);
var valuesAttr = gl.getAttribLocation(shaderProgram, "values");
gl.enableVertexAttribArray(valuesAttr);
gl.bindBuffer(gl.ARRAY_BUFFER, valueBuffer);
gl.vertexAttribPointer(valuesAttr, 1, gl.FLOAT, false, 0, 0);
transform = gl.getUniformLocation(shaderProgram, "transform");
zoomAttr = gl.getUniformLocation(shaderProgram, "zoom");
pointSize = gl.getUniformLocation(shaderProgram, "pointSize");
}
function setColors(colors){
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
}
//var angle = 1/3 * Math.PI;
var angle = 0;
//var rotationQuaternion = [0, 0, 0, 1];
//var rotationMatrix = matrixFromQuaternion(rotationQuaternion);
function render(transformMatrix, zoom){
if(vrEnabled){
var translationMatrix = makeTranslationMatrix([0.0001, 0, 0]);
transformMatrix = matrix4Multiply(transformMatrix, translationMatrix);
}
gl.uniformMatrix4fv(transform, false, transformMatrix);
gl.uniform1f(zoomAttr, zoom);
gl.uniform1f(pointSize, 0.0001/zoom);
// Clear the canvas
gl.clearColor(0, 0, 0, 0);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
/*gl.enable(gl.BLEND);
gl.blendEquation( gl.FUNC_ADD );
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);*/
// Clear the color buffer bit
gl.clear(gl.COLOR_BUFFER_BIT);
if(vrEnabled){
// Set the view port
gl.viewport(-canvas.width/16, 0, 9*canvas.width/16, canvas.height);
// Draw the triangle
gl.drawArrays(gl.POINTS, 0, size);
translationMatrix = makeTranslationMatrix([-0.0002, 0, 0]);
transformMatrix = matrix4Multiply(transformMatrix, translationMatrix);
gl.uniformMatrix4fv(transform, false, transformMatrix);
// Set the view port
gl.viewport(9*canvas.width/16, 0, canvas.width/2, canvas.height);
// Draw the triangle
gl.drawArrays(gl.POINTS, 0, size);
} else {
// Set the view port
gl.viewport(0, 0, canvas.width, canvas.height);
// Draw the triangle
gl.drawArrays(gl.POINTS, 0, size);
}
}
return{
addVertices: addVertices
,render: render
,setColors: setColors
};
}