Skip to content

Commit 0b07082

Browse files
committed
Updated the FBO quad to use a vec2 for position instead, cutting down on vert data a little. Also removed array length look-ups and hard coded values in.
1 parent 9600ed0 commit 0b07082

1 file changed

Lines changed: 41 additions & 22 deletions

File tree

src/renderer/webgl/WebGLRenderer.js

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ Phaser.Renderer.WebGL.prototype = {
233233
gl.enable(gl.BLEND);
234234
// gl.enable(gl.DEPTH_TEST);
235235

236+
gl.clearColor(0, 0.3, 0, 1);
237+
236238
// this.shaderManager.init();
237239
this.batch.init();
238240
// this.filterManager.init();
@@ -424,13 +426,18 @@ Phaser.Renderer.WebGL.prototype = {
424426

425427
var gl = this.gl;
426428

427-
gl.viewport(0, 0, this.width, this.height);
429+
// viewport only needs to be set when the canvas is resized, not every render pass
430+
// gl.viewport(0, 0, this.width, this.height);
431+
432+
// Render 1/4 size view into the top left
433+
// gl.viewport(0, this.height / 2, this.width / 2, this.height / 2);
428434

429435
// Make sure we are bound to the main frame buffer
430436
// gl.bindFramebuffer(gl.FRAMEBUFFER, null);
431437
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
432438

433-
gl.clearColor(0, 0, 0, 0);
439+
// clearColor only needs to be set once, then 'clear' picks the value up every time
440+
// gl.clearColor(0, 0, 0.3, 1);
434441
gl.clear(gl.COLOR_BUFFER_BIT);
435442

436443
// gl.clearColor(0, 0, 0, 1);
@@ -480,6 +487,18 @@ Phaser.Renderer.WebGL.prototype = {
480487

481488
this.batch.stop();
482489

490+
// Render the whole lot again after changing the viewport
491+
// It does what you'd expect, but literally draws _everything_ again!
492+
/*
493+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
494+
gl.viewport(400, 300, 400, 300);
495+
this.batch.start(true);
496+
stage.render(this, stage);
497+
this.batch.stop();
498+
*/
499+
500+
501+
// Draw the FBO to the main context
483502
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
484503

485504
// gl.clearColor(0, 0, 0, 1);
@@ -505,10 +524,10 @@ Phaser.Renderer.WebGL.prototype = {
505524
verticesTextureBuffer: gl.createBuffer(),
506525
indices: [ 0, 1, 2, 2, 1, 3 ],
507526
vertices: [
508-
-1.0, -1.0, 0.0,//0
509-
1.0, -1.0, 0.0, //1
510-
-1.0, 1.0, 0.0, //2
511-
1.0, 1.0, 0.0 //3
527+
-1.0, -1.0,//0
528+
1.0, -1.0, //1
529+
-1.0, 1.0, //2
530+
1.0, 1.0 //3
512531
],
513532
uvs: [
514533
[ 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 ]
@@ -525,9 +544,6 @@ Phaser.Renderer.WebGL.prototype = {
525544
gl.bindBuffer(gl.ARRAY_BUFFER, this.square.vbo);
526545
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.square.vertices), gl.STATIC_DRAW);
527546

528-
this.square.vbo.itemSize = 3;
529-
this.square.vbo.numItems = this.square.vertices.length / 3;
530-
531547
gl.bindBuffer(gl.ARRAY_BUFFER, this.square.verticesTextureBuffer);
532548
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.square.uvs[0]), gl.STATIC_DRAW);
533549

@@ -565,14 +581,14 @@ Phaser.Renderer.WebGL.prototype = {
565581
// Create the quad shader
566582

567583
var vertexSrc = [
568-
'attribute vec3 aVertexPosition;',
584+
'attribute vec2 aVertexPosition;',
569585
'attribute vec2 aTextureCoord;',
570586

571587
'varying vec2 vTextureCoord;',
572588

573589
'void main(void) {',
574590
' vTextureCoord = aTextureCoord;',
575-
' gl_Position = vec4(aVertexPosition, 1.0);',
591+
' gl_Position = vec4(aVertexPosition, 0.0, 1.0);',
576592
'}'
577593
];
578594

@@ -644,7 +660,7 @@ Phaser.Renderer.WebGL.prototype = {
644660

645661

646662
// This compiles, attaches and links the shader
647-
this.postProcessShaderProgram = this.compileProgram(vertexSrc, fragmentSrc);
663+
this.postProcessShaderProgram = this.compileProgram(vertexSrc, standardFragmentSrc);
648664

649665
this.postProcessShaderProgram.vertexPositionAttribute = gl.getAttribLocation(this.postProcessShaderProgram, 'aVertexPosition');
650666
this.postProcessShaderProgram.textureCoordAttribute = gl.getAttribLocation(this.postProcessShaderProgram, 'aTextureCoord');
@@ -658,23 +674,26 @@ Phaser.Renderer.WebGL.prototype = {
658674

659675
gl.useProgram(program);
660676

661-
//gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
662-
gl.enableVertexAttribArray(program.vertexPositionAttribute);
663-
gl.bindBuffer(gl.ARRAY_BUFFER, this.square.vbo);
664-
665-
gl.vertexAttribPointer(program.vertexPositionAttribute, this.square.vbo.itemSize, gl.FLOAT, false, 0, 0);
666-
gl.enableVertexAttribArray(program.textureCoordAttribute);
667-
668677
gl.activeTexture(gl.TEXTURE0);
669678
gl.bindTexture(gl.TEXTURE_2D, this.frameTexture);
670679
gl.uniform1i(gl.getUniformLocation(program, 'uSampler'), 0);
671680

672-
gl.bindBuffer(gl.ARRAY_BUFFER, this.square.verticesTextureBuffer);
681+
gl.enableVertexAttribArray(program.textureCoordAttribute);
682+
gl.enableVertexAttribArray(program.vertexPositionAttribute);
683+
684+
gl.bindBuffer(gl.ARRAY_BUFFER, this.square.vbo); // vertex buffer object
685+
gl.vertexAttribPointer(program.vertexPositionAttribute, 2, gl.FLOAT, false, 0, 0);
686+
687+
gl.bindBuffer(gl.ARRAY_BUFFER, this.square.verticesTextureBuffer); // texture buffer
673688
gl.vertexAttribPointer(program.textureCoordAttribute, 2, gl.FLOAT, false, 0, 0);
674689

675-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.square.ibo);
676-
gl.drawElements(gl.TRIANGLES, this.square.indices.length, gl.UNSIGNED_SHORT, 0);
690+
// Draw
691+
692+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.square.ibo); // index buffer
693+
694+
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
677695

696+
this.textureArray[0] = this.frameTexture;
678697
},
679698

680699
/**

0 commit comments

Comments
 (0)