gpt4 book ai didi

java - GLES 3.0 安卓工作室

转载 作者:行者123 更新时间:2023-11-30 00:04:45 26 4
gpt4 key购买 nike

我完全无法编写一个在 Android Studio 中绘制三角形的简单 GLES 3.0 应用程序。虽然我在用 C++ 编写普通现代 OpenGl 程序方面有很多经验,但我无法弄清楚为什么我的代码不起作用。在大多数 GLES 教程中,没有使用诸如 VertexArrayObjects 或 Buffers 之类的东西。这就是为什么很难在 Internet 上找到示例或源代码的原因。请帮忙!!!

public class Triangle {
private int m_program;
private int[] m_vao = new int[1];
private int[] m_vbo = new int[1];
private float[] m_vertices = {
-0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};

private final String vertexShaderCode =
"#version 300 es \n" +
"layout(location = 0) in vec3 position; \n" +
"void main(){ \n" +
" gl_Position = vec4(position, 1.0); \n" +
"} ";

private final String fragmentShaderCode =
"#version 300 es \n" +
"precision mediump float; \n" +
"out vec4 color; \n" +
"void main(){ \n" +
" color = vec4(1.0,0.0,0.0,1.0); \n" +
"} ";

public Triangle()
{
System.out.println("Triangle Created\n");
m_program = CreateProgram(loadShader(GLES30.GL_VERTEX_SHADER,vertexShaderCode),loadShader(GLES30.GL_FRAGMENT_SHADER,fragmentShaderCode));

ByteBuffer bb = ByteBuffer.allocateDirect(m_vertices.length * 4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(m_vertices);
vertexBuffer.position(0);

GLES30.glGenVertexArrays(1,m_vao,0);
GLES30.glBindVertexArray(m_vao[0]);
GLES30.glGenBuffers(1,m_vbo,0);
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER,m_vbo[0]);
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER,m_vertices.length * 4, vertexBuffer,GLES30.GL_STATIC_DRAW);
GLES30.glEnableVertexAttribArray(0);
GLES30.glVertexAttribPointer(0, 3, GLES30.GL_FLOAT, false, m_vertices.length * 4, 0);
GLES30.glBindVertexArray(0);

System.out.println(m_vbo[0]);
}

public void Draw()
{
GLES30.glUseProgram(m_program);
GLES30.glBindVertexArray(m_vao[0]);
GLES30.glDrawArrays(GLES30.GL_TRIANGLES,0,3);
GLES30.glBindVertexArray(0);
}

private int loadShader(int type, String shaderCode)
{
int shader = GLES30.glCreateShader(type);
GLES30.glShaderSource(shader,shaderCode);
GLES30.glCompileShader(shader);
System.out.println(GLES30.glGetShaderInfoLog(shader));

return shader;
}

private int CreateProgram(int vertexShader, int fragmentShader)
{
int id;
id = GLES30.glCreateProgram();
GLES30.glAttachShader(id,vertexShader);
GLES30.glAttachShader(id,fragmentShader);
GLES30.glLinkProgram(id);
System.out.println(GLES30.glGetProgramInfoLog(id));
GLES30.glDeleteShader(vertexShader);
GLES30.glDeleteShader(fragmentShader);

return id;
}
}

最佳答案

glVertexAttribPointer 方法的倒数第二个参数是步幅。步幅定义了两个连续顶点属性开始之间的字节距离。在您的情况下,每个顶点在前一个顶点之后开始 3 个 float ,因此步幅应为 3 * 4(3 个 float 和 4 个字节)。正确的代码可能如下所示:

GLES30.glEnableVertexAttribArray(0);
GLES30.glVertexAttribPointer(0, 3, GLES30.GL_FLOAT, false, 3 * 4, 0);

请注意,由于数据是紧密打包的,您也可以将 0 作为步幅传递。

关于java - GLES 3.0 安卓工作室,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49222373/

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