GMS 2 Vertex buffer tilesets and changing tiles

Discussion in 'Programming' started by vdweller, Mar 26, 2019.

  1. vdweller

    vdweller Member

    Joined:
    Jun 24, 2016
    Posts:
    134
    Hello!

    The past few days I have been fiddling with using vertex buffers for rendering tilesets. I understand that this is a quite fast method. However, what happens when you have a map where tiles change?

    Do I need to re-create the entire vertex buffer in that case? If so, doesn't that defeat the purpose of using vertex buffers for speed?

    For example, if you have a 300x300 map with tiles, every time a tile changes you would have to iterate through 90,000 tiles and recreate the vertex buffer data. Have I understood that correctly or is there some trick to update vertex buffer contents without having to go through all available tiles?

    The same goes with tile smoothing. Every time a tile changes, the only way to update the "edge vertices" that will fade out to give that nice blend is to recreate the buffer from scratch?
     
  2. Misty

    Misty Member

    Joined:
    Jun 22, 2016
    Posts:
    1,019
    I have the same problem. Trying to use vertex buffers for 3d decals but the system doesnt allow me to.
     
  3. TheSnidr

    TheSnidr Heavy metal viking dentist GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    462
    There are a couple of useful functions that let you convert between vertex buffers and regular buffers. I believe they're named something along the lines of vertex_create_buffer_from_buffer and buffer_create_from_vertex_buffer.
    Once your vertex buffer has been converted to a regular one, you can overwrite the vertices of the mesh, and then convert back to vertex buffer. The conversion is actually fairly fast.
     
  4. vdweller

    vdweller Member

    Joined:
    Jun 24, 2016
    Posts:
    134
    That's very interesting to know! Another interesting approach could be breaking down the terrain into chunks and simply updating a chunk instead of the entire thing.
     
  5. Misty

    Misty Member

    Joined:
    Jun 22, 2016
    Posts:
    1,019
    I tried but it gave me an error saying "Error vertex buffer does not exists". The typo was really disheartening and I tried mixing different types of buffers to no avail.
     
  6. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    Something that has worked pretty well for me is to draw the entire map as a single quad, and to use a second texture that has encoded into it the tile map. In other words each fragment looks up its tile in the texture, calculates its position within the tile, and then samples the tileset texture. With this setup, the tilemap can be a surface texture.

    Misty, do you mind showing a sample of the code you're trying to use to convert a buffer into a vertex buffer?
     
  7. Misty

    Misty Member

    Joined:
    Jun 22, 2016
    Posts:
    1,019
    I never saved the project since the code never worked. The code I did amounted to creating some buffers then trying to use the functions that the Snidr said. It kept saying the buffer didn't exist even though I made sure I created it and it existed.

    I just wish there was some kind of tutorial or example project I could look at instead of aimlessly being in the dark.
     
  8. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    @Misty

    Here's an example of converting buffer to vertex buffer, written in GMS1. It should also work in GMS2.

    https://www.dropbox.com/s/f299l55xa5ozvaf/buffer_to_vertex.gmz?dl=0

    If you still get errors, try changing the vertex buffer compatability type in the global game settings.

    EDIT: top and bottom vertex positions were mixed up, and are now fixed.
    EDIT2: now really fixed, lol.
     
    Last edited: Mar 26, 2019
    RujiK and Misty like this.
  9. Misty

    Misty Member

    Joined:
    Jun 22, 2016
    Posts:
    1,019
    Thanks I'll check it out soon.

    Okay I got it to work with the moving objects. However I have not tried it yet for decals. For decals I will need to convert the vertex buffer back into a regular buffer, then reconvert the regular buffer back into a vertex buffer. If that doesn't work then I can just use ds_lists. If ds_lists are faster then I can just use ds_list instead anyway.
     
    Last edited: Mar 26, 2019
  10. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    you don't need to convert the vertex buffer back to a regular buffer. The regular buffer still remains.
     
    Last edited: Mar 26, 2019
  11. Misty

    Misty Member

    Joined:
    Jun 22, 2016
    Posts:
    1,019
    Hmm. Not sure what I'm doing wrong then because the moving objects won't leave a trail.

    If I delete buffer_seek or change it to buffer_seek_end or buffer_seek_relative, the objects just won't move. If I put it to buffer_seek_start the objects will move, but no trail.
     
    Last edited: Mar 26, 2019
  12. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    @Misty

    can you describe exactly what you are intending to do?
     
  13. Misty

    Misty Member

    Joined:
    Jun 22, 2016
    Posts:
    1,019

    Just want a decal system. I am using your example as a training grounds to do future 3d things. I made your objects move to prove that your code works Per Step. The next part I need to do is have them add their data to the buffer instead of erasing each time. If it is erased each step I can't use decals in games. Right now it refuses to draw trails and keeps erasing each step.

    I put it to buffer_seek_relative and buffer_seek_end but then it is not updating the buffer at all for some odd reason. If I keep it to buffer_seek_start then it just draw the objects moving but no trail because it erases the old buffer data each step.


    Wait nvm lol I forgot you clamped the buffer to too small a size lol. It is working now xD
     
  14. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    @Misty do you mind me asking, do you have any idea what was causing the error message you were getting before? "Error vertex buffer does not exists"
     
  15. Misty

    Misty Member

    Joined:
    Jun 22, 2016
    Posts:
    1,019
    Haven't the slightest idea, my code looked very similar to yours. One of these days I'll try and see if I can replicate the original problem.

    Okay so I decided to look at it today...I still have no idea why my original code didn't work.

    Basically in my Create event I had

    global.v_buffer=buffer_create(1024*1024,buffer_wrap,1)

    then in step I had

    global.v_x=vertex_create_buffer_from_buffer(global.v_buffer, global.my_format);


    Yet it keeps saying it doesn't "exists".
     
    Last edited: Mar 26, 2019

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice