MarkelZ / pygame-light2d

Fast 2D dynamic lighting engine for Pygame.
MIT License
16 stars 0 forks source link

Problems with mac? #1

Closed OlaKenji closed 10 months ago

OlaKenji commented 10 months ago

You have made an amazing work! I made it work on my windows but I have trouble with my mac, and I get this error:

fragment_shader

ERROR: 0:1: '' : version '450' is not supported ERROR: 0:1: '' : syntax error: #version ERROR: 0:2: '' : #version required and missing. ERROR: 0:11: '1' : syntax error: integers in layouts require GLSL 140 or later

I looks like it may be something with the modernGL but not sure. Do you know what it may be?

the code:

`import pygame import pygame_light2d as pl2d from pygame_light2d import LightingEngine, PointLight, Hull

Initialize pygame

pygame.init() pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MAJOR_VERSION, 3)#has to be before set_mode for mac pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MINOR_VERSION, 3)#has to be before set_mode for mac pygame.display.gl_set_attribute(pygame.GL_CONTEXT_PROFILE_MASK,pygame.GL_CONTEXT_PROFILE_CORE)#has to be before set_mode for mac

Set 720p screen resolution

screen_res = (1280, 720) pygame.display.set_mode(screen_res)

Create a lighting engine

native_res = (320, 180) lights_engine = LightingEngine( native_res=native_res, lightmap_res=native_res)

Set the ambient light to 50%

lights_engine.set_ambient(128, 128, 128, 128)

Load two sprites

sprite = lights_engine.load_texture('assets/dragon.png')

Create and add a light

light = PointLight(position=(0, 0), power=1., radius=250) light.set_color(50, 100, 200, 200) lights_engine.lights.append(light)

Create and add a hull

vertices = [(125, 50), (200, 50), (200, 125), (125, 125)] hull = Hull(vertices) lights_engine.hulls.append(hull)

Game loop

clock = pygame.time.Clock() running = True while running:

Tick the clock at 60 frames per second

clock.tick(60)

# Mouse pointer's position on the screen
mp = pygame.mouse.get_pos()
# Convert to native coordinates
mouse_native_pos = [mp[0]*native_res[0]/screen_res[0],
                    mp[1]*native_res[1]/screen_res[1]]
# Assign the mouse pointer's position to the light
light.position = mouse_native_pos

# Clear the background with black color
lights_engine.clear(255, 255, 255)

# Render sprite1 in the background
lights_engine.render_texture(
    sprite, pl2d.BACKGROUND,
    pygame.Rect(40, 50, sprite.width, sprite.height),
    pygame.Rect(0, 0, sprite.width, sprite.height))

# Render sprite2 in the foreground
lights_engine.render_texture(
    sprite, pl2d.FOREGROUND,
    pygame.Rect(210, 50, sprite.width, sprite.height),
    pygame.Rect(0, 0, sprite.width, sprite.height))

# Render the scene
lights_engine.render()

# Update the display
pygame.display.flip()

# Process events
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

`

MarkelZ commented 10 months ago

@OlaKenji Thank you so much for the feedback!

Could you please let me know which changes you made to make it work on Windows?

For the issue with Mac, the problem is actually your OpenGL version. The shaders need OpenGL 4.5 but you probably do not have it installed. You could install 4.5 but it seems like Mac has very limited support for newer OpenGL versions.

Instead, I made a new branch and switched all the code to OpenGL 3.3:

https://github.com/MarkelZ/pygame-light2d/tree/glsl-v330-switch

Your Mac probably has OpenGL 3.3. The code runs fine on my linux machine and it would be very helpful if you could test it on your Mac.

You can follow these steps:

  1. Uninstall pygame-light2d so that it won't interfere with the testing:

    pip3 uninstall pygame-light2d
  2. Clone the branch:

    git clone -b glsl-v330-switch https://github.com/MarkelZ/pygame-light2d.git
  3. You cannot directly run the examples because you don't have the package installed anymore. Just copy all the contents of the examples/ folder to src/. So, you should have all three example*.py files and the assets/ folder inside of src/.

  4. Enter src/ and run the examples:

    cd src/
    python3 example1.py

Please tell me if it works! If it runs fine I'll update the package with the new code.

Thank you :)

OlaKenji commented 10 months ago

Thanks for all the detailed explanations. You are right, It works nicely now even on my mac. However, I did 3 modifications to make it work. In your example1.py:

1, pygame.display.set_mode(screen_res) -> pygame.display.set_mode(screen_res,pygame.HWSURFACE | pygame.OPENGL | pygame.DOUBLEBUF)

2, I commented row 32 in engine.py

3, I needed to add these lines after pygame.init() but before pygame.display.set_mode() in example.py

pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MAJOR_VERSION, 3) pygame.display.gl_set_attribute(pygame.GL_CONTEXT_MINOR_VERSION, 3) pygame.display.gl_set_attribute(pygame.GL_CONTEXT_PROFILE_MASK,pygame.GL_CONTEXT_PROFILE_CORE)

I should clarify that your example.py files were working as is for my windows machine (even for OpenGL 4.5). :D but these changes were needed for my mac. But the code still works on my Windows even with having these changes implemented.

MarkelZ commented 10 months ago

@OlaKenji Those are such good news! Thanks again for the feedback!!!

I merged the branch with main and published it as version 1.2.0 on PyPI. By installing the package through pip it should now use OpenGL 3.3. It's still not compatible with Mac out-of-the-box because it's missing the three modifications that you mention.

Your modifications were very useful and I created two new branches based on them: https://github.com/MarkelZ/pygame-light2d/tree/mac_support_test1 https://github.com/MarkelZ/pygame-light2d/tree/mac_support_test2

They both run fine on Linux. Could you please check if any of them works on Mac? You can follow the 4 steps from my previous reply.

It is more desirable if test1 runs well, because it has minor changes and test2 has some major changes.

In case they both don't work, could you please fork the repository and make the changes to the engine yourself to have it work on Mac? The changes should be done to the engine, for instance the gl_set_attribute stuff would probably go in LightingEngine._check_and_configure_pygame, and the examples should require as few changes as possible (ideally zero). Then, you can create a pull request, and after we make sure it runs on Windows and Linux I would merge it with the main branch and publish it on PyPI.

OlaKenji commented 10 months ago

Great! :D

I tested both of them. Test2 works as is but test1 didn't. For test1, I just get a black screen (so no errors but some rendering problem it seems). The reason is probably how the display is setup? For me, it looks like pygame.set_mode() is run twice for test1 (once in example and once in engine). I read somewhere that gl_set_attribute stuff need to be set before the display.set_mode. So perhaps it is a problem because set_mode is ran once already in example.py?

Since test2 is working, would you still like me to fork them?

MarkelZ commented 10 months ago

@OlaKenji I suspected the exact same thing! That's why test2 does pygame.set_mode() only once, after gl_set_attribute.

I just merged the test2 branch with the main branch and published it on PyPI as version 2.0.0. It's a major version because it breaks backwards compatibility.

Could you please check that it still works? You can install it with pip, make sure it's version 2.0.0, and check that the engine still works. If it's good I'll close this issue.

And about forking the repo, if you notice that you can improve the source code (not only regarding Mac support, it could be anything) feel free to fork the project. Also if you want to implement a new feature you can open an issue and I'll be happy to discuss it.

Thank you for collaborating and being so helpful! :)

OlaKenji commented 10 months ago

Sounds good! I just tested the main branch it seems to be working as is. :D

btw, regarding suggestions/improvements. I think the current light engine is very nicely written. But, one thing to consider is if it is possible to upgrade it to a general "shader engine". For example, if the user would like to add a costume shader, the user have to go into the source and modify it so that it reads in the costume shader files and add some lines for the rendering.

If on the other hand one could split your light_engine into 1) light shader part and 2) shader engine, the shader engine could handle the light shader stuff and also any other user input costume shader.

This is probably an even bigger revision though and I don't have a good grasp on OpenGL or ModernGL (yet) so I don't know how easy it is to make something like this. But perhaps something to have in mind. :)

MarkelZ commented 10 months ago

@OlaKenji Thanks for testing it! I will close this issue now.

Your shader engine idea is very interesting, do you mind creating a new issue in this repo so that we can discuss it in detail?