Как мне переместить форму с текстурой в OpenGL ES? - PullRequest
0 голосов
/ 26 ноября 2011

Как мне переместить форму? Я попытался изменить float, который содержит все вершины, но это не сработало ... Тогда я попробовал 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);
}
}

Ответы [ 2 ]

2 голосов
/ 26 ноября 2011

Проблема в предоставленном вами источнике заключалась в том, что glTranslatef необходимо вызвать перед выполнением операции рисования.Установите режим матрицы на модель просмотра установите перевод, и весь чертеж будет нарисован в новой позиции.

1 голос
/ 26 ноября 2011

Вы упоминаете, что вызываете обновление из другого потока, но вызовы OpenGL действительны только в том же потоке, который создал контекст.

Также вы должны прочитать о преобразованиях OpenGL. Требуется некоторое усилие, чтобы понять, так что наберитесь терпения.

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...