diff options
author | gman@chromium.org <gman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-30 23:02:18 +0000 |
---|---|---|
committer | gman@chromium.org <gman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-30 23:02:18 +0000 |
commit | d174615ea4ff395f7b924b6b07fd408a136ecc99 (patch) | |
tree | f983e8935b226cdf284ad461bd178f2423d99a37 /o3d/samples | |
parent | 3e7824dcdcd1f84a553395630bbbe72a27612930 (diff) | |
download | chromium_src-d174615ea4ff395f7b924b6b07fd408a136ecc99.zip chromium_src-d174615ea4ff395f7b924b6b07fd408a136ecc99.tar.gz chromium_src-d174615ea4ff395f7b924b6b07fd408a136ecc99.tar.bz2 |
Make primitives and lineprimitives share
the implemtation of createShape
Review URL: http://codereview.chromium.org/453010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33367 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d/samples')
-rw-r--r-- | o3d/samples/o3djs/lineprimitives.js | 67 | ||||
-rw-r--r-- | o3d/samples/o3djs/primitives.js | 175 |
2 files changed, 111 insertions, 131 deletions
diff --git a/o3d/samples/o3djs/lineprimitives.js b/o3d/samples/o3djs/lineprimitives.js index 07ea7f8..f9f6164 100644 --- a/o3d/samples/o3djs/lineprimitives.js +++ b/o3d/samples/o3djs/lineprimitives.js @@ -116,71 +116,8 @@ o3djs.lineprimitives.LineVertexInfo.prototype.setLine = function( o3djs.lineprimitives.LineVertexInfo.prototype.createShape = function( pack, material) { - this.validate(); - - var positionStream = this.findStream(o3djs.base.o3d.Stream.POSITION); - var numVertices = positionStream.numElements(); - - // create a shape and primitive for the vertices. - var shape = pack.createObject('Shape'); - var primitive = pack.createObject('Primitive'); - var streamBank = pack.createObject('StreamBank'); - primitive.owner = shape; - primitive.streamBank = streamBank; - primitive.material = material; - primitive.numberPrimitives = this.indices.length / 2; - primitive.primitiveType = o3djs.base.o3d.Primitive.LINELIST; - primitive.numberVertices = numVertices; - primitive.createDrawElement(pack, null); - - // Provide default streams or fail if the effect requires them. - var streamInfos = material.effect.getStreamInfo(); - for (var s = 0; s < streamInfos.length; ++s) { - var semantic = streamInfos[s].semantic; - var semanticIndex = streamInfos[s].semanticIndex; - - var requiredStream = this.findStream(semantic, semanticIndex); - if (!requiredStream) { - switch (semantic) { - case o3djs.base.o3d.Stream.COLOR: - requiredStream = this.addStream(4, semantic, semanticIndex); - for (var i = 0; i < numVertices; ++i) { - requiredStream.addElement(1, 1, 1, 1); - } - break; - default: - throw 'Missing stream for semantic ' + semantic + - ' with semantic index ' + semanticIndex; - } - } - } - - // These next few lines take our javascript streams and load them into a - // 'buffer' where the 3D hardware can find them. We have to do this - // because the 3D hardware can't 'see' javascript data until we copy it to - // a buffer. - var vertexBuffer = pack.createObject('VertexBuffer'); - var fields = []; - for (var s = 0; s < this.streams.length; ++s) { - var stream = this.streams[s]; - var fieldType = (stream.semantic == o3djs.base.o3d.Stream.COLOR && - stream.numComponents == 4) ? 'UByteNField' : 'FloatField'; - fields[s] = vertexBuffer.createField(fieldType, stream.numComponents); - streamBank.setVertexStream(stream.semantic, - stream.semanticIndex, - fields[s], - 0); - } - vertexBuffer.allocateElements(numVertices); - for (var s = 0; s < this.streams.length; ++s) { - fields[s].setAt(0, this.streams[s].elements); - } - - var indexBuffer = pack.createObject('IndexBuffer'); - indexBuffer.set(this.indices); - primitive.indexBuffer = indexBuffer; - o3djs.primitives.setCullingInfo(primitive); - return shape; + this.createShapeByType( + pack, material, o3djs.base.o3d.Primitive.LINELIST); }; /** diff --git a/o3d/samples/o3djs/primitives.js b/o3d/samples/o3djs/primitives.js index 2e057af..d3e5419 100644 --- a/o3d/samples/o3djs/primitives.js +++ b/o3d/samples/o3djs/primitives.js @@ -465,76 +465,41 @@ o3djs.primitives.VertexInfoBase.prototype.reorient = function(matrix) { }; /** - * A VertexInfo is a specialization of VertexInfoBase for triangle based - * geometry. - * @constructor - * @extends {o3djs.primitives.VertexInfoBase} - */ -o3djs.primitives.VertexInfo = function() { - o3djs.primitives.VertexInfoBase.call(this); -} - -o3djs.base.inherit(o3djs.primitives.VertexInfo, - o3djs.primitives.VertexInfoBase); - -/** - * Returns the number of triangles represented by the VertexInfo. - * @return {number} The number of triangles represented by VertexInfo. - */ -o3djs.primitives.VertexInfo.prototype.numTriangles = function() { - return this.indices.length / 3; -}; - -/** - * Adds a triangle. - * @param {number} index1 The index of the first vertex of the triangle. - * @param {number} index2 The index of the second vertex of the triangle. - * @param {number} index3 The index of the third vertex of the triangle. - */ -o3djs.primitives.VertexInfo.prototype.addTriangle = function( - index1, index2, index3) { - this.indices.push(index1, index2, index3); -}; - -/** - * Gets the vertex indices of the triangle at the given triangle index. - * @param {number} triangleIndex The index of the triangle. - * @return {!Array.<number>} An array of three triangle indices. - */ -o3djs.primitives.VertexInfo.prototype.getTriangle = function( - triangleIndex) { - var indexIndex = triangleIndex * 3; - return [this.indices[indexIndex + 0], - this.indices[indexIndex + 1], - this.indices[indexIndex + 2]]; -}; - -/** - * Sets the vertex indices of the triangle at the given triangle index. - * @param {number} triangleIndex The index of the triangle. - * @param {number} index1 The index of the first vertex of the triangle. - * @param {number} index2 The index of the second vertex of the triangle. - * @param {number} index3 The index of the third vertex of the triangle. - */ -o3djs.primitives.VertexInfo.prototype.setTriangle = function( - triangleIndex, index1, index2, index3) { - var indexIndex = triangleIndex * 3; - this.indices[indexIndex + 0] = index1; - this.indices[indexIndex + 1] = index2; - this.indices[indexIndex + 2] = index3; -}; - -/** - * Creates a shape from a VertexInfo + * Creates a shape from a VertexInfoBase * @param {!o3d.Pack} pack Pack to create objects in. * @param {!o3d.Material} material to use. + * @param {!o3d.Primitive.PrimitiveType} primitiveType The type of primitive. * @return {!o3d.Shape} The created shape. */ -o3djs.primitives.VertexInfo.prototype.createShape = function( +o3djs.primitives.VertexInfoBase.prototype.createShapeByType = function( pack, - material) { + material, + primitiveType) { this.validate(); + var numIndices = this.indices.length; + var numPrimitives; + switch (primitiveType) { + case o3djs.base.o3d.Primitive.POINTLIST: + numPrimitives = numIndices / 1; + break; + case o3djs.base.o3d.Primitive.LINELIST: + numPrimitives = numIndices / 2; + break; + case o3djs.base.o3d.Primitive.LINESTRIP: + numPrimitives = numIndices - 1; + break; + case o3djs.base.o3d.Primitive.TRIANGLELIST: + numPrimitives = numIndices / 3; + break; + case o3djs.base.o3d.Primitive.TRIANGLESTRIP: + case o3djs.base.o3d.Primitive.TRIANGLEFAN: + numPrimitives = numIndices - 2; + break; + default: + throw 'unknown primitive type'; + } + var positionStream = this.findStream(o3djs.base.o3d.Stream.POSITION); var numVertices = positionStream.numElements(); @@ -545,8 +510,8 @@ o3djs.primitives.VertexInfo.prototype.createShape = function( primitive.owner = shape; primitive.streamBank = streamBank; primitive.material = material; - primitive.numberPrimitives = this.indices.length / 3; - primitive.primitiveType = o3djs.base.o3d.Primitive.TRIANGLELIST; + primitive.numberPrimitives = numPrimitives; + primitive.primitiveType = primitiveType; primitive.numberVertices = numVertices; primitive.createDrawElement(pack, null); @@ -562,7 +527,12 @@ o3djs.primitives.VertexInfo.prototype.createShape = function( switch (semantic) { case o3djs.base.o3d.Stream.TANGENT: case o3djs.base.o3d.Stream.BINORMAL: - this.addTangentStreams(semanticIndex); + if (primitiveType == o3djs.base.o3d.Primitive.TRIANGLELIST) { + this.addTangentStreams(semanticIndex); + } else { + throw 'Can not create tangents and binormals for primitive type' + + primitiveType; + } break; case o3djs.base.o3d.Stream.COLOR: requiredStream = this.addStream(4, semantic, semanticIndex); @@ -606,6 +576,79 @@ o3djs.primitives.VertexInfo.prototype.createShape = function( }; /** + * A VertexInfo is a specialization of VertexInfoBase for triangle based + * geometry. + * @constructor + * @extends {o3djs.primitives.VertexInfoBase} + */ +o3djs.primitives.VertexInfo = function() { + o3djs.primitives.VertexInfoBase.call(this); +} + +o3djs.base.inherit(o3djs.primitives.VertexInfo, + o3djs.primitives.VertexInfoBase); + +/** + * Returns the number of triangles represented by the VertexInfo. + * @return {number} The number of triangles represented by VertexInfo. + */ +o3djs.primitives.VertexInfo.prototype.numTriangles = function() { + return this.indices.length / 3; +}; + +/** + * Adds a triangle. + * @param {number} index1 The index of the first vertex of the triangle. + * @param {number} index2 The index of the second vertex of the triangle. + * @param {number} index3 The index of the third vertex of the triangle. + */ +o3djs.primitives.VertexInfo.prototype.addTriangle = function( + index1, index2, index3) { + this.indices.push(index1, index2, index3); +}; + +/** + * Gets the vertex indices of the triangle at the given triangle index. + * @param {number} triangleIndex The index of the triangle. + * @return {!Array.<number>} An array of three triangle indices. + */ +o3djs.primitives.VertexInfo.prototype.getTriangle = function( + triangleIndex) { + var indexIndex = triangleIndex * 3; + return [this.indices[indexIndex + 0], + this.indices[indexIndex + 1], + this.indices[indexIndex + 2]]; +}; + +/** + * Sets the vertex indices of the triangle at the given triangle index. + * @param {number} triangleIndex The index of the triangle. + * @param {number} index1 The index of the first vertex of the triangle. + * @param {number} index2 The index of the second vertex of the triangle. + * @param {number} index3 The index of the third vertex of the triangle. + */ +o3djs.primitives.VertexInfo.prototype.setTriangle = function( + triangleIndex, index1, index2, index3) { + var indexIndex = triangleIndex * 3; + this.indices[indexIndex + 0] = index1; + this.indices[indexIndex + 1] = index2; + this.indices[indexIndex + 2] = index3; +}; + +/** + * Creates a shape from a VertexInfo + * @param {!o3d.Pack} pack Pack to create objects in. + * @param {!o3d.Material} material to use. + * @return {!o3d.Shape} The created shape. + */ +o3djs.primitives.VertexInfo.prototype.createShape = function( + pack, + material) { + this.createShapeByType( + pack, material, o3djs.base.o3d.Primitive.TRIANGLELIST); +}; + +/** * Calculate tangents and binormals based on the positions, normals and * texture coordinates found in existing streams. * @param {number} opt_semanticIndex The semantic index of the texture |