• Stars
    star
    176
  • Rank 216,987 (Top 5 %)
  • Language
    C
  • License
    The Unlicense
  • Created over 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Raspberry Pi OpenGL ES 2 without an X server (using EGL)

Raspberry Pi OpenGL ES 2 without an X server (using EGL)

Have you ever wanted to render things with headless (no screen) Raspberry Pi? Then this is something you might be interested in!

The following file triangle.c contains an example that renders a triangle and saves it into a output.raw file. The example uses EGL to create a pixel buffer as a surface. Normally, when using X server, you would create a window surface, but in here we do not do that at all. This example also uses OpenGL ES 2 to setup a very simple shader to render a triangle.

The triangle.c contains comments which should explain you all the necessary parts. Please note that this example will not teach you fundamentals of OpenGL! The purpose of this file is solely to demonstrate creating an OpenGL ES 2 context and getting the raw pixel output without using any virtual Linux framebuffers or physical screen (no X server).

Which Raspberry Pi works?

  • Raspberry Pi 1 (tested and works)
  • Raspberry Pi 2 (tested and works)
  • Raspberry Pi 3 (tested and works)
  • Raspberry Pi 4 (tested and works, but won't work in headless mode, you need a HDMI output works just fine in the headless mode, no longer true, see issue #11)

Raspberry Pi 1,2,3

For the Raspberry Pi 4 instructions see the next section

What do I need?

You need a GCC compiler, EGL, and GLES libraries. If you are using the latest Raspbian distro, all those are already located on the image, no extra apt-get needed. You can check if you have the GCC installed by executing gcc --version. You can also check if the GLES and EGL are installed by executing ls /opt/vc/lib which should list libbrcmEGL.so and libbrcmGLESv2.so. They are all already included in the Jessie/Stretch/Buster Raspbian! If you don't have libbrcmEGL.so, you will have to use the libEGL.so instead, which is located in the same folder.

The problem of mesa apt package

The following packages mesa-common-dev and mesa-utils do NOT work and instead they may break EGL on your OS. The libraries installed through any of the mesa packages will install incompatible version of the EGL, most likely into the /lib/arm-linux-gnueabihf folder. Don't use these! Use the ones provided by the official Raspbian OS image in the /opt/vc/lib folder!

How do I try it?

Copy or download the triangle.c file onto your Raspberry Pi. Use the following commands to compile the source file:

gcc -c triangle.c -o triangle.o -I/opt/vc/include
gcc -o triangle triangle.o -lbrcmEGL -lbrcmGLESv2 -L/opt/vc/lib

To run the executable, type the following:

./triangle

You should see the following output:

Initialized EGL version: 1.4
GL Viewport size: 800x600

At the same time, a new file should be created: output.raw. This file contains raw 800x600 RGB pixels. You can use Photoshop or any other software to import and view this file. You should be able to see the following purple triangle. Please note that the image is mirrored vertically as the pixel coordinates in OpenGL start from the bottom, not from the top. Example of the image:

Screenshot of a purple triangle

Raspberry Pi 4

Raspberry Pi 4, at the moment of writing this, has no full KMS driver, because the GPU is different from the previous ones. Instead of using the vc libraries, you will need to use the DRM/GBM.

What do I need?

You need a GCC compiler, GDM, EGL, and GLES libraries. The GCC compiler is already included in the Raspbian image. To install the other libraries, simply run:

sudo apt-get install libegl1-mesa-dev libgbm-dev libgles2-mesa-dev

You will also need to connect your Raspberry Pi to a screen. The boot config from /boot/config.txt that I have used for my tests, if it helps in any way:

dtoverlay=vc4-fkms-v3d
max_framebuffers=2
hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=81

How do I try it?

Copy or download the triangle_rpi4.c file onto your Raspberry Pi. Using any terminal, write the following commands to compile the source file:

gcc -o triangle_rpi4 triangle_rpi4.c -ldrm -lgbm -lEGL -lGLESv2 -I/usr/include/libdrm -I/usr/include/GLES2

To run the executable, type the following:

./triangle_rpi4

You should see the following output:

resolution: 1366x768
Initialized EGL version: 1.4
GL Viewport size: 1366x768

At the same time, a new file should be created: output.raw. This file contains raw 1366x768 RGB pixels. You can use Photoshop or any other software to import and view this file. You should be able to see the following purple triangle. Please note that the image is mirrored vertically as the pixel coordinates in OpenGL start from the bottom, not from the top. Example of the image:

Screenshot of a purple triangle

Troubleshooting and Questions

Failed to get EGL version! Error:

Your EGL might be faulty! Make sure you are using the libraries provided by Raspbian and not the ones installed through apt-get or other package managers. Use the ones in the /opt/vc/lib folder. If that does not work, try reinstalling your Raspberry Pi OS.

I get "Error! The glViewport/glGetIntegerv are not working! EGL might be faulty!" What should I do?

Same as above.

How do I change the pixelbuffer resolution?

Find pbufferAttribs and change EGL_WIDTH and EGL_HEIGHT.

I get "undefined reference" on some gl functions!

Some GL functions may not come from GLES library. You may need to get GL1 library by executing sudo apt-get install libgl1-mesa-dev and then simply add: -lGL flag to the linker, so you get: gcc -o triangle triangle.o -lbrcmEGL -lbrcmGLESv2 -lGL -L/opt/vc/lib

How do I change the buffer sample size or the depth buffer size or the stencil size?

Find configAttribs at the top of the triangle.c file and modify the attributes you need. All config attributes are located here https://www.khronos.org/registry/EGL/sdk/docs/man/html/eglChooseConfig.xhtml

Can I use glBegin() and glEnd()?

You should not, but you can. However, it seems that OpenGL ES does not like that and nothing will be rendered. For this you will need to link the GL library as well. Simply, add -lGL to the gcc command.

Failed to add service - already in use?

As mentioned in this issue, you have to remove both vc4-kms-v3d and vc4-fkms-v3d from R-Pi config. Also relevant discussion here: https://stackoverflow.com/questions/40490113/eglfs-on-raspberry2-failed-to-add-service-already-in-use. If you are using Raspberry Pi 4 (the triangle_rpi4.c) it might happen sometimes. I could not fully figure it out, but it might be due to incorrect EGL or DRM/GBM cleanup in the code. I say "incorrect", but I don't know where.

License

Do whatever you want.

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>

More Repositories

1

doxybook2

Doxygen XML to Markdown (or JSON)
C++
176
star
2

prometheus-smartctl

HDD S.M.A.R.T exporter for Prometheus written in Python
Python
154
star
3

wrenbind17

A header only library for binding C++17 classes and functions to Wren, an embeddable programming language
C++
65
star
4

simplesquirrel

Yet another simple binding in C++11 for Squirrel scripting language
C++
46
star
5

python-embedded-example-project

This is an example project using mebedded python in C++ console application using CMake
CMake
38
star
6

doxybook

Generate GitBook, VuePress, Docsify, or MkDocs out of Doxygen XML output
Python
32
star
7

homelab

Homelab configuration via docker-compose
Python
27
star
8

prometheus-zfs

Prometheus exporter for (some) ZFS statistics via zpool iostatus and zfs get space
Python
18
star
9

rpi-tftgl

Raspberry Pi SSD1963 TFT Display driver with OpenGL ES (via EGL) support including MSAA
C
14
star
10

cpp-vcpkg-template

C++17 + Vcpkg + GitHub Actions + Create release with changelog automatically
CMake
12
star
11

finegui

The FineGui is a standalone library that provides a simple utf8 enabled graphical user interface with a linear layout system similar to Android GUI development. The library also offers experimental XML parser that generates GUI structure for you.
C++
11
star
12

tiva-c-series-cmake-example

Tiva Ware C Series blinky example using CMake and Visual Studio Code
C
9
star
13

gitbook-plugin-insert-logo

GitBook Plugin to insert logo image into the navigation above search input
JavaScript
8
star
14

texture-compression

Hardware texture Compression via OpenGL
C++
7
star
15

space-3d

Generate a random space skybox with stars and nebulas written in GLSL and C++
C++
5
star
16

HGS2561281

HGS2561281 OLED 256x128 Display library for Arduino and Energia
C++
3
star
17

finegraphics

FineGraphics is a standalone library that provides the basic API wrappers around OpenGL, such as textures, framebuffers, renderbuffers, shaders, including window handling and basic 2D canvas rendering.
C++
2
star
18

fineframework

FineFramework - FFW
2
star
19

finemedia

The FineMedia is a simple image reading and writing library, capable of writing and reading PNG, JPG, BMP, DDS (with mipmaps and cubemaps!), TIFF, PBM, and TGA files.
C++
1
star
20

SM2OBJ

StarMade Blueprint to OBJ Exporter
C++
1
star
21

aspnet-core-vuejs-example

.Net Core 3.0 VueJs Example Project
C#
1
star
22

libui-cmake

libui with CMake instead of Menson
CMake
1
star
23

cudd-example-project

CUDD Example Project
C
1
star
24

energise

Jinja
1
star
25

doxybook2-bsl-license

This is a fork of https://github.com/matusnovak/doxybook2 with BSL 1.0 license
C++
1
star
26

serverless-aws-node-bootstrap-vue

Serverless framework example using AWS Lambda + DyanmoDB + Node.js 8.10 + Bootstrap-Vue (Bootstrap 4 with Vue.js 2.x), serving the front end through API Gateway to avoid CloudFront.
Vue
1
star
27

esp-idf-m95-eeprom

A simple EEPROM library for the m950x0 and m95mx0 series SPI EEPROM chips for the ESP32 ESP-IDF framework. Can be used with PlatformIO.
C
1
star
28

finedata

The FineData is a standalone library that provides the basic API wrappers around reading and writing JSON, YAML, and XML files using a universal API. Meaning, the same piece of code that you can use to load a JSON file can be used to load a YAML file.
C++
1
star
29

asio-tls-example

An example usage of asio and openssl to create encrypted TCP connection via TLS v1.3
C++
1
star