scripts/machine.js
author Eugen Sawin <sawine@me73.com>
Fri, 01 Apr 2011 23:28:15 +0200
changeset 5 ac9ba687a05c
parent 4 3f61f8af55ff
child 6 cd14e5b5296c
permissions -rwxr-xr-x
Refact: Render.
     1 var machine;
     2 var render;
     3 
     4 function main()
     5 {
     6     var canvas = document.getElementById("machine");
     7     window.onresize = resize;
     8     var context = new Context(canvas);
     9     make_fullscreen(context);
    10     var gl = context.gl;
    11     var object = new Box(1, context);
    12     gl.clearColor(0.0, 0.0, 0.0, 1.0);
    13     gl.enable(gl.DEPTH_TEST);
    14     machine = new Machine(object);
    15     render = new Render(context);
    16     render.draw(machine.scene);
    17 }
    18 
    19 function resize()
    20 {
    21     make_fullscreen(machine.context);
    22     machine.draw();
    23 }
    24 
    25 function make_fullscreen(context)
    26 {
    27     var width = window.innerWidth;
    28     var height = window.innerHeight;
    29     context.canvas.width = width;
    30     context.canvas.height = height;
    31     context.viewport.width = width;
    32     context.viewport.height = height;
    33 }
    34 
    35 function Render(context)
    36 {
    37     this.context = context;
    38     this.gl = context.gl;
    39 }
    40 Render.prototype.draw = function(scene)
    41 { 
    42     var gl = this.context.gl;
    43     var viewport = this.context.viewport;
    44     var shader = this.context.shader;
    45     var mvMatrix = this.context.mvMatrix;
    46     var pMatrix = this.context.pMatrix;
    47 
    48     gl.viewport(0, 0, viewport.width, viewport.height);
    49     gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    50     mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix);
    51 
    52     mat4.identity(mvMatrix);
    53     mat4.translate(mvMatrix, [0.0, 0.0, -7.0]);
    54     gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer);
    55     gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0);
    56 
    57     gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer);
    58     gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0);
    59 
    60     this.context.updateMatrixUniforms();
    61     gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems);
    62 
    63     if (this.next) this.next.draw(scene);
    64 }
    65 
    66 function Machine(scene)
    67 {
    68     this.scene = scene;
    69 }
    70 Machine.prototype.draw = function(render)
    71 {
    72     render.draw(this.scene);
    73 }
    74 
    75 function Context(canvas)
    76 {
    77     this.canvas = canvas;
    78     try 
    79     {
    80 	this.gl = canvas.getContext("experimental-webgl");
    81 	this.viewport = {'width': canvas.width,
    82 			 'height': canvas.height};
    83     } 
    84     catch(e) 
    85     {
    86 	alert(e);
    87     }
    88     if (!this.gl) alert("Failed: WebGL init.");
    89     this.mvMatrix = mat4.create();
    90     this.pMatrix = mat4.create();
    91     this.shader = new Shader(this);    
    92 }
    93 Context.prototype.updateMatrixUniforms = function()
    94 {
    95     var gl = this.gl;
    96     var program = this.shader;
    97     var pMatrix = this.pMatrix;
    98     var mvMatrix = this.mvMatrix;
    99     gl.uniformMatrix4fv(program.pMatrixUniform, false, pMatrix);
   100     gl.uniformMatrix4fv(program.mvMatrixUniform, false, mvMatrix);
   101 }
   102 
   103 function Shader(context)
   104 {
   105     var gl = context.gl;
   106     var fragment = load_shader(gl, "fragment-shader");
   107     var vertex = load_shader(gl, "vertex-shader");
   108     this.program = gl.createProgram();
   109     gl.attachShader(this.program, vertex);
   110     gl.attachShader(this.program, fragment);
   111     gl.linkProgram(this.program);
   112 
   113     if (!gl.getProgramParameter(this.program, gl.LINK_STATUS))
   114     {
   115 	alert("Failed: Shader init.");
   116     }
   117 
   118     gl.useProgram(this.program);
   119     this.vertexPosition = gl.getAttribLocation(this.program, "aVertexPosition");
   120     gl.enableVertexAttribArray(this.vertexPosition);
   121     
   122     this.vertexColour = gl.getAttribLocation(this.program, "aVertexColour");
   123     gl.enableVertexAttribArray(this.vertexColour);
   124 
   125     this.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix");
   126     this.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix");
   127 
   128     function load_shader(gl, id)
   129     {
   130 	var script = document.getElementById(id);
   131 	if (!script) return null;
   132 	
   133 	var str = "";
   134 	var child = script.firstChild;
   135 	while (child)
   136 	{
   137 	    if (child.nodeType == 3) str += child.textContent;
   138 	    child = child.nextSibling;
   139 	}
   140 	
   141 	var shader;
   142 	var common = "x-shader/x-";
   143 	if (script.type == common + "fragment") shader = gl.createShader(gl.FRAGMENT_SHADER);
   144 	else if (script.type == common + "vertex") shader = gl.createShader(gl.VERTEX_SHADER);
   145 	else return null;
   146 
   147 	gl.shaderSource(shader, str);
   148 	gl.compileShader(shader);
   149 
   150 	if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
   151         {
   152 	    alert(gl.getShaderInfoLog(shader));
   153 	    return null;
   154 	}
   155 
   156 	return shader;
   157     }
   158 }
   159 
   160 
   161 function Box(size, context)
   162 {
   163     var gl = context.gl;
   164     this.size = size || 1;
   165     
   166     this.positionBuffer = gl.createBuffer();
   167     gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
   168     var vertices = [1.0, 1.0, 0.0,
   169 		    -1.0, 1.0, 0.0,
   170 		    1.0, -1.0, 0.0,
   171 		    -1.0, -1.0, 0.0];
   172     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
   173     this.positionBuffer.itemSize = 3;
   174     this.positionBuffer.numItems = 4;
   175 
   176     this.colourBuffer = gl.createBuffer();
   177     gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
   178     var colours = [1.0, 0.0, 0.0, .5,
   179 		   0.0, 1.0, 0.0, .5,
   180 		   0.0, 0.0, 1.0, .5,
   181 		   1.0, 0.0, 1.0, .5];
   182     //colours = [];
   183     for (var i = 0; i < 4; i++)
   184     {
   185 	//	colours = colours.concat([0.5, 0.5, 1.0, 1.0]);
   186     }
   187     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colours), gl.STATIC_DRAW);
   188     this.colourBuffer.itemSize = 4;
   189     this.colourBuffer.numItems = 4;
   190 }