2011-11-26 57 views
0

如何移動形狀?我試圖改變擁有所有的頂點,但它沒有工作的浮動...然後,我已經嘗試glTranslateF,但它也沒有工作。我究竟做錯了什麼?如何在OpenGL ES中移動具有紋理的形狀?

這裏是我的代碼:

package com.chrypthic.android.reference; 

import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.nio.FloatBuffer; 
import java.nio.ShortBuffer; 

import javax.microedition.khronos.opengles.GL10; 

import android.content.Context; 
import android.util.Log; 

public class Square 
{ 
final int VERTEX_SIZE = (2+2) *4; 
FloatBuffer vertices; 
ShortBuffer indices; 

Texture texture; 

GL10 gl; 
Context c; 

int x; 
int y; 
int w; 
int h; 

public Square(GL10 gl, Context context, int x, int y, int w, int h, String imageTexture) 
{ 
    this.gl = gl; 
    this.c = context; 

    this.x = x; 
    this.y = y; 
    this.w = w; 
    this.h = h; 

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * VERTEX_SIZE); 
    byteBuffer.order(ByteOrder.nativeOrder()); 
    vertices = byteBuffer.asFloatBuffer(); 
    /*vertices.put(new float[]{ 
     10.0f, 10.0f, 0.0f, 1.0f, //bl 
     160.0f, 10.0f, 1.0f, 1.0f, //br 
     160.0f, 160.0f, 1.0f, 0.0f, //tr  
     10.0f, 160.0f, 0.0f, 0.0f, //tl 
    });*/ 
    vertices.put(new float[]{ 
      (float)x, ((float)y+(float)h), 0.0f, 1.0f, //bl 
      ((float)x+(float)w), ((float)y+(float)h), 1.0f, 1.0f, //br 
      ((float)x+(float)w), (float)y, 1.0f, 0.0f, //tr 
      (float)x, (float)y, 0.0f, 0.0f, //tl  
     }); 
    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(); 

    texture = new Texture(imageTexture, c, gl); 
    texture.load(); 
} 

public void draw() 
{ 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glEnable(GL10.GL_TEXTURE_2D); 
    texture.bind(); 
    gl.glColor4f(0f, 0f, 0f, 1f); 

    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 

    vertices.position(0); 
    gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices); 
    vertices.position(2); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices); 

    gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indices); 
} 

public void update() 
{ 
    //this doesnt work. I call the method every 10 milliseconds from a thread in another class. 
    gl.glEnable(GL10.GL_TEXTURE_2D); 
    gl.glTranslatef(10, y, 0); 
} 
} 

回答

2

您所提供的事實是glTranslatef需要進行抽籤操作之前被稱爲源的問題。將矩陣模式設置爲modelview設置翻譯並將所有繪圖繪製在新位置。

1

你提到你從另一個線程調用更新,但OpenGL調用都只有創建上下文在同一線程上有效。

此外,您應該閱讀有關OpenGL轉換。這需要一定的努力來理解,所以有耐心。

http://glprogramming.com/red/chapter03.html

+0

沒關係,我得到了更新頂點的方式工作。現在將它包裝在輔助類中,這樣我就不會再看到它了。 – chrypthic