-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
243 lines (187 loc) · 7.4 KB
/
renderer.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/***
* Vex webgl port
* Autor: Bernard Bekker
* Year: 2013
*
* This code is shared under the:
* Creative Commons Attribution-NonCommercial 4.0 International Public License
*
* http://creativecommons.org/licenses/by-nc/4.0/deed.en_US
*
*/
var renderer = {}; // renderer
var gl; // webgl context
var program; //shader program
var loop=0;
renderer.init = function init(){
//get canvas
canvas = document.getElementById("vex-canv");
//fix canvas
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
//get gl context
gl = canvas.getContext("experimental-webgl") || canvas.getContext("webgl");
//view port
gl.viewport(0, 0, canvas.width, canvas.height);
gl.aspect = canvas.width / canvas.height;
console.log("width "+canvas.width+" height:"+canvas.height);
gl.aspectTransform = mat4.identity(mat4.create());
mat4.scale(gl.aspectTransform, vec3.create([2.0/canvas.width,2.0/canvas.height,1]));
//init shaders
renderer.initShaders();
//get shader variables
program.uColor = gl.getUniformLocation(program, "uColor");
program.uTransformation = gl.getUniformLocation(program, "uTransformation");
program.uAspectTransformation = gl.getUniformLocation(program, "uAspectTransformation");
//Set shaders to use
gl.useProgram(program);
//set clear color
gl.clearColor(0.0, 0.0, 0.0, 0.0);
gl.lineWidth(4.0);
//clear screen using color buffer.
gl.clear(gl.COLOR_BUFFER_BIT);
//load ships into memory
renderer.loadShips();
renderer.loadBullets();
renderer.loadPowerups();
}
renderer.shipObject = {};
renderer.loadShips = function(){
//Objects to draw
renderer.shipObject.vertices = new Float32Array([
25.0, 0.0,
-15.0, -15.0,
25.0, 0.0,
-15.0, 15.0,
-15.0, -15.0,
-15.0, 15.0
]);
//Bookkeeping
renderer.shipObject.itemSize = 2;
renderer.shipObject.numItems = renderer.shipObject.vertices.length / renderer.shipObject.itemSize;
//Store data in GFX memory
renderer.shipObject.vbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, renderer.shipObject.vbuffer);
gl.bufferData(gl.ARRAY_BUFFER, renderer.shipObject.vertices, gl.STATIC_DRAW);
}
renderer.powerupObject = {};
renderer.loadPowerups = function(){
//Objects to draw
renderer.powerupObject.vertices = new Float32Array([
25.0, 0.0,
-15.0, -15.0,
25.0, 0.0,
-15.0, 15.0,
-15.0, -15.0,
-15.0, 15.0
]);
//Bookkeeping
renderer.powerupObject.itemSize = 2;
renderer.powerupObject.numItems = renderer.powerupObject.vertices.length / renderer.powerupObject.itemSize;
//Store data in GPU memory
renderer.powerupObject.vbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, renderer.powerupObject.vbuffer);
gl.bufferData(gl.ARRAY_BUFFER, renderer.powerupObject.vertices, gl.STATIC_DRAW);
}
renderer.bulletObject = {};
renderer.loadBullets = function(){
//Objects to draw
renderer.bulletObject.vertices = new Float32Array([
-2.0, 0.0,
0.0, 6.0,
0.0, 6.0,
2.0, 0.0,
-2.0, 0.0,
2.0, 0.0
]);
//Bookkeeping
renderer.bulletObject.itemSize = 2;
renderer.bulletObject.numItems = renderer.bulletObject.vertices.length / renderer.bulletObject.itemSize;
//Store data in GFX memory
renderer.bulletObject.vbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, renderer.bulletObject.vbuffer);
gl.bufferData(gl.ARRAY_BUFFER, renderer.bulletObject.vertices, gl.STATIC_DRAW);
}
renderer.drawStuff = function drawStuff(){
//set aspect matrix
gl.uniformMatrix4fv(program.uAspectTransformation, false, gl.aspectTransform);
//clear the screen before rendering
gl.clear(gl.COLOR_BUFFER_BIT);
renderer.drawShips();
renderer.drawBullets();
renderer.drawPowerups();
}
renderer.drawShips = function(){
gl.bindBuffer(gl.ARRAY_BUFFER, renderer.shipObject.vbuffer);
for(var iship in vex.ships){
var ship = vex.ships[iship];
//generate transformation matrix
var transformation = mat4.create();
mat4.identity(transformation);
mat4.rotateZ(mat4.translate(transformation,ship.location),ship.rotation);
gl.uniform4fv(program.uColor, ship.color);
gl.uniformMatrix4fv(program.uTransformation, false, transformation);
//Link shader variable to buffer data
program.aVertexPosition = gl.getAttribLocation(program, "aVertexPosition");
gl.enableVertexAttribArray(program.aVertexPosition);
gl.vertexAttribPointer(program.aVertexPosition, renderer.shipObject.itemSize, gl.FLOAT, false, 0, 0);
//draw!
gl.drawArrays(gl.LINES, 0, renderer.shipObject.numItems);
}
}
renderer.drawBullets = function(){
gl.bindBuffer(gl.ARRAY_BUFFER, renderer.bulletObject.vbuffer);
for(var ibullet in vex.bullets){
var bullet = vex.bullets[ibullet];
//generate transformation matrix
var transformation = mat4.create();
mat4.identity(transformation);
mat4.rotateZ(mat4.translate(transformation,bullet.location),vec3.rotation(bullet.vel));
gl.uniform4fv(program.uColor, bullet.owner.color);
gl.uniformMatrix4fv(program.uTransformation, false, transformation);
//Link shader variable to buffer data
program.aVertexPosition = gl.getAttribLocation(program, "aVertexPosition");
gl.enableVertexAttribArray(program.aVertexPosition);
gl.vertexAttribPointer(program.aVertexPosition, renderer.bulletObject.itemSize, gl.FLOAT, false, 0, 0);
//draw!
gl.drawArrays(gl.LINES, 0, renderer.bulletObject.numItems);
}
}
renderer.drawPowerups = function(){
gl.bindBuffer(gl.ARRAY_BUFFER, renderer.powerupObject.vbuffer);
for(var ipowerup in vex.powerups){
var powerup = vex.powerups[ipowerup];
//generate transformation matrix
var transformation = mat4.create();
mat4.identity(transformation);
mat4.rotateZ(mat4.translate(transformation,powerup.location),vec3.create(0,0,0));
gl.uniform4fv(program.uColor, [0.9 , 0.1, 0.1, 1.0]);
gl.uniformMatrix4fv(program.uTransformation, false, transformation);
//Link shader variable to buffer data
program.aVertexPosition = gl.getAttribLocation(program, "aVertexPosition");
gl.enableVertexAttribArray(program.aVertexPosition);
gl.vertexAttribPointer(program.aVertexPosition, renderer.powerupObject.itemSize, gl.FLOAT, false, 0, 0);
//draw!
gl.drawArrays(gl.LINES, 0, renderer.powerupObject.numItems);
}
}
renderer.initShaders = function initShaders(){
var v = document.getElementById("vertex").firstChild.nodeValue;
var f = document.getElementById("fragment").firstChild.nodeValue;
var vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, v);
gl.compileShader(vs);
var fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, f);
gl.compileShader(fs);
program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS))
console.log(gl.getShaderInfoLog(vs));
if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS))
console.log(gl.getShaderInfoLog(fs));
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
console.log(gl.getProgramInfoLog(program));
}