MslEngr
Posts: 2
Joined: Sun Nov 10, 2019 6:22 am

Open GL 2.1 on RPi 4

Fri Nov 15, 2019 9:35 pm

Hi Everyone,

I wrote a flight simulator in Python on a Linux machine that uses OpenGL and PiGame for its 3D graphics. I want to get this same program running on my Raspberry Pi 4. I understand that unlike previous Pi versions that only supported OpenGL ES, the Pi 4 supports OpenGL 2.1. When I try and run my script it dies and gives various errors about OpenGL not being compatible. I’ve installed the Mesa toolbox and confirmed OpenGL 2.1 is recognized and supported. I’ve also changed the pi.config to use the OpenGL driver. What else am I missing? It says it can’t compile shaders and it doesn’t support the GL version I have. I’m hoping for some help and direction. Thank you.

User avatar
Gavinmc42
Posts: 4068
Joined: Wed Aug 28, 2013 3:31 am

Re: Open GL 2.1 on RPi 4

Sat Nov 16, 2019 8:49 am

See if you can get these working.
https://github.com/Blakkis/GLSL_Python

Change

Code: Select all

#version 330 core
to

Code: Select all

#version 300 es
I suspect you just have the wrong version set.

Pi4 is OpenGL2.1 but it is running on OpenGLES 3.0 hardware, so the shader compiler needs to know that.
Pyglet and PyOpenGL are a bit newer than Pygame.
I have tested all three and they work.

OpenGLES 3.1 is mostly working but not released yet?
The hardware is 3.2 capable and there are people working on the driver.
There was a blog about this
Keep your eye one the mesa3d commits
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

User avatar
paddyg
Posts: 2440
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Open GL 2.1 on RPi 4

Sat Nov 16, 2019 8:49 am

Quite a few things without code or errors to look at. You might glean something by looking at pi3d shaders and the Shader class which does some parsing (near end of file) Between GL21 and GLES20 I think the only difference was #version and //precision GLES30 had in and out types to sub for varying and gl_FragColor
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
Gavinmc42
Posts: 4068
Joined: Wed Aug 28, 2013 3:31 am

Re: Open GL 2.1 on RPi 4

Sat Nov 16, 2019 9:49 am

This explains the current status better than I can.
https://www.raspberrypi.org/blog/vc4-an ... an-update/
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

MslEngr
Posts: 2
Joined: Sun Nov 10, 2019 6:22 am

Re: Open GL 2.1 on RPi 4

Fri Dec 06, 2019 3:33 am

Gavinmc42 wrote:
Sat Nov 16, 2019 8:49 am
See if you can get these working.
https://github.com/Blakkis/GLSL_Python

Change

Code: Select all

#version 330 core
to

Code: Select all

#version 300 es
I suspect you just have the wrong version set.

Pi4 is OpenGL2.1 but it is running on OpenGLES 3.0 hardware, so the shader compiler needs to know that.
Pyglet and PyOpenGL are a bit newer than Pygame.
I have tested all three and they work.

OpenGLES 3.1 is mostly working but not released yet?
The hardware is 3.2 capable and there are people working on the driver.
There was a blog about this
Keep your eye one the mesa3d commits
I changed the version on each of the three files and tried compiling them. In each caraymarch_setup.pyse i got errors. I've included the error outputs for the minimal_setup.py and the raymarch_setup.py below. The issue seems very similar to what i described in my original post.

python minimal_setup.py
pygame 1.9.4.post1
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "minimal_setup.py", line 116, in <module>
Main().mainloop()
File "minimal_setup.py", line 60, in __init__
self.fragment_shader = shaders.compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER)
File "/home/pi/.local/lib/python2.7/site-packages/OpenGL/GL/shaders.py", line 226, in compileShader
shaderType,
RuntimeError: ('Shader compile failure (0): 0:2(10): error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.00 ES, and 3.00 ES\n', ['\n#version 330 core\n\n#define fragCoord gl_FragCoord.xy\n\nuniform vec2 iMouse;\nuniform float iTime;\nuniform vec2 iResolution;\n\nout vec4 fragColor;\n\nvoid main()\n{\n // Set origin to center of the screen\n vec2 uv = fragCoord/iResolution.xy * 2.0 - 1.0;\n // Fix aspect ratio\n uv.x *= iResolution.x / iResolution.y;\n\n // Time varying pixel color (Copied from ShaderToy default scene)\n vec3 color = 0.5 + 0.5 * cos(iTime + uv.xyx + vec3(0.0, 2.0, 4.0));\n\n fragColor = vec4(color, 1.0);\n}\n'], GL_FRAGMENT_SHADER)

