0
好吧,這裏是代碼,它運行的很好,我只是沒有看到紋理。我看到一個白色正方形,正好與我指定的大小不同,而不是紋理。爲什麼我的OPEN GL ES紋理不顯示
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引擎,以便我可以在很長一段時間內重複使用它。
你是非常接近的,我只是需要重新排列幾行,然後添加我錯過的一行。謝謝 – WIllJBD 2012-08-02 22:04:36