Skip to content

Commit

Permalink
Add missing file for lesson 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sketchpunk committed Oct 10, 2017
1 parent 49431ec commit 230ffc5
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lesson_002/gl.js
@@ -0,0 +1,47 @@
function GLInstance(canvasID){
var canvas = document.getElementById(canvasID),
gl = canvas.getContext("webgl2");

if(!gl){ console.error("WebGL context is not available."); return null; }

//...................................................
//Setup GL, Set all the default configurations we need.
gl.clearColor(1.0,1.0,1.0,1.0); //Set clear color


//...................................................
//Methods

//Reset the canvas with our set background color.
gl.fClear = function(){ this.clear(this.COLOR_BUFFER_BIT | this.DEPTH_BUFFER_BIT); return this; }

//Create and fill our Array buffer.
gl.fCreateArrayBuffer = function(floatAry,isStatic){
if(isStatic === undefined) isStatic = true; //So we can call this function without setting isStatic

var buf = this.createBuffer();
this.bindBuffer(this.ARRAY_BUFFER,buf);
this.bufferData(this.ARRAY_BUFFER, floatAry, (isStatic)? this.STATIC_DRAW : this.DYNAMIC_DRAW );
this.bindBuffer(this.ARRAY_BUFFER,null);
return buf;
}

//...................................................
//Setters - Getters

//Set the size of the canvas html element and the rendering view port
gl.fSetSize = function(w,h){
//set the size of the canvas, on chrome we need to set it 3 ways to make it work perfectly.
this.canvas.style.width = w + "px";
this.canvas.style.height = h + "px";
this.canvas.width = w;
this.canvas.height = h;

//when updating the canvas size, must reset the viewport of the canvas
//else the resolution webgl renders at will not change
this.viewport(0,0,w,h);
return this;
}

return gl;
}

0 comments on commit 230ffc5

Please sign in to comment.