OpenGL ES Framework: Vertices3

public class Vertices3 
{
final GL10 gl;
final boolean hasColor;
final boolean hasTexCoords;
final int vertexSize;
final IntBuffer vertices;
final int[] tmpBuffer;
final ShortBuffer indices;

public Vertices3(GL10 gl, int maxVertices, int maxIndices,
boolean hasColor, boolean hasTexCoords) {
this.gl = gl;
this.hasColor = hasColor;
this.hasTexCoords = hasTexCoords;
this.vertexSize = (3 + (hasColor ? 4 : 0) + (hasTexCoords ? 2 : 0)) * 4;
this.tmpBuffer = new int[maxVertices * vertexSize / 4];

ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize);
buffer.order(ByteOrder.nativeOrder());
vertices = buffer.asIntBuffer();

if (maxIndices > 0) {
buffer = ByteBuffer.allocateDirect(maxIndices * Short.SIZE / 8);
buffer.order(ByteOrder.nativeOrder());
indices = buffer.asShortBuffer();
} else {
indices = null;
}
}

public void setVertices(float[] vertices, int offset, int length) {
this.vertices.clear();
int len = offset + length;
for (int i = offset, j = 0; i < len; i++, j++)
tmpBuffer[j] = Float.floatToRawIntBits(vertices[i]);
this.vertices.put(tmpBuffer, 0, length);
this.vertices.flip();
}

public void setIndices(short[] indices, int offset, int length) {
this.indices.clear();
this.indices.put(indices, offset, length);
this.indices.flip();
}

public void bind()
{
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
vertices.position(0);
gl.glVertexPointer(3, GL10.GL_FLOAT, vertexSize, vertices);

if (hasColor)
{
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
vertices.position(3);
gl.glColorPointer(4, GL10.GL_FLOAT, vertexSize, vertices);
}

if (hasTexCoords)
{
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
vertices.position(hasColor ? 7 : 3);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, vertexSize, vertices);
}
}

public void draw(int primitiveType, int offset, int numVertices)
{
if (indices != null)
{
indices.position(offset);
gl.glDrawElements(primitiveType, numVertices,
GL10.GL_UNSIGNED_SHORT, indices);
}
else
{
gl.glDrawArrays(primitiveType, offset, numVertices);
}
}

public void unbind()
{
if (hasTexCoords)
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

if (hasColor)
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}


使用:

public class MyRenderer implements Renderer
{
Texture texture;
// Vertices vertices;
Context context;

Vertices3 vertices;

final int VERTEX_SIZE = (2 + 2) * 4;

public MyRenderer(Context context)
{
this.context = context;
}

@Override
public void onDrawFrame(GL10 gl)
{
gl.glViewport(0, 0, 320, 480);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
// gl.glOrthof(0, 320, 480, 0, 1, -1);
gl.glOrthof(-1, 1, -1, 1, 10, -10);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();

// texture.bind();
// vertices.draw(GL10.GL_TRIANGLES, 0, 6);
vertices.bind();
vertices.draw(GL10.GL_TRIANGLES, 0, 6);
vertices.unbind();
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
// texture = new Texture(gl, context.getAssets(), "bobargb8888.png");
//
// vertices = new Vertices(gl, 4, 6, false, true);
//
// vertices.setVertices(new float[] { 100.0f, 100.0f, 0.0f, 1.0f, 228.0f,
// 100.0f, 1.0f, 1.0f, 228.0f, 228.0f, 1.0f, 0.0f, 100.0f, 228.0f,
// 0.0f, 0.0f }, 0, 16);
// vertices.setIndices(new short[] { 0, 1, 2, 2, 3, 0 }, 0, 6);

vertices = new Vertices3(gl, 6, 0, true, false);
vertices.setVertices(new float[]
{
-0.5f, -0.5f, -3, 1, 0, 0, 1,
0.5f, -0.5f, -3, 1, 0, 0, 1,
0.0f, 0.5f, -3, 1, 0, 0, 1,
0.0f, -0.5f, -5, 0, 1, 0, 1,
1.0f, -0.5f, -5, 0, 1, 0, 1,
0.5f, 0.5f, -5, 0, 1, 0, 1
}, 0, 7 * 6);
}

}

 

posted @ 2012-01-09 14:02  LiLiNiuNiu  阅读(245)  评论(0编辑  收藏  举报