scripts/cube.js
author Eugen Sawin <sawine@me73.com>
Tue, 26 Apr 2011 01:48:56 +0200
changeset 24 8c2ee41d3727
child 25 3b66f92dbc4d
permissions -rw-r--r--
Broken light.
     1 function Cube(size, context)
     2 {
     3     var gl = context.gl;
     4     this.size = size || 1;
     5     this.rotation = {'x': 0.0, 'y': 0.0, 'z': 0.0};
     6     
     7     this.positionBuffer = gl.createBuffer();
     8     gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
     9     var vertices = [
    10 	     // Front face
    11             -1.0, -1.0,  1.0,
    12              1.0, -1.0,  1.0,
    13              1.0,  1.0,  1.0,
    14             -1.0,  1.0,  1.0,
    15  
    16             // Back face
    17             -1.0, -1.0, -1.0,
    18             -1.0,  1.0, -1.0,
    19              1.0,  1.0, -1.0,
    20              1.0, -1.0, -1.0,
    21  
    22             // Top face
    23             -1.0,  1.0, -1.0,
    24             -1.0,  1.0,  1.0,
    25              1.0,  1.0,  1.0,
    26              1.0,  1.0, -1.0,
    27  
    28             // Bottom face
    29             -1.0, -1.0, -1.0,
    30              1.0, -1.0, -1.0,
    31              1.0, -1.0,  1.0,
    32             -1.0, -1.0,  1.0,
    33  
    34             // Right face
    35              1.0, -1.0, -1.0,
    36              1.0,  1.0, -1.0,
    37              1.0,  1.0,  1.0,
    38              1.0, -1.0,  1.0,
    39  
    40             // Left face
    41             -1.0, -1.0, -1.0,
    42             -1.0, -1.0,  1.0,
    43             -1.0,  1.0,  1.0,
    44             -1.0,  1.0, -1.0];
    45     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
    46     this.positionBuffer.itemSize = 3;
    47     this.positionBuffer.numItems = 24;
    48 
    49     this.colourBuffer = gl.createBuffer();
    50     gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
    51     var alpha = 1.0;
    52     var colours = [[1.0, 0.0, 0.0, alpha],
    53 		   [0.0, 1.0, 0.0, alpha],
    54 		   [0.0, 0.0, 1.0, alpha],
    55 		   [1.0, 0.0, 1.0, alpha],
    56 		   [1.0, 1.0, 0.0, alpha],
    57 		   [0.0, 1.0, 1.0, alpha]];
    58     var unpackedColours = [];
    59     for (var i in colours)
    60     {
    61 	var colour = colours[i];
    62 	for (var j = 0; j < 4; j++)
    63 	{
    64 	    unpackedColours = unpackedColours.concat(colour);
    65 	}
    66 	//colours = colours.concat([0.5, 0.5, 1.0, 1.0]);
    67     }
    68     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColours), gl.STATIC_DRAW);
    69     this.colourBuffer.itemSize = 4;
    70     this.colourBuffer.numItems = 24;
    71 
    72     this.indexBuffer = gl.createBuffer();
    73     gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
    74     var indices = [0, 1, 2, 0, 2, 3,
    75 		   4, 5, 6, 4, 6, 7,
    76 		   8, 9, 10, 8, 10, 11,
    77 		   12, 13, 14, 12, 14, 15,
    78 		   16, 17, 18, 16, 18, 19,
    79 		   20, 21, 22, 20, 22, 23];
    80     gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
    81     this.indexBuffer.itemSize = 1;
    82     this.indexBuffer.numItems = 36;
    83 
    84     this.normalBuffer = gl.createBuffer();
    85     gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
    86     var normals = [// font face
    87 		   0.0, 0.0, 1.0,
    88 		   0.0, 0.0, 1.0,
    89 		   0.0, 0.0, 1.0,
    90 		   // back face
    91 		   0.0, 0.0, -1.0,
    92 		   0.0, 0.0, -1.0,
    93 		   0.0, 0.0, -1.0,
    94 		   0.0, 0.0, -1.0,
    95 		   // top face
    96 		   0.0, 1.0, 0.0,
    97 		   0.0, 1.0, 0.0,
    98 		   0.0, 1.0, 0.0, 
    99 		   0.0, 1.0, 0.0,
   100 		   // bottom face
   101 		   0.0, -1.0, 0.0,
   102 		   0.0, -1.0, 0.0,
   103 		   0.0, -1.0, 0.0,
   104 		   0.0, -1.0, 0.0,
   105 		   // right face
   106 		   1.0, 0.0, 0.0,
   107 		   1.0, 0.0, 0.0,
   108 		   1.0, 0.0, 0.0,
   109 		   1.0, 0.0, 0.0,
   110 		   // left face
   111 		   -1.0, 0.0, 0.0,
   112 		   -1.0, 0.0, 0.0,
   113 		   -1.0, 0.0, 0.0,
   114 		   -1.0, 0.0, 0.0];
   115     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
   116     this.normalBuffer.itemSize = 3;
   117     this.normalBuffer.numItems = 24;
   118 }