scripts/cube.js
author Eugen Sawin <sawine@me73.com>
Tue, 26 Apr 2011 03:06:56 +0200
changeset 25 3b66f92dbc4d
parent 24 8c2ee41d3727
child 28 9df7034275e8
permissions -rw-r--r--
Basic 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.normalBuffer = gl.createBuffer();
    50     gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
    51     var normals = [// font face
    52 		   0.0, 0.0, 1.0,
    53 		   0.0, 0.0, 1.0,
    54 		   0.0, 0.0, 1.0,
    55 		   0.0, 0.0, 1.0,
    56 		   // back face
    57 		   0.0, 0.0, -1.0,
    58 		   0.0, 0.0, -1.0,
    59 		   0.0, 0.0, -1.0,
    60 		   0.0, 0.0, -1.0,
    61 		   // top face
    62 		   0.0, 1.0, 0.0,
    63 		   0.0, 1.0, 0.0,
    64 		   0.0, 1.0, 0.0, 
    65 		   0.0, 1.0, 0.0,
    66 		   // bottom face
    67 		   0.0, -1.0, 0.0,
    68 		   0.0, -1.0, 0.0,
    69 		   0.0, -1.0, 0.0,
    70 		   0.0, -1.0, 0.0,
    71 		   // right face
    72 		   1.0, 0.0, 0.0,
    73 		   1.0, 0.0, 0.0,
    74 		   1.0, 0.0, 0.0,
    75 		   1.0, 0.0, 0.0,
    76 		   // left face
    77 		   -1.0, 0.0, 0.0,
    78 		   -1.0, 0.0, 0.0,
    79 		   -1.0, 0.0, 0.0,
    80 		   -1.0, 0.0, 0.0];
    81     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
    82     this.normalBuffer.itemSize = 3;
    83     this.normalBuffer.numItems = 24;
    84 
    85     this.colourBuffer = gl.createBuffer();
    86     gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
    87     var alpha = 1.0;
    88     var uni = [1.0, 1.0, 1.0, alpha];
    89     var colours = [[0.0, 0.0, 0.0, alpha],
    90 		   [0.0, 0.0, 0.0, alpha],
    91 		   [0.0, 0.0, 0.0, alpha],
    92 		   [0.0, 0.0, 0.0, alpha],
    93 		   [0.0, 0.0, 0.0, alpha],
    94 		   [0.0, 0.0, 0.0, alpha]];
    95     var unpackedColours = [];
    96     for (var i in colours)
    97     {
    98 	var colour = uni;//colours[i];
    99 	for (var j = 0; j < 4; j++)
   100 	{
   101 	    unpackedColours = unpackedColours.concat(colour);
   102 	}
   103 	//colours = colours.concat([0.5, 0.5, 1.0, 1.0]);
   104     }
   105     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColours), gl.STATIC_DRAW);
   106     this.colourBuffer.itemSize = 4;
   107     this.colourBuffer.numItems = 24;
   108 
   109     this.indexBuffer = gl.createBuffer();
   110     gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
   111     var indices = [0, 1, 2, 0, 2, 3,
   112 		   4, 5, 6, 4, 6, 7,
   113 		   8, 9, 10, 8, 10, 11,
   114 		   12, 13, 14, 12, 14, 15,
   115 		   16, 17, 18, 16, 18, 19,
   116 		   20, 21, 22, 20, 22, 23];
   117     gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
   118     this.indexBuffer.itemSize = 1;
   119     this.indexBuffer.numItems = 36;
   120 }