LWJGL VBO content is always drawn to the center of

2019-09-09 17:38发布

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.

2条回答
Summer. ? 凉城
2楼-- · 2019-09-09 17:50

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-09-09 18:01

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();

查看更多
登录 后发表回答