How I Added Shaders to Pygame

Поделиться
HTML-код
  • Опубликовано: 12 июн 2024
  • Shaders aren't natively supported in Pygame and I advised against trying to use them in the past, but it's not impossible to use shaders with Pygame. Recently I added shaders to my Pygame project by mixing PyOpenGL with Pygame.
    Music:
    Adhesive Wombat - Night Shade
    • AdhesiveWombat - Night...
    Patreon (get access to Vaegrant's source code!):
    / dafluffypotato
    My Discord Server:
    / discord
    Shader Code:
    github.com/DaFluffyPotato/pyg...
    0:00 - so many questions
    0:32 - why Pygame doesn't natively support shaders
    2:10 - mixing Pygame and OpenGL
    3:06 - getting the most out of Pygame while using shaders
    3:36 - my implementation of shaders
    3:56 - shaders in Aeroblaster
    4:25 - Vaegrant, my code, and final notes
    5:30 - how to learn about OpenGL with Pygame
    5:42 - thanks
    #gamedev #python #pygame
  • НаукаНаука

Комментарии • 59

  • @DaFluffyPotato
    @DaFluffyPotato  Год назад +18

    Get access to Vaegrant's source code (+shaders) and maybe pay for my lunch? 😳
    www.patreon.com/dafluffypotato
    Also, there’s a mistake at 4:20. I may have been talking to someone in Discord at the time and not noticed that I highlighted an unrelated section of code from a file that wasn’t even the associated shader. lol
    The base shader functionality is open source though:
    github.com/DaFluffyPotato/pygame-shaderlib

  • @leonstansfield
    @leonstansfield Год назад +45

    I am constantly amazed at your work... just knowing the background footage was made with pygame is amazing

  • @charliereviews7448
    @charliereviews7448 2 месяца назад

    I enjoy periodically coming back to your videos. Every time I see one pop up on my feed, I think, what's Da Fluffy Potato been up to in Pygame now, and im always impressed by how much content you will have released in the meantime to binge.

  • @Jay1001
    @Jay1001 Год назад +2

    I would like to personally thank you for uploading your code for this. I know you have a more up to date video using modernGL, and that this code probably isn't for everyone, but honestly this code was exactly what I needed. I have an existing pygame game and was looking to add a few openGL features to it but wanted precise control over the integration of GL and was having a HELL of a time getting ANYTHING to run, and I literally was frustrated in a way I had never been before. Then I came across this video, downloaded your code, and after a little tweaking got it to run and was finally able to start playing around with and testing everything. I got a shader programmed up for my needs and I don't know if I ever would've made it this far without you. Thanks, thanks, thanks. You da man :)

  • @ShotgunLlama
    @ShotgunLlama Год назад +17

    I ran a test once comparing pygame's software blit vs opengl rendering a quad to draw many copies of an image to the screen with random sizes and positions. With any large number of images to draw, opengl was 1-2 orders of magnitude faster than the standard blit function even when each instance of the image was a separate vertex array. I'm using opengl for the pygame project I'm currently working, but I'm using the moderngl wrappings instead of PyOpenGL

    • @michaelpalmer2143
      @michaelpalmer2143 Год назад +1

      When I switched from software surfaces to pygame._sdl2 GPU rendering my CPU usage was reduced from 7% to 0.5% or less. And this simple wrapper is far easier than the opengl wrappers, while also letting you use the directx or vulkan backend of SDL2 instead of opengl.

    • @undeadpresident
      @undeadpresident Год назад

      So I suppose it takes more work to get all the images rendered and placed correctly, but then you get much better performance and can use shaders without it running super slow?

  • @Skeffles
    @Skeffles Год назад

    Great video. I wasn't aware of any pygame games that use this so it's cool to know it's possible.

  • @RC.TheFirst
    @RC.TheFirst 4 месяца назад

    Since I'm Supposed To Learn C++, I Won't Learn Pygame Anymore, But You Fill Me With So Much Inspiration, Thx So Much!!!

  • @zicraftian
    @zicraftian Год назад

    Dude you are a big inspiration for me when it comes to learning python

  • @iamtheprarieking7137
    @iamtheprarieking7137 Год назад +1

    I was wondering if you have a public version of the current engine you've made?
    Love your videos!

  • @lodybaguette2487
    @lodybaguette2487 Год назад

    Hey , for your dialog system , I think if you calculate before how words will appear avoid words warped at the end line , anyways good job bro

  • @giabaonguyenvan3279
    @giabaonguyenvan3279 Год назад +1

    I can feel how happy you are because of the update.

  • @dclxviclan
    @dclxviclan Год назад

    Cool style, i love this

  • @ghostsakai4442
    @ghostsakai4442 Год назад

    Live your work dude

  • @illusealrr1006
    @illusealrr1006 11 месяцев назад

    Thanks for the video. I have my own game engine written with c++/SDL2.0. With the game mostly finished, I decide that I want to use a bloom shader. I guess that what I need to do is just convert the SDL texture for main screen to opengl like you do.

  • @bmorr
    @bmorr Год назад +1

    I spent a few weeks in the fall of 21 trying to implement shaders in pygame for a fireworks sandbox, and I thought I reached a dead end cus I really didn’t want to rewrite the whole program to use OpenGL rendering. I was only doing this as a month long project and the time constraint meant it wasn’t worth it to switch to OpenGL.
    I will have to implement this bypass into my code and then I can finally make my fireworks have proper bloom in real-time!!!

  • @AngelGonzalez-yy8dh
    @AngelGonzalez-yy8dh Год назад +5

    Good video

  • @ech0_exe243
    @ech0_exe243 Год назад

    just… WOW! It’s soooo amazing

  • @Avaxar
    @Avaxar Год назад +6

    How about using ModernGL? Wouldn't it be a more friendly library (Basically a pythonic wrapper for OpenGL) than raw direct PyOpenGL, which directly ports every function there is from C's OpenGL?

    • @DaFluffyPotato
      @DaFluffyPotato  Год назад +4

      Blubberquark was recommending that. That's not what I did because I wasn't aware of it at the time, but that would probably be easier.

  • @zsirothehero4330
    @zsirothehero4330 Год назад

    Hi DafluffyPotato. Your work is wonderful no doubt about that :) I just want to ask if you have a tutorial on how to make sprite I'm really bad at drawing or even designing. If you have a suggestion on where should I start. That would be greatly appreciated. Again, thanks for the work you've done!!

    • @DaFluffyPotato
      @DaFluffyPotato  Год назад

      ruclips.net/video/4cYAkHfh9wM/видео.html

  • @petthepotat
    @petthepotat Год назад

    your huge brain never ceases to amazes. Keep up the great work Mr potato master >:D

  • @Willerhide
    @Willerhide Год назад

    @Smelly Frog used an OpenGL texture to add Steam Overlay to his pygame game. Honestly worth checking out if you ever wanted to add it to your own games at some point.

    • @DaFluffyPotato
      @DaFluffyPotato  Год назад

      I saw that. The steam overlay only works with OpenGL, so you kind of have to do that for it to work.

  • @t3st3d
    @t3st3d Год назад

    This is very impressive

  • @leobozkir5425
    @leobozkir5425 Год назад

    Could you make a video on how to convert a pygame surface to an opengl texture and render that to the opengl display? If not no problem, but I think other people might be interested into that as well perhaps. And as a side note, how did you learn all this? Through documentation or any other online sources?

    • @undeadpresident
      @undeadpresident Год назад

      Everyone I've seen do it uses the pygame.image.tostring() function (which is slow af) then glTexImage2D function and I think it may not feasible unless you have low-res graphics or create all the textures ahead of time and do all the graphics through opengl and not try to mix the two.

  • @boxhead-zk7sn
    @boxhead-zk7sn 25 дней назад

    great video love what your doing, i have a question what does the ftexcoords stand for in 4:18 cause i try to copy the CRT tv style in my game, it will help if you told me

  • @CloudlessStudio
    @CloudlessStudio Год назад

    I’ve been using both for simple 3D renders

  • @itsmemattagain9841
    @itsmemattagain9841 Год назад

    You reckon it would be possible to make a 2d tile game online with a decent performance in python?

  • @josuebarros5727
    @josuebarros5727 11 месяцев назад

    This is incredible! Can you create a Quake Clone with OpenGL that renders oficial Quake BSP maps and TrenchBroom maps?

  • @AngelGonzalez-vp2en
    @AngelGonzalez-vp2en Год назад

    You could use this lighting thing for fire particles but it's probably super slow.
    It's based off your particles video, removing during iteration video, and lighting in pygame video:
    import pygame
    import sys
    import random
    from pygame.locals import *
    pygame.init()
    Clock = pygame.time.Clock()
    Screen = pygame.display.set_mode((800, 700))
    pygame.display.set_caption("Base")
    particles = []
    particle_colors = [(127, 63, 15), (127, 63, 31), (127, 63, 0)]
    click = False
    def glow(radius, color):
    glow_surf = pygame.Surface((radius * 2, radius * 2))
    pygame.draw.circle(glow_surf, color, (radius, radius), radius)
    glow_surf.set_colorkey((0, 0, 0))
    return glow_surf
    while True:
    Screen.fill((0, 0, 0))
    mx, my = pygame.mouse.get_pos()
    if click:
    particles.append([[mx, my], [random.randint(3, 10) / 5, random.randint(6, 15) / 3], random.randint(6, 12), random.choice(particle_colors)])
    for i, particle in sorted(enumerate(particles), reverse=True):
    particle[0][0] += particle[1][0]
    particle[0][1] -= particle[1][1]
    particle[0][1] += 1
    particle[2] -= 0.1
    if particle[2] 9:
    color = (255, 255, 255)
    elif particle[2] > 6:
    color = (224, 112, 61)
    elif particle[2] > 3:
    color = (112, 61, 30)
    else:
    color = (56, 23, 11)
    # pygame.draw.circle(Screen, color, (int(particle[0][0]), int(particle[0][1])), int(particle[2])) #will look ugly with this
    radius = int(particle[2]) * 2
    Screen.blit(glow(radius, (particle[3])), (int(particle[0][0]), int(particle[0][1])), special_flags=BLEND_RGB_ADD)
    for i in range(3):
    copy = Screen.copy()
    copy.set_colorkey((0, 0, 0))
    copy.set_alpha(127)
    Screen.blit(copy, (i * 2, i * 2))
    for event in pygame.event.get():
    if event.type == QUIT:
    pygame.quit()
    sys.exit()
    if event.type == KEYDOWN:
    if event.key == K_q:
    pygame.quit()
    sys.exit()
    if event.type == MOUSEBUTTONDOWN:
    if event.button == 1:
    click = True
    if event.type == MOUSEBUTTONUP:
    if event.button == 1:
    click = False
    pygame.display.update()
    Clock.tick(60)

  • @RoRaresGaming
    @RoRaresGaming Год назад

    Can u make a video about saving/loading ?

  • @dclxviclan
    @dclxviclan Год назад

    Super, awe$ome

  • @FollowNdFeel
    @FollowNdFeel Год назад

    Your game is looking incredible.
    So…. I’m on episode 2 of the devlog so this might not work for you… catching up.
    Have buffs in the game that make you really strong but are unusable. Pair those with debuffs to make it usable. I.e. +100% run speed(making you zip back and forth on the screen) and then get something that does more dmg but slows you down.

  • @fada9238
    @fada9238 Месяц назад

    Dude, the game in the background is PyGame...?
    If so, I would like to ask: how to make such exact collisions?
    All my attempts to do at least something that we see in game engines have not been successful, it just doesn't work correctly and with terrible errors...

  • @pccoder9547
    @pccoder9547 Год назад

    I use c++/sdl2 for mini projects and unity for big games

  • @riyamandal7667
    @riyamandal7667 Год назад

    Can u pls make a tutorial on this topic?

  • @nj0086
    @nj0086 Год назад

    yayy

  • @electricimpulsetoprogramming
    @electricimpulsetoprogramming Год назад

    Is programming hard? I feel very dumb trying to learn it.

  • @spenceabeen
    @spenceabeen Год назад +1

    Gonk :o

  • @bobdillon1138
    @bobdillon1138 Год назад

    Why would you dig a hole with a teaspoon.

  • @noobintheinternet
    @noobintheinternet Год назад +1

    Hello Fluffy! Please make new video.

  • @HalfAsleepSam
    @HalfAsleepSam Год назад

    you could always make your own library
    wait you already did that

  • @rudyfaile
    @rudyfaile Год назад

    the best way to learn is by teaching :)

    • @DaFluffyPotato
      @DaFluffyPotato  Год назад +1

      Yes, but putting questionable content out there isn’t good for the people trying to learn from it.

    • @rudyfaile
      @rudyfaile Год назад

      @@DaFluffyPotato yeah I'm not suggesting you publish guesswork, but as you're refining your tutorial I'm sure you'd master the concepts

  • @owaisoumera7624
    @owaisoumera7624 Год назад +1

    second

  • @304nokia
    @304nokia Год назад +1

    I'm suggesting to try Ebitengine. It very similar to SDL2 but with fragment shader support out of the box. Also Python is too bad for multi-platform development.

  • @michaelpalmer2143
    @michaelpalmer2143 Год назад +3

    I use pygame._sdl2.video for GPU rendering without using pyopengl at all. In fact I even converted Aeroblaster to use this GPU rendering. This way I can run games at full HD resolution and work with SDL2 Textures from within pygame. I wonder if I can combine pygame._sdl2.video with shaders?
    I don't know why everyone ignores the experimental GPU rendering in pygame. This is the only way to get good performance on underpowered devices, such as Linux or Android retro handhelds.

    • @DaFluffyPotato
      @DaFluffyPotato  Год назад

      I haven’t used it since it’s still sorta undocumented (at least the last time I checked). I believe you should be able to use shaders on SDL2’s textures. Not sure if that’s implemented though.

    • @michaelpalmer2143
      @michaelpalmer2143 Год назад

      @@DaFluffyPotato It is undocumented except for the docstrings. I just run 'pydoc -b' and use my browser to navigate to the pygame.sdl2.video documentation.