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

时间:2016-04-25 14:00:12

标签: lwjgl vbo jslider

抱歉我的英语不好。 我有一个问题,通过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);
}

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

最好的问候

1 个答案:

答案 0 :(得分:0)

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循环(frame loop)中使用上面看到的方法解决了它。