尝试使用 LWJGL3 绘制基本形状

Trying to draw a basic shape with LWJGL3

所以我想使用 VAO 和 VBO 绘制一个形状,我认为我做的一切都是正确的,但是每当我 运行 我的代码时,我只会得到 window 和清晰的颜色。我在调用创建功能之前尝试初始化三角形时遇到了一个问题,我是否缺少一些开始绘图的功能?

这是我的代码:

int vaoId, vboId, vertexCount;

float[] vertices = {
    // Left bottom triangle
    -0.5f, 0.5f,
    -0.5f, -0.5f,
    0.5f, -0.5f,};

private void init() {
    if (!glfwInit()) {
        throw new IllegalStateException("Failed to Initialize GLFW!");
    }

    int width = 1000;
    int height = 1000;

    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    window = glfwCreateWindow(width, height, "App", NULL, NULL);

    if (window == 0) {
        throw new IllegalStateException("Failed to create Window!");
    }

    GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2);

    // Make the OpenGL context current
    glfwMakeContextCurrent(window);
    // Enable v-sync
    glfwSwapInterval(1);

    glfwShowWindow(window);
}

private void loop() {
    // This line is critical for LWJGL's interoperation with GLFW's
    // OpenGL context, or any context that is managed externally.
    // LWJGL detects the context that is current in the current thread,
    // creates the GLCapabilities instance and makes the OpenGL
    // bindings available for use.
    GL.createCapabilities();

    initTriangle();

    // Run the rendering loop until the user has attempted to close
    // the window or has pressed the ESCAPE key.
    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT); // clear the framebuffer

        glBindVertexArray(vaoId);
        glEnableVertexAttribArray(0);

        glDrawArrays(GL_TRIANGLES, 0, vertexCount);

        glDisableVertexAttribArray(0);
        glBindVertexArray(0);

        glfwSwapBuffers(window); // swap the color buffers

        // Poll for window events. The key callback above will only be
        // invoked during this call.
        glfwPollEvents();

    }
}

private void initTriangle() {

    FloatBuffer vertBuf = MemoryUtil.memAllocFloat(vertices.length);
    vertBuf.put(vertices);
    vertBuf.flip();

    vaoId = glGenVertexArrays();
    glBindVertexArray(vaoId);

    vboId = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vboId);
    glBufferData(GL_ARRAY_BUFFER, vertBuf, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

希望大家帮帮忙,万分感谢

I am not using shaders. Is that my problem? And is it a necessity

OpenGL 中最先进的渲染方式是使用 Shader

如果您不使用着色器,则必须通过 glVertexPointer

定义顶点数据数组
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);

vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertBuf, GL_STATIC_DRAW);

glVertexPointer( 2, GL_FLOAT, 0, 0 ); // <---------------

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

并且您必须通过 glEnableClientState( GL_VERTEX_ARRAY ):

启用顶点坐标的客户端功能
glBindVertexArray(vaoId);
glEnableClientState( GL_VERTEX_ARRAY ); // <---------------

glDrawArrays(GL_TRIANGLES, 0, vertexCount);

glDisableClientState( GL_VERTEX_ARRAY ); // <---------------
glBindVertexArray(0);

注意,客户端能力(或顶点属性数组)的这种状态存储在Vertex Array Object中。 因此,当指定顶点数组对象时,启用顶点坐标就足够了。顶点坐标的启用和禁用,在绘制几何时可以省略:

vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);

vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertBuf, GL_STATIC_DRAW);

glVertexPointer( 2, GL_FLOAT, 0, 0 ); 
glEnableClientState( GL_VERTEX_ARRAY ); // <---------------

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

glBindVertexArray(vaoId);

glDrawArrays(GL_TRIANGLES, 0, vertexCount);

glBindVertexArray(0);