pub struct Framebuffer { /* private fields */ }
Expand description

A framebuffer object that can be used for offscreen rendering.

Implementations§

source§

impl Framebuffer

source

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();
source

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();
source

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();
source

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();
source

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();
source

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();
source

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();
source

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();
source

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();
source

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();
source

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();
source

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

source§

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)

Performs copy-assignment from source. Read more
source§

impl Debug for Framebuffer

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Framebuffer

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.