好的,这是代码,它运行得很好,我只是没有看到纹理。我看到的 A White Square 与我指定的大小完全一致,而不是纹理。
public class MeshRect {
MyRect rect;
final int VERTEX_SIZE = (2 + 4 + 2) * 4;
FloatBuffer vertices;
ShortBuffer indices;
long MyID;
public MeshRect(MyRect rect, long ID) {
super();
this.rect = rect;
MyID = ID;
Assemble();
}
private void Assemble() {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * VERTEX_SIZE);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
float x1 = (float) rect.BottomLeftCorner.x;
float x2 = (float) rect.BottomRightCorner.x;
float x3 = (float) rect.TopRightCorner.x;
float x4 = (float) rect.TopLeftCorner.x;
float y1 = (float) rect.BottomLeftCorner.y;
float y2 = (float) rect.BottomRightCorner.y;
float y3 = (float) rect.TopRightCorner.y;
float y4 = (float) rect.TopLeftCorner.y;
vertices.put(new float[] { x1, y1, 1, 1, 1, 1, 0.0f, 1.0f,
x2, y2, 1, 1, 1, 1, 1.0f, 1.0f,
x3, y3, 1, 1, 1, 1, 1.0f, 0.0f,
x4, y4, 1, 1, 1, 1, 0.0f, 0.0f });
// x y r g b A u|s v|t
vertices.flip();
byteBuffer = ByteBuffer.allocateDirect(6 * 2);
byteBuffer.order(ByteOrder.nativeOrder());
indices = byteBuffer.asShortBuffer();
indices.put(new short[] { 0, 1, 2,
2, 3, 0 });
indices.flip();
}
public FloatBuffer getVertices() {
return vertices;
}
public ShortBuffer getIndices() {
return indices;
}
public long getMyID() {
return MyID;
}
public void Draw(int primitiveType, GL10 gl) {
int numVertices = 6;
//set vertex array
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
vertices.position(0);
gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
//set color
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
vertices.position(2);
gl.glColorPointer(4, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
// set texture coords
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
vertices.position(6);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
//set indices
indices.position(0);
gl.glDrawElements(primitiveType, numVertices, GL10.GL_UNSIGNED_SHORT, indices);
//reset
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}
和渲染
public void Render(GL10 gl, long deltaTime) {
//render all
Log.d("Game", "Render");
gl.glViewport(0, 0, Screen.SCREEN_WIDTH, Screen.SCREEN_HEIGHT);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, Screen.WIDTH_SCALE, 0, Screen.HEIGHT_SCALE, 1, -1);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
/**
* so psuedo would look something like this
*
* *.Update();
* gl.glEnable(GL10.GL_TEXTURE_2D);
* Textures.BindTexture(*.getCurrentTextureFileID);
* MeshRects.draw((*.getMyRect) ,GL10.GL_TRIANGLES, gl);
*
* update();
* bind texture;
* draw rect;
*/
ball.Update(deltaTime);
gl.glEnable(GL10.GL_TEXTURE_2D);
texture.bind(gl);
MeshRect mRect = new MeshRect(ball.getMyRect(), 1);
mRect.Draw(GL10.GL_TRIANGLES, gl);
}
和纹理绑定方法
public void bind(GL10 gl) {
Log.d("Texture", "Bind");
gl.glBindTexture(GL10.GL_TEXTURE_2D, TextureID);
}
我忍不住想我错过了什么。这是我第一次潜入 OPEN GL 的世界,而这一切都是我今天通过阅读和测试来自互联网的所有代码而创造的。没有关于如何做的简单资源,所以我一直在制作自己的 2D 引擎,以便我可以在很长一段时间内重复使用它。