Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add texture drawing example [help needed] #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ glib = "0.19.0"
glium = "0.34.0"
gl_loader = "0.1.2"
gtk4 = "0.8.0"

[dev-dependencies]
image = "0.24"
Binary file added examples/opengl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 164 additions & 0 deletions examples/texture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
use glium::backend::Facade;
use glium::index::{NoIndices, PrimitiveType};
use glium::texture::{CompressedTexture2d, RawImage2d};
use glium::{implement_vertex, program, uniform, Frame, Program, Surface, VertexBuffer};
use gtk4::prelude::*;
use gtk4::{glib::signal::Propagation, Application, ApplicationWindow, GLArea};
use gtk4_glium::GtkFacade;
use std::io::Cursor;
use std::time::Duration;

#[derive(Copy, Clone)]
pub struct TexVertex {
position: [f32; 2],
tex_coords: [f32; 2],
}
implement_vertex!(TexVertex, position, tex_coords);

fn create_rectangle_buffer<F: Facade>(context: &F) -> VertexBuffer<TexVertex> {
glium::VertexBuffer::new(
context,
&[
TexVertex {
position: [-1.0, 1.0],
tex_coords: [0.0, 1.0],
},
TexVertex {
position: [1.0, 1.0],
tex_coords: [1.0, 1.0],
},
TexVertex {
position: [-1.0, -1.0],
tex_coords: [0.0, 0.0],
},
TexVertex {
position: [1.0, -1.0],
tex_coords: [1.0, 0.0],
},
],
)
.unwrap()
}

fn load_texture<F: Facade>(context: &F) -> CompressedTexture2d {
let image = image::load(
Cursor::new(&include_bytes!("opengl.png")[..]),
image::ImageFormat::Png,
)
.unwrap()
.to_rgba8();

let image_dimensions = dbg!(image.dimensions());
let image = RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);

CompressedTexture2d::new(context, image).unwrap()
}

fn create_program<F: Facade>(context: &F) -> Program {
program!(context,
140 => {
vertex: "
#version 140

uniform mat4 matrix;
in vec2 position;
in vec2 tex_coords;

out vec2 v_tex_coords;

void main() {
gl_Position = matrix * vec4(position, 0.0, 1.0);
v_tex_coords = tex_coords;
}
",

fragment: "
#version 140

uniform sampler2D tex;
in vec2 v_tex_coords;

out vec4 f_color;

void main() {
f_color = texture(tex, v_tex_coords);

// Just setting the color draws a rectangle.
// So everything in the setup seems to work, except sampling the texture
// f_color = vec4(0.5, 0.1, 0.2, 1.0);
}
"
},
)
.unwrap()
}

fn main() {
let application = Application::builder()
.application_id("com.remcokranenburg.Texture")
.build();

application.connect_activate(|app| {
let window = ApplicationWindow::builder()
.application(app)
.title("Texture")
.default_width(600)
.default_height(400)
.build();

let glarea = GLArea::builder().vexpand(true).build();

window.set_child(Some(&glarea));
window.show();

let facade = GtkFacade::from_glarea(&glarea).unwrap();

let vertex_buffer = create_rectangle_buffer(&facade);
let program = create_program(&facade);

glarea.connect_render(move |_glarea, _glcontext| {
let context = facade.get_context();

// GTK messes up the texture if I load it once on program start.
// If I load it just before I will draw with it, it works!
let opengl_texture = load_texture(&facade);

let uniforms = uniform! {
matrix: [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0f32]
],
tex: &opengl_texture
};

let mut frame = Frame::new(context.clone(), context.get_framebuffer_dimensions());

frame.clear_color(0.0, 0.0, 0.0, 1.0);

frame
.draw(
&vertex_buffer,
NoIndices(PrimitiveType::TriangleStrip),
&program,
&uniforms,
&Default::default(),
)
.unwrap();

frame.finish().unwrap();
Propagation::Proceed
});

// This makes the GLArea redraw 60 times per second
// You can remove this if you want to redraw only when focused/resized
let frame_time = Duration::new(0, 1_000_000_000 / 60);
glib::source::timeout_add_local(frame_time, move || {
glarea.queue_draw();
glib::ControlFlow::Continue
});
});

application.run();
}