Subversion Repositories AndroidProjects

Rev

Rev 805 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Tao.OpenGl;

#pragma warning disable 0618

namespace BauzoidNET.graphics.model
{
    public class SimpleGeometry : GraphicsObject
    {
        public const int POSITION_COORD_PER_ELEMENT = 3;
            public const int NORMAL_COORD_PER_ELEMENT = 3;
            public const int COLOR_COORD_PER_ELEMENT = 4;
        public const int TEXCOORD_COORD_PER_ELEMENT = 2;
       
            private Geometry.PrimitiveType mPrimitiveType = Geometry.PrimitiveType.TRIANGLES;
            private int mPrimitiveTypeGL = Gl.GL_TRIANGLES;
       
            private VertexStream mPositionStream = null;
            private VertexStream mNormalStream = null;
            private VertexStream mColorStream = null;
            private VertexStream mTexCoordStream = null;
            private IndexStream mIndexStream = null;
       
            public SimpleGeometry(Graphics graphics, Geometry.PrimitiveType primitiveType)
            : base(graphics)
            {          
                    mPrimitiveType = primitiveType;
                    mPrimitiveTypeGL = Geometry.toPrimitiveGL(primitiveType);
            }
       
            public void dispose()
            {
                    destroyPositionStream();
                    destroyNormalStream();
                    destroyColorStream();
                    destroyTexCoordStream();
                    destroyIndexStream();
            }
       
            public void destroyPositionStream()
            {
                    if (mPositionStream != null)
                    {
                            mPositionStream.dispose();
                            mPositionStream = null;
                    }
            }  
       
            public void destroyNormalStream()
            {
                    if (mNormalStream != null)
                    {
                            mNormalStream.dispose();
                            mNormalStream = null;
                    }
            }  
       
            public void destroyColorStream()
            {
                    if (mColorStream != null)
                    {
                            mColorStream.dispose();
                            mColorStream = null;
                    }
            }  
       
            public void destroyTexCoordStream()
            {
                    if (mTexCoordStream != null)
                    {
                            mTexCoordStream.dispose();
                            mTexCoordStream = null;
                    }
            }  
       
            public void destroyIndexStream()
            {
                    if (mIndexStream != null)
                    {
                            mIndexStream.dispose();
                            mIndexStream = null;
                    }
            }
       
            public void setPositions(float[] positions)
            {
                    setPositions(positions, false);
            }
       
            public void setPositions(float[] positions, bool isDynamic)
            {
                    if ((mPositionStream == null) ||
                            (positions.Length != (mPositionStream.getVertexCount() * mPositionStream.getCoordsPerElement())) ||
                            (isDynamic != mPositionStream.isDynamic()))
                    {
                            destroyPositionStream();
                       
                            mPositionStream = new VertexStream(getGraphics(), VertexStream.StreamType.INDIVIDUAL, "Positions",
                                            new VertexAttribute[] { new VertexAttribute(VertexAttribute.POSITION, POSITION_COORD_PER_ELEMENT, 0) },
                                            isDynamic);
                    }
                    mPositionStream.setData(positions, POSITION_COORD_PER_ELEMENT, POSITION_COORD_PER_ELEMENT * sizeof(float));
                    mPositionStream.reupload();
            }
       
            public void setNormals(float[] normals)
            {
                    setNormals(normals, false);
            }
       
            public void setNormals(float[] normals, bool isDynamic)
            {
                    if ((mNormalStream == null) ||
                            (normals.Length != (mNormalStream.getVertexCount() * mNormalStream.getCoordsPerElement())) ||
                            (isDynamic != mNormalStream.isDynamic()))
                    {
                            destroyNormalStream();

                mNormalStream = new VertexStream(getGraphics(), VertexStream.StreamType.INDIVIDUAL, "Normals",
                                            new VertexAttribute[] { new VertexAttribute(VertexAttribute.NORMAL, NORMAL_COORD_PER_ELEMENT, 0) },
                                            isDynamic);
                    }
                    mNormalStream.setData(normals, NORMAL_COORD_PER_ELEMENT, NORMAL_COORD_PER_ELEMENT * sizeof(float));
                    mNormalStream.reupload();
            }
       
