-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicHelper.js
More file actions
102 lines (95 loc) · 3.74 KB
/
basicHelper.js
File metadata and controls
102 lines (95 loc) · 3.74 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
function createGlslProgram(gl, vertexShaderId, fragmentShaderId) {
var program = gl.createProgram();
gl.attachShader(program, createShader(gl, vertexShaderId));
gl.attachShader(program, createShader(gl, fragmentShaderId));
gl.linkProgram(program);
gl.validateProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
var infoLog = gl.getProgramInfoLog(program);
gl.deleteProgram(program);
throw new Error("An error occurred linking the program: " + infoLog);
} else {
return program;
}
}
function createShape(gl, shapeData) {
var shape = {};
var vertexData = [];
var vertexCount = shapeData.positions.length / 3;
var i;
for (i = 0; i < vertexCount; i++) {
vertexData.push(shapeData.positions[3 * i], shapeData.positions[3 * i + 1], shapeData.positions[3 * i + 2]);
vertexData.push(shapeData.normals[3 * i], shapeData.normals[3 * i + 1], shapeData.normals[3 * i + 2]);
vertexData.push(shapeData.texCoords[2 * i], shapeData.texCoords[2 * i + 1]);
}
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(shapeData.indices);
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 = shapeData.indices.length;
shape.stride = 4 * (3 + 3 + 2);
shape.positionOffset = 4 * 0;
shape.normalOffset = 4 * 3;
shape.texCoordOffset = 4 * (3 + 3);
return shape;
}
function createShader(gl, shaderScriptId) {
var shaderScript = $("#" + shaderScriptId);
var shaderSource = shaderScript[0].text;
var shaderType = null;
if (shaderScript[0].type == "x-shader/x-vertex") {
shaderType = gl.VERTEX_SHADER;
} else if (shaderScript[0].type == "x-shader/x-fragment") {
shaderType = gl.FRAGMENT_SHADER;
} else {
throw new Error("Invalid shader type: " + shaderScript[0].type)
}
var shader = gl.createShader(shaderType);
gl.shaderSource(shader, shaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
var infoLog = gl.getShaderInfoLog(shader);
gl.deleteShader(shader);
throw new Error("An error occurred compiling the shader: " + infoLog);
} else {
return shader;
}
}
function initializeWebGL(canvas) {
// Getting WebGL context the right way
var gl = null;
try {
gl = canvas[0].getContext("experimental-webgl");
if (!gl) {
gl = canvas[0].getContext("webgl");
}
} catch (error) {
}
if (!gl) {
alert("Could not get WebGL context!");
throw new Error("Could not get WebGL context!");
}
return gl;
}
function createTexture(gl, img) {
// Step 1: Create the texture object.
var texture = gl.createTexture();
// Step 2: Bind the texture object to the "target" TEXTURE_2D
gl.bindTexture(gl.TEXTURE_2D, texture);
// Step 3: (Optional) Tell WebGL that pixels are flipped vertically,
// so that we don't have to deal with flipping the y-coordinate.
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
// Step 4: Download the image data to the GPU.
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
// Step 5: Creating a mipmap so that the texture can be anti-aliased.
gl.generateMipmap(gl.TEXTURE_2D);
return texture;
}