-
Notifications
You must be signed in to change notification settings - Fork 0
/
skybox.js
113 lines (95 loc) · 3.59 KB
/
skybox.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
/**
* @fileoverview This file is concerned with all parts of the rendering process for the skybox.
* It sets up the vertex position and tri-index buffers. It also contains the helper
* function to draw the skybox. This file is dependent upon gl-matrix-min.js and webgl-utils.js.
*/
/**
* Function to setup the vertex and tri-index buffers for the skybox cube.
* @return None
*/
function setupSkybox() {
// Create a buffer for the cube's vertices.
cubeVertexBuffer = gl.createBuffer();
// Select the cubeVerticesBuffer as the one to apply vertex
// operations to from here out.
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexBuffer);
// Now create an array of vertices for the cube.
var vertices = [
// Front face
-100.0, -100.0, 100.0,
100.0, -100.0, 100.0,
100.0, 100.0, 100.0,
-100.0, 100.0, 100.0,
// Back face
-100.0, -100.0, -100.0,
-100.0, 100.0, -100.0,
100.0, 100.0, -100.0,
100.0, -100.0, -100.0,
// Top face
-100.0, 100.0, -100.0,
-100.0, 100.0, 100.0,
100.0, 100.0, 100.0,
100.0, 100.0, -100.0,
// Bottom face
-100.0, -100.0, -100.0,
100.0, -100.0, -100.0,
100.0, -100.0, 100.0,
-100.0, -100.0, 100.0,
// Right face
100.0, -100.0, -100.0,
100.0, 100.0, -100.0,
100.0, 100.0, 100.0,
100.0, -100.0, 100.0,
// Left face
-100.0, -100.0, -100.0,
-100.0, -100.0, 100.0,
-100.0, 100.0, 100.0,
-100.0, 100.0, -100.0
];
// Now pass the list of vertices into WebGL to build the shape. We
// do this by creating a Float32Array from the JavaScript array,
// then use it to fill the current vertex buffer.
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
// Build the element array buffer; this specifies the indices
// into the vertex array for each face's vertices.
cubeTriIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeTriIndexBuffer);
// This array defines each face as two triangles, using the
// indices into the vertex array to specify each triangle's
// position.
var cubeVertexIndices = [
0, 1, 2, 0, 2, 3, // front
4, 5, 6, 4, 6, 7, // back
8, 9, 10, 8, 10, 11, // top
12, 13, 14, 12, 14, 15, // bottom
16, 17, 18, 16, 18, 19, // right
20, 21, 22, 20, 22, 23 // left
]
// Now send the element array to GL
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW);
}
/**
* Helper function to draw() routine to set the vertex positions before drawing the
* skybox for each frame. Also switches the shader to the skybox settings.
* @return None
*/
function drawSkybox(){
switchShaders(true);
// Draw the cube by binding the array buffer to the cube's vertices
// array, setting attributes, and pushing it to GL.
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);
// Draw the cube by binding the array buffer to the cube's vertices
// array, setting attributes, and pushing it to GL.
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexBuffer);
gl.vertexAttribPointer(shaderProgram.vertexNormalAttribute, 3, gl.FLOAT, false, 0, 0);
// Specify the texture to map onto the faces.
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubeTexture);
gl.uniform1i(gl.getUniformLocation(shaderProgram, "uSampler"), 0);
// Draw the cube.
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeTriIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);
}