1
+ var WebGL = function ( ) {
2
+ var gl , shaderProgram ;
3
+
4
+ gl = initializeWebGL ( gl ) ;
5
+
6
+ //Step 1 (Set background color): First specify the color with the help of Quadlet(R,G,B,Alpha) and the clear the buffer related to background.
7
+ gl . clearColor ( 0 , 0 , 0 , 1.0 ) ;
8
+ gl . clear ( gl . COLOR_BUFFER_BIT ) ;
9
+ //Note: The default background color in WebGl is white.
10
+
11
+ //Step 2 (Speficy vertices data): Speficy the coordinates (X,Y,Z) of the geometry and other information related to each coordinates.
12
+ var verticesDataArrayJS =
13
+ [ // X, Y, Z
14
+ - 0.3 , 0.7 , 0 , //A
15
+ 0.3 , 0.7 , 0 , //B
16
+ 0 , 0 , 0 , //C
17
+ 0.3 , - 0.7 , 0 , //D
18
+ - 0.3 , - 0.7 , 0 //E
19
+ ] ;
20
+
21
+ //Step 3 (Specify how to connect the points): Specify the order with which the coordinates defined in Step2 will be joined.
22
+ var IndicesArrayJS =
23
+ [
24
+ // A-0, B-1, C-2, D-3, E-4
25
+ 0 , 1 , 2 , //ABC
26
+ 2 , 3 , 4 //CDE
27
+ ] ;
28
+
29
+ //Step 4 (Create GPU meomry buffer): In the GPU for holding vertices data of type ARRAY_BUFFER.
30
+ //gl.ARRAY_BUFFER type is used for holding vertex coordinates, color, texture coordinates or other informations on the GPU memory.
31
+ var rectVBO = gl . createBuffer ( ) ;
32
+ gl . bindBuffer ( gl . ARRAY_BUFFER , rectVBO ) ;
33
+
34
+ //Step 5 (Pass the vertices data to the buffer created previously).
35
+ gl . bufferData ( gl . ARRAY_BUFFER , new Float32Array ( verticesDataArrayJS ) , gl . STATIC_DRAW ) ;
36
+ //Notes: gl.STATIC_DRAW is used to specify that the data is passed once.
37
+ //gl.DYNAMIC_DRAW is used to specify that the data may be respecified repeatedly.
38
+ //gl.STREAM_DRAW is used to specify that the data may be respecified but not frequently.
39
+
40
+ //Step 6 (Pass the indices data to GPU buffer): repeat the steps 4 and 5 for the indices data but use ELEMENT_ARRAY_BUFFER.
41
+ //ELEMENT_ARRAY_BUFFER is used for holding the indices information on the GPU memory
42
+ var rectIBO = gl . createBuffer ( ) ;
43
+ gl . bindBuffer ( gl . ELEMENT_ARRAY_BUFFER , rectIBO ) ;
44
+ gl . bufferData ( gl . ELEMENT_ARRAY_BUFFER , new Uint16Array ( IndicesArrayJS ) , gl . STATIC_DRAW ) ;
45
+
46
+ //Seven Steps Shader side coding in JS to get the shader program.
47
+ shaderProgram = getShaderProgram ( gl ) ;
48
+
49
+ //Step 14 (Use the shader program):
50
+ gl . useProgram ( shaderProgram ) ;
51
+
52
+ //Step 15 (Get access to GPU's geometry coordinates): Get the pointer to the geometry coordinates defined in vertex shader through the shader program.
53
+ var positionAttribLocation = gl . getAttribLocation ( shaderProgram , 'geometryCoordinatesGPU' ) ;
54
+
55
+
56
+ //Step 16 (Enable Vertex Attribute Array): It enables the pointer defined in Step 8 to access the vertex buffered data.
57
+ gl . enableVertexAttribArray ( positionAttribLocation ) ;
58
+
59
+ //Step 17 (Buffer data definition): Define how the data on the GPU buffer is arranged. SO that the pointer defined in Step 8 can access the data from the buffer.
60
+ gl . vertexAttribPointer (
61
+ positionAttribLocation , // Attribute location
62
+ 3 , // Number of elements per attribute
63
+ gl . FLOAT , // Type of elements
64
+ gl . FALSE ,
65
+ 3 * Float32Array . BYTES_PER_ELEMENT , // Size of an individual vertex
66
+ 0 // Offset from the beginning of a single vertex to this attribute
67
+ ) ;
68
+
69
+ //Step 18 (Draw the geometry): Issue the draw command to generate the geometry as defined by the indices and the type of primitive to create.
70
+ //
71
+ //gl.drawElements(gl.LINES, IndicesArrayJS.length, gl.UNSIGNED_SHORT, 0);
72
+ //gl.drawElements(gl.LINE_STRIP, IndicesArrayJS.length, gl.UNSIGNED_SHORT, 0);
73
+ //gl.drawElements(gl.LINE_LOOP, IndicesArrayJS.length, gl.UNSIGNED_SHORT, 0);
74
+ gl . drawElements ( gl . TRIANGLES , IndicesArrayJS . length , gl . UNSIGNED_SHORT , 0 ) ;
75
+ } ;
76
+
77
+ function initializeWebGL ( gl )
78
+ {
79
+ var canvas = document . getElementById ( 'canvas' ) ;
80
+
81
+ canvas . width = window . innerWidth ; ;
82
+ canvas . height = window . innerHeight ; ;
83
+
84
+ gl = canvas . getContext ( 'webgl' ) ;
85
+
86
+ if ( ! gl ) {
87
+ console . log ( 'WebGL not supported, falling back on experimental-webgl' ) ;
88
+ gl = canvas . getContext ( 'experimental-webgl' ) ;
89
+ }
90
+
91
+ if ( ! gl ) {
92
+ alert ( 'Your browser does not support WebGL' ) ;
93
+ return ;
94
+ }
95
+ return gl ;
96
+ }
97
+
98
+ //Seven steps of Shader side coding
99
+ function getShaderProgram ( gl )
100
+ {
101
+ //Step 7 (Define vertex shader text): Define the code of the vertex shader in the form of JS text.
102
+ var vertexShaderText =
103
+ ' precision mediump float; ' +
104
+ ' attribute vec3 geometryCoordinatesGPU; ' +
105
+ ' void main() ' +
106
+ ' { ' +
107
+ ' gl_Position = vec4(geometryCoordinatesGPU, 1.0); ' +
108
+ ' } ' ;
109
+
110
+ //Step 8 (Create actual vertex shader): Create the actual vertex shader with the text defined in Step 1.
111
+ var vertexShader = gl . createShader ( gl . VERTEX_SHADER ) ;
112
+ gl . shaderSource ( vertexShader , vertexShaderText ) ;
113
+
114
+ //Step 9 (Compile vertex shader):
115
+ gl . compileShader ( vertexShader ) ;
116
+ if ( ! gl . getShaderParameter ( vertexShader , gl . COMPILE_STATUS ) ) {
117
+ console . error ( 'ERROR compiling vertex shader!' , gl . getShaderInfoLog ( vertexShader ) ) ;
118
+ return ;
119
+ }
120
+
121
+ //Step 10: Repeat the above 3 steps for fragment shader.
122
+ var fragmentShaderText =
123
+ ' void main() ' +
124
+ ' { ' +
125
+ ' gl_FragColor = vec4(0, 1, 0, 1); ' +
126
+ ' } ' ;
127
+
128
+ var fragmentShader = gl . createShader ( gl . FRAGMENT_SHADER ) ;
129
+ gl . shaderSource ( fragmentShader , fragmentShaderText ) ;
130
+
131
+ gl . compileShader ( fragmentShader ) ;
132
+ if ( ! gl . getShaderParameter ( fragmentShader , gl . COMPILE_STATUS ) ) {
133
+ console . error ( 'ERROR compiling fragment shader!' , gl . getShaderInfoLog ( fragmentShader ) ) ;
134
+ return ;
135
+ }
136
+
137
+ //Step 11 (Shader program): With the compiled vertex and fragment shader, create the shader program.
138
+ var shaderProgram = gl . createProgram ( ) ;
139
+ gl . attachShader ( shaderProgram , vertexShader ) ;
140
+ gl . attachShader ( shaderProgram , fragmentShader ) ;
141
+
142
+ //Step 12 (Link shader program):
143
+ gl . linkProgram ( shaderProgram ) ;
144
+ if ( ! gl . getProgramParameter ( shaderProgram , gl . LINK_STATUS ) ) {
145
+ console . error ( 'ERROR linking program!' , gl . getProgramInfoLog ( shaderProgram ) ) ;
146
+ return ;
147
+ }
148
+
149
+ //Step 13 (Validate Shader program): Checks if the shader program has been succesfully linked and can be used further.
150
+ gl . validateProgram ( shaderProgram ) ;
151
+ if ( ! gl . getProgramParameter ( shaderProgram , gl . VALIDATE_STATUS ) ) {
152
+ console . error ( 'ERROR validating program!' , gl . getProgramInfoLog ( shaderProgram ) ) ;
153
+ return ;
154
+ }
155
+ return shaderProgram ;
156
+ }
0 commit comments