Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 17th, 2012  |  syntax: None  |  size: 4.84 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // (c) dean@gmail.com
  2.  
  3. var plask = require('plask');
  4.  
  5. plask.simpleWindow({
  6.   settings: {
  7.     width: 1280,
  8.     height: 720,
  9.     type: '3d',  // Create an OpenGL window.
  10.     vsync: true,  // Prevent tearing.
  11.     multisample: true  // Anti-alias.
  12.   },
  13.  
  14.   init: function() {
  15.     var gl = this.gl;
  16.  
  17.     this.framerate(60);
  18.  
  19.     // Read/compile/link the shaders into a MagicProgram.  The |mprogram|
  20.     // object created has methods to set the shader uniform variables.
  21.     this.mprogram = plask.gl.MagicProgram.createFromBasename(
  22.         gl, __dirname, 'app');
  23.  
  24.     // Create the three 3d (xyz) vertices for the triangle.  This creates the
  25.     // geometry and loads it into an Vertex Buffer Object (VBO).
  26.     function makeBuffer(ar,itemSize) {
  27.       var buffer = gl.createBuffer();
  28.       var data = new Float32Array(ar);
  29.       gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  30.       gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
  31.       return {buffer: buffer, num: data.length / itemSize, data: data};
  32.     }
  33.    
  34.     this.triangleMesh = makeBuffer([
  35.                                    0.0,  1.0,  0.0,
  36.                                   -1.0, -1.0,  0.0,
  37.                                    1.0, -1.0,  0.0
  38.                                 ],3);
  39.                                
  40.     this.triangleVertexColorBuffer=makeBuffer([
  41.                                             1.0, 0.0, 0.0, 1.0,
  42.                                             0.0, 1.0, 0.0, 1.0,
  43.                                             0.0, 0.0, 1.0, 1.0,
  44.                                               ],4);    
  45.     this.squareMesh = makeBuffer([
  46.                            1.0,  1.0,  0.0,
  47.                           -1.0,  1.0,  0.0,
  48.                            1.0, -1.0,  0.0,
  49.                           -1.0, -1.0,  0.0,
  50.                         ],3);
  51. //    this.squareVertexColorBuffer = makeBuffer([
  52. //    0.5, 0.5, 1.0, 1.0,
  53. //    0.5, 0.5, 1.0, 1.0,
  54. //    0.5, 0.5, 1.0, 1.0,
  55. //    0.5, 0.5, 1.0, 1.0],4);
  56.     this.squareVertexColorBuffer = makeBuffer((function () {
  57.         var ar =[],i=0;
  58.         for (i; i < 4; i+=1) {
  59.             ar = ar.concat([0.5, 0.5, 1.0, 1.0]);
  60.         }
  61.         return ar;
  62.     }()),4);
  63.  
  64.     gl.viewport(0, 0, this.width, this.height);
  65.     gl.clearColor(0.0, 0.0, 0.0, 1.0);
  66.     gl.clearDepth(1.0)
  67.     gl.enable(gl.DEPTH_TEST);
  68.     gl.depthFunc(gl.LEQUAL);
  69.  
  70.   },
  71.  
  72.   draw: function() {
  73.     var gl = this.gl;
  74.     var triangleMesh = this.triangleMesh;
  75.     var squareMesh = this.squareMesh;
  76.     var triangleVertexColorBuffer = this.triangleVertexColorBuffer;
  77.     var squareVertexColorBuffer = this.squareVertexColorBuffer;
  78.     var mprogram = this.mprogram;
  79.  
  80.     // Clear the background to gray.  The depth buffer isn't really needed for
  81.     // a single triangle, but generally the depth buffer should also be cleared.
  82.     gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  83.  
  84.     // Build the "model-view" matrix, which will rotate the triangle based on
  85.     // the mouse controls.
  86.     var mv = new plask.Mat4();
  87.     mv.translate(-1.5, 0.0, -7.0);
  88.    
  89.     // Build the "perspective" matrix, which will apply the aspect ratio,
  90.     // perspective divide, and clipping planes.
  91.     var persp = new plask.Mat4();
  92.     persp.perspective(45, this.width / this.height, 0.1, 100.0);
  93.  
  94.     // Set the shaders to be used during drawing, and pass the current
  95.     // transformation matrices to them.
  96.     mprogram.use();
  97.     mprogram.set_uPMatrix(persp);
  98.     mprogram.set_uMVMatrix(mv);
  99.  
  100.     // Draw the geometry from the VBO, passing the vertices to the vertex
  101.     // shader as a vec3 named |a_xyz|.
  102.     gl.bindBuffer(gl.ARRAY_BUFFER, triangleMesh.buffer);
  103.     gl.vertexAttribPointer(mprogram.location_aVertexPosition,
  104.                            3,
  105.                            gl.FLOAT,
  106.                            false, 0, 0);
  107.     gl.enableVertexAttribArray(mprogram.location_aVertexPosition);
  108.  
  109.     gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer.buffer);
  110.     gl.vertexAttribPointer(mprogram.location_aVertexColor,
  111.                            4,
  112.                            gl.FLOAT,
  113.                            false, 0, 0);    
  114.     gl.enableVertexAttribArray(mprogram.location_aVertexColor);
  115.     gl.drawArrays(gl.TRIANGLES, 0, triangleMesh.num);
  116.    
  117.     mv.translate(3.0, 0.0, 0.0);
  118.     mprogram.set_uMVMatrix(mv);
  119.    
  120.     gl.bindBuffer(gl.ARRAY_BUFFER, squareMesh.buffer);
  121.     gl.vertexAttribPointer(mprogram.location_aVertexPosition,
  122.                            3,
  123.                            gl.FLOAT,
  124.                            false, 0, 0);
  125.     gl.enableVertexAttribArray(mprogram.location_aVertexPosition);
  126.     gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexColorBuffer.buffer);
  127.     gl.vertexAttribPointer(mprogram.location_aVertexColor,
  128.                            4,
  129.                            gl.FLOAT,
  130.                            false, 0, 0);    
  131.     gl.enableVertexAttribArray(mprogram.location_aVertexColor);
  132.  
  133.     gl.drawArrays(gl.TRIANGLE_STRIP, 0, squareMesh.num);
  134.  
  135.  
  136.   }
  137. });