-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
150 lines (113 loc) · 4.33 KB
/
script.js
File metadata and controls
150 lines (113 loc) · 4.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
var gl;
var program;
var modelMatrix2dUniformLocation;
var viewMatrixUniformLocation;
var projectionMatrixUniformLocation;
var positionAttributeLocation;
var positionBuffer;
/** Initialize WebGL (only once) */
function init() {
/** Get canvas element */
var canvas = document.getElementById("webgl-canvas");
/** Create a WebGL context in the canvas */
gl = canvas.getContext("webgl");
/** Abort if creating a context failed */
if (!gl) return false;
/** Set clear color */
gl.clearColor(0.2, 0.2, 0.2, 1.0);
/** [helpers.js] Create shader program */
program = createShaderProgram(gl, "vertex-shader", "fragment-shader");
/** Bind uniforms to JS variables (think of IDs) */
modelMatrix2dUniformLocation = gl.getUniformLocation(program, "u_modelMatrix2d");
viewMatrixUniformLocation = gl.getUniformLocation(program, "u_viewMatrix");
projectionMatrixUniformLocation = gl.getUniformLocation(program, "u_projectionMatrix");
/** Create a binding for the vertex data to the shader */
positionAttributeLocation = gl.getAttribLocation(program, "a_position");
/** Create buffer for the vertex data */
positionBuffer = gl.createBuffer();
/** Create array with vertex data */
var vertices = [
/** x, y */
0.0, 0.0, /** Vertex bottom-left */
0.0, 0.5, /** Vertex top-left */
0.5, 0.0, /** Vertex bottom-right */
];
/** Bind buffer to WebGL */
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
/** Fill bound buffer with array (only bound buffers can be filled with data!) */
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
return true;
}
/** Must be called when data is modified */
function render() {
/** Set the canvas size to the current size of the browser content */
gl.canvas.width = gl.canvas.clientWidth;
gl.canvas.height = gl.canvas.clientHeight;
/** Then set the viewport of WebGL */
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
/** Clear back buffer with the previously set clear color */
gl.clear(gl.COLOR_BUFFER_BIT);
/** Activate shader program */
gl.useProgram(program);
/** Bind a_position for the following functions */
gl.enableVertexAttribArray(positionAttributeLocation);
/** Bind the vertex buffer */
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
/** Set important parameters */
gl.vertexAttribPointer(positionAttributeLocation, 2, gl.FLOAT, false, 0, 0);
/** Convert to world coordinates (Local -> World = Model) */
{
var translation = [
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
];
var rotation = [
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
];
var scale = [
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
];
var modelMatrix = math.chain(math.matrix(translation)).multiply(rotation).multiply(scale).done();
var modelMatrixData = [].concat.apply([], modelMatrix.valueOf());
gl.uniformMatrix3fv(modelMatrix2dUniformLocation, false, modelMatrixData);
}
/** TODO Convert to camera coordinates (World -> Eye = View) */
{
var identity = [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
];
var projectionMatrix = math.matrix(identity);
var projectionMatrixData = [].concat.apply([], projectionMatrix.valueOf());
gl.uniformMatrix4fv(projectionMatrixUniformLocation, false, projectionMatrixData);
}
/** TODO Convert to viewport coordinates (View -> Clip = Projection) */
{
var identity = [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
];
var viewMatrix = math.matrix(identity);
var viewMatrixData = [].concat.apply([], viewMatrix.valueOf());
gl.uniformMatrix4fv(viewMatrixUniformLocation, false, viewMatrixData);
}
/** Submit vertices to WebGL (1 draw call) */
gl.drawArrays(gl.TRIANGLES, 0, 3);
/** Repeat render function */
requestAnimationFrame(render);
}
function main() {
if (init()) {
requestAnimationFrame(render);
}
}
main();