LWJGL - Quad未渲染

时间:2014-11-14 20:53:50

标签: java opengl shader render lwjgl

我一直试图让这段代码工作一段时间,我仍然无法弄清楚我做错了什么。 (LWJGL - Java)

我试图在网上查看其他人的代码,但我找不到任何重大差异。我实际上学会了使用OpenGL和C ++,所以我的想法可能会被卡在上面,这可能就是我无法找到错误的原因。

这是init(称为一次)

FloatBuffer vertices = BufferUtils.createFloatBuffer(4 * 5);
    vertices.put(new float[]{
            // pos                  // Color
            0.5f, 0.5f,             0.5f, 0.0f, 0.5f,
            0.5f, -0.5f,            0.5f, 0.0f, 0.75f,
            -0.5f, -0.5f,           0.0f, 1.0f, 0.0f,
            -0.5f, 0.5f,            0.5f, 0.5f, 1.0f
    });
    vertices.flip();

    ByteBuffer indices = BufferUtils.createByteBuffer(2 * 3);
    indices.put(new byte[]{
            0, 1, 3,
            1, 2, 3
    });
    indices.flip();

    // VAO
    VAO = GL30.glGenVertexArrays();
    GL30.glBindVertexArray(VAO);

    // VBO
    VBO = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);

    // EBO
    EBO = glGenBuffers();
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glEnableVertexAttribArray(0);
    //                         v - position in layout (see shader)
    //                            v - Nb of component per vertex (2 for 2D (x, y))
    //                                                v - Normalized ? (between 0 - 1)
    //                                                       v - Offset between things (size of a line)
    //                                                                     v - Where to start ?
    glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 5 * Float.SIZE , 0);

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, 5 * Float.SIZE , 2 * Float.SIZE);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);

    // Unbinds the VAO
    GL30.glBindVertexArray(0);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

这是渲染功能:

shaderProgram.bind();

GL30.glBindVertexArray(VAO);

GL11.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_BYTE, 0);

GL30.glBindVertexArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);

着色器:

顶点:

#version 330 core

layout(location = 0) in vec2 position;
layout(location = 1) in vec3 color;

out vec4 Color;

void main()
{
    gl_Position = vec4(position, 0.0, 1.0);
    Color = vec4(color, 1.0);
}

Framgent:

#version 330 core

in vec4 Color;

out vec4 color;

void main()
{
    color = Color;
}

1 个答案:

答案 0 :(得分:0)

official Java documentation中,Float.SIZE定义为:

  

用于表示浮点值的的数量。

由于glVertexAttribPointer()需要以字节为单位的步幅和偏移量参数,因此必须将其除以8,而使用(Float.SIZE / 8)代替。