glow
repository·main·Indexed 23 days ago
https://github.com/grovesnl/glowA set of cross-platform bindings for OpenGL, OpenGL ES, and WebGL (version 0.18.0) that enables writing graphics code for native platforms and the web without target-specific logic. It provides a unified interface via the HasContext trait to manage graphics objects like shaders, programs, buffers, and textures across different backends.
What's inside glow
- glow is a set of bindings designed to run OpenGL, OpenGL ES, and WebGL anywhere. It provides a unified interface to avoid writing target-specific graphics code, allowing you to write code that works across native platforms and the web.
Build and run Glow natively
mainTo run the
helloexample on native platforms, usecargo runwith the appropriate feature flag for your windowing backend:- For glutin and winit:
cargo run --features=glutin_winit - For sdl2:
cargo run --features=sdl2
cargo run --features=glutin_winit # or cargo run --features=sdl2- For glutin and winit:
Build glow for WebAssembly (web-sys)
mainTo target web environments using
web-sys, build the project using thewasm32-unknown-unknowntarget.# web-sys cargo build --target wasm32-unknown-unknownBuild and run Glow for Web (wasm64/memory64)
mainBuilding for
wasm64-unknown-unknownrequires a nightly Rust toolchain and a from-source build ofstdbecause it is a tier-3 target.- Install the nightly toolchain and
rust-srccomponent:rustup toolchain install nightly --component rust-src. - Navigate to
examples/hello. - Build using the
+nightlytoolchain with the-Z build-stdflag:cargo +nightly build --target wasm64-unknown-unknown -Z build-std=std,panic_abort. - Generate web assets with
wasm-bindgenand copyindex.htmlto thegeneratedfolder.
To view the result, open
generated/index.htmlin a browser that supports the WebAssembly memory64 proposal (recent versions of Chrome and Firefox enable this by default).rustup toolchain install nightly --component rust-src cargo +nightly build --target wasm64-unknown-unknown -Z build-std=std,panic_abort mkdir -p generated wasm-bindgen ../../target/wasm64-unknown-unknown/debug/hello.wasm --out-dir generated --target web cp index.html generated- Install the nightly toolchain and
Build glow for native platforms
mainTo build glow for your current native operating system, use the standard cargo build command.
# native cargo buildBuild and run Glow for Web (wasm32)
mainTo build the
helloexample for the web using thewasm32-unknown-unknowntarget, follow these steps:- Navigate to the example directory:
cd examples/hello. - Ensure your
wasm-bindgen-cliversion matches the version inCargo.lockby installing it viacargo install wasm-bindgen-cli --version <version-from-Cargo.lock>. - Build the target:
cargo build --target wasm32-unknown-unknown. - Generate the web assets using
wasm-bindgenand move theindex.htmlinto the output directory.
Note: The
wasm-bindgencommand expects the path to the.wasmfile relative to your current location in theexamples/hellodirectory.cd examples/hello cargo build --target wasm32-unknown-unknown mkdir -p generated wasm-bindgen ../../target/wasm32-unknown-unknown/debug/hello.wasm --out-dir generated --target web cp index.html generated- Navigate to the example directory:
Core OpenGL object types in glow
mainThe following types are associated with a
ContextimplementingHasContext. They represent the primary handles used in graphics programming:Shader: Handle to a shader object.Program: Handle to a shader program.Buffer: Handle to a buffer object.VertexArray: Handle to a vertex array object (VAO).Texture: Handle to a texture object.Sampler: Handle to a sampler object.Fence: Handle to a sync object/fence.Framebuffer: Handle to a framebuffer object.Renderbuffer: Handle to a renderbuffer object.Query: Handle to a query object.UniformLocation: Handle to a uniform location within a program.TransformFeedback: Handle to a transform feedback object.
Understand the HasContext trait and OpenGL abstraction
mainThe
HasContexttrait is the core abstraction inglow. It allows the library to provide a unified API for different graphics backends (like native OpenGL or WebGL viaweb-sys).Any type implementing
HasContextacts as a provider for OpenGL functions and manages the lifecycle of various graphics objects. The trait uses associated types to define what specific types represent shaders, programs, buffers, etc., for that particular context.Safety Warning: All GL API usage must be valid according to the relevant GL specification.
glowdoes not enforce these rules; the caller is responsible for ensuring that the context is valid and that calls follow the specification for the specific backend being used.Manage pixel data with PixelPackData and PixelUnpackData
mainWhen performing texture operations like
get_tex_image,glowuses enums to specify how pixel data is provided or received:PixelPackData<'a>: Used when sending data to the GPU (e.g.,BufferOffset(u32)orSlice(Option<&'a mut [u8]>)).PixelUnpackData<'a>: Used when receiving data from the GPU (e.g.,BufferOffset(u32)orSlice(Option<&'a [u8]>)).CompressedPixelUnpackData<'a>: Specifically for compressed data, usingBufferRange(core::ops::Range<u32>)orSlice(&'a [u8]).
WebGL Texture Sub-Image Operations
mainThe
tex_sub_image_2dandtex_sub_image_3dmethods allow updating parts of textures. The behavior depends on thePixelUnpackDataprovided:2D Sub-Image (
tex_sub_image_2d)- WebGL 1: Supports
PixelUnpackData::Slice. UsingPixelUnpackData::BufferOffsetis not supported and willpanic!. - WebGL 2: Supports both
PixelUnpackData::SliceandPixelUnpackData::BufferOffset.
3D Sub-Image (
tex_sub_image_3d)- WebGL 1: Not supported (will
panic!). - WebGL 2: Supports both
PixelUnpackData::SliceandPixelUnpackData::BufferOffset.
Compressed Sub-Image (
compressed_tex_sub_image_2d/3d)- WebGL 1: Supports
CompressedPixelUnpackData::Slice. UsingCompressedPixelUnpackData::BufferRangeis not supported and willpanic!. - WebGL 2: Supports both
CompressedPixelUnpackData::SliceandCompressedPixelUnpackData::BufferRange.
- WebGL 1: Supports
Native OpenGL resource types
mainThe native backend uses specific wrapper types to represent OpenGL objects. These types ensure type safety when interacting with the
Context. Common types include:NativeShaderNativeProgramNativeBufferNativeVertexArrayNativeTextureNativeSamplerNativeFenceNativeFramebufferNativeRenderbufferNativeQueryNativeUniformLocationNativeTransformFeedback
WebGL2-only 3D texture operations
mainThe following operations are WebGL2 only. Attempting to call them while using a WebGL1 context will result in a panic:
tex_image_3d_with_image_bitmaptex_image_3d_with_html_canvas_elementtex_image_3d_with_html_image_elementtex_image_3d_with_html_video_elementtex_image_3d_with_video_frametex_image_3d_with_image_datatex_sub_image_3d_with_image_bitmaptex_sub_image_3d_with_html_canvas_elementtex_sub_image_3d_with_html_image_elementtex_sub_image_3d_with_html_video_elementtex_sub_image_3d_with_video_frametex_sub_image_3d_with_image_data