我试图使用FloatBuffer为我的顶点和索引的ShortBuffer绘制基元。一切正常,没有崩溃或警告,但屏幕是空白的。我可以毫不费力地使用glDrawTexfOES绘图。 如果有人可以看看代码我会很感激。
public abstract class PrimitiveBase extends DrawableEntity {
protected float[] _vertices;
protected short[] _indices;
private FloatBuffer _verticesBuffer;
private ShortBuffer _indicesBuffer;
public PrimitiveBase(Sprite sprite, float x, float y) {
super(sprite, x, y);
}
protected abstract void setVertices();
@Override
public void draw(float x, float y, float scaleX, float scaleY) {
GL10 gl = GlSystem.getGl();
gl.glPushMatrix();
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glScalef(scaleX, scaleY, 1.0F);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, _verticesBuffer);
gl.glColor4f(1.0F, 0, 0, 0);
gl.glDrawElements(GL10.GL_TRIANGLES, _indices.length,
GL10.GL_UNSIGNED_SHORT, _indicesBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPushMatrix();
}
@Override
public void load(EntityManager parent) {
setVertices();
ByteBuffer vbb = ByteBuffer.allocateDirect(_vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
_verticesBuffer = vbb.asFloatBuffer();
_verticesBuffer.put(_vertices);
_verticesBuffer.position(0);
ByteBuffer ibb = ByteBuffer.allocateDirect(_indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
_indicesBuffer = ibb.asShortBuffer();
_indicesBuffer.put(_indices);
_indicesBuffer.position(0);
}
}
public class Rectangle extends PrimitiveBase {
private float _width;
private float _heigth;
public Rectangle(Sprite sprite, float x, float y, float width, float height) {
super(sprite, x, y);
_heigth = height;
_width = width;
}
@Override
protected void setVertices() {
_vertices = new float[8];
_indices = new short[] { 0, 1, 2, 0, 2, 3 };
_vertices[0] = getX() - (_width / 2);
_vertices[1] = getY() + (_heigth / 2);
_vertices[2] = getX() + (_width / 2);
_vertices[3] = getY() + (_heigth / 2);
_vertices[4] = getX() + (_width / 2);
_vertices[5] = getY() - (_heigth / 2);
_vertices[6] = getX() - (_width / 2);
_vertices[7] = getY() - (_heigth / 2);
}
}