Java 如何在OpenGL ES中移动带有纹理的形状?

Java 如何在OpenGL ES中移动带有纹理的形状?,java,android,opengl-es,Java,Android,Opengl Es,如何移动形状?我已经尝试过更改包含所有顶点的浮动,但它不起作用。。。然后我尝试了glTranslateF,但它也不起作用。我做错了什么 这是我的密码: package com.chrypthic.android.reference; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.ShortBuffer; import javax.micro

如何移动形状?我已经尝试过更改包含所有顶点的浮动,但它不起作用。。。然后我尝试了glTranslateF,但它也不起作用。我做错了什么

这是我的密码:

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);
}
}

您提到您从另一个线程调用update,但是OpenGL调用仅在创建上下文的同一线程上有效

您还应该阅读有关OpenGL转换的内容。理解需要一些努力,所以要有耐心


您提到从另一个线程调用update,但OpenGL调用仅在创建上下文的同一线程上有效

您还应该阅读有关OpenGL转换的内容。理解需要一些努力,所以要有耐心


您提供的源代码中的问题是执行绘图操作之前需要调用
glTranslatef
。将矩阵模式设置为modelview设置转换,所有图形将在新位置绘制。

您提供的源代码中的问题是执行绘制操作之前需要调用
glTranslatef
。将矩阵模式设置为modelview设置平移,所有图形都将在新位置绘制。

没关系,我的顶点更新方式正常。现在将它包装在助手类中,这样我就再也不用看到它了。没关系,我已经找到了更新顶点的方法。现在把它包装在助手类中,这样我就再也不用看到它了。