python raymarch_setup.py
1.9.4.post1
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "raymarch_setup.py", line 193, in <module>
Main().mainloop()
File "raymarch_setup.py", line 129, in __init__
self.fragment_shader = shaders.compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER)
File "/home/pi/.local/lib/python2.7/site-packages/OpenGL/GL/shaders.py", line 226, in compileShader
shaderType,
RuntimeError: ('Shader compile failure (0): 0:2(10): error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.00 ES, and 3.00 ES\n', ['\n#version 330 core\n\n#define fragCoord gl_FragCoord.xy\n\nuniform vec2 iMouse;\nuniform float iTime;\nuniform vec2 iResolution;\n\nout vec4 fragColor;\n\n\nfloat sdSphere(vec3 p, float r)\n{\n return length(p) - r;\n}\n\n\nfloat map_the_world(in vec3 pos)\n{\n float sphere_0 = sdSphere(pos, 2.5);\n\n return sphere_0;\n}\n\n\nvec3 calculate_normal(in vec3 pos)\n{\n const vec3 small_step = vec3(0.001, 0.0, 0.0);\n\n float gradient_x = map_the_world(pos + small_step.xyy) - map_the_world(pos - small_step.xyy);\n float gradient_y = map_the_world(pos + small_step.yxy) - map_the_world(pos - small_step.yxy);\n float gradient_z = map_the_world(pos + small_step.yyx) - map_the_world(pos - small_step.yyx);\n\n vec3 normal = vec3(gradient_x, gradient_y, gradient_z);\n\n return normalize(normal);\n}\n\n\nvec3 ray_march(in vec3 ro, in vec3 rd)\n{\n float total_distance_traveled = 0.0;\n const int NUMBER_OF_STEPS = 128;\n\n const float MINIMUM_HIT_DISTANCE = 0.001;\n const float MAXIMUM_TRACE_DISTANCE = 512.0;\n const float AMBIENT = 0.0;\n\n for (int i = 0; i < NUMBER_OF_STEPS; ++i)\n {\n vec3 current_position = ro + total_distance_traveled * rd;\n\n float distance_to_closest = map_the_world(current_position);\n\n if (distance_to_closest < MINIMUM_HIT_DISTANCE) \n {\n vec3 normal = calculate_normal(current_position);\n vec3 light_position = vec3(-iMouse.x, iMouse.y, 4.0);\n vec3 direction_to_light = normalize(current_position - light_position);\n\n float diffuse_intensity = max(AMBIENT, pow(dot(normal, direction_to_light), 16.0));\n\n return vec3(1.0, 0.0, 0.0) * diffuse_intensity;\n }\n\n if (total_distance_traveled > MAXIMUM_TRACE_DISTANCE){\n break;\n }\n total_distance_traveled += distance_to_closest;\n }\n return vec3(0.0);\n}\n\n\nvoid main()\n{\n vec2 uv = fragCoord / iResolution.xy * 2.0 - 1.0;\n uv.x *= iResolution.x / iResolution.y;\n\n vec3 camera_position = vec3(0.0, 0.0, -5.0);\n vec3 ray_origin = camera_position;\n vec3 ray_direction = vec3(uv, 1.0);\n\n vec3 result = ray_march(ray_origin, ray_direction);\n\n fragColor = vec4(result, 1.0);\n}\n'], GL_FRAGMENT_SHADER)

User avatar
paddyg
Posts: 2440
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Open GL 2.1 on RPi 4

Fri Dec 06, 2019 11:09 am

Looks like you haven't changed that first line of your fragment shader (you have to fix the vertex and fragment shaders. i.e. two edits) as per @Gavinmc42's suggestion. Try that and see what you get.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
Gavinmc42
Posts: 4068
Joined: Wed Aug 28, 2013 3:31 am

Re: Open GL 2.1 on RPi 4

Sat Dec 07, 2019 10:51 am

I got a similar error, the clue was this.
GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.00 ES, and 3.00 ES\n',
use version 300 es

Every Mesa upgrade I keep checking glxinfo for ES 3.1, that could happen anytime.
I don't know how to use the GLX extension, lots of those now.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Return to “Graphics programming”