            public void setColors(float[] colors)
            {
                    setColors(colors, false);
            }
       
            public void setColors(float[] colors, bool isDynamic)
            {
                    if ((mColorStream == null) ||
                            (colors.Length != (mColorStream.getVertexCount() * mColorStream.getCoordsPerElement())) ||
                            (isDynamic != mColorStream.isDynamic()))
                    {
                            destroyColorStream();

                mColorStream = new VertexStream(getGraphics(), VertexStream.StreamType.INDIVIDUAL, "Colors",
                                            new VertexAttribute[] { new VertexAttribute(VertexAttribute.COLOR, COLOR_COORD_PER_ELEMENT, 0) },
                                            isDynamic);
                    }
                    mColorStream.setData(colors, COLOR_COORD_PER_ELEMENT, COLOR_COORD_PER_ELEMENT * sizeof(float));
                    mColorStream.reupload();
            }
       
            public void setTexCoords(float[] texCoords)
            {
                    setTexCoords(texCoords, false);
            }
       
            public void setTexCoords(float[] texCoords, bool isDynamic)
            {
                    if ((mTexCoordStream == null) ||
                            (texCoords.Length != (mTexCoordStream.getVertexCount() * mTexCoordStream.getCoordsPerElement())) ||
                            (isDynamic != mTexCoordStream.isDynamic()))
                    {
                            destroyTexCoordStream();

                mTexCoordStream = new VertexStream(getGraphics(), VertexStream.StreamType.INDIVIDUAL, "TexCoord",
                                            new VertexAttribute[] { new VertexAttribute(VertexAttribute.TEXCOORD0, TEXCOORD_COORD_PER_ELEMENT, 0) },
                                            isDynamic);
                    }
                    mTexCoordStream.setData(texCoords, TEXCOORD_COORD_PER_ELEMENT, TEXCOORD_COORD_PER_ELEMENT * sizeof(float));
                    mTexCoordStream.reupload();
            }
       
            public void setIndices(short[] indices)
            {
                    if ((mIndexStream == null) || (indices.Length != (mIndexStream.getIndexCount())))
                    {
                            destroyIndexStream();
                       
                            mIndexStream = new IndexStream(getGraphics());
                    }
                    mIndexStream.setData(indices);
                    mIndexStream.reupload();
            }
       
            public void render(int first, int num)
            {
                    if (mPositionStream == null)
                            return;
               
                    mPositionStream.activate();
               
                    if (mNormalStream != null)
                            mNormalStream.activate();

                    if (mColorStream != null)
                            mColorStream.activate();
               
                    if (mTexCoordStream != null)
                            mTexCoordStream.activate();
       
                    if (mIndexStream != null)
                    {
                            mIndexStream.activate();
                       
                            Gl.glDrawElements(mPrimitiveTypeGL, num, Gl.GL_UNSIGNED_SHORT, (IntPtr)(first * sizeof(short)));
                       
                            mIndexStream.deactivate();
                    }
                    else
                    {
                            Gl.glDrawArrays(mPrimitiveTypeGL, first, num);
                    }
               
                    mPositionStream.deactivate();
               
                    if (mNormalStream != null)
                            mNormalStream.deactivate();

                    if (mColorStream != null)
                            mColorStream.deactivate();
               
                    if (mTexCoordStream != null)
                            mTexCoordStream.deactivate();
            }
       
            public void render()
            {
                    if (hasIndices())
                    {
                            //Gdx.gl20.glDrawElements(mPrimitiveTypeGL, mIndexStream.getIndexCount(), GL20.GL_UNSIGNED_SHORT, 0);
                            render(0, mIndexStream.getIndexCount());
                    }
                    else
                    {
                            if (mPositionStream == null)
                                    return;

                            render(0, mPositionStream.getVertexCount());
                            //Gdx.gl20.glDrawArrays(mPrimitiveTypeGL, 0, mPositionStream.getVertexCount());
                    }
            }
       
            public Geometry.PrimitiveType getPrimitiveType()
            {
                    return mPrimitiveType;
            }
       
            public bool hasIndices()
            {
                    return (mIndexStream != null);
            }
    }
}