[Solved] (LWJGL) VBO problems

I have a basic triangle in a float buffer.

FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(new float[]{-0.5f, -0.5f, 0, 0.5f, -0.5f, 0, 0.5f, 0.5f, 0});
        vertexData.flip();

And I think this renders it in the gameloop.

glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
            glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);

            glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
            glColorPointer(colorSize, GL_FLOAT, 0, 0L);

            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY); 
            glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);//what does
            //this do ^? how would i draw another triangle?
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);

To add more points, I know you increase the number of verticies and add more puts to this.

FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(new float[]{-0.5f, -0.5f, 0, 0.5f, -0.5f, 0, 0.5f, 0.5f, 0});
        vertexData.flip();

But If that does make a second triangle. How do I move only one of them instead of moving both of them. I still don’t fully understand this. Also how do I convert ints to floats for this?

 glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);//what does
            //this do ^? how would i draw another triangle?

This draws your triangles with the data that you provided. Every 9 floats that you provide (3 vertices make up a triangle, 3 floats per vertex for x,y,z) will draw a new triangle.

[quote=“AppleSauce,post:1,topic:49961”]
I don’t understand your problem / question. Are you asking how to move only one of the triangles? I guess that you are coming from the land of [icode]glTranslate()[/icode]? All you need to do is modify the vertices to different positions.

Add an [icode]f[/icode] after the number. I assume that this is what you are asking.

Yes I mean to move only one if them. Would I be able to assign the points an int and just translate those ints?(if I make any sense) I’m using my phone right now but I mean instead if this

FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(new float[]{-0.5f, -0.5f, 0, 0.5f, -0.5f, 0, 0.5f, 0.5f, 0});
        vertexData.flip();

Would I replace the numbers with ints that I just trabslate?

Edit: by convert ints to floats I mean I don’t know where 0.5f is. It draws the triangle in the center of the screen somehow.

Ok, I get you now. You want to use something like pixels to define your positions, correct?

For this, you are going to need a perspective projection matrix (or orthographic if you’re wanting 2d). These basically transform the points that you provide (in pixels) to the coordinates that Opengl uses (-1f to 1f).

To learn about matrices:
Matrices
Perspective Projection
Orthographic Projection

To translate your point (from a pixel) to opengl (as a normalized float) you would need to multiply by a matrix. You can do this on the CPU in your program, or on the GPU in your vertex shader.

Search around on these forums and you will find something. These kind of matrix questions have been asked a lot. Also google around, many questions like this have been asked (and answered) all over the internet, with specific implementations).

Great, thanks. Was I correct about moving the triangles though?

You would just provide a different float. The coordinates range from -1 to 1, so anything between that will be on your screen. So, if you want your triangle to be translated .1f to the right, you would just change it to:


float trans= 0.1f;
vertexData.put(new float[]{-0.5f + trans, -0.5f, 0, 0.5f + trans, -0.5f, 0, 0.5f + trans, 0.5f, 0});

See how I modified all of the x coordinates?

I understand now. I’ll just have to read more into the matrices.

if I do

trans+= 0.1f;

in my gameloop, it doesn’t move. trans just increases.

That is because your vbo is static. You have to make it dynamic (use [icode]GL_DYNAMIC_DRAW[/icode]) and reupload the contents to the vbo.

I noticed that in your tutorial series. It still doesn’t move the triangle drawn.

You have to re-upload the data using glBufferData(); (or glBufferSubData():wink:

int vboVertexHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
        glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        int vboColorHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
        glBufferData(GL_ARRAY_BUFFER, colorData, GL_DYNAMIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

I tried

GL_DYNAMIC_DRAW

and

GL_STREAM_DRAW

Those just provide hints to OpenGL to help it optimise the buffer. Every time you want the data to change, you have to call glBufferData() again to provide it the updated data.

By that, do you mean put the glBufferData() in the gameloop?

If you update the data every frame, then yes.

You can upload using either [icode]glBufferData()[/icode] or [icode]glBufferSubData()[/icode] The difference is that [icode]glBufferData()[/icode] re-creates the data store. The latter, [icode]glBufferSubData()[/icode] only replaces the data.

Now, in the game loop, you have to clear the contents of the floatbuffer you are using, and then add the updated vertices to it. The, you can call the above functions just as you called when creating the VBO.


glBindBuffer(GL_ARRAY_BUFFER, vboID);
@@glBufferData(GL_ARRAY_BUFFER, vertices, GL_DYNAMIC_DRAW);

Or using [icode]glBufferSubData()[/icode] it will look like this.


glBindBuffer(GL_ARRAY_BUFFER, vboID);

// Here, the 0 is the offset. An offset of 0 means that you are
// replacing all the contents of the VBO.

@@glBufferSubData(GL_ARRAY_BUFFER, 0, vertices);

Hope this helps.

I’m just getting a flashy effect on the triangle.

(It’s not really green or that weird, the gif recorder exagerated it)

The problem is that you are increasing the trans variable, but the values in the vertices FloatBuffer is not updated. You have to clear the FloatBuffer and then add the vertices again, so that the values get updated.

How would I clear them?

Use the clear method of the FloatBuffer.