Giter Club home page Giter Club logo

pygame_shaders's Introduction

Pygame Shaders

wakatime Lines of code

Downloads PyPI PyPI - Format Downloads

Easily integrate shaders into your new or existing pygame projects

This project allows for GLSL shaders to easily be intergrated with either your new or existing Pygame projects without having to touch OpenGL.

import pygame
import pygame_shaders

pygame.init()

clock = pygame.time.Clock()

#Create an opengl pygame Surface, this will act as our opengl context.  
screen = pygame.display.set_mode((600, 600), pygame.OPENGL | pygame.DOUBLEBUF)

#This is our main display we will do all of our standard pygame rendering on.
display = pygame.Surface((600, 600))

#The shader we are using to communicate with the opengl context (standard pygame drawing functionality does not work on opengl displays)
screen_shader = pygame_shaders.DefaultScreenShader(display) # <- Here we supply our default display, it's this display which will be displayed onto the opengl context via the screen_shader

#This is our shader object which we can use to render the given shaders onto the screen in various ways. 
shader = pygame_shaders.Shader(pygame_shaders.DEFAULT_VERTEX_SHADER, "fragment.glsl", screen) #<- Because we plan on using this shader for direct rendering (we supply the surface on which we plan to do said direct rendering in this case, screen) 

while True:
    #Fill the display with white
    display.fill((255, 255, 255))
    
    #Standard pygame event stuff
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    #Render a rect onto the display using the standard pygame method for drawing rects.
    pygame.draw.rect(display, (255, 0, 0), (200, 200, 20, 20))
    
    #Render the contents of "display" (main surface) onto the opengl screen.
    screen_shader.render() 

    #Render the shader directly onto the display.
    shader.render_direct(pygame.Rect(0, 0, 100, 100)) 

    #Update the opengl context
    pygame.display.flip()
    clock.tick(60)

pygame_shaders's People

Contributors

scriptlinestudios avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pygame_shaders's Issues

[bug]the surface read from Shader.render lost its alpha value[solution provided]

Describe the bug
the surface returned by Shader.render method is always opaque (all pixels' alpha value are always 255)

To Reproduce
1.copy the code provided below, paste it into a file
2.put an image file with alpha values next to your source file
3.replace "{your_image}" in the provided source with the name of your image
4.run the source

(you can also try to change the alpha value of target_surface in glsl source, this bug also happens)

the source code is borrowed from file examples/water/main.py

import pygame
import pygame_shaders

pygame.init()
clock = pygame.time.Clock()
pygame.display.set_mode((600, 600), pygame.OPENGL | pygame.DOUBLEBUF)

display = pygame.Surface((600, 600))

screen_shader = pygame_shaders.DefaultScreenShader(display) # <- Here we supply our default display, it's this display which will be displayed onto the opengl context via the screen_shader

target_surface = pygame.image.load({your_image}), (0, 0))
shader = pygame_shaders.Shader(
    pygame_shaders.DEFAULT_VERTEX_SHADER, 
    pygame_shaders.DEFAULT_FRAGMENT_SHADER, 
    target_surface
) #<- give it to our shader

while True:
    display.fill((0, 255, 255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            
    #our tested code
    display.blit(shader.render(), (0, 0))

    screen_shader.render() 

    pygame.display.flip()
    clock.tick(30)

Expected behavior
Assume the 1x1 image have a pixel with alpha 140, if you didn't modify its alpha in shader, the alpha of returned pixel should also be 140.

solution
In file pygame_shaders.py line 160, just replace self.framebuffer.read() with self.framebuffer.read(components=4)
and then add an 'A' to the end of the string "RGB".
at last it looks like this:

surf​ ​=​ ​pygame​.​image​.​frombuffer​(​self​.​framebuffer​.​read​(components=4), ​self​.​target_surface​.​get_size​(), ​"RGBA"​)

(I edit this issues in my phone, so I'm sorry that if there's any issue)

File "C........Python312\Lib\site-packages\moderngl\__init__.py", line 1647, in _vertex_array attribs = [types[x] if type(x) is int else types[locations[x]] for x in attribs] ^^^ KeyError: 'vertexTexCoord'

The following error persists when you try to output a color using:-

#version 330 core
in vec3 fragmentColor;
in vec2 fragmentTexCoord;

uniform sampler2D imageTexture;

out vec4 color;
void main()
    {    color = vec4(1.0);       // White color   }

// Should've produce white screen

In short, you cant get an output rather than the texture fed to the shader (the screen).
It worked in previous versions

Thank you!

Wow! So cool!👍
But in examples:
hello world shader didn`t work
and bg shader error
Скриншот 27-10-2022 19 19 45

rendering fails when done command is called through a thread

Describe the bug
rendering fails when done command is called through a thread

To Reproduce
Steps to reproduce the behavior:
put main function on thread fails to render the anything.

Expected behavior
something to render

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • windows

Increasing memory useage.

Running a simple test program with this, I've noticed it using more and more memory constantly.

In visual studio code I monitored the program performance with resource monitor.
shader screenshot

Please ask if you need me to provide any more information.

ValueError: Requested OpenGL version 330, got version 0

Describe the bug

I tried to run the first example from the README and I get the following:

raise ValueError('Requested OpenGL version {0}, got version {1}'.format(
ValueError: Requested OpenGL version 330, got version 0

I am able to use moderngl by itself and render stuff, and I verified I have OpenGL engine 4.1 (410).

But when I comment out the check, I get version '330' is not supported, so I am inclined to believe that this lib is unable to find my OpenGL?

I am not sure! I have to admit I am a noob when it comes to OpenGL stuff.

To Reproduce

  1. Steal my mac
  2. Use it to follow README

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.