Texture Batching | Coding a 2D Game Engine in Java #13

Sdílet
Vložit
  • čas přidán 11. 05. 2020
  • Join the Discord: / discord
    In this tutorial, I go over how to add texture batching to our current batched renderer. This allows us to bind up to 16 different textures per draw call, which means that you can batch together a lot more quads with different textures for each quad. This is important, because in 2D games, you typically have a variety of assets coming from multiple files, and if you do not want a mega-spritesheet, then you need to enable multiple textures for your batched renderer.
    Source Code: github.com/codingminecraft/Ma...
    ---------------------------------------------------------------------
    Website: ambrosiogabe.github.io/
    Github: github.com/ambrosiogabe
    Here are some books I recommend if you want to learn about game engine development more thoroughly. I do not profit off any of these sales, these are just some books that have helped me out :)
    My Recommended Game Engine Books:
    Game Engine Architecture: www.gameenginebook.com/
    Game Physics Cookbook (Read this before the next physics book): www.amazon.com/Game-Physics-C...
    Game Physics (Ian Millington): www.amazon.com/Game-Physics-E...
    Game Programming Patterns (Free): gameprogrammingpatterns.com/
    My Recommended Beginning Game Programming Books:
    JavaScript Game Design: www.apress.com/gp/book/978143...
    My Recommended Java Books:
    Data Structures/Algorithms: www.amazon.com/Data-Structure...
    LWJGL (Free, but I haven't read this thoroughly): lwjglgamedev.gitbooks.io/3d-g...
    Outro Music: www.bensound.com/royalty-free...

Komentáře • 42

  • @vedrankovacevic8170
    @vedrankovacevic8170 Před 2 lety +1

    Hey Gabe, thanks for the great content. Just wanted to say that the problem with the Time class was because the timeStarted variable was declared as a float and System.nanoTime() returns a long so there was probably some loss of precision. If you change timeStarted to be a long everything should work fine.

  • @Shadowblitz16
    @Shadowblitz16 Před 3 lety +2

    Loving these tutorials, thankyou for uploading them

    • @GamesWithGabe
      @GamesWithGabe  Před 3 lety

      Thanks for watching :)

    • @yaeljared6138
      @yaeljared6138 Před 2 lety

      Sorry to be so offtopic but does any of you know of a trick to log back into an instagram account?
      I somehow forgot the account password. I love any assistance you can offer me

    • @maximusdeshawn4409
      @maximusdeshawn4409 Před 2 lety

      @Yael Jared Instablaster =)

    • @yaeljared6138
      @yaeljared6138 Před 2 lety

      @Maximus Deshawn I really appreciate your reply. I got to the site on google and I'm waiting for the hacking stuff atm.
      Takes a while so I will get back to you later with my results.

    • @yaeljared6138
      @yaeljared6138 Před 2 lety

      @Maximus Deshawn It did the trick and I finally got access to my account again. Im so happy:D
      Thanks so much, you saved my ass!

  • @adrianobonzanigo9253
    @adrianobonzanigo9253 Před 4 lety +1

    awesome! Great video as always =)

  • @ibrahimsoliman1842
    @ibrahimsoliman1842 Před 3 lety +1

    Great effort.thanks

  • @PipeGamer_03
    @PipeGamer_03 Před 4 lety +5

    Man you're wonderful!!!

  • @stefanoarcaro2856
    @stefanoarcaro2856 Před 2 lety +3

    Hi Gabe, loving this series of tutorials, thank you for making such helpful videos!
    I've always managed to fix any bugs or errors so far, but now I really feel like I'm stumped: right around the 27:05 timestamp, when you fix the black screen and get to see the colored rectangle again, something goes horribly wrong on my pc and I can't seem to understand why.
    Basically I can see the background just fine, and the rectangle is there as well, but most of it is black with the exception of smaller rectangles on the "top row", which seem to be colored just the way they should be (as if the rectangle was actually colored but most of it was then covered with black).
    I checked the code on your github and it seems to be the same I have, I even rewatched this video up to this point to make sure I didn't miss anything... do you have any idea as to why this could be happening?
    Edit: I've noticed that it has something to do with the Max_Batch_Size, the bigger it is the larger the portion of the rectangle that gets covered in black (for example, if I use values 1 to 5 there's no black and it renders just the way it should).
    Also, if I modify the color vector of each sprite to have the blue value (the third parameter) to be 0 this issue disappears

    • @YouTubeDoesntSupportMyUsername
      @YouTubeDoesntSupportMyUsername Před rokem

      I had this problem too, I figured out that the issue for me was when defining COLOR_OFFSET and such I had it formatted as "COLOR_OFFSET = (POS_OFFSET + POS_SIZE) * Float.BYTES" which is wrong, it had to be "COLOR_OFFSET = POS_OFFSET + POS_SIZE * Float.BYTES" to get the order of operations you want and thus getting the correct numbers

    • @stefanoarcaro2856
      @stefanoarcaro2856 Před rokem +1

      @@CZcamsDoesntSupportMyUsername Hi, thank you for your answer! Unfortunately, I had it already formatted the way it works for you. I also tried different orders of operations just to be sure, but it does not seem like that is the source of my problem. Thank you again though, I'll keep looking for the error!

  • @mwbgeometrydashpcaccount6372

    I get the exact same thing as you! Except... mine has black upside-down right-pointing triangles all over it

  • @giovanniprete5730
    @giovanniprete5730 Před rokem +2

    Hi gabe, -i was following your beautiful tutorial on macos for a school project. Unfortunately it doesn't render anything... It's just a lonely black screen. Do you have any idea of what I can do to solve this problem?

    • @Chamarel
      @Chamarel Před rokem

      Try to add -XstartOnFirstThread as a VM option

  • @lastyhopper2792
    @lastyhopper2792 Před rokem

    31:38 might as well make our whole texture images upside-down xD

  • @shurablack38
    @shurablack38 Před 2 lety +2

    It makes me sad to see how much the viewer count decreases with each episode :| Great work.. every single video

  • @maxfreeza9571
    @maxfreeza9571 Před 6 měsíci

    To avoid using the stbi_set_flip_vertically_on_load() function. its work for me. I use these UV coordinates:
    Vector2f[] textureCord = {
    new Vector2f(1,0),
    new Vector2f(1,1),
    new Vector2f(0,1),
    new Vector2f(0,0)
    };

  • @mikayy5777
    @mikayy5777 Před rokem

    Hello! I am from the future hopefully you will still see this. We pretty much have the same code but I keep getting a NullPointException error pointing to the loadVertexProperties method more specifically vertices[offset + 6] = texCords[i].x;
    vertices[offset + 7] = texCords[i].y;
    It's saying that is cannot load from object array because "texCords" is null. I assume that is because texCords is never intialized or it is thinking texCords is something else rather than the things defined in SpriteRenderer class. Regardless I have been trying to fix this to no avail any help will be appreciated!

  • @voxelrifts
    @voxelrifts Před 4 lety +1

    You can do GL45.glBindTextureSlot(texSlot, textureID); instead of activating the slot and then binding the texture. It's just nicer in my opinion

    • @GamesWithGabe
      @GamesWithGabe  Před 4 lety

      Thanks for the suggestion Nikhil! I'll try it out in the future :)

    • @avatar098
      @avatar098 Před 3 lety +2

      I've actually tried this and now I feel smart making my first modification outside of the video series!

    • @monkey5266
      @monkey5266 Před 2 lety +1

      Im not sure of I am doing something horrible wrong but I dont have that method in GL45

    • @spaghettiking653
      @spaghettiking653 Před 2 lety

      @@monkey5266 Me neither, the method just doesn't exist for me when I try to import it

  • @henribebo
    @henribebo Před 2 lety +1

    Hi Gabe I'm doing your whole tutorial right now but i'm kind of stuck atm.
    i get this following message when I try to compile at the end:"0:15(27): error: sampler arrays indexed with non-constant expressions are forbidden in GLSL 1.30 and later"
    If i change the integer id in the default.glsl file to a number for example 3 then the program runs but when i put in id, like you did i get the error. Do you have an idea what i should do? I saw online that people were able to get rid of the bug by updating to a newer OpenGL version, but that surely can't be the reason why it workes for you but not for me.
    Best regards Henri

    • @GamesWithGabe
      @GamesWithGabe  Před 2 lety

      Hey Henri! So I later found out that the technique I showed in this video is technically undefined behavior. I saw another video of somebody doing it this way, and it was a lot easier than what I was doing previously, unfortunately like you found out its not supported everywhere. The easiest way to get around this is to have a large switch statement in your shader and then based on the index you can just access the texture array using a constant number. I can dig up an old example if you want, and if you join the Discord I know there are some other developers who can show you some examples of this :)

    • @henribebo
      @henribebo Před 2 lety

      @@GamesWithGabe Thanks you for answering, first of all i want to say thank you for the tutorial i really appreciate it. Tbh i think I'm just fine if you send me the link on where they show how you would normally do it. I don't have too much time because my new Semester starts next week and I thought why not get in touch with java again with a project. Yea so the link would be amazing and if I decide I want to fully complete the tutorial and understand OpenGL even more I'll text you again :).
      Cheers

    • @GamesWithGabe
      @GamesWithGabe  Před 2 lety

      Hey Henri it's no problem at all! Here's a link to a community project with the static indexing github.com/azurite-engine/Azurite/blob/65f99bd783bf87fd475005adc0c166c065c63f51/src/assets/shaders/default.glsl#L43 . You can just replace the code where you sample a texture with this and it should work alright. Best of luck with university!

    • @henribebo
      @henribebo Před 2 lety

      @@GamesWithGabe Thank you very much!!

  • @13a1Vana
    @13a1Vana Před 2 lety +1

    I have a strange bug where I get solid color bars instead of my image. It looks like I only have like 4 points of color. I'm not sure what I'm doing wrong

    • @GamesWithGabe
      @GamesWithGabe  Před 2 lety

      It sounds like you may have a mismatch of color components in your texture vs the actual image. I would make sure you're actually using rgba if the color has 4 components, or rgb if it only has 3 when you do glTexImage :)

    • @13a1Vana
      @13a1Vana Před 2 lety

      @@GamesWithGabe I couldn't figure it out for the longest time but I finally came back to it after setting it aside. Turns out I number of dimensions wrong for my Texture coordinates. I was using 1 instead of 2 and that obviously causes everything to be misaligned. Whew that took me longer than it should have, but thanks for the help though :)

  • @Daniel-eu4uh
    @Daniel-eu4uh Před 4 lety

    do you program a 2d game later?

    • @GamesWithGabe
      @GamesWithGabe  Před 4 lety +1

      Yea, we will be building Mario later with the engine

    • @Daniel-eu4uh
      @Daniel-eu4uh Před 4 lety

      @@GamesWithGabe cool, i am currently trying to program a 2d game with java and eclipse

    • @GamesWithGabe
      @GamesWithGabe  Před 4 lety +1

      Nice! I hope development goes well!

  • @LuminaryGames
    @LuminaryGames Před 2 lety

    Any reason you didn't just use int texId = textures.indexOf(sprite.getTexture)