GLES 3.0 Android Studio

时间:2018-03-11 16:37:39

标签: java android opengl-es opengl-es-3.0

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

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

1 个答案:

答案 0 :(得分:1)

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

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

请注意,由于数据紧密包装,您也可以向前传递0。