# HG changeset patch # User Eugen Sawin # Date 1301664295 -7200 # Node ID 3f61f8af55ff7c15e7806ffa13588025d1c32292 # Parent a768f6393f943d5681ea12f588bef0cd28b162ec Added colours. diff -r a768f6393f94 -r 3f61f8af55ff machine.html --- a/machine.html Fri Apr 01 14:49:58 2011 +0200 +++ b/machine.html Fri Apr 01 15:24:55 2011 +0200 @@ -10,20 +10,28 @@ #ifdef GL_ES precision highp float; #endif + + varying vec4 vColour; - void main(void) { - gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); + void main(void) + { + gl_FragColor = vColour; } diff -r a768f6393f94 -r 3f61f8af55ff scripts/machine.js --- a/scripts/machine.js Fri Apr 01 14:49:58 2011 +0200 +++ b/scripts/machine.js Fri Apr 01 15:24:55 2011 +0200 @@ -1,15 +1,3 @@ -function Machine(canvas, context, scene) -{ - this.canvas = canvas; - this.context = context; - this.scene = scene; - this.gl = context.gl; -} -Machine.prototype.draw = function() -{ - draw(this.context, this.scene); -} - var machine; function main() @@ -23,7 +11,7 @@ gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.enable(gl.DEPTH_TEST); //draw(context, object); - machine = new Machine(canvas, context, object); + machine = new Machine(context, object); machine.draw(); } @@ -44,6 +32,17 @@ context.viewport.height = height; } +function Machine(context, scene) +{ + this.context = context; + this.scene = scene; + this.gl = context.gl; +} +Machine.prototype.draw = function() +{ + draw(this.context, this.scene); +} + function Context(canvas) { this.canvas = canvas; @@ -90,6 +89,10 @@ gl.useProgram(this.program); this.vertexPosition = gl.getAttribLocation(this.program, "aVertexPosition"); gl.enableVertexAttribArray(this.vertexPosition); + + this.vertexColour = gl.getAttribLocation(this.program, "aVertexColour"); + gl.enableVertexAttribArray(this.vertexColour); + this.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix"); this.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix"); @@ -130,15 +133,31 @@ { var gl = context.gl; this.size = size || 1; - this.buffer = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer); + + this.positionBuffer = gl.createBuffer(); + gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer); var vertices = [1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, -1.0, 0.0]; gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); - this.itemSize = 3; - this.numItems = 4; + this.positionBuffer.itemSize = 3; + this.positionBuffer.numItems = 4; + + this.colourBuffer = gl.createBuffer(); + gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer); + var colours = [1.0, 0.0, 0.0, .5, + 0.0, 1.0, 0.0, .5, + 0.0, 0.0, 1.0, .5, + 1.0, 0.0, 1.0, .5]; + //colours = []; + for (var i = 0; i < 4; i++) + { + // colours = colours.concat([0.5, 0.5, 1.0, 1.0]); + } + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colours), gl.STATIC_DRAW); + this.colourBuffer.itemSize = 4; + this.colourBuffer.numItems = 4; } function draw(context, scene) @@ -148,13 +167,19 @@ var shader = context.shader; var mvMatrix = context.mvMatrix; var pMatrix = context.pMatrix; + gl.viewport(0, 0, viewport.width, viewport.height); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix); + mat4.identity(mvMatrix); mat4.translate(mvMatrix, [0.0, 0.0, -7.0]); - gl.bindBuffer(gl.ARRAY_BUFFER, scene.buffer); - gl.vertexAttribPointer(shader.vertexPosition, scene.itemSize, gl.FLOAT, false, 0, 0); + gl.bindBuffer(gl.ARRAY_BUFFER, scene.positionBuffer); + gl.vertexAttribPointer(shader.vertexPosition, scene.positionBuffer.itemSize, gl.FLOAT, false, 0, 0); + + gl.bindBuffer(gl.ARRAY_BUFFER, scene.colourBuffer); + gl.vertexAttribPointer(shader.vertexColour, scene.colourBuffer.itemSize, gl.FLOAT, false, 0, 0); + context.updateMatrixUniforms(); - gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.numItems); + gl.drawArrays(gl.TRIANGLE_STRIP, 0, scene.positionBuffer.numItems); } \ No newline at end of file