Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  VBO order of operations  (Read 977 times)
0 Members and 1 Guest are viewing this topic.
Offline QuicK

Senior Newbie





« Posted 2012-10-31 02:58:42 »

Okay...so like everybody else, I'm trying to create a "simple" voxel engine.

I'm trying to create a VBO that will draw the entire chunk of 16x16x16 cubes.

My questions are:

1) FloatBuffer vertexData = BufferUtils.createFloatBuffer(294912);
    Is the above code bad? it's got 16*16*16*72 slots available for the vertex buffer which means it can store every vertex of every cube.
    Should I be storing this many floats in the same buffer? (it's my understanding that VBO's are to Immediate mode as the GPU is to the CPU) which would mean that
    what I'm doing is correct.

2) what does glGenBuffers() actually do, and when should it be called?
    before the vertices are stored in the float buffer? while they're being stored? after they're stored? etc.

3) what actually happens to the FloatBuffer when it is flipped?
    should it only be flipped once (always)?

4) what does the following code do?
1  
2  
3  
4  
int vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);



Any information on any of the above questions would be much appreciated!
*as always, any source code will be supplied upon request Smiley



Thanks!

QuicK
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2012-10-31 04:20:15 »

1) Yes. 1.2MB of data for the same cube? How about just a VBO that holds the data for 1 cube that you render with different offsets 16*16*16 times.

2) glGenBuffers generates an id for a named location to store data. This location's memory will be allocated with the call to glBufferData.

3) When a NIO Buffer is flipped, the limit is set to the position and the position is set to 0. This way, you can read all the data from the beginning to the limit.

4)
1  
2  
3  
4  
int vboVertexHandle = glGenBuffers();                        //Generate ID
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);              //Bind ID
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);   //Store the data in that allocated space
glBindBuffer(GL_ARRAY_BUFFER, 0);                            //Un-bind the ID

Offline theagentd
« Reply #2 - Posted 2012-10-31 04:31:39 »

1) That's no problem, but most of the buffer should be unused in the end. You need to cull (remove) faces that border to other solid blocks so only faces that can actually be visible are stored in VRAM. The size of vertexData should be enough to store even worse case scenarios, but the actual amount of data being uploaded should be pretty low per chunk in 99% of all cases.

2) glGenBuffers() creates a buffer ID used to refer to data stored on in VRAM on the GPU. You should create a vertex buffer per chunk.

3) Flipping sets the limit (Kind of like the size of an ArrayList. Note that size != capacity, the array length.) to the current position and resets the position to 0. Basically, you put data into the buffer with buffer.put() and when you're finished you flip. That way only the actual used part of the array is used thanks to the new limit. If you want to reuse the buffer later, just do a buffer.clear() which resets the limit to the capacity of the buffer.

4) That code uploads the vertexData buffer to VRAM and stores it there. First line creates a buffer handle. Second binds that buffer handle (they works like texture handles). Third line uploads the data in vertexData to the VBO. 4th line just unbinds the VBO.

How about just a VBO that holds the data for 1 cube that you render with different offsets 16*16*16 times.
No, you really need to batch the cubes together into chunks if you want to render more than a few thousand of them. You also really need to ignore faces that are inside walls/the floor/etc to reduce the number of vertices and triangles as much as possible.


View distance of 400m (1 cube = 1m^3) = Minecraft
With culling: 480 FPS, 150MB of VRAM used
Without: Instant out of VRAM crash


View distance of 100m:
With: 2200 FPS, 40MB of VRAM used
Without: Instant out of VRAM crash


View distance of 50m:
With: 2500 FPS, 30MB of VRAM used
Without: Instant out of VRAM crash


View distance of 25m:
With: 2500 FPS, 30MB of VRAM used
Without: 400 FPS, Crashes after loading around 5 16x16x16 chunks due to out of memory.


Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline QuicK

Senior Newbie





« Reply #3 - Posted 2012-10-31 04:44:34 »

Thanks for the replies guys! That really clears up some things that I didn't know about VBOs!

@ra4king the reason I'm now using FloatBuffer vertexData = BufferUtils.createFloatBuffer(294912); is because I had tried what you suggested prior to posting this, and couldn't get it to work.

I'll take what you guys said, and try to fix my chunk code! I'll update this post after I've attempted it! Smiley

Edit:

Okay, so...I've tried many things to no avail.

Let me make sure I have things right in my mind. (Note: this is to create a VBO for 16x16x16 cubes)

1) create the chunk-specific FloatBuffer (vertexData) with enough space to hold all indices of all vertices (294912)
2) generate the ID for the buffer (glGenBuffers()) -- there should be only one of these right?
3) run through 3 nested loops (x, y, z) to create the indices
4) put the indices in vertexData
5) after the nested loops, flip vertexData
6) bind the ID generated in step 2, provide the VBO with the vertex data, and unbind the ID generated in step 2
7) after all of the above steps have been completed, run this every draw tick:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public void draw() {
     
      glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
      glVertexPointer(3, GL_FLOAT, 0, 0L);
     
      glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
      glColorPointer(3, GL_FLOAT, 0, 0L);
     
     
      glTranslatef(curx, cury, curz);
     
      glEnable(GL_CULL_FACE);
      glCullFace(GL_BACK);
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);
      glDrawArrays(GL_QUADS, 0, 98304); //98304 derived from 4 vertices per face and 6 faces (4*6) * (16*16*16)
     glDisableClientState(GL_COLOR_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);
      glDisable(GL_CULL_FACE);
     
   }




And....as usual I feel extremely dumb because I found out my problem as I was writing this out.
M issue was in glDrawArrays()...I was only allowing enough indices for 1 cube.

I decided to go ahead and post this in case anybody else finds it useful Smiley thanks for the help guys! I still learned a good bit about VBOs from this Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!