簡體   English   中英

嘗試使用LWJGL3繪制基本形狀

[英]Trying to draw a basic shape with LWJGL3

所以我想使用VAO和VBO繪制形狀,我想我所做的一切都正確,但是每當我運行我的代碼時,我都會獲得具有清晰顏色的窗口。 在調用創建功能之前嘗試初始化三角形之前,我遇到了一個問題,我是否缺少一些開始繪制的功能?

這是我的代碼:

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

希望你們能有所幫助,非常感謝。

我沒有使用着色器。 那是我的問題嗎? 是否有必要

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

注意,客戶端功能(或頂點屬性數組)的這種狀態存儲在“ 頂點數組對象”中 因此,當指定頂點數組對象時,啟用頂點坐標就足夠了。 在繪制幾何圖形時,可以省略啟用和禁用頂點坐標的操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM