-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl.js
151 lines (117 loc) · 4.94 KB
/
gl.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
function initializeCanvas(canvasName) {
const canvas = document.getElementById(canvasName);
var gl = canvas.getContext("experimental-webgl");
if (!gl) { gl = canvas.getContext("webgl"); }
if (!gl) { throw new Error("Cannot get WebGL context"); }
return gl;
}
function createShader(gl, source, type) {
const shader = gl.createShader(type);
if (!shader) { throw new Error("Unable to create GL shader"); }
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
const infoLog = gl.getShaderInfoLog(shader);
gl.deleteShader(shader);
throw new Error("An error occurred compiling the shader: " + infoLog);
} else {
return shader;
}
}
function createProgram(gl, vertex, fragment) {
const program = gl.createProgram();
if (!program) throw new Error("Failed to create GLSL program");
gl.attachShader(program, createShader(gl, vertex, gl.VERTEX_SHADER));
gl.attachShader(program, createShader(gl, fragment, gl.FRAGMENT_SHADER));
gl.linkProgram(program);
gl.validateProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
const info = gl.getProgramInfoLog(program);
gl.deleteProgram(program);
throw new Error("An error occurred linking the program: " + info);
}
return program;
}
function createShape(gl, vertexData, indexData) {
var shape = {};
var vertexArray = new Float32Array(vertexData);
var vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertexArray, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
var indexArray = new Uint16Array(indexData);
var indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indexArray, gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
shape.vertexBuffer = vertexBuffer;
shape.indexBuffer = indexBuffer;
shape.size = indexData.length;
return shape;
}
function drawShape(gl, program, shape) {
gl.bindBuffer(gl.ARRAY_BUFFER, shape.vertexBuffer);
var positionLocation = gl.getAttribLocation(program, "vert_position");
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 4*8, 0);
var texCoordLocation = gl.getAttribLocation(program, "vert_texCoord");
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 4*8, 4*3);
var normalLocation = gl.getAttribLocation(program, "vert_normal");
gl.enableVertexAttribArray(normalLocation);
gl.vertexAttribPointer(normalLocation, 3, gl.FLOAT, false, 4*8, 4*5);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, shape.indexBuffer);
gl.drawElements(gl.TRIANGLES, shape.size, gl.UNSIGNED_SHORT, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
}
function createTexture(gl, image) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
// gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);
return texture;
}
function startWebGL(canvasName, vertexShader, fragmentShader, setup, during) {
var gl = initializeCanvas(canvasName);
var program = createProgram(gl, vertexShader, fragmentShader);
// program.chevron_angles = gl.getUniformLocation(program, "chevron_angles[0]");
// program.num_chevrons = gl.getUniformLocation(program, "num_chevrons");
setup(gl, program);
// var vertexData = [
// -1.0, -1.0, 0.0,
// -1.0, -1.0,
// 1.0, -1.0, 0.0,
// 1.0, -1.0,
// 1.0, 1.0, 0.0,
// 1.0, 1.0,
// -1.0, 1.0, 0.0,
// -1.0, 1.0,
// ];
// var indexData = [
// 0, 1, 2, 0, 2, 3
// ];
// var rectangle = createShape(gl, vertexData, indexData);
function updateWebGL() {
// gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(program);
//drawShape(gl, program, rectangle);
// if (program.chevron_angles != null) {
// gl.uniform1f(program.chevron_angles, GRAPH);
// }
// if (program.num_chevrons != null) {
// gl.uniform1i(program.num_chevrons, GRAPH.getNode().outgoingAngles().length);
// }
// console.log( GRAPH);
during(gl, program);
gl.useProgram(null);
window.requestAnimationFrame(updateWebGL);
}
window.requestAnimationFrame(updateWebGL);
}