package com.gebauz.Bauzoid.graphics.model;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
import com.gebauz.Bauzoid.app.Consts;
import com.gebauz.Bauzoid.graphics.Graphics;
import com.gebauz.Bauzoid.graphics.GraphicsObject;
import com.gebauz.Bauzoid.math.Vector2;
import com.gebauz.Bauzoid.math.Vector3;
import com.gebauz.Bauzoid.math.Vector4;
/** Geometry class implementing the SUX specification.
*
*/
public class Geometry
extends GraphicsObject
{
public enum PrimitiveType
{
TRIANGLES,
TRIANGLE_STRIP,
LINES,
LINE_STRIP,
POINTS
};
private int mPrimitiveTypeGL = GLES20.
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
;
mVertexCount = vertexCount
;
mIndexCount = indexCount
;
}
/** 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
)
{
GLES20.
glDrawArrays(mPrimitiveTypeGL, firstVertex, lastVertex - firstVertex +
1);
}
/** Render with indices - requires IndexStream. */
public void renderIndices
(int firstIndex,
int lastIndex
)
{
// offset is the offset in bytes!
GLES20.
glDrawElements(mPrimitiveTypeGL, lastIndex - firstIndex +
1, GLES20.
GL_UNSIGNED_SHORT, firstIndex
* Consts.
SIZEOF_SHORT);
}
/** Sets the vertex streams. */
public final void setVertexStreams
(VertexStream
[] streams
)
{
unload
();
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
;
}
/** Convert from Primitive Type to GL Primitive Type. */
private static int toPrimitiveGL
(PrimitiveType type
)
{
switch (type
)
{
case TRIANGLES:
return GLES20.
GL_TRIANGLES;
case TRIANGLE_STRIP:
return GLES20.
GL_TRIANGLE_STRIP;
case LINES:
return GLES20.
GL_LINES;
case LINE_STRIP:
return GLES20.
GL_LINE_STRIP;
case POINTS:
default:
return GLES20.
GL_POINTS;
}
}
}