LWJGL VBO content is always drawn to the center of the screen (0,0,0)

廉价感情. 提交于 2019-12-12 00:27:23

问题


I started to follow a tutorial about modern OpenGL rendering and altered the c++ code from a VBO lesson to work with LWJGL. I initialized the VBO with the following code:

int vbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);

"buffer" is initialized as

FloatBuffer buffer = BufferUtils.createFloatBuffer(9);

and then filled with {-0.5f, -0.5f, 0, 0.5f, -0.5f, 0, 0, 0.5f, 0} via

buffer.put(val)

My game loop looks like this:

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL20.glEnableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
GL11.glDrawArrays(GL11.GL_POINTS, 0, 3);
GL20.glDisableVertexAttribArray(0);
Display.update();

But all I get is a white dot in the center of the screen. I found a similar question (VBO: Array not drawn), but the solution did not fix my problem. The tutorial I used is http://ogldev.atspace.co.uk/www/tutorial02/tutorial02.html and http://ogldev.atspace.co.uk/www/tutorial03/tutorial03.html. When I try to draw GL_TRIANGLES instead of GL_POINTS, nothing is drawn onto the screen.


回答1:


Use glTranslate*() to set a position where you want to draw something. It can be sorta deprecated, but it works.
Here you can find a discussion about it.
Your code should look the following way:

glPushMatrix();
glTranslate*();
//draw something
glPopMatrix();




回答2:


After wasting hours reading different tutorials, I figured out that I did not rewind() my buffer...

GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

and

GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);

as suggested in VBO: Array not drawn was not necessary.



来源:https://stackoverflow.com/questions/17394704/lwjgl-vbo-content-is-always-drawn-to-the-center-of-the-screen-0-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!