user7986617
user7986617

Reputation:

GLES 3.0 Android Studio

I'm totaly screwed with writing a simple GLES 3.0 application which draws a Triangle in Android Studio. Though I have quiet a lot of experience in programming normal modern OpenGl in C++, I can't figure out why my Code is not working.In most GLES tutorials such things as VertexArrayObjects or Buffers are not used. That's why it is very hard to find examples or source codes on the internet. Please help!!!

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

Upvotes: 1

Views: 489

Answers (1)

BDL
BDL

Reputation: 22168

The second to last argument to the glVertexAttribPointer method is the stride. Stride defines the distance in bytes between the start of two consecutive vertex attributes. In your case, each vertex starts 3 floats after the previous one, thus the stride should be 3 * 4 (3 floats á 4 bytes). The correct code could look like this:

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

Note, that since the data is tightly packed, you could also pass 0 as stride.

Upvotes: 1

Related Questions