lwjgl 通过 JSlider 更新 VBO(颜色、顶点)
Posted
技术标签:
【中文标题】lwjgl 通过 JSlider 更新 VBO(颜色、顶点)【英文标题】:lwjgl Update VBO (color, vertices) through JSlider 【发布时间】:2016-04-25 14:00:12 【问题描述】:抱歉我的英语不好。 我在通过 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);
我希望有人可以帮助我解决这个问题。如果您需要更多信息 - 我会密切关注这篇文章,并会立即回复。
最好的问候
【问题讨论】:
启用initBuffers();
时会发生什么?
当我在“while”循环中启用它时,程序会立即关闭:(即使我在 try 块中禁用它。
能否在 try-finally 块中放置一个 catch 块来捕获 GLUtil.checkGLError()
可能抛出的异常?然后发布堆栈跟踪
【参考方案1】:
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循环(帧循环)中看到的方法解决了它。
【讨论】:
插入缓冲区后不要flip()
。使用rewind()
或position(0)
以上是关于lwjgl 通过 JSlider 更新 VBO(颜色、顶点)的主要内容,如果未能解决你的问题,请参考以下文章