简体   繁体   English

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

[英]How do I move a shape with a texture in OpenGL ES?

How can I move the shape? 如何移动形状? I have tried changing the float that holds all the vertices but it didnt work... Then I have tried glTranslateF, but it didnt work either. 我尝试过更改包含所有顶点的浮点型,但是它没有用...然后我尝试了glTranslateF,但是它也没有用。 What am I doing wrong? 我究竟做错了什么?

Here is my code: 这是我的代码:

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

The problem in the source you provided was the fact that glTranslatef needs to be called before performing the draw operation. 您提供的源代码中存在的问题是,在执行绘制操作之前需要调用glTranslatef Set the matrix mode to modelview set the translation and all the drawing will be drawn at the new position. 将矩阵模式设置为modelview,设置平移,所有图形将绘制在新位置。

You mention you call update from another thread, but OpenGL calls are only valid on the same thread that created the context. 您提到从另一个线程调用更新,但是OpenGL调用仅在创建上下文的同一线程上有效。

Also you should read about OpenGL transformations. 您还应该阅读有关OpenGL转换的信息。 It takes some effort to understand so have patience. 要花些时间才能理解,因此要有耐心。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM