-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetBuffers.js
50 lines (45 loc) · 2.44 KB
/
setBuffers.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
//**********************************************************************************//
//**********************************************************************************//
// //
// set buffers //
// //
//**********************************************************************************//
//**********************************************************************************//
function createPositionBuffer(object,positionAttributeLocation) {
let positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(object.obj.getVertices()), gl.STATIC_DRAW);
gl.enableVertexAttribArray(positionAttributeLocation);
gl.vertexAttribPointer(positionAttributeLocation, 3, gl.FLOAT, false, 0, 0);
}
function createUvBuffer(object,uvAttributeLocation) {
let uvBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, uvBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(object.obj.getTextureCoord()), gl.STATIC_DRAW);
gl.enableVertexAttribArray(uvAttributeLocation);
gl.vertexAttribPointer(uvAttributeLocation, 2, gl.FLOAT, false, 0, 0);
}
function createIndexBuffer(object) {
let indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(object.obj.getIndices()), gl.STATIC_DRAW);
}
function createNormalBuffer(object, norm) {
let normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(object.obj.getVertexNormals()), gl.STATIC_DRAW);
gl.enableVertexAttribArray(norm);
gl.vertexAttribPointer(norm, 3, gl.FLOAT, false, 0, 0);
}
function setImage(object) {
let image2 = new Image();
image2.src = baseDir + object.texturePath;
image2.onload = function () {
gl.bindTexture(gl.TEXTURE_2D, textures[object.texture]);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image2);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.generateMipmap(gl.TEXTURE_2D);
};
}