@@ -4,13 +4,15 @@ main();
4
4
// Start here
5
5
//
6
6
function main ( ) {
7
- const canvas = document . querySelector ( ' #glcanvas' ) ;
8
- const gl = canvas . getContext ( ' webgl' ) ;
7
+ const canvas = document . querySelector ( " #glcanvas" ) ;
8
+ const gl = canvas . getContext ( " webgl" ) ;
9
9
10
10
// If we don't have a GL context, give up now
11
11
12
12
if ( ! gl ) {
13
- alert ( 'Unable to initialize WebGL. Your browser or machine may not support it.' ) ;
13
+ alert (
14
+ "Unable to initialize WebGL. Your browser or machine may not support it."
15
+ ) ;
14
16
return ;
15
17
}
16
18
@@ -45,11 +47,14 @@ function main() {
45
47
const programInfo = {
46
48
program : shaderProgram ,
47
49
attribLocations : {
48
- vertexPosition : gl . getAttribLocation ( shaderProgram , ' aVertexPosition' ) ,
50
+ vertexPosition : gl . getAttribLocation ( shaderProgram , " aVertexPosition" ) ,
49
51
} ,
50
52
uniformLocations : {
51
- projectionMatrix : gl . getUniformLocation ( shaderProgram , 'uProjectionMatrix' ) ,
52
- modelViewMatrix : gl . getUniformLocation ( shaderProgram , 'uModelViewMatrix' ) ,
53
+ projectionMatrix : gl . getUniformLocation (
54
+ shaderProgram ,
55
+ "uProjectionMatrix"
56
+ ) ,
57
+ modelViewMatrix : gl . getUniformLocation ( shaderProgram , "uModelViewMatrix" ) ,
53
58
} ,
54
59
} ;
55
60
@@ -68,7 +73,6 @@ function main() {
68
73
// have one object -- a simple two-dimensional square.
69
74
//
70
75
function initBuffers ( gl ) {
71
-
72
76
// Create a buffer for the square's positions.
73
77
74
78
const positionBuffer = gl . createBuffer ( ) ;
@@ -80,30 +84,23 @@ function initBuffers(gl) {
80
84
81
85
// Now create an array of positions for the square.
82
86
83
- const positions = [
84
- 1.0 , 1.0 ,
85
- - 1.0 , 1.0 ,
86
- 1.0 , - 1.0 ,
87
- - 1.0 , - 1.0 ,
88
- ] ;
87
+ const positions = [ 1.0 , 1.0 , - 1.0 , 1.0 , 1.0 , - 1.0 , - 1.0 , - 1.0 ] ;
89
88
90
89
// Now pass the list of positions into WebGL to build the
91
90
// shape. We do this by creating a Float32Array from the
92
91
// JavaScript array, then use it to fill the current buffer.
93
92
94
- gl . bufferData ( gl . ARRAY_BUFFER ,
95
- new Float32Array ( positions ) ,
96
- gl . STATIC_DRAW ) ;
93
+ gl . bufferData ( gl . ARRAY_BUFFER , new Float32Array ( positions ) , gl . STATIC_DRAW ) ;
97
94
}
98
95
99
96
//
100
97
// Draw the scene.
101
98
//
102
99
function drawScene ( gl , programInfo ) {
103
- gl . clearColor ( 0.0 , 0.0 , 0.0 , 1.0 ) ; // Clear to black, fully opaque
104
- gl . clearDepth ( 1.0 ) ; // Clear everything
105
- gl . enable ( gl . DEPTH_TEST ) ; // Enable depth testing
106
- gl . depthFunc ( gl . LEQUAL ) ; // Near things obscure far things
100
+ gl . clearColor ( 0.0 , 0.0 , 0.0 , 1.0 ) ; // Clear to black, fully opaque
101
+ gl . clearDepth ( 1.0 ) ; // Clear everything
102
+ gl . enable ( gl . DEPTH_TEST ) ; // Enable depth testing
103
+ gl . depthFunc ( gl . LEQUAL ) ; // Near things obscure far things
107
104
108
105
// Clear the canvas before we start drawing on it.
109
106
@@ -116,19 +113,15 @@ function drawScene(gl, programInfo) {
116
113
// and we only want to see objects between 0.1 units
117
114
// and 100 units away from the camera.
118
115
119
- const fieldOfView = 45 * Math . PI / 180 ; // in radians
116
+ const fieldOfView = ( 45 * Math . PI ) / 180 ; // in radians
120
117
const aspect = gl . canvas . clientWidth / gl . canvas . clientHeight ;
121
118
const zNear = 0.1 ;
122
119
const zFar = 100.0 ;
123
120
const projectionMatrix = mat4 . create ( ) ;
124
121
125
122
// note: glmatrix.js always has the first argument
126
123
// as the destination to receive the result.
127
- mat4 . perspective ( projectionMatrix ,
128
- fieldOfView ,
129
- aspect ,
130
- zNear ,
131
- zFar ) ;
124
+ mat4 . perspective ( projectionMatrix , fieldOfView , aspect , zNear , zFar ) ;
132
125
133
126
// Set the drawing position to the "identity" point, which is
134
127
// the center of the scene.
@@ -137,9 +130,11 @@ function drawScene(gl, programInfo) {
137
130
// Now move the drawing position a bit to where we want to
138
131
// start drawing the square.
139
132
140
- mat4 . translate ( modelViewMatrix , // destination matrix
141
- modelViewMatrix , // matrix to translate
142
- [ - 0.0 , 0.0 , - 6.0 ] ) ; // amount to translate
133
+ mat4 . translate (
134
+ modelViewMatrix , // destination matrix
135
+ modelViewMatrix , // matrix to translate
136
+ [ - 0.0 , 0.0 , - 6.0 ]
137
+ ) ; // amount to translate
143
138
144
139
// Tell WebGL how to pull out the positions from the position
145
140
// buffer into the vertexPosition attribute.
@@ -150,14 +145,14 @@ function drawScene(gl, programInfo) {
150
145
const stride = 0 ;
151
146
const offset = 0 ;
152
147
gl . vertexAttribPointer (
153
- programInfo . attribLocations . vertexPosition ,
154
- numComponents ,
155
- type ,
156
- normalize ,
157
- stride ,
158
- offset ) ;
159
- gl . enableVertexAttribArray (
160
- programInfo . attribLocations . vertexPosition ) ;
148
+ programInfo . attribLocations . vertexPosition ,
149
+ numComponents ,
150
+ type ,
151
+ normalize ,
152
+ stride ,
153
+ offset
154
+ ) ;
155
+ gl . enableVertexAttribArray ( programInfo . attribLocations . vertexPosition ) ;
161
156
}
162
157
163
158
// Tell WebGL to use our program when drawing
@@ -167,13 +162,15 @@ function drawScene(gl, programInfo) {
167
162
// Set the shader uniforms
168
163
169
164
gl . uniformMatrix4fv (
170
- programInfo . uniformLocations . projectionMatrix ,
171
- false ,
172
- projectionMatrix ) ;
165
+ programInfo . uniformLocations . projectionMatrix ,
166
+ false ,
167
+ projectionMatrix
168
+ ) ;
173
169
gl . uniformMatrix4fv (
174
- programInfo . uniformLocations . modelViewMatrix ,
175
- false ,
176
- modelViewMatrix ) ;
170
+ programInfo . uniformLocations . modelViewMatrix ,
171
+ false ,
172
+ modelViewMatrix
173
+ ) ;
177
174
178
175
{
179
176
const offset = 0 ;
@@ -199,7 +196,10 @@ function initShaderProgram(gl, vsSource, fsSource) {
199
196
// If creating the shader program failed, alert
200
197
201
198
if ( ! gl . getProgramParameter ( shaderProgram , gl . LINK_STATUS ) ) {
202
- alert ( 'Unable to initialize the shader program: ' + gl . getProgramInfoLog ( shaderProgram ) ) ;
199
+ alert (
200
+ "Unable to initialize the shader program: " +
201
+ gl . getProgramInfoLog ( shaderProgram )
202
+ ) ;
203
203
return null ;
204
204
}
205
205
@@ -224,11 +224,12 @@ function loadShader(gl, type, source) {
224
224
// See if it compiled successfully
225
225
226
226
if ( ! gl . getShaderParameter ( shader , gl . COMPILE_STATUS ) ) {
227
- alert ( 'An error occurred compiling the shaders: ' + gl . getShaderInfoLog ( shader ) ) ;
227
+ alert (
228
+ "An error occurred compiling the shaders: " + gl . getShaderInfoLog ( shader )
229
+ ) ;
228
230
gl . deleteShader ( shader ) ;
229
231
return null ;
230
232
}
231
233
232
234
return shader ;
233
235
}
234
-
0 commit comments