Struct cgl_rs::graphics::Framebuffer
source · pub struct Framebuffer { /* private fields */ }
Expand description
A framebuffer object that can be used for offscreen rendering.
Implementations§
source§impl Framebuffer
impl Framebuffer
sourcepub fn from_default(window: &Window) -> Result<Framebuffer, &'static str>
pub fn from_default(window: &Window) -> Result<Framebuffer, &'static str>
Creates a new framebuffer object from the default window.
Arguments
window
- A reference to the window object to create the framebuffer from.
Returns
Returns a Result
containing the newly created Framebuffer
object or an error message if the creation failed.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let framebuffer = cgl_rs::graphics::Framebuffer::from_default(&window).unwrap();
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn new(width: i32, height: i32) -> Result<Framebuffer, &'static str>
pub fn new(width: i32, height: i32) -> Result<Framebuffer, &'static str>
Creates a new framebuffer object with the specified width and height.
Arguments
width
- The width of the framebuffer.height
- The height of the framebuffer.
Returns
Returns a Result
containing the newly created Framebuffer
object or an error message if the creation failed.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn new_basic(width: i32, height: i32) -> Result<Framebuffer, &'static str>
pub fn new_basic(width: i32, height: i32) -> Result<Framebuffer, &'static str>
Creates a new basic framebuffer object with the specified width and height.
Arguments
width
- The width of the framebuffer.height
- The height of the framebuffer.
Returns
Returns a Result
containing the newly created Framebuffer
object or an error message if the creation failed.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let framebuffer = cgl_rs::graphics::Framebuffer::new_basic(800, 600).unwrap();
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn destroy(&mut self)
pub fn destroy(&mut self)
Destroys the framebuffer object.
Note: This function is called automatically when the framebuffer object goes out of scope. But this can also be called manually to destroy the framebuffer object before it goes out of scope.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let mut framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
framebuffer.destroy(); // This is not necessary, but can be called manually.
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn add_color_attachment(&mut self, texture: Texture)
pub fn add_color_attachment(&mut self, texture: Texture)
Adds a color attachment to the framebuffer object.
Note: The texture object passed in will be destroyed automatically after being added to the framebuffer object. So even if you clone the parent texture
Arguments
texture
- The texture object to be added as a color attachment to the framebuffer object.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let mut framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
let texture = cgl_rs::graphics::Texture::dummy().unwrap();
framebuffer.add_color_attachment(texture);
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn get_color_atttachment(&self, index: i32) -> Option<Texture>
pub fn get_color_atttachment(&self, index: i32) -> Option<Texture>
Gets the color attachment at the specified index.
Arguments
index
- The index of the color attachment to retrieve.
Returns
Returns Some(Texture)
if the color attachment exists, otherwise returns None
.
This returned texture object is managed by the framebuffer object and will be
destroyed automatically when the framebuffer object is destroyed.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let mut framebuffer = cgl_rs::graphics::Framebuffer::new_basic(800, 600).unwrap();
let texture = cgl_rs::graphics::Texture::dummy().unwrap();
framebuffer.add_color_attachment(texture);
let color_attachment = framebuffer.get_color_atttachment(0);
assert!(color_attachment.is_some());
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn bind(&self)
pub fn bind(&self)
Binds the framebuffer object for rendering.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let mut framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
framebuffer.bind();
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn get_size(&self) -> (i32, i32)
pub fn get_size(&self) -> (i32, i32)
Returns
Returns a tuple containing the width and height of the framebuffer object.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
let (width, height) = framebuffer.get_size();
assert_eq!(width, 800);
assert_eq!(height, 600);
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn read_pixels(&self, x: i32, y: i32, width: i32, height: i32) -> Vec<u8>
pub fn read_pixels(&self, x: i32, y: i32, width: i32, height: i32) -> Vec<u8>
Reads the pixels from the framebuffer object.
Note: This function currently has some issues and may crash the program.
Arguments
x
- The x coordinate of the lower left corner of the rectangle of pixels to read.y
- The y coordinate of the lower left corner of the rectangle of pixels to read.width
- The width of the rectangle of pixels to read.height
- The height of the rectangle of pixels to read.
Returns
Returns a vector of bytes containing the pixel data.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
let pixels = framebuffer.read_pixels(0, 0, 100, 100);
assert_eq!(pixels.len(), 100 * 100 * 4 * 4);
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn get_mouse_pick_id(&self, x: i32, y: i32, index: i32) -> i32
pub fn get_mouse_pick_id(&self, x: i32, y: i32, index: i32) -> i32
Gets the mouse pick ID at the specified screen coordinates and index.
Arguments
x
- The x coordinate of the screen position to check.y
- The y coordinate of the screen position to check.index
- The index of the mouse pick ID to retrieve.
Returns
Returns the mouse pick ID at the specified screen coordinates and index.
Safety
This function is marked as unsafe because it directly calls an unsafe C function.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
let mouse_pick_id = framebuffer.get_mouse_pick_id(400, 300, 0);
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn get_color_texture(&self) -> Texture
pub fn get_color_texture(&self) -> Texture
Gets the color texture of the framebuffer.
Returns
Returns a Texture
object representing the color texture of the framebuffer.
Safety
This function is marked as unsafe because it directly calls an unsafe C function.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
let texture = framebuffer.get_color_texture();
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
sourcepub fn get_depth_texture(&self) -> Texture
pub fn get_depth_texture(&self) -> Texture
Gets the depth texture of the framebuffer.
Returns
Returns a Texture
object representing the depth texture of the framebuffer.
Safety
This function is marked as unsafe because it directly calls an unsafe C function.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
let depth_texture = framebuffer.get_depth_texture();
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
Trait Implementations§
source§impl Clone for Framebuffer
impl Clone for Framebuffer
source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the framebuffer object.
NOTE: The new instance will have the same handle, has_been_destroyed
flag.
This means that the new instance will not be able to receive events nor will the internal window handle be
destroyed when the new instance is dropped. The internal window handle will be destroyed when the original
instance is dropped.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
let cloned_framebuffer = framebuffer.clone();
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more