1
+ < html >
2
+ < head >
3
+ < title > WebGL - Two Triangle Transform Program</ title >
4
+ </ head >
5
+ < body >
6
+ < canvas id ="canvas " width ="1200 " height ="600 ">
7
+ Your browser does not support HTML5
8
+ </ canvas >
9
+ < br />
10
+ < script >
11
+ var gl , shaderProgram ;
12
+
13
+ gl = initializeWebGL ( gl ) ;
14
+
15
+ //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.
16
+ gl . clearColor ( 0 , 0 , 0 , 1.0 ) ;
17
+ gl . clear ( gl . COLOR_BUFFER_BIT ) ;
18
+ //Note: The default background color in WebGl is white.
19
+
20
+ //Step 2 (Speficy vertices data): Speficy the coordinates (X,Y,Z) of the geometry and other information related to each coordinates.
21
+ var verticesDataArrayJS =
22
+ [ // X, Y, Z
23
+ - 0.3 , 0.7 , 0 , //A
24
+ 0.3 , 0.7 , 0 , //B
25
+ 0 , 0 , 0 , //C
26
+ 0.3 , - 0.7 , 0 , //D
27
+ - 0.3 , - 0.7 , 0 //E
28
+ ] ;
29
+
30
+ //Step 3 (Specify how to connect the points): Specify the order with which the coordinates defined in Step2 will be joined.
31
+ var IndicesArrayJS =
32
+ [
33
+ // A-0, B-1, C-2, D-3, E-4
34
+ 0 , 1 , 2 , //ABC
35
+ //2, 3, 4 //CDE
36
+ ] ;
37
+
38
+ //Step 4 (Create GPU meomry buffer): In the GPU for holding vertices data of type ARRAY_BUFFER.
39
+ var rectVBO = gl . createBuffer ( ) ;
40
+ gl . bindBuffer ( gl . ARRAY_BUFFER , rectVBO ) ;
41
+
42
+ //Step 5 (Pass the vertices data to the buffer created previously).
43
+ gl . bufferData ( gl . ARRAY_BUFFER , new Float32Array ( verticesDataArrayJS ) , gl . STATIC_DRAW ) ;
44
+
45
+ //Step 6 (Pass the indices data to GPU buffer): repeat the steps 4 and 5 for the indices data but use ELEMENT_ARRAY_BUFFER.
46
+ var rectIBO = gl . createBuffer ( ) ;
47
+ gl . bindBuffer ( gl . ELEMENT_ARRAY_BUFFER , rectIBO ) ;
48
+ gl . bufferData ( gl . ELEMENT_ARRAY_BUFFER , new Uint16Array ( IndicesArrayJS ) , gl . STATIC_DRAW ) ;
49
+
50
+ //Seven Steps Shader side coding in JS to get the shader program.
51
+ shaderProgram = getShaderProgram ( gl ) ;
52
+
53
+ //Step 14 (Use the shader program):
54
+ gl . useProgram ( shaderProgram ) ;
55
+
56
+ //Step 15 (Get access to GPU's geometry coordinates): Get the pointer to the geometry coordinates defined in vertex shader through the shader program.
57
+ var positionAttribLocation = gl . getAttribLocation ( shaderProgram , 'geometryCoordinatesGPU' ) ;
58
+
59
+ var GPU_T1 = gl . getUniformLocation ( shaderProgram , 'GPU_T1' ) ;
60
+ var GPU_T2 = gl . getUniformLocation ( shaderProgram , 'GPU_T2' ) ;
61
+ var GPU_T3 = gl . getUniformLocation ( shaderProgram , 'GPU_T3' ) ;
62
+
63
+ //Step 16 (Enable Vertex Attribute Array): It enables the pointer defined in Step 8 to access the vertex buffered data.
64
+ gl . enableVertexAttribArray ( positionAttribLocation ) ;
65
+
66
+ //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.
67
+ gl . vertexAttribPointer (
68
+ positionAttribLocation , // Attribute location
69
+ 3 , // Number of elements per attribute
70
+ gl . FLOAT , // Type of elements
71
+ gl . FALSE ,
72
+ 3 * Float32Array . BYTES_PER_ELEMENT , // Size of an individual vertex
73
+ 0 // Offset from the beginning of a single vertex to this attribute
74
+ ) ;
75
+
76
+
77
+ //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.
78
+ var angle = 45 ;
79
+ var radian = angle * ( 3.14 / 180 ) ;
80
+ var Sx = 0.5 , Sy = 0.5 ;
81
+ var m = 0.5 , n = 0.5 ;
82
+ var T1 = new Float32Array ( 16 ) ;
83
+ var T2 = new Float32Array ( 16 ) ;
84
+ var T3 = new Float32Array ( 16 ) ;
85
+
86
+ //T1 = [1,0,0,0, 0,1,0,0, 0,0,1,0, -0.5,-0.5,0,1]; //for translation
87
+ T1 = [ 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 1 ] ; //for translation
88
+ T2 = [ Math . cos ( radian ) , Math . sin ( radian ) , 0 , 0 , - Math . sin ( radian ) , Math . cos ( radian ) , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 ] ; //for translation
89
+ //T2 = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]; //for translation
90
+ //T3 = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0.5,0.5,0,1]; //for translation
91
+ T3 = [ 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , - 1 , - 1 , 0 , 1 ] ; //for translation
92
+
93
+ //tranArrayJS = [Math.cos(radian),-Math.sin(radian),0,0, Math.sin(radian),Math.cos(radian),0,0, 0,0,1,0, 0,0,0,1]; //for rotation
94
+ //tranArrayJS = [Sx,0,0,0, 0,Sy,0,0, 0,0,1,0, 0,0,0,1]; //for scaling
95
+ //tranArrayJS = [Sx*Math.cos(radian),- Sx*Math.sin(radian),0,0, Sy*Math.sin(radian),Sy*Math.cos(radian),0,0, 0,0,1,0, m,n,0,1]; //for composite transformation
96
+
97
+ gl . uniformMatrix4fv ( GPU_T1 , gl . FALSE , T1 ) ;
98
+ gl . uniformMatrix4fv ( GPU_T2 , gl . FALSE , T2 ) ;
99
+ gl . uniformMatrix4fv ( GPU_T3 , gl . FALSE , T3 ) ;
100
+
101
+ gl . drawElements ( gl . TRIANGLES , IndicesArrayJS . length , gl . UNSIGNED_SHORT , 0 ) ;
102
+
103
+ function initializeWebGL ( gl )
104
+ {
105
+ var canvas = document . getElementById ( 'canvas' ) ;
106
+
107
+ canvas . width = window . innerWidth ; ;
108
+ canvas . height = window . innerHeight ; ;
109
+
110
+ gl = canvas . getContext ( 'webgl' ) ;
111
+
112
+ if ( ! gl ) {
113
+ console . log ( 'WebGL not supported, falling back on experimental-webgl' ) ;
114
+ gl = canvas . getContext ( 'experimental-webgl' ) ;
115
+ }
116
+
117
+ if ( ! gl ) {
118
+ alert ( 'Your browser does not support WebGL' ) ;
119
+ return ;
120
+ }
121
+ return gl ;
122
+ }
123
+
124
+ //Seven steps of Shader side coding
125
+ function getShaderProgram ( gl )
126
+ {
127
+ //Step 7 (Define vertex shader text): Define the code of the vertex shader in the form of JS text.
128
+ var vertexShaderText =
129
+ ' precision mediump float; ' +
130
+ ' attribute vec3 geometryCoordinatesGPU; ' +
131
+ ' uniform mat4 GPU_T1; ' +
132
+ ' uniform mat4 GPU_T2; ' +
133
+ ' uniform mat4 GPU_T3; ' +
134
+ ' void main() ' +
135
+ ' { ' +
136
+ ' gl_Position = vec4(geometryCoordinatesGPU, 1.0) * GPU_T1 * GPU_T2 * GPU_T3; ' +
137
+ ' } ' ;
138
+
139
+ //Step 8 (Create actual vertex shader): Create the actual vertex shader with the text defined in Step 1.
140
+ var vertexShader = gl . createShader ( gl . VERTEX_SHADER ) ;
141
+ gl . shaderSource ( vertexShader , vertexShaderText ) ;
142
+
143
+ //Step 9 (Compile vertex shader):
144
+ gl . compileShader ( vertexShader ) ;
145
+ if ( ! gl . getShaderParameter ( vertexShader , gl . COMPILE_STATUS ) ) {
146
+ console . error ( 'ERROR compiling vertex shader!' , gl . getShaderInfoLog ( vertexShader ) ) ;
147
+ return ;
148
+ }
149
+
150
+ //Step 10: Repeat the above 3 steps for fragment shader.
151
+ var fragmentShaderText =
152
+ ' void main() ' +
153
+ ' { ' +
154
+ ' gl_FragColor = vec4(0, 1, 0, 1); ' +
155
+ ' } ' ;
156
+
157
+ var fragmentShader = gl . createShader ( gl . FRAGMENT_SHADER ) ;
158
+ gl . shaderSource ( fragmentShader , fragmentShaderText ) ;
159
+
160
+ gl . compileShader ( fragmentShader ) ;
161
+ if ( ! gl . getShaderParameter ( fragmentShader , gl . COMPILE_STATUS ) ) {
162
+ console . error ( 'ERROR compiling fragment shader!' , gl . getShaderInfoLog ( fragmentShader ) ) ;
163
+ return ;
164
+ }
165
+
166
+ //Step 11 (Shader program): With the compiled vertex and fragment shader, create the shader program.
167
+ var shaderProgram = gl . createProgram ( ) ;
168
+ gl . attachShader ( shaderProgram , vertexShader ) ;
169
+ gl . attachShader ( shaderProgram , fragmentShader ) ;
170
+
171
+ //Step 12 (Link shader program):
172
+ gl . linkProgram ( shaderProgram ) ;
173
+ if ( ! gl . getProgramParameter ( shaderProgram , gl . LINK_STATUS ) ) {
174
+ console . error ( 'ERROR linking program!' , gl . getProgramInfoLog ( shaderProgram ) ) ;
175
+ return ;
176
+ }
177
+
178
+ //Step 13 (Validate Shader program): Checks if the shader program has been succesfully linked and can be used further.
179
+ gl . validateProgram ( shaderProgram ) ;
180
+ if ( ! gl . getProgramParameter ( shaderProgram , gl . VALIDATE_STATUS ) ) {
181
+ console . error ( 'ERROR validating program!' , gl . getProgramInfoLog ( shaderProgram ) ) ;
182
+ return ;
183
+ }
184
+ return shaderProgram ;
185
+ }
186
+ </ script >
187
+ </ body >
188
+ </ html >
0 commit comments