lwjgl 通过 JSlider 更新 VBO(颜色、顶点)



对不起,我的英语不好。我在通过JSlider和stateChanged-Event更新VBO时遇到问题。总的来说,我想自己弄清楚,但我现在被困住了。所以我的第一个问题是当给出以下代码时如何操作(更新)vbo:

    public void update(){
    farbe = (float)slider_c.getValue()/100f;
    // x = (float)slider_x.getValue()/100f;
    // y = (float)slider_y.getValue()/100f;
}
public float[] getPosition()
{
    return new float[]{x,y};
}

/* Run Methode: window is started here
 * Definition of frame loop
 */
public void run() {
    try{
        initWindow();
        GL.createCapabilities();
        initBuffers();
        initShaders();
        // Game Loop
        while (glfwWindowShouldClose(window) == GL_FALSE) {
            // initBuffers();
            frame();
            glfwSwapBuffers(window);
            glfwPollEvents();
        }
    }
    finally {
        // Clean up
        glfwTerminate();
        errorCallback.release();
        glDeleteProgram(program);
        System.exit(0);
    }
}
private void frame() {
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBindVertexArray(vao);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}
private void initBuffers() {
    // from the secound one
    if (vao > -1) {
        glDeleteBuffers(vao);
        glDeleteVertexArrays(vboPosition);
        glDeleteVertexArrays(vboColor);
    }
    vao = glGenVertexArrays();
    glBindVertexArray(vao);
    vboPosition = glGenBuffers();
    // x y z w
    float[] positions2 = new float[]{
            0.5f, 0.0f, 0.0f, 1.0f, // 1. Point
            0.0f, 0.5f, 0.0f, 1.0f,  // 2. Point
            0.0f, 0.0f, 0.5f, 1.0f
    };

    FloatBuffer dataBuffer = BufferUtils.createFloatBuffer(positions2.length);
    dataBuffer.put(positions2);
    dataBuffer.flip();
    glBindBuffer(GL_ARRAY_BUFFER, vboPosition);
    // alloctate memory on the graphics card and upload bytes to it
    glBufferData(GL_ARRAY_BUFFER, dataBuffer, GL_STATIC_DRAW);
    GLUtil.checkGLError(); 
    glEnableVertexAttribArray(0); // Position is index 0
    glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0L);
    GLUtil.checkGLError();
    vboColor = glGenBuffers();
    // x y z w
    float[] colors = new float[]{
            1f, 0f, 0f, 1f, // P1 = red
            0f, 1f, 0f, 1f, // green
            0f, 0f, 1f, 1f, // blue
    };
    dataBuffer = BufferUtils.createFloatBuffer(colors.length);
    dataBuffer.put(colors);
    dataBuffer.flip();
    glBindBuffer(GL_ARRAY_BUFFER, vboColor);
    glBufferData(GL_ARRAY_BUFFER, dataBuffer, GL_STATIC_DRAW);
    GLUtil.checkGLError(); 
    glEnableVertexAttribArray(1); // COLOR is index 1
    glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, 0L);
    GLUtil.checkGLError();
}
private void initWindow() {
    System.out.println("LWJGL version: " + Version.getVersion());
    glfwSetErrorCallback(errorCallback = GLFWErrorCallback
            .createPrint(System.err));
    if (glfwInit() != GL_TRUE)
        throw new IllegalStateException("Unable to initialize GLFW");
    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    window = glfwCreateWindow(WIDTH, HEIGHT, "Demo", NULL, NULL);
    if (window == NULL)
        throw new RuntimeException("Failed to create the GLFW window");
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);
    glfwShowWindow(window);
}

我希望有人可以帮助我。 如果您需要更多信息 - 我正在关注这篇文章,并会立即回复。

此致敬意

private void updateColors() {
    float[] newColors = new float[]{
            newColor, 0f, 0f, 1f, // P1 = red
            0f, 1f, 0f, 1f, // green
            0f, 0f, 1f, 1f, // blue
    };
    FloatBuffer dataBuffer = BufferUtils.createFloatBuffer(newColors.length);
    dataBuffer.put(newColors);
    dataBuffer.flip();
    glBindBuffer(GL_ARRAY_BUFFER, vboColor);
    // alloctate memory on the graphics card and upload bytes to it
    glBufferData(GL_ARRAY_BUFFER, dataBuffer, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1); // Color at index 1
}

我通过使用上面在 while 循环(帧循环)中看到的方法解决了它。

最新更新