Sign in to follow this  
RippeR37

OpenGL Managing scene's (level's) geometry - how to do if efficiently

Recommended Posts

RippeR37    102

Hey, im working on engine for future game and i stumbled on a problem. I'm not sure how to efficiently manage (mostly store and send to GPU) data of my level geometry.

Game will be with Top-Down camera (3D), so visibile area will be reduced, also most of objects will be pretty low-poly since nobody needs extra sharp(/smooth) models if u barely can see some details on it. ;) Problem is that i'm not sure how to store it, (update if needed* - though geometry is static, just what is visible) and send to GPU.
I've come up with some ideas:

1. Most basic - load everything right in one VBO (as models are very low-poly it won't have too much data as you might think?).
I think that though vertex shadders will work on every vertex, fragment shaders will work only on visible fragments and "discard" others automatic, right?

+ simple to write
+ dont have to bind multiple VAOs/VBOs/textures every frame
+ don't have to send new data every frame to GPU (using CPU time) and compute it!
- lot of data is on VBO (though size won't change during game) and will go through Vertex Shader (i think not by F.S. - right?)
- it seem's not so optimized?
- everything must be in one texture atlas which limits possibilities

2. Load every distinct object at startup of level, and then every frame create new VBO with new mesh, that would be made from objects that are currently visible at scene (modified with transformation matrix so that it all works ok).

+ it sounds as its an optimization ;p (but after some thought i'm not so sure)
+ i manage what i send to draw, so i could do some work (like LOD, though not in that kind of game, becouse all models are usally at same distance from camera...) so it's not actually "pro" per se...
+ i bind one VAO/VBO after creating that whole scene's mesh and dont change it for frame.
+ only visible vertexes (objects, somevertex might be little out of course) are sent to GPU
- loooooot of work on CPU, which may result in: GPU waits for CPU data ..... CPU waits for rendering by GPU ... and again GPU waits...
- looks much more complicated to build efficiently (at least as much as can)
- everything visible must be in one texture atlas...

3. Split level on smaller areas like 2D-tiles, each tile contains every object's data that is (at least partial) in this tile. Just not too small tiles, so that per visible scene there would be 3x3 or 4x4 tiles max. (or even 2x2?).

+ sound pretty fast (only needed data, not many VAO/VBO binds per frame, VAO/VBO would be already set-up at loading-time so no computing time needed per frame, only draw call).
+ somehow more flexible with textures, becouse i need current texture only for small tile which can be only couple meshes.
- seems hard to code it efficient, some problems (look below)
- some meshe's that would be on more than one tile would have to be doubled or splitted (not easy job i guess to do it right and fast! doubled seems better somehow)
- lot of pre-processing so as long as #1 pre-processing or even longer.

4. Render each object separate. If it's visible multiple times, render it using some kind of mechanism to do it efficiently like instancing (or something else? suggestons? i dont know instancing yet, but i heard of it).

+ normally it may be common way to render scenes (when meshes have many triangles its ok, becouse switching vbo doesnt cost so much then)
+ easiest to implement
+ easy to manage scene
+ easy to create a scene (just add description of mesh - pos/rotate/scale and which object it is just to add it).
+ i could create VAOs/VBOs earlier so no computing time needed on CPU side every frame
+ only visible models would be rendered
- lot of switches between every objects (maybe not that much, but when meshes are low-poly, i guess switching would take long time compared to rendering)
- it doesn't seems too optimized.

As i said, mostly i'd like to balance between complexity and performance, since i'm not working on some top-level, AAA project, just smaller project that may be very playable, may not ;)

PS. Could someone write in some order or with "weights" how much OpenGL actions cost (about)? Like: single draw call, bind VBO, bind VAO, bind texture, etc... so i'd knew what to watch for :)

Share this post


Link to post
Share on other sites
marcClintDion    435
fragment shaders will work only on visible fragments and "discard" others automatic, right?

Not necessarily, deferred rendering is meant to address this issue.  Some other behind the scenes optimizations require some thought on your part.  Depth sorting for one...

 

If you load 1 big VBO then you can still access them individually by using the models indices.  I would not do this.  Accessing a component still requires additional draw calls if you want to change anything.  If you combine this with the headache and nuisance of adding and subtracting and hopping through indices then it may not be worth the extra few percentage points of performance increase.  Some implementations may slow down, who knows?  Messing up the index management can crash a computer. You won't know all your particular  issues until you try what you've built on every single machine that you can get your hands on.

 

Even most of those silly little handheld phones can handle at least several dozen VBO draw calls before they start complaining too much, some of them will do 100 or more per frame, easily.  This is combined with changing shaders and textures for many of those models.

 

Depending on your setup:

(i)You can use a distance check to disable whatever is off screen.  if(modelPosX > -5.0 && modelPosX < 5.0){displayArea_1();} helps a lot to manage top view and side view games.  Also if you isolate collision + animation within these screen-sized chunks then you can get really carried away for every discrete area.

(ii)If, when you change rooms it's only vertically or horizontally, then you will never have to display more than two areas at a time which gives you lots of flexibility in how many draw calls, texture and shader swaps can take place. 

 

You could pack all the floor tiles for a room into one VBO, I think this would be practical and easy and safe.  Same for trees, rocks, bushes.   Now for the tiles, rocks, trees, and bushes you would have only 4 VBO's to switch between.  Even a wrist watch running Java could handle that much.

 

Pay attention to optimization but don't become so hung up on it that you set yourself behind several years worrying about it.  Stability is far more important.  Finishing something is also good.

 

p.s. You mentioned something about creating the required VBO's every frame.  I would say, avoid that.  Even changing them per-frame can stall things, I imagine creating them per-frame will be far worse. My imagination can't be stretched far enough for me to come up with a good reason to do this.

Edited by Josh Petrie

Share this post


Link to post
Share on other sites
RippeR37    102
You can use a distance check to disable whatever is off screen.  if(modelPosX > -5.0 && modelPosX < 5.0){displayArea_1();} helps a lot to manage top view and side view games.  Also if you isolate collision + animation within these screen-sized chunks then you can get really carried away for every discrete area.

But then i guess u mean that every mesh has its own VBO, right?
 

 

when you change rooms it's only vertically or horizontally, then you will never have to display more than two areas at a time which gives you lots of flexibility in how many draw calls, texture and shader swaps can take place.

I plan for it to be outdoor top-down shooter. Something similiar to Left 4 Dead, but top-down, now for PC and maybe in the future for Android(/iOS). Test pic (a lot to do yet): http://i.stack.imgur.com/XC0ga.png S(it had some debugging on so actually rendering time is not even half now)

I thought about making group of objects but it doesnt help me too much, if i put - in example - all trees in one VBO...
 

 

p.s. You mentioned something about creating the required VBO's every frame.  I would say, avoid that.  Even changing them per-frame can stall things, I imagine creating them per-frame will be far worse. My imagination can't be stretched far enough for me to come up with a good reason to do this.

I guess i'll have to update my shadow-map generation so that i'm not creating new texture every frame (for each light) but use same texture and just update it... Luckily its only couple lines...

In the end i still dont know how should i keep it. Group it by position in bigger VBOs and use only 2-4 VBOs per frame looks best, right? But the question is - should i keep it in VRAM or RAM and just send it if i need it? 2nd option would allow me to create very big maps since it holds very little data in vram (and ram is usally much bigger), but seems slower...

Btw. as i asked before - how slow is binding VAOs, binding VBOs, binding Texture, drawing 100 triangles, drawing 100k triangles, drawing 500k triangles, turning of depth test, something else that could be important here too, etc. ? I'd like to know what should i avoid and how hard should i optimize things to use as few as possible. 

Edited by RippeR37

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now