Last active
May 4, 2019 21:05
-
-
Save AregevDev/5a1d75afcd3977fbe640c3d812b89aac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use glium::glutin::{ | |
dpi::LogicalSize, Api, ContextBuilder, Event, EventsLoop, GlRequest, WindowBuilder, WindowEvent, | |
}; | |
use glium::index::{NoIndices, PrimitiveType}; | |
use glium::texture::RawImage2d; | |
use glium::{implement_vertex, uniform, Display, DrawParameters, Program, Surface, VertexBuffer}; | |
use image::GenericImageView; | |
use std::io::Cursor; | |
use glium::uniforms::{Sampler, MagnifySamplerFilter}; | |
#[derive(Copy, Clone)] | |
struct Vertex { | |
position: [f32; 2], | |
tex_coords: [f32; 2], | |
} | |
implement_vertex!(Vertex, position, tex_coords); | |
fn main() { | |
let mut events_loop = EventsLoop::new(); | |
let wb = WindowBuilder::new() | |
.with_dimensions(LogicalSize::new(500.0, 500.0)) | |
.with_title("Triangle"); | |
let cb = ContextBuilder::new() | |
.with_gl(GlRequest::Specific(Api::OpenGl, (3, 2))) | |
.with_vsync(true); | |
let display = Display::new(wb, cb, &events_loop).unwrap(); | |
let image = image::load(Cursor::new(&include_bytes!("../tex.png")[..]), image::PNG).unwrap(); | |
let image_dims = image.dimensions(); | |
let image = RawImage2d::from_raw_rgba(image.raw_pixels(), image_dims); | |
let texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap(); | |
let sampler = Sampler::new(&texture); | |
let sampler = sampler.magnify_filter(MagnifySamplerFilter::Nearest); | |
let v1 = Vertex { | |
position: [0.0, 0.0], | |
tex_coords: [0.0, 0.0], | |
}; | |
let v2 = Vertex { | |
position: [1.0, 0.0], | |
tex_coords: [1.0, 0.0], | |
}; | |
let v3 = Vertex { | |
position: [1.0, 1.0], | |
tex_coords: [1.0, 1.0], | |
}; | |
let triangle = vec![v1, v2, v3]; | |
let vbuf = VertexBuffer::new(&display, &triangle).unwrap(); | |
let indices = NoIndices(PrimitiveType::TrianglesList); | |
let vertex_src = include_str!("../shader.glslv"); | |
let fragment_src = include_str!("../shader.glslf"); | |
let program = Program::from_source(&display, vertex_src, fragment_src, None).unwrap(); | |
let mut t: f32 = -1.0; | |
let mut closed = false; | |
while !closed { | |
t += 0.002; | |
if t > 0.0 { | |
t = -1.0; | |
} | |
let mut target = display.draw(); | |
target.clear_color(0.0, 0.0, 0.0, 1.0); | |
let uniform = 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], | |
[t, t, 0.0, 1.0], | |
], | |
tex: sampler, | |
}; | |
target | |
.draw( | |
&vbuf, | |
&indices, | |
&program, | |
&uniform, | |
&DrawParameters::default(), | |
) | |
.unwrap(); | |
target.finish().unwrap(); | |
events_loop.poll_events(|ev| match ev { | |
Event::WindowEvent { event, .. } => match event { | |
WindowEvent::CloseRequested => closed = true, | |
_ => (), | |
}, | |
_ => (), | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 130 | |
in vec2 v_tex_coords; | |
out vec4 color; | |
uniform sampler2D tex; | |
void main() { | |
color = texture(tex, v_tex_coords); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 130 | |
in vec2 position; | |
in vec2 tex_coords; | |
out vec2 v_tex_coords; | |
uniform mat4 matrix; | |
void main() { | |
v_tex_coords = tex_coords; | |
gl_Position = matrix * vec4(position, 0.0, 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
texture: