Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] (LWJGL) VBO problems  (Read 818 times)
0 Members and 1 Guest are viewing this topic.
Offline AppleSauce
« Posted 2014-07-08 05:44:33 »

I have a basic triangle in a float buffer.

1  
2  
3  
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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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.

1  
2  
3  
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?
Offline Longarmx
« Reply #1 - Posted 2014-07-08 05:58:10 »

1  
2  
 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.

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.

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
glTranslate()
? All you need to do is modify the vertices to different positions.

Also how do I convert ints to floats for this?

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

Offline AppleSauce
« Reply #2 - Posted 2014-07-08 06:04:30 »

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
1  
2  
3  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #3 - Posted 2014-07-08 06:19:59 »

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).

Offline AppleSauce
« Reply #4 - Posted 2014-07-08 06:32:34 »

Great, thanks. Was I correct about moving the triangles though?
Offline Longarmx
« Reply #5 - Posted 2014-07-08 06:38:15 »

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

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

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:

1  
2  
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?

Offline AppleSauce
« Reply #6 - Posted 2014-07-08 07:13:36 »

I understand now. I'll just have to read more into the matrices.
Offline AppleSauce
« Reply #7 - Posted 2014-07-08 18:23:04 »

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

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

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:

1  
2  
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?

if I do
1  
trans+= 0.1f;
in my gameloop, it doesn't move. trans just increases.
Offline SHC
« Reply #8 - Posted 2014-07-08 18:29:43 »

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

Offline AppleSauce
« Reply #9 - Posted 2014-07-08 18:32:11 »

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

I noticed that  in your tutorial series. It still doesn't move the triangle drawn.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #10 - Posted 2014-07-08 18:33:39 »

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

Offline AppleSauce
« Reply #11 - Posted 2014-07-08 18:36:51 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
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
1  
GL_DYNAMIC_DRAW
and
1  
GL_STREAM_DRAW
Offline Longarmx
« Reply #12 - Posted 2014-07-08 18:47:42 »

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.

Offline AppleSauce
« Reply #13 - Posted 2014-07-08 18:52:15 »

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?
Offline Longarmx
« Reply #14 - Posted 2014-07-08 18:57:10 »

If you update the data every frame, then yes.

Offline SHC
« Reply #15 - Posted 2014-07-08 19:01:08 »

You can upload using either
glBufferData()
or
glBufferSubData()
The difference is that
glBufferData()
re-creates the data store. The latter,
glBufferSubData()
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.

1  
2  
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_DYNAMIC_DRAW);

Or using
glBufferSubData()
it will look like this.

1  
2  
3  
4  
5  
6  
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.

Offline AppleSauce
« Reply #16 - Posted 2014-07-08 19:07:45 »

You can upload using either
glBufferData()
or
glBufferSubData()
The difference is that
glBufferData()
re-creates the data store. The latter,
glBufferSubData()
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.

1  
2  
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_DYNAMIC_DRAW);

Or using
glBufferSubData()
it will look like this.

1  
2  
3  
4  
5  
6  
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)

http://i.imgur.com/2XCfmpQ.gif
Offline SHC
« Reply #17 - Posted 2014-07-08 19:12:46 »

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.

Offline AppleSauce
« Reply #18 - Posted 2014-07-08 19:17:18 »

How would I clear them?
Offline SHC
« Reply #19 - Posted 2014-07-08 19:20:19 »

Use the clear method of the FloatBuffer.

Offline Longarmx
« Reply #20 - Posted 2014-07-08 19:20:28 »

How would I clear them?

vertexData.clear()


Edit: Ninja'd

Offline AppleSauce
« Reply #21 - Posted 2014-07-08 19:26:03 »

I didn't know there was a clear method. Well, I tried that and now the triangle has even more shading.
1  
2  
3  
4  
5  
6  
trans += 0.1f;
           
           
            glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);
           
            vertexData.clear();


I just pasted my code so you can check it.

http://pastebin.java-gaming.org/ee4aa0315041c
Offline SHC
« Reply #22 - Posted 2014-07-08 19:32:38 »

You are not updating the vertices. Add this code in your gameloop.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
// Clear the vertices buffer
vertexData.clear();

// Update the trans variable
trans += 0.1f;

// Re-create the vertex data
vertexData.put(new float[]{-0.5f + trans, -0.5f, 0, 0.5f + trans, -0.5f, 0, 0.5f + trans, 0.5f, 0});
vertexData.flip();

// Now, upload to the GPU
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);

And then render as you usually would. Hope this helps.

Offline AppleSauce
« Reply #23 - Posted 2014-07-08 19:45:29 »

It works!  Grin I also looked at redbladewarriors' tutorial on youtube and learned some more things Thank you.
Offline SHC
« Reply #24 - Posted 2014-07-08 19:46:40 »

Glad that you got it working. Had to get some sleep now.

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!