2016-04-25 24 views
0

對不起我的英語不好。 我有一個問題通過JSlider和stateChanged事件上傳VBO。一般情況下,我想自己弄清楚,但我現在卡住了。所以我的第一個問題是如何操作(更新)VBO給出下面的代碼時:lwjgl通過JSlider更新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); 
} 

我希望有人能幫助我了這一點。如果您需要更多信息 - 我會密切關注這篇文章,並會立即回覆。

問候

+0

當你啓用'initBuffers();'時會發生什麼? – Nadir

+0

當我在「while」-loop內啓用它時,程序立即關閉:(即使當我在try-block中禁用它時) – manCRO

+0

你可以在try-finally塊中放置一個catch塊來捕獲GLUtil的異常。 checkGLError()'可能會拋出?然後發佈棧跟蹤 – Nadir

回答

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循環(幀循環)內上方看到的方法來解決它。

+0

插入緩衝區後不要'flip()'使用'rewind()'或'position(0)'' – elect