gpt4 book ai didi

java - 在opengl es 2.0中绘制纹理

转载 作者:行者123 更新时间:2023-12-01 14:36:08 25 4
gpt4 key购买 nike

我在 android 上的 opengl es 2 中绘制纹理时遇到了大约 3 天的问题,最终决定我应该提供帮助,因为我的所有研究都陷入了死胡同。问题是纹理被绘制为黑色矩形而不是实际图像。

public class Sprite 
{
private static final Shader spriteShader = ShaderManager.getInstance().getShader(1);
private static final short[] drawOrder = { 0, 1, 2, 0, 2, 3 };
private static ShortBuffer drawListBuffer;

static
{
ByteBuffer bb = ByteBuffer.allocateDirect(drawOrder.length * 2);
bb.order(ByteOrder.nativeOrder());

drawListBuffer = bb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
}

private Context m_context;
private int m_textureId = 0;
private FloatBuffer m_vertexBuffer;
private float[] m_coords =
{
-0.5f, 0.5f, 0.0f, // Position 0
0.0f, 0.0f, // TexCoord 0
-0.5f, -0.5f, 0.0f, // Position 1
0.0f, 1.0f, // TexCoord 1
0.5f, -0.5f, 0.0f, // Position 2
1.0f, 1.0f, // TexCoord 2
0.5f, 0.5f, 0.0f, // Position 3
1.0f, 0.0f // TexCoord 3
};


public Sprite(Context context)
{
m_context = context;

ByteBuffer bb = ByteBuffer.allocateDirect(m_coords.length * 4);
bb.order(ByteOrder.nativeOrder());

m_vertexBuffer = bb.asFloatBuffer();
m_vertexBuffer.put(m_coords);
m_vertexBuffer.position(0);
}

public void initialize(String file)
{
InputStream inputStream = null;

try
{
inputStream = m_context.getAssets().open(file);
m_textureId = loadTexture(inputStream);
}
catch (IOException e)
{}
finally
{
Util.closeStream(inputStream);
}
}

public void draw()
{
spriteShader.use();

int position = spriteShader.getAttribLocation("vPosition");
int textureCoords = spriteShader.getAttribLocation("aTexCoords");
int texture = spriteShader.getUniformLocation("uTexture");

m_vertexBuffer.position(0);
GLES20.glVertexAttribPointer(position, 3, GLES20.GL_FLOAT, false, 20, m_vertexBuffer);
Util.checkGlError("vertex pointer poistion");
m_vertexBuffer.position(3);
GLES20.glVertexAttribPointer(textureCoords, 2, GLES20.GL_FLOAT, false, 20, m_vertexBuffer);
Util.checkGlError("vertex pointer textureCoords");
GLES20.glEnableVertexAttribArray(position);
GLES20.glEnableVertexAttribArray(textureCoords);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
Util.checkGlError("active trexture");
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, m_textureId);
Util.checkGlError("bind texture");

GLES20.glUniform1i(texture, 0);
Util.checkGlError("uniform1i");
GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
Util.checkGlError("drawelements");

}

private int loadTexture(InputStream inputStream)
{
int[] texture = new int[1];
GLES20.glGenTextures(1, texture, 0);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]);

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();

return texture[0];
}
}

顶点着色器

attribute vec2 aTexCoords;
attribute vec4 vPosition;

varying vec2 vTexCoords;

void main()
{
vTexCoords = aTexCoords;
gl_Position = vPosition;
}

fragment 着色器

precision mediump float;

uniform sampler2D uTexture;
varying vec2 vTexCoords;

void main()
{
gl_FragColor = texture2D(uTexture, vTexCoords);
}

最佳答案

您可以尝试为顶点坐标和纹理坐标使用单独的数组。

private float[] texCoords = {0,0,
0,1,
1,1,
1,0};

并为纹理坐标创建一个单独的 float 缓冲区。您还可以在 glVertexAtrribPointer 调用中使用步幅值 0,这意味着数组被认为是紧密包装的。

关于java - 在opengl es 2.0中绘制纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16473872/

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