Skip to content

Commit 0e6b408

Browse files
committed
Add a Rust example that demonstrates GL rendering under a scene
1 parent 18bba6e commit 0e6b408

File tree

14 files changed

+644
-1
lines changed

14 files changed

+644
-1
lines changed

.github/workflows/nightly_snapshot.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ jobs:
279279
rm -rf snapshots/$target_branch/demos
280280
mkdir -p snapshots/$target_branch/demos
281281
282-
for demo_subdir in gallery, printerdemo,rust printerdemo_old,rust todo,rust slide_puzzle, memory, imagefilter, plotter,; do
282+
for demo_subdir in gallery, printerdemo,rust printerdemo_old,rust todo,rust slide_puzzle, memory, imagefilter, plotter, opengl_underlay,; do
283283
IFS=',' read demo subdir <<< "${demo_subdir}"
284284
285285
mkdir -p snapshots/$target_branch/demos/$demo

.github/workflows/wasm_demos.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ jobs:
6464
sed -i "s/#wasm# //" Cargo.toml
6565
wasm-pack build --release --target web
6666
working-directory: examples/plotter
67+
- name: OpenGL underlay example WASM build
68+
run: |
69+
sed -i "s/#wasm# //" Cargo.toml
70+
wasm-pack build --release --target web
71+
working-directory: examples/opengl_underlay
6772
- name: "Upload Demo Artifacts"
6873
uses: actions/upload-artifact@v2
6974
with:

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ members = [
1414
'examples/gallery',
1515
'examples/imagefilter',
1616
'examples/memory',
17+
'examples/opengl_underlay',
1718
'examples/plotter',
1819
'examples/printerdemo_old/rust',
1920
'examples/printerdemo/rust',

cmake/FindOpenGLES2.cmake

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright © SixtyFPS GmbH <[email protected]>
2+
# SPDX-License-Identifier: (GPL-3.0-only OR LicenseRef-SixtyFPS-commercial)
3+
4+
include(CheckCXXSourceCompiles)
5+
6+
find_path(OPENGLES2_INCLUDE_DIR NAMES "GLES2/gl2.h" "OpenGLES/ES2/gl.h" DOC "The path where the OpenGL ES 2.0 headers are located")
7+
find_library(OPENGLES2_LIBRARY NAMES GLESv2 OpenGLES)
8+
9+
# Sometimes EGL linkage is required, so look it up and always use if available.
10+
find_package(OpenGL COMPONENTS EGL)
11+
12+
# See if we can compile some example code with what we've found.
13+
set(saved_libraries "${CMAKE_REQUIRED_LIBRARIES}")
14+
set(saved_includes "${CMAKE_REQUIRED_INCLUDES}")
15+
16+
if (OPENGLES2_LIBRARY)
17+
list(APPEND CMAKE_REQUIRED_INCLUDES "${OPENGLES2_INCLUDE_DIR}")
18+
list(APPEND CMAKE_REQUIRED_LIBRARIES "${OPENGLES2_LIBRARY}")
19+
endif()
20+
21+
if(OPENGL_egl_LIBRARY)
22+
list(APPEND CMAKE_REQUIRED_INCLUDES "${OPENGL_EGL_INCLUDE_DIRS}")
23+
list(APPEND CMAKE_REQUIRED_LIBRARIES "${OPENGL_egl_LIBRARY}")
24+
endif()
25+
26+
check_cxx_source_compiles("
27+
#include <GLES2/gl2.h>
28+
#include <GLES2/gl2platform.h>
29+
30+
int main(int argc, char *argv[]) {
31+
glClear(GL_STENCIL_BUFFER_BIT);
32+
glUseProgram(0);
33+
}" HAVE_OPENGLES2)
34+
35+
set(CMAKE_REQUIRED_INCLUDES "${saved_includes}")
36+
set(CMAKE_REQUIRED_LIBRARIES "${saved_libraries}")
37+
38+
# Standard CMake package dance
39+
set(package_args OPENGLES2_INCLUDE_DIR OPENGLES2_LIBRARY HAVE_OPENGLES2)
40+
include(FindPackageHandleStandardArgs)
41+
find_package_handle_standard_args(OpenGLES2 DEFAULT_MSG ${package_args})
42+
mark_as_advanced(${package_args})
43+
44+
# Create a convenience target for linkage
45+
if (OPENGLES2_FOUND AND NOT TARGET OpenGLES2::OpenGLES2)
46+
add_library(OpenGLES2::OpenGLES2 UNKNOWN IMPORTED)
47+
set_property(TARGET OpenGLES2::OpenGLES2 PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${OPENGLES2_INCLUDE_DIR}")
48+
set_property(TARGET OpenGLES2::OpenGLES2 PROPERTY IMPORTED_LOCATION "${OPENGLES2_LIBRARY}")
49+
if (TARGET OpenGL::EGL)
50+
target_link_libraries(OpenGLES2::OpenGLES2 INTERFACE OpenGL::EGL)
51+
endif()
52+
endif()

examples/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
cmake_minimum_required(VERSION 3.19)
44
project(SixtyFPSExamples LANGUAGES CXX)
55

6+
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
7+
8+
find_package(OpenGLES2 QUIET)
9+
610
if (NOT TARGET SixtyFPS::SixtyFPS)
711
find_package(SixtyFPS REQUIRED)
812
include(FetchContent)
@@ -22,3 +26,6 @@ endif()
2226
if (SIXTYFPS_FEATURE_INTERPRETER)
2327
add_subdirectory(iot-dashboard/)
2428
endif()
29+
if (OpenGLES2_FOUND AND SIXTYFPS_FEATURE_BACKEND_GL)
30+
add_subdirectory(opengl_underlay)
31+
endif()

examples/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ graph and integrate the result into SixtyFPS.
9191

9292
Some examples of how to use the `sixtyfps-viewer` to add a GUI to shell scripts.
9393

94+
### [`opengl_underlay`](./opengl_underlay)
95+
96+
A Rust and C++ example that shows how render SixtyFPS on top of graphical effect rendered using custom OpenGL code. For more details check out the [Readme](./opengl_underlay).
97+
98+
| `.60` Design | Rust Source | C++ Source | Online wasm Preview |
99+
| --- | --- | --- | --- |
100+
| [`scene.60`](./opengl_underlay/scene.60) | [`main.rs`](./opengl_underlay/main.rs) | [`main.cpp`](./opengl_underlay/main.cpp) | [Online simulation](https://sixtyfps.io/snapshots/master/demos/opengl_underlay/) |
101+
102+
![Screenshot of the OpenGL Underlay Example on Windows](https://sixtyfps.io/resources/opengl_underlay_screenshot.png "OpenGL Underlay")
103+
94104
### External examples
95105

96106
* [Cargo UI](https://github.com/sixtyfpsui/cargo-ui): A rust application that makes use of threads in the background.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright © SixtyFPS GmbH <[email protected]>
2+
# SPDX-License-Identifier: (GPL-3.0-only OR LicenseRef-SixtyFPS-commercial)
3+
4+
cmake_minimum_required(VERSION 3.14)
5+
project(opengl_cpp_underlay LANGUAGES CXX)
6+
7+
if (NOT TARGET SixtyFPS::SixtyFPS)
8+
find_package(SixtyFPS REQUIRED)
9+
endif()
10+
11+
add_executable(opengl_underlay main.cpp)
12+
target_link_libraries(opengl_underlay PRIVATE SixtyFPS::SixtyFPS OpenGLES2::OpenGLES2)
13+
sixtyfps_target_60_sources(opengl_underlay scene.60)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright © SixtyFPS GmbH <[email protected]>
2+
# SPDX-License-Identifier: (GPL-3.0-only OR LicenseRef-SixtyFPS-commercial)
3+
4+
[package]
5+
name = "opengl_underlay"
6+
version = "0.1.6"
7+
authors = ["SixtyFPS <[email protected]>"]
8+
edition = "2021"
9+
build = "build.rs"
10+
license = "(GPL-3.0-only OR LicenseRef-SixtyFPS-commercial)"
11+
publish = false
12+
13+
[[bin]]
14+
path = "main.rs"
15+
name = "opengl_underlay"
16+
17+
[dependencies]
18+
sixtyfps = { path = "../../api/sixtyfps-rs" }
19+
glow = { version = "0.11" }
20+
instant = { version = "0.1", features = [ "now" ] }
21+
22+
[build-dependencies]
23+
sixtyfps-build = { path = "../../api/rs/build" }
24+
25+
# Remove the `#wasm#` to uncomment the wasm build.
26+
# This is commented out by default because we don't want to build it as a library by default
27+
# The CI has a script that does sed "s/#wasm# //" to generate the wasm build.
28+
29+
#wasm# [lib]
30+
#wasm# crate-type = ["cdylib"]
31+
#wasm# path = "main.rs"
32+
#wasm#
33+
#wasm# [target.'cfg(target_arch = "wasm32")'.dependencies]
34+
#wasm# wasm-bindgen = { version = "0.2" }
35+
#wasm# web-sys = { version = "0.3", features=["console"] }
36+
#wasm# console_error_panic_hook = "0.1.5"
37+
#wasm# instant = { version = "0.1", features = [ "wasm-bindgen" ] }
38+

examples/opengl_underlay/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# OpenGL Underlay Example
2+
3+
This example application demonstrates how layer two scenes together in a window:
4+
5+
1. First a graphical effect is rendered using low-level OpenGL code (underlay).
6+
2. A scene of SixtyFPS elements is rendered above.
7+
8+
This is implemented using the `set_rendering_notifier` function on the `sixtyfps::Window` type. It takes a callback as a parameter and that is invoked during different phases of the rendering. In this example the invocation during the setup phase is used to prepare the pipeline for OpenGL rendering later. Then the `BeforeRendering` phase is used to render the graphical effect with OpenGL. Afterwards, SixtyFPS will render the scene of elements into the same back-buffer as the previous OpenGL code rendered into.
9+
10+
Since the graphical effect is continuous, the code in the callback requests a redraw of the contents by calling `sixtyfps::Window::request_redraw()`.

examples/opengl_underlay/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright © SixtyFPS GmbH <[email protected]>
2+
// SPDX-License-Identifier: (GPL-3.0-only OR LicenseRef-SixtyFPS-commercial)
3+
4+
fn main() {
5+
sixtyfps_build::compile("scene.60").unwrap();
6+
}

0 commit comments

Comments
 (0)