How can I sort objects?

120 views Asked by At

I'm trying draw some objects in an opengl world . these objects have transparency so. and when I rotate them the transparency doesn't blend color . How can I sort the draw order, so I can draw from more distant object to closest? I've tryed with bounding box and get the more distance point . then sort objects with a bubblesort. Then I give to Renderer the IndexArray sorted.

This is Bounding box.

public class BoundingBox extends java.lang.Object  {


    private UlVertexBuffer mVb=null;
    private UlIndexBuffer mIb=null;
    private UlTransform mTransform=null;
    private UlVertexBuffer subsetVertexBuffer;
    private UlMatrix4x4[] mM=new UlMatrix4x4[2];
    private float array[][]=new float[2][16];
    private float minX,minY,minZ,maxX,maxY,maxZ=0; // min and max value of the vector components

    public BoundingBox (UlVertexBuffer vb, UlIndexBuffer ib)
    {
        FloatBuffer fb =(FloatBuffer) vb.getData(UlVertexBuffer.VERTEX_FIELD_POSITION).position(0);
        float [] fa = new float [fb.capacity()];
        fb.get(fa);


        ShortBuffer sb = (ShortBuffer)ib.getData();
        short [] sa = new short[sb.capacity()];
        sb.get(sa); 


        calcBox(fa,sa);

        mM[0]=new UlMatrix4x4(array[0]);
        mM[1]=new UlMatrix4x4(array[1]);
    }
    public void calcBox(UlVertexBuffer vb, UlIndexBuffer ib)
    {
        FloatBuffer fb =(FloatBuffer) vb.getData(UlVertexBuffer.VERTEX_FIELD_POSITION).position(0);
        float [] fa = new float [fb.capacity()];
        fb.get(fa);


        ShortBuffer sb = (ShortBuffer)ib.getData();
        short [] sa = new short[sb.capacity()];
        sb.get(sa); 


        calcBox(fa,sa);
    }
    public void calcBox(float[] vertexArray,short[] indexArray)
    {
        for(int j=0;j<indexArray.length ;j++)
        {  

            short i=(short) (indexArray[j]*3);


            if(vertexArray[i]>maxX)
            {
                maxX=vertexArray[i];
            }
            if(vertexArray[i]<minX)
            {
                minX=vertexArray[i];
            }


            if(vertexArray[i+1]>maxY)
            {
                maxY=vertexArray[i+1];
            }
            if(vertexArray[i]<minY)
            {
                minY=vertexArray[i+1];
            }

            if(vertexArray[i+2]>maxZ)
            {
                maxZ=vertexArray[i+2];
            }
            if(vertexArray[i+2]<minZ)
            {
                minZ=vertexArray[i+2];
            }
        }

        array[0][0]=minX;
        array[0][5]=minY;
        array[0][10]=minZ;
        array[0][15]=1;
        array[1][0]=maxX;
        array[1][5]=maxY;
        array[1][10]=maxZ;
        array[1][15]=1;

    }
    public float[][] getBoundingBox()
    {
        return array;
    }
    public UlMatrix4x4 matrixBox(int index)
    {
        if(mM[0]==null||mM[1]==null){


        mM[0]= new UlMatrix4x4(array[0]);
        mM[1]= new UlMatrix4x4(array[1]);
        }

        return mM[index];
    }
    public UlVector3 getVertex(int Index)
    {
        UlVector3 vertex=null;
        switch (Index)
        {
            case 0:
                vertex.set(array[0][0], array[0][1], array[0][2]);
                break;
            case 1:
                vertex.set(array[1][0], array[0][1], array[0][2]);
                break;
            case 2:
                vertex.set(array[1][0], array[0][1], array[1][2]);
                break;
            case 3:
                vertex.set(array[0][0], array[0][1], array[1][2]);
                break;
            case 4:
                vertex.set(array[0][0], array[1][1], array[0][2]);
                break;
            case 5:
                vertex.set(array[1][0], array[1][1], array[0][2]);
                break;
            case 6:
                vertex.set(array[1][0], array[1][1], array[1][2]);
                break;
            case 7:
                vertex.set(array[0][0], array[1][1], array[1][2]);
                break;

        }
        return vertex;
    }
    public float getMaxDistance(UlVector3 P1)
    {float Mdistance=getDistance(P1, getVertex(0));
        for(int i=1; i<8;i++)
        {
            if(Mdistance<getDistance(P1, getVertex(i)))
            {
                Mdistance=getDistance(P1, getVertex(i));
            }
        }
        return Mdistance;
    }
    private float getDistance(UlVector3 P1,UlVector3 P2)
    {
        float distance =0;
        float dx=P2.getX()-P1.getX();
        float dy=P2.getY()-P1.getY();
        float dz=P2.getZ()-P1.getZ();

        return (float) Math.sqrt(dx*dx+dy*dy+dz*dz);

    }
    public void reverseMatrix()
    {
        array[0]=mM[0].toArray();
        array[1]=mM[1].toArray();
    }

}
this is the DrawUnscrambler 
public class DrawUnscrambler {

    private UlVector3 mCamPos=null;
    private UlMesh mMesh= null;
    private UlSubset mSubsets[]=null;
    private int mIndex[]=null;
    private boolean sorted=false;
    public UlVector3 getmCamPos() {
        return mCamPos;
    }
    public void setmCamPos(UlVector3 mCamPos) {
        this.mCamPos = mCamPos;
    }
    public UlMesh getmMesh() {
        return mMesh;
    }
    public void setmMesh(UlMesh mMesh) {
        this.mMesh = mMesh;
    }
    public UlSubset[] getmSubsets() {
        return mSubsets;
    }
    public void setmSubsets(UlSubset[] mSubsets) {
        this.mSubsets = mSubsets;
    }
    public int[] getmIndex() {
        return mIndex;
    }
    public void setmIndex(int[] mIndex) {
        this.mIndex = mIndex;
    }


    public DrawUnscrambler(UlMesh mesh,UlVector3 CamPosition)
    {
        mMesh=mesh;
        setmCamPos(CamPosition);
        initIndex();
        getSubset();
        SubsetSort();
    }

    private void getSubset()
    {mSubsets= new UlSubset[mMesh.subsetCount()];
        for(int i =0 ; i <mMesh.subsetCount()-1;i++ )
        {
            mSubsets[i]=mMesh.getSubset(i);
        }
    }
    private void initIndex()
    {
        mIndex= new int[mMesh.subsetCount()];
        for(int i =0 ; i <=mMesh.subsetCount()-1;i++)
        {
            mIndex[i]=i;
        }

    }
    private void SubsetSort()
    {
        for(int i =0; i <mMesh.subsetCount()-1;i++)
        {
            for(int j=0;j<mMesh.subsetCount()-2-i;j++)
            {// controlla qui
                if(mSubsets[mIndex[j]].getmBoundingBox().getMaxDistance(mCamPos)<mSubsets[mIndex[j+1]].getmBoundingBox().getMaxDistance(mCamPos))
                {
                    int  t=mIndex[j];
                    mIndex[j]=mIndex[j+1];
                    mIndex[j+1]=t;
                }
            }
        }
        sorted=true;
    }
    public int getSubsetIndex(int i)
    {
        if(sorted)
        {
        return mIndex[i];
        }
        else
        {
            if(mMesh!=null)
            {
                initIndex();
                getSubset();
                SubsetSort();
            }
        }
        return mIndex[i];
    }

    }

The indexArray is the array sorted that is passed to Renderer in draw function but it's don't change draw order. How can I do it?

0

There are 0 answers