Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.graphics.model;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.gebauz.bauzoid.app.Consts;
import com.gebauz.bauzoid.graphics.Graphics;
import com.gebauz.bauzoid.graphics.GraphicsObject;

/** Simpler Geometry class for creating geometry from code. */
public class SimpleGeometry extends GraphicsObject
{
        public static final int POSITION_COORD_PER_ELEMENT = 3;
        public static final int NORMAL_COORD_PER_ELEMENT = 3;
        public static final int COLOR_COORD_PER_ELEMENT = 4;
        public static final int TEXCOORD_COORD_PER_ELEMENT = 2;
       
        private Geometry.PrimitiveType mPrimitiveType = Geometry.PrimitiveType.TRIANGLES;
        private int mPrimitiveTypeGL = GL20.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)
        {              
                super(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, boolean 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 * Consts.SIZEOF_FLOAT);
                mPositionStream.reupload();
        }
       
        public void setNormals(float[] normals)
        {
                setNormals(normals, false);
        }
       
        public void setNormals(float[] normals, boolean 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 * Consts.SIZEOF_FLOAT);
                mNormalStream.reupload();
        }
       
        public void setColors(float[] colors)
        {
                setColors(colors, false);
        }
       
        public void setColors(float[] colors, boolean 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 * Consts.SIZEOF_FLOAT);
                mColorStream.reupload();
        }
       
        public void setTexCoords(float[] texCoords)
        {
                setTexCoords(texCoords, false);
        }
       
        public void setTexCoords(float[] texCoords, boolean 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 * Consts.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();
                       
                        Gdx.gl20.glDrawElements(mPrimitiveTypeGL, num, GL20.GL_UNSIGNED_SHORT, first * Consts.SIZEOF_SHORT);
                       
                        mIndexStream.deactivate();
                }
                else
                {
                        Gdx.gl20.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 boolean hasIndices()
        {
                return (mIndexStream != null);
        }
       
}