scripts/cube.js
author Eugen Sawin <sawine@me73.com>
Wed, 27 Apr 2011 15:10:42 +0200
changeset 33 2af408534f28
parent 25 3b66f92dbc4d
child 35 7769f6e1e2c5
permissions -rw-r--r--
Added specular lighting.
     1 function Node(position, rotation, geometry)
     2 {
     3     this.pos = position;
     4     this.rot = rotation;
     5     this.geometry = geometry;
     6     this.children = [];
     7 }
     8 Node.prototype.update = function(time)
     9 {
    10     // this.right = [1, 0, 0];
    11     // this.up = [0, 1, 0];
    12     // this.target = [0, 0, -1];
    13     // var x = vec3.dot([1, 0, 0], this.pos);
    14     // var y = vec3.dot([0, 1, 0], this.pos);
    15     // var z = vec3.dot([0, 0, -1], this.pos);
    16     // this.matrix = mat4.create([this.right[0], this.up[0], -this.target[0], 0,
    17     //  			       this.right[1], this.up[1], -this.target[1], 0,
    18     //  			       this.right[2], this.up[2], -this.target[2], 0,
    19     //  			       -x, -y, z, 1]); 
    20     for (var id in this.children) this.children[id].update(time);
    21 }
    22 
    23 
    24 function Cube(size, context)
    25 {
    26     var gl = context.gl;
    27     this.size = size || 1;    
    28     this.positionBuffer = gl.createBuffer();
    29     gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
    30     var vertices = [
    31 	     // Front face
    32             -1.0, -1.0,  1.0,
    33              1.0, -1.0,  1.0,
    34              1.0,  1.0,  1.0,
    35             -1.0,  1.0,  1.0,
    36  
    37             // Back face
    38             -1.0, -1.0, -1.0,
    39             -1.0,  1.0, -1.0,
    40              1.0,  1.0, -1.0,
    41              1.0, -1.0, -1.0,
    42  
    43             // Top face
    44             -1.0,  1.0, -1.0,
    45             -1.0,  1.0,  1.0,
    46              1.0,  1.0,  1.0,
    47              1.0,  1.0, -1.0,
    48  
    49             // Bottom face
    50             -1.0, -1.0, -1.0,
    51              1.0, -1.0, -1.0,
    52              1.0, -1.0,  1.0,
    53             -1.0, -1.0,  1.0,
    54  
    55             // Right face
    56              1.0, -1.0, -1.0,
    57              1.0,  1.0, -1.0,
    58              1.0,  1.0,  1.0,
    59              1.0, -1.0,  1.0,
    60  
    61             // Left face
    62             -1.0, -1.0, -1.0,
    63             -1.0, -1.0,  1.0,
    64             -1.0,  1.0,  1.0,
    65             -1.0,  1.0, -1.0];
    66     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
    67     this.positionBuffer.itemSize = 3;
    68     this.positionBuffer.numItems = 24;
    69 
    70     this.normalBuffer = gl.createBuffer();
    71     gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
    72     var normals = [// font face
    73 		   0.0, 0.0, 1.0,
    74 		   0.0, 0.0, 1.0,
    75 		   0.0, 0.0, 1.0,
    76 		   0.0, 0.0, 1.0,
    77 		   // back face
    78 		   0.0, 0.0, -1.0,
    79 		   0.0, 0.0, -1.0,
    80 		   0.0, 0.0, -1.0,
    81 		   0.0, 0.0, -1.0,
    82 		   // top face
    83 		   0.0, 1.0, 0.0,
    84 		   0.0, 1.0, 0.0,
    85 		   0.0, 1.0, 0.0, 
    86 		   0.0, 1.0, 0.0,
    87 		   // bottom face
    88 		   0.0, -1.0, 0.0,
    89 		   0.0, -1.0, 0.0,
    90 		   0.0, -1.0, 0.0,
    91 		   0.0, -1.0, 0.0,
    92 		   // right face
    93 		   1.0, 0.0, 0.0,
    94 		   1.0, 0.0, 0.0,
    95 		   1.0, 0.0, 0.0,
    96 		   1.0, 0.0, 0.0,
    97 		   // left face
    98 		   -1.0, 0.0, 0.0,
    99 		   -1.0, 0.0, 0.0,
   100 		   -1.0, 0.0, 0.0,
   101 		   -1.0, 0.0, 0.0];
   102     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
   103     this.normalBuffer.itemSize = 3;
   104     this.normalBuffer.numItems = 24;
   105 
   106     this.colourBuffer = gl.createBuffer();
   107     gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
   108     var alpha = 1.0;
   109     var uni = [1.0, 1.0, 1.0, alpha];
   110     var colours = [[0.0, 0.0, 0.0, alpha],
   111 		   [0.0, 0.0, 0.0, alpha],
   112 		   [0.0, 0.0, 0.0, alpha],
   113 		   [0.0, 0.0, 0.0, alpha],
   114 		   [0.0, 0.0, 0.0, alpha],
   115 		   [0.0, 0.0, 0.0, alpha]];
   116     var unpackedColours = [];
   117     for (var i in colours)
   118     {
   119 	var colour = uni;//colours[i];
   120 	for (var j = 0; j < 4; j++)
   121 	{
   122 	    unpackedColours = unpackedColours.concat(colour);
   123 	}
   124 	//colours = colours.concat([0.5, 0.5, 1.0, 1.0]);
   125     }
   126     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColours), gl.STATIC_DRAW);
   127     this.colourBuffer.itemSize = 4;
   128     this.colourBuffer.numItems = 24;
   129 
   130     this.indexBuffer = gl.createBuffer();
   131     gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
   132     var indices = [0, 1, 2, 0, 2, 3,
   133 		   4, 5, 6, 4, 6, 7,
   134 		   8, 9, 10, 8, 10, 11,
   135 		   12, 13, 14, 12, 14, 15,
   136 		   16, 17, 18, 16, 18, 19,
   137 		   20, 21, 22, 20, 22, 23];
   138     gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
   139     this.indexBuffer.itemSize = 1;
   140     this.indexBuffer.numItems = 36;
   141 }