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;
/** Geometry class.
*
*/
public class Geometry
extends GraphicsObject
{
public enum PrimitiveType
{
TRIANGLES,
TRIANGLE_STRIP,
LINES,
LINE_STRIP,
POINTS
};
private int mPrimitiveTypeGL = GL20.
GL_TRIANGLES;
private PrimitiveType mPrimitiveType = PrimitiveType.
TRIANGLES;
private VertexStream
[] mStreams =
null;
private IndexStream mIndexStream =
null;
private int mVertexCount =
0;
private int mIndexCount =
0;
/** Constructor. */
public Geometry
(Graphics graphics, PrimitiveType primitiveType,
int vertexCount,
int indexCount
)
{
super(graphics
);
mPrimitiveType = primitiveType
;
mPrimitiveTypeGL = toPrimitiveGL
(primitiveType
);
mVertexCount = vertexCount
;
mIndexCount = indexCount
;
}
/** Destroy internal data. */
public void dispose
()
{
destroyIndexStream
();
destroyVertexStreams
();
}
/** Destroy vertex streams. */
public void destroyVertexStreams
()
{
if (mIndexStream
!=
null)
{
mIndexStream.
unload();
mIndexStream =
null;
}
}
/** Destroy index stream. */
public void destroyIndexStream
()
{
if (mStreams
!=
null)
{
for (VertexStream stream : mStreams
)
stream.
unload();
mStreams =
null;
}
}
/** Upload streams to hardware. */
public void upload
()
{
if (mStreams
!=
null)
{
for (int i =
0; i
< mStreams.
length; i++
)
{
mStreams
[i
].
upload();
}
}
if (mIndexStream
!=
null)
{
mIndexStream.
upload();
}
}
/** Unload streams from hardware. */
public void unload
()
{
if (mStreams
!=
null)
{
for (int i =
0; i
< mStreams.
length; i++
)
{
if (mStreams
[i
] !=
null)
{
mStreams
[i
].
unload();
mStreams
[i
] =
null;
}
}
mStreams =
null;
}
if (mIndexStream
!=
null)
{
mIndexStream.
unload();
mIndexStream =
null;
}
}
/** Activate the streams of this Geometry. */
public void activate
()
{
if (mStreams
!=
null)
{
for (int i =
0; i
< mStreams.
length; i++
)
{
mStreams
[i
].
activate();
}
}
if (mIndexStream
!=
null)
{
mIndexStream.
activate();
}
}
/** Deactivate the streams of this Geometry. */
public void deactivate
()
{
if (mStreams
!=
null)
{
for (int i =
0; i
< mStreams.
length; i++
)
{
mStreams
[i
].
deactivate();
}
}
if (mIndexStream
!=
null)
{
mIndexStream.
deactivate();
}
}
/** Render either with or without indices depending on whether there is an IndexStream. */
public void render
(int firstIndex,
int lastIndex,
int firstVertex,
int lastVertex
)
{
if (mIndexStream
!=
null)
{
renderIndices
(firstIndex, lastIndex
);
}
else
{
renderVertices
(firstVertex, lastVertex
);
}
//GLES20.glDrawArrays(GLES20.GL_POINTS, firstVertex, lastVertex - firstVertex + 1);
}
/** Render without indices. */
public void renderVertices
(int firstVertex,
int lastVertex
)
{
Gdx.
gl20.
glDrawArrays(mPrimitiveTypeGL, firstVertex, lastVertex - firstVertex +
1);
}
/** Render with indices - requires IndexStream. */
public void renderIndices
(int firstIndex,
int lastIndex
)
{
// offset is the offset in bytes!
Gdx.
gl20.
glDrawElements(mPrimitiveTypeGL, lastIndex - firstIndex +
1, GL20.
GL_UNSIGNED_SHORT, firstIndex
* Consts.
SIZEOF_SHORT);
}
/** Sets the vertex streams. */
public final void setVertexStreams
(VertexStream
[] streams
)
{
mStreams = streams
;
}
/** Get a vertex stream. */
public final VertexStream getVertexStream
(int n
)
{
return mStreams
[n
];
}
/** Get the number of vertex streams. */
public final int getVertexStreamCount
()
{
return mStreams.
length;
}
/** Set the index stream. */
public final void setIndexStream
(IndexStream indexStream
)
{
mIndexStream = indexStream
;
}
/** Get the index stream. */
public final IndexStream getIndexStream
()
{
return mIndexStream
;
}
/** Set primitive type. */
public final void setPrimitiveType
(PrimitiveType type
)
{
mPrimitiveType = type
;
mPrimitiveTypeGL = toPrimitiveGL
(type
);
}
/** Get primitive type. */
public final PrimitiveType getPrimitiveType
()
{
return mPrimitiveType
;
}
/** Get number of vertices. */
public final int getVertexCount
()
{
return mVertexCount
;
}
/** Get number of indices. */
public final int getIndexCount
()
{
return mIndexCount
;
}
/** Convert from Primitive Type to GL Primitive Type. */
public static int toPrimitiveGL
(PrimitiveType type
)
{
switch (type
)
{
case TRIANGLES:
return GL20.
GL_TRIANGLES;
case TRIANGLE_STRIP:
return GL20.
GL_TRIANGLE_STRIP;
case LINES:
return GL20.
GL_LINES;
case LINE_STRIP:
return GL20.
GL_LINE_STRIP;
case POINTS:
default:
return GL20.
GL_POINTS;
}
}
}