diff options
Diffstat (limited to 'graphics/java/android/renderscript/SimpleMesh.java')
-rw-r--r-- | graphics/java/android/renderscript/SimpleMesh.java | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/graphics/java/android/renderscript/SimpleMesh.java b/graphics/java/android/renderscript/SimpleMesh.java index d80551e..e66fb8a 100644 --- a/graphics/java/android/renderscript/SimpleMesh.java +++ b/graphics/java/android/renderscript/SimpleMesh.java @@ -167,5 +167,150 @@ public class SimpleMesh extends BaseObj { } } + public static class TriangleMeshBuilder { + float mVtxData[]; + int mVtxCount; + int mIndexData[]; + int mIndexCount; + RenderScript mRS; + Element mElement; + + int mVtxSize; + boolean mNorm; + boolean mTex; + + public TriangleMeshBuilder(RenderScript rs, int vtxSize, boolean norm, boolean tex) { + mRS = rs; + mVtxCount = 0; + mIndexCount = 0; + mVtxData = new float[128]; + mIndexData = new int[128]; + mVtxSize = vtxSize; + mNorm = norm; + mTex = tex; + + if(vtxSize < 2 || vtxSize > 3) { + throw new IllegalArgumentException("Vertex size out of range."); + } + } + + private void makeSpace(int count) { + if((mVtxCount + count) >= mVtxData.length) { + float t[] = new float[mVtxData.length * 2]; + System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); + mVtxData = t; + } + } + + public void add_XY(float x, float y) { + if((mVtxSize != 2) || mNorm || mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(2); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + } + + public void add_XYZ(float x, float y, float z) { + if((mVtxSize != 3) || mNorm || mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(3); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = z; + } + + public void add_XY_ST(float x, float y, float s, float t) { + if((mVtxSize != 2) || mNorm || !mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(4); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = s; + mVtxData[mVtxCount++] = t; + } + + public void add_XYZ_ST(float x, float y, float z, float s, float t) { + if((mVtxSize != 3) || mNorm || !mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(5); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = z; + mVtxData[mVtxCount++] = s; + mVtxData[mVtxCount++] = t; + } + + public void add_XYZ_ST_NORM(float x, float y, float z, float s, float t, float nx, float ny, float nz) { + if((mVtxSize != 3) || !mNorm || !mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(8); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = z; + mVtxData[mVtxCount++] = s; + mVtxData[mVtxCount++] = t; + mVtxData[mVtxCount++] = nx; + mVtxData[mVtxCount++] = ny; + mVtxData[mVtxCount++] = nz; + } + + public void addTriangle(int idx1, int idx2, int idx3) { + if((mIndexCount + 3) >= mIndexData.length) { + int t[] = new int[mIndexData.length * 2]; + System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); + mIndexData = t; + } + mIndexData[mIndexCount++] = idx1; + mIndexData[mIndexCount++] = idx2; + mIndexData[mIndexCount++] = idx3; + } + + public SimpleMesh create() { + Element.Builder b = new Element.Builder(mRS); + int floatCount = mVtxSize; + if(mVtxSize == 2) { + b.addFloatXY(); + } else { + b.addFloatXYZ(); + } + if(mTex) { + floatCount += 2; + b.addFloatST(); + } + if(mNorm) { + floatCount += 3; + b.addFloatNorm(); + } + mElement = b.create(); + + Builder smb = new Builder(mRS); + smb.addVertexType(mElement, mVtxCount / floatCount); + smb.setIndexType(Element.INDEX_16, mIndexCount); + smb.setPrimitive(Primitive.TRIANGLE); + SimpleMesh sm = smb.create(); + + Allocation vertexAlloc = sm.createVertexAllocation(0); + Allocation indexAlloc = sm.createIndexAllocation(); + sm.bindVertexAllocation(vertexAlloc, 0); + sm.bindIndexAllocation(indexAlloc); + + vertexAlloc.data(mVtxData); + vertexAlloc.uploadToBufferObject(); + + // This is safe because length is a pow2 + for(int ct=0; ct < (mIndexCount+1); ct += 2) { + mIndexData[ct >> 1] = mIndexData[ct] | (mIndexData[ct+1] << 16); + } + indexAlloc.data(mIndexData); + indexAlloc.uploadToBufferObject(); + + return sm; + } + } } |