gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:34:03 27 4
gpt4 key购买 nike

如何移动形状?我尝试过更改保存所有顶点的 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);
}
}

最佳答案

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

关于java - 如何在 OpenGL ES 中移动带有纹理的形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8279672/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com