scripts/cube.js
author Eugen Sawin <sawine@me73.com>
Sat, 30 Apr 2011 04:32:22 +0200
changeset 38 ce3efaacb700
parent 35 7769f6e1e2c5
permissions -rw-r--r--
Added test texture.
     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 function CubeVbo(size, context, texture)
    24 {
    25     var gl = context.gl;
    26     this.texture = texture;
    27     this.useTexture = texture;
    28     this.size = size || 1;    
    29     this.positionBuffer = gl.createBuffer();
    30     gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
    31     var vertices = [
    32 	     // Front face
    33             -1.0, -1.0,  1.0,
    34              1.0, -1.0,  1.0,
    35              1.0,  1.0,  1.0,
    36             -1.0,  1.0,  1.0,
    37  
    38             // Back face
    39             -1.0, -1.0, -1.0,
    40             -1.0,  1.0, -1.0,
    41              1.0,  1.0, -1.0,
    42              1.0, -1.0, -1.0,
    43  
    44             // Top face
    45             -1.0,  1.0, -1.0,
    46             -1.0,  1.0,  1.0,
    47              1.0,  1.0,  1.0,
    48              1.0,  1.0, -1.0,
    49  
    50             // Bottom face
    51             -1.0, -1.0, -1.0,
    52              1.0, -1.0, -1.0,
    53              1.0, -1.0,  1.0,
    54             -1.0, -1.0,  1.0,
    55  
    56             // Right face
    57              1.0, -1.0, -1.0,
    58              1.0,  1.0, -1.0,
    59              1.0,  1.0,  1.0,
    60              1.0, -1.0,  1.0,
    61  
    62             // Left face
    63             -1.0, -1.0, -1.0,
    64             -1.0, -1.0,  1.0,
    65             -1.0,  1.0,  1.0,
    66             -1.0,  1.0, -1.0];
    67     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
    68     this.positionBuffer.itemSize = 3;
    69     this.positionBuffer.numItems = 24;
    70 
    71     this.normalBuffer = gl.createBuffer();
    72     gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
    73     var normals = [// font face
    74 		   0.0, 0.0, 1.0,
    75 		   0.0, 0.0, 1.0,
    76 		   0.0, 0.0, 1.0,
    77 		   0.0, 0.0, 1.0,
    78 		   // back face
    79 		   0.0, 0.0, -1.0,
    80 		   0.0, 0.0, -1.0,
    81 		   0.0, 0.0, -1.0,
    82 		   0.0, 0.0, -1.0,
    83 		   // top face
    84 		   0.0, 1.0, 0.0,
    85 		   0.0, 1.0, 0.0,
    86 		   0.0, 1.0, 0.0, 
    87 		   0.0, 1.0, 0.0,
    88 		   // bottom face
    89 		   0.0, -1.0, 0.0,
    90 		   0.0, -1.0, 0.0,
    91 		   0.0, -1.0, 0.0,
    92 		   0.0, -1.0, 0.0,
    93 		   // right face
    94 		   1.0, 0.0, 0.0,
    95 		   1.0, 0.0, 0.0,
    96 		   1.0, 0.0, 0.0,
    97 		   1.0, 0.0, 0.0,
    98 		   // left face
    99 		   -1.0, 0.0, 0.0,
   100 		   -1.0, 0.0, 0.0,
   101 		   -1.0, 0.0, 0.0,
   102 		   -1.0, 0.0, 0.0];
   103     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
   104     this.normalBuffer.itemSize = 3;
   105     this.normalBuffer.numItems = 24;
   106 
   107     //if (this.texture)
   108     //{
   109 	this.textureCoordBuffer = gl.createBuffer();
   110 	gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordBuffer);
   111 	var textureCoords = [// front face
   112 	    0.0, 0.0,
   113 	    1.0, 0.0,
   114 	    1.0, 1.0,
   115 	    0.0, 1.0,
   116 	    //back face
   117 	    1.0, 0.0,
   118 	    1.0, 1.0,
   119 	    0.0, 1.0,
   120 	    0.0, 0.0,
   121 	    // top face
   122 	    0.0, 1.0,
   123 	    0.0, 0.0,
   124 	    1.0, 0.0,
   125 	    1.0, 1.0,
   126 	    // bottom face
   127 	    1.0, 1.0,
   128 	    0.0, 1.0,
   129 	    0.0, 0.0,
   130 	    1.0, 0.0,
   131 	    // right face
   132 	    1.0, 0.0, 
   133 	    1.0, 1.0,
   134 	    0.0, 1.0,
   135 	    0.0, 0.0,
   136 	    // left face
   137 	    0.0, 0.0,
   138 	    1.0, 0.0,
   139 	    1.0, 1.0,
   140 	    0.0, 1.0];
   141 	gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
   142 	this.textureCoordBuffer.itemSize = 2;
   143 	this.textureCoordBuffer.numItems = 24;
   144     //}
   145    
   146     this.colourBuffer = gl.createBuffer();
   147     gl.bindBuffer(gl.ARRAY_BUFFER, this.colourBuffer);
   148     var alpha = 1.0;
   149     var uni = [1.0, 1.0, 1.0, alpha];
   150     var colours = [[0.0, 0.0, 0.0, alpha],
   151 		   [0.0, 0.0, 0.0, alpha],
   152 		   [0.0, 0.0, 0.0, alpha],
   153 		   [0.0, 0.0, 0.0, alpha],
   154 		   [0.0, 0.0, 0.0, alpha],
   155 		   [0.0, 0.0, 0.0, alpha]];
   156     var unpackedColours = [];
   157     for (var i in colours)
   158     {
   159 	var colour = uni;//colours[i];
   160 	for (var j = 0; j < 4; j++)
   161 	{
   162 	    unpackedColours = unpackedColours.concat(colour);
   163 	}
   164 	//colours = colours.concat([0.5, 0.5, 1.0, 1.0]);
   165     }
   166     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColours), gl.STATIC_DRAW);
   167     this.colourBuffer.itemSize = 4;
   168     this.colourBuffer.numItems = 24;
   169     
   170     this.indexBuffer = gl.createBuffer();
   171     gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
   172     var indices = [0, 1, 2, 0, 2, 3,
   173 		   4, 5, 6, 4, 6, 7,
   174 		   8, 9, 10, 8, 10, 11,
   175 		   12, 13, 14, 12, 14, 15,
   176 		   16, 17, 18, 16, 18, 19,
   177 		   20, 21, 22, 20, 22, 23];
   178     gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
   179     this.indexBuffer.itemSize = 1;
   180     this.indexBuffer.numItems = 36;
   181 }
   182 
   183 var id = 0;
   184 function Cube(vbo)
   185 {
   186     this.id = id++;
   187     this.vbo = vbo;
   188 }