Some very hacky texture display
Getty Ritter
7 years ago
Binary diff not shown
| 6 | 6 | mod errors; |
| 7 | 7 | pub use self::errors::Error; |
| 8 | 8 | use std::{ffi,mem,ptr}; |
| 9 | use imagefmt::{Image, ColFmt}; | |
| 9 | 10 | |
| 10 | 11 | // |
| 11 | 12 | |
| 181 | 182 | ) |
| 182 | 183 | }) |
| 183 | 184 | } |
| 185 | ||
| 186 | pub fn set_texture(&self, s: &str, t: &Texture) -> Result<(), Error>{ | |
| 187 | unsafe { | |
| 188 | gl::ActiveTexture(gl::TEXTURE0); | |
| 189 | gl::BindTexture(gl::TEXTURE_2D, t.idx); | |
| 190 | let t = gl::GetUniformLocation( | |
| 191 | self.p, | |
| 192 | ffi::CString::new(s)?.as_ptr(), | |
| 193 | ); | |
| 194 | gl::Uniform1i(t, 0); | |
| 195 | } | |
| 196 | Ok(()) | |
| 197 | } | |
| 184 | 198 | } |
| 185 | 199 | |
| 186 | 200 | // |
| 264 | 278 | } |
| 265 | 279 | } |
| 266 | 280 | } |
| 281 | ||
| 282 | ||
| 283 | pub struct Texture { | |
| 284 | idx: u32, | |
| 285 | } | |
| 286 | ||
| 287 | impl Texture { | |
| 288 | ||
| 289 | pub fn new_from_bitmap(image: &Image<u8>) -> Texture { | |
| 290 | let mut idx = 0; | |
| 291 | unsafe { | |
| 292 | gl::GenTextures(1, &mut idx); | |
| 293 | gl::BindTexture(gl::TEXTURE_2D, idx); | |
| 294 | ||
| 295 | gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as gl::GLint); | |
| 296 | gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as gl::GLint); | |
| 297 | gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as gl::GLint); | |
| 298 | gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as gl::GLint); | |
| 299 | ||
| 300 | let fmt = match image.fmt { | |
| 301 | ColFmt::RGB => gl::RGB, | |
| 302 | ColFmt::RGBA => gl::RGBA, | |
| 303 | _ => panic!("Unknown format: {:?}", image.fmt), | |
| 304 | }; | |
| 305 | ||
| 306 | gl::TexImage2D( | |
| 307 | gl::TEXTURE_2D, | |
| 308 | 0, | |
| 309 | fmt as gl::GLint, | |
| 310 | image.w as gl::GLint, | |
| 311 | image.h as gl::GLint, | |
| 312 | 0, | |
| 313 | fmt, | |
| 314 | gl::UNSIGNED_BYTE, | |
| 315 | mem::transmute(&image.buf[0]), | |
| 316 | ); | |
| 317 | ||
| 318 | gl::BindTexture(gl::TEXTURE_2D, 0); | |
| 319 | } | |
| 320 | Texture { idx } | |
| 321 | } | |
| 322 | } | |
| 1 | // mod pan { | |
| 2 | // extern crate panem_nostrum_quotidianum; | |
| 3 | // pub use self::panem_nostrum_quotidianum::*; | |
| 4 | // } | |
| 5 | 1 | mod gl { |
| 6 | 2 | extern crate gl; |
| 7 | 3 | pub use self::gl::types::*; |
| 37 | 33 | } |
| 38 | 34 | |
| 39 | 35 | static TILE_DATA: [Vertex; 4] = [ |
| 36 | Vertex { x: 0.5, y: -0.5, u: 1.0, v: 0.0 }, | |
| 40 | 37 | Vertex { x: -0.5, y: -0.5, u: 0.0, v: 0.0 }, |
| 41 | Vertex { x: 0.5, y: -0.5, u: 1.0, v: 0.0 }, | |
| 42 | 38 | Vertex { x: 0.5, y: 0.5, u: 1.0, v: 1.0 }, |
| 43 | 39 | Vertex { x: -0.5, y: 0.5, u: 0.0, v: 1.0 }, |
| 44 | 40 | ]; |
| 54 | 50 | #version 150 |
| 55 | 51 | in vec2 position; |
| 56 | 52 | in vec2 uv; |
| 53 | ||
| 54 | out vec2 t_uv; | |
| 55 | ||
| 57 | 56 | void main() { |
| 58 | 57 | gl_Position = vec4(position.x, position.y, 0.5, 1.0); |
| 58 | t_uv = uv; | |
| 59 | 59 | }"; |
| 60 | 60 | |
| 61 | 61 | static FS_SRC: &'static str = " |
| 62 | 62 | #version 150 |
| 63 | ||
| 64 | in vec2 t_uv; | |
| 63 | 65 | out vec4 out_color; |
| 66 | ||
| 67 | uniform sampler2D tex; | |
| 68 | ||
| 64 | 69 | void main() { |
| 65 |
out_color = |
|
| 70 | out_color = texture(tex, t_uv); | |
| 71 | // out_color = vec4(1.0 - t_uv.x, 1.0 - t_uv.y, 1.0, 1.0); | |
| 66 | 72 | }"; |
| 67 | 73 | |
| 68 | 74 | pub fn main_loop() -> Result<(), pan::Error> { |
| 75 | let mut f = std::fs::File::open("sample.png").unwrap(); | |
| 76 | let texture: imagefmt::Image<u8> = imagefmt::png::read(&mut f, imagefmt::ColFmt::RGBA).unwrap(); | |
| 77 | println!("{:#?}", texture); | |
| 78 | ||
| 69 | 79 | let window = pan::Window::create()?; |
| 70 | 80 | |
| 71 | 81 | let vs = pan::Shader::compile(pan::ShaderType::Vertex, VS_SRC)?; |
| 72 | 82 | let fs = pan::Shader::compile(pan::ShaderType::Fragment, FS_SRC)?; |
| 73 | 83 | let program = pan::Program::link(vec![vs, fs])?; |
| 84 | ||
| 85 | let tex = pan::Texture::new_from_bitmap(&texture); | |
| 74 | 86 | |
| 75 | 87 | let vbo = pan::VertexBuffer::new_array_buffer( |
| 76 | 88 | pan::VertexArray::new(), |
| 133 | 145 | gl::Clear(gl::COLOR_BUFFER_BIT); |
| 134 | 146 | |
| 135 | 147 | vbo.bind(); |
| 148 | program.set_texture("tex", &tex).unwrap(); | |
| 136 | 149 | |
| 137 |
gl::DrawArrays(gl::TRIANGLE |
|
| 150 | gl::DrawArrays(gl::TRIANGLE_STRIP, 0, 4); | |
| 138 | 151 | } |
| 139 | 152 | |
| 140 | 153 | ControlFlow::Continue |