2016-03-16 14:25:35 -04:00
|
|
|
// Copyright 2016 Matthew Collins
|
2015-09-17 11:21:56 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-09-17 11:04:25 -04:00
|
|
|
|
|
|
|
mod atlas;
|
|
|
|
pub mod glsl;
|
2015-10-06 18:49:52 -04:00
|
|
|
#[macro_use]
|
|
|
|
pub mod shaders;
|
2015-09-17 11:04:25 -04:00
|
|
|
pub mod ui;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
use resources;
|
|
|
|
use gl;
|
|
|
|
use image;
|
2015-10-07 14:36:59 -04:00
|
|
|
use image::GenericImage;
|
2015-09-17 11:04:25 -04:00
|
|
|
use byteorder::{WriteBytesExt, NativeEndian};
|
|
|
|
use serde_json;
|
2016-03-18 13:16:03 -04:00
|
|
|
use cgmath::{self, Vector, Point};
|
2015-09-17 11:04:25 -04:00
|
|
|
|
|
|
|
const ATLAS_SIZE: usize = 1024;
|
|
|
|
|
2016-03-18 13:16:03 -04:00
|
|
|
// TEMP
|
|
|
|
const NUM_SAMPLES: i32 = 1;
|
|
|
|
const LIGHT_LEVEL: f32 = 0.8;
|
|
|
|
const SKY_OFFSET: f32 = 1.0;
|
|
|
|
|
|
|
|
pub struct Camera {
|
|
|
|
pub pos: cgmath::Point3<f64>,
|
|
|
|
pub yaw: f64,
|
|
|
|
pub pitch: f64,
|
|
|
|
}
|
|
|
|
|
2015-09-17 11:04:25 -04:00
|
|
|
pub struct Renderer {
|
2015-10-07 14:36:59 -04:00
|
|
|
resource_version: usize,
|
|
|
|
pub resources: Arc<RwLock<resources::Manager>>,
|
|
|
|
textures: Arc<RwLock<TextureManager>>,
|
|
|
|
pub ui: ui::UIState,
|
2015-09-17 11:04:25 -04:00
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
gl_texture: gl::Texture,
|
|
|
|
texture_layers: usize,
|
2015-09-17 11:04:25 -04:00
|
|
|
|
2016-03-16 13:33:06 -04:00
|
|
|
chunk_shader: ChunkShader,
|
|
|
|
chunk_shader_alpha: ChunkShaderAlpha,
|
2016-03-18 11:19:21 -04:00
|
|
|
trans_shader: TransShader,
|
2016-03-16 13:33:06 -04:00
|
|
|
|
2016-03-18 13:16:03 -04:00
|
|
|
camera: Camera,
|
2016-03-16 13:33:06 -04:00
|
|
|
perspective_matrix: cgmath::Matrix4<f32>,
|
|
|
|
|
|
|
|
trans: Option<TransInfo>,
|
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
last_width: u32,
|
|
|
|
last_height: u32,
|
2015-09-17 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:33:06 -04:00
|
|
|
init_shader! {
|
|
|
|
Program ChunkShader {
|
|
|
|
vert = "chunk_vertex",
|
|
|
|
frag = "chunk_frag",
|
|
|
|
attribute = {
|
|
|
|
position => "aPosition",
|
|
|
|
texture_info => "aTextureInfo",
|
|
|
|
texture_offset => "aTextureOffset",
|
|
|
|
color => "aColor",
|
|
|
|
lighting => "aLighting",
|
|
|
|
},
|
|
|
|
uniform = {
|
|
|
|
perspective_matrix => "perspectiveMatrix",
|
|
|
|
camera_matrix => "cameraMatrix",
|
|
|
|
offset => "offset",
|
|
|
|
texture => "textures",
|
|
|
|
light_level => "lightLevel",
|
|
|
|
sky_offset => "sky_offset",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init_shader! {
|
|
|
|
Program ChunkShaderAlpha {
|
|
|
|
vert = "chunk_vertex",
|
|
|
|
frag = "chunk_frag", #alpha
|
|
|
|
attribute = {
|
|
|
|
position => "aPosition",
|
|
|
|
texture_info => "aTextureInfo",
|
|
|
|
texture_offset => "aTextureOffset",
|
|
|
|
color => "aColor",
|
|
|
|
lighting => "aLighting",
|
|
|
|
},
|
|
|
|
uniform = {
|
|
|
|
perspective_matrix => "perspectiveMatrix",
|
|
|
|
camera_matrix => "cameraMatrix",
|
|
|
|
offset => "offset",
|
|
|
|
texture => "textures",
|
|
|
|
light_level => "lightLevel",
|
|
|
|
sky_offset => "sky_offset",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 11:04:25 -04:00
|
|
|
impl Renderer {
|
2015-10-07 14:36:59 -04:00
|
|
|
pub fn new(res: Arc<RwLock<resources::Manager>>) -> Renderer {
|
2016-03-16 13:53:04 -04:00
|
|
|
use cgmath::Matrix;
|
2015-10-07 14:36:59 -04:00
|
|
|
let version = {
|
|
|
|
res.read().unwrap().version()
|
|
|
|
};
|
|
|
|
let tex = gl::Texture::new();
|
|
|
|
tex.bind(gl::TEXTURE_2D_ARRAY);
|
|
|
|
tex.image_3d(gl::TEXTURE_2D_ARRAY,
|
|
|
|
0,
|
|
|
|
ATLAS_SIZE as u32,
|
|
|
|
ATLAS_SIZE as u32,
|
|
|
|
1,
|
|
|
|
gl::RGBA,
|
|
|
|
gl::UNSIGNED_BYTE,
|
|
|
|
&[0; ATLAS_SIZE * ATLAS_SIZE * 4]);
|
|
|
|
tex.set_parameter(gl::TEXTURE_2D_ARRAY, gl::TEXTURE_MAG_FILTER, gl::NEAREST);
|
|
|
|
tex.set_parameter(gl::TEXTURE_2D_ARRAY, gl::TEXTURE_MIN_FILTER, gl::NEAREST);
|
|
|
|
tex.set_parameter(gl::TEXTURE_2D_ARRAY, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE);
|
|
|
|
tex.set_parameter(gl::TEXTURE_2D_ARRAY, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
let textures = Arc::new(RwLock::new(TextureManager::new(res.clone())));
|
|
|
|
|
|
|
|
let mut greg = glsl::Registry::new();
|
|
|
|
shaders::add_shaders(&mut greg);
|
|
|
|
let ui = ui::UIState::new(&greg, textures.clone(), res.clone());
|
|
|
|
|
|
|
|
gl::enable(gl::DEPTH_TEST);
|
|
|
|
gl::enable(gl::CULL_FACE_FLAG);
|
|
|
|
gl::cull_face(gl::BACK);
|
|
|
|
gl::front_face(gl::CLOCK_WISE);
|
2015-10-06 18:49:52 -04:00
|
|
|
|
2016-03-16 13:33:06 -04:00
|
|
|
// Shaders
|
|
|
|
let chunk_shader = ChunkShader::new(&greg);
|
|
|
|
let chunk_shader_alpha = ChunkShaderAlpha::new(&greg);
|
2016-03-18 11:19:21 -04:00
|
|
|
let trans_shader = TransShader::new(&greg);
|
2015-10-06 18:49:52 -04:00
|
|
|
|
2016-03-16 13:33:06 -04:00
|
|
|
// UI
|
|
|
|
// Line Drawer
|
|
|
|
// Models
|
|
|
|
// Clouds
|
2015-10-06 18:49:52 -04:00
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
gl::blend_func(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
|
|
|
Renderer {
|
|
|
|
resource_version: version,
|
|
|
|
textures: textures,
|
|
|
|
ui: ui,
|
|
|
|
resources: res,
|
|
|
|
gl_texture: tex,
|
|
|
|
texture_layers: 1,
|
2016-03-16 13:33:06 -04:00
|
|
|
|
|
|
|
chunk_shader: chunk_shader,
|
|
|
|
chunk_shader_alpha: chunk_shader_alpha,
|
2016-03-18 11:19:21 -04:00
|
|
|
trans_shader: trans_shader,
|
2016-03-16 13:33:06 -04:00
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
last_width: 0,
|
|
|
|
last_height: 0,
|
2016-03-16 13:33:06 -04:00
|
|
|
|
2016-03-18 13:16:03 -04:00
|
|
|
camera: Camera {
|
|
|
|
pos: cgmath::Point3::new(0.0, 0.0, 0.0),
|
|
|
|
yaw: 0.0,
|
|
|
|
pitch: ::std::f64::consts::PI,
|
|
|
|
},
|
2016-03-16 13:33:06 -04:00
|
|
|
perspective_matrix: cgmath::Matrix4::zero(),
|
|
|
|
|
|
|
|
trans: None,
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self, delta: f64, width: u32, height: u32) {
|
2016-03-18 13:16:03 -04:00
|
|
|
use std::f64::consts::PI as PI64;
|
2015-10-07 14:36:59 -04:00
|
|
|
{
|
|
|
|
let rm = self.resources.read().unwrap();
|
|
|
|
if rm.version() != self.resource_version {
|
|
|
|
self.resource_version = rm.version();
|
|
|
|
println!("Updating textures to {}", self.resource_version);
|
|
|
|
self.textures.write().unwrap().update_textures(self.resource_version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.update_textures(delta);
|
|
|
|
|
|
|
|
if self.last_height != height || self.last_width != width {
|
|
|
|
self.last_width = width;
|
|
|
|
self.last_height = height;
|
|
|
|
gl::viewport(0, 0, width as i32, height as i32);
|
2016-03-16 13:33:06 -04:00
|
|
|
|
|
|
|
self.perspective_matrix = cgmath::Matrix4::from(
|
|
|
|
cgmath::PerspectiveFov {
|
2016-03-16 13:53:04 -04:00
|
|
|
fovy: cgmath::Rad::from(cgmath::Deg{s: 90f32}),
|
2016-03-16 13:33:06 -04:00
|
|
|
aspect: (width / height) as f32,
|
|
|
|
near: 0.1f32,
|
|
|
|
far: 500.0f32,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
self.init_trans(width, height);
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
|
|
|
|
2016-03-18 11:19:21 -04:00
|
|
|
let trans = self.trans.as_mut().unwrap();
|
|
|
|
trans.main.bind();
|
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
gl::active_texture(0);
|
|
|
|
self.gl_texture.bind(gl::TEXTURE_2D_ARRAY);
|
|
|
|
|
2016-03-16 13:33:06 -04:00
|
|
|
gl::enable(gl::MULTISAMPLE);
|
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
gl::clear_color(14.0 / 255.0, 48.0 / 255.0, 92.0 / 255.0, 1.0);
|
2015-09-17 11:04:25 -04:00
|
|
|
gl::clear(gl::ClearFlags::Color | gl::ClearFlags::Depth);
|
|
|
|
|
2016-03-18 11:19:21 -04:00
|
|
|
// Chunk rendering
|
2016-03-18 13:16:03 -04:00
|
|
|
self.chunk_shader.program.use_program();
|
|
|
|
|
|
|
|
let view_vector = cgmath::Vector3::new(
|
|
|
|
((self.camera.yaw - PI64/2.0).cos() * -self.camera.pitch.cos()) as f32,
|
|
|
|
(-self.camera.pitch.sin()) as f32,
|
|
|
|
(-(self.camera.yaw - PI64/2.0).sin() * -self.camera.pitch.cos()) as f32
|
|
|
|
);
|
|
|
|
let camera = cgmath::Point3::new(-self.camera.pos.x as f32, -self.camera.pos.y as f32, self.camera.pos.z as f32);
|
|
|
|
let camera_matrix = cgmath::Matrix4::look_at(
|
|
|
|
camera,
|
|
|
|
camera + cgmath::Point3::new(-view_vector.x, -view_vector.y, view_vector.z).to_vec(),
|
|
|
|
cgmath::Vector3::new(0.0, -1.0, 0.0)
|
|
|
|
);
|
|
|
|
let camera_matrix = camera_matrix * cgmath::Matrix4::from_nonuniform_scale(-1.0, 1.0, 1.0);
|
|
|
|
|
|
|
|
// TODO Frustum
|
|
|
|
|
|
|
|
self.chunk_shader.perspective_matrix.set_matrix4(&self.perspective_matrix);
|
|
|
|
self.chunk_shader.camera_matrix.set_matrix4(&camera_matrix);
|
|
|
|
self.chunk_shader.texture.set_int(0);
|
|
|
|
self.chunk_shader.light_level.set_float(LIGHT_LEVEL);
|
|
|
|
self.chunk_shader.sky_offset.set_float(SKY_OFFSET);
|
2016-03-18 11:19:21 -04:00
|
|
|
|
|
|
|
// Line rendering
|
2016-03-18 13:16:03 -04:00
|
|
|
// Model rendering
|
2016-03-18 11:19:21 -04:00
|
|
|
// Cloud rendering
|
|
|
|
|
|
|
|
// Trans chunk rendering
|
|
|
|
|
|
|
|
// TODO: trans chunk shader stuff
|
|
|
|
|
|
|
|
// Copy the depth buffer
|
|
|
|
trans.main.bind_read();
|
|
|
|
trans.trans.bind_draw();
|
|
|
|
gl::blit_framebuffer(
|
|
|
|
0, 0, width as i32, height as i32,
|
|
|
|
0, 0, width as i32, height as i32,
|
|
|
|
gl::ClearFlags::Depth, gl::NEAREST
|
|
|
|
);
|
|
|
|
|
|
|
|
trans.trans.bind();
|
|
|
|
gl::enable(gl::BLEND);
|
|
|
|
gl::depth_mask(false);
|
|
|
|
gl::clear_color(0.0, 0.0, 0.0, 1.0);
|
|
|
|
gl::clear(gl::ClearFlags::Color);
|
|
|
|
gl::clear_buffer(gl::COLOR, 0, &[0.0, 0.0, 0.0, 1.0]);
|
|
|
|
gl::clear_buffer(gl::COLOR, 1, &[0.0, 0.0, 0.0, 0.0]);
|
|
|
|
gl::blend_func_separate(gl::ONE_FACTOR, gl::ONE_FACTOR, gl::ZERO_FACTOR, gl::ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
|
|
|
// TODO: Draw chunks
|
|
|
|
|
|
|
|
gl::unbind_framebuffer();
|
|
|
|
gl::disable(gl::DEPTH_TEST);
|
|
|
|
gl::clear(gl::ClearFlags::Color);
|
|
|
|
gl::disable(gl::BLEND);
|
|
|
|
trans.draw(&self.trans_shader);
|
|
|
|
|
|
|
|
gl::enable(gl::DEPTH_TEST);
|
|
|
|
gl::depth_mask(true);
|
|
|
|
gl::blend_func(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
|
|
|
|
gl::disable(gl::MULTISAMPLE);
|
|
|
|
|
2015-09-17 11:04:25 -04:00
|
|
|
self.ui.tick(width, height);
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:33:06 -04:00
|
|
|
fn do_pending_textures(&mut self) {
|
2015-10-07 14:36:59 -04:00
|
|
|
let len = {
|
|
|
|
let tex = self.textures.read().unwrap();
|
2016-03-16 13:33:06 -04:00
|
|
|
// Rebuild the texture if it needs resizing
|
2015-10-07 14:36:59 -04:00
|
|
|
if self.texture_layers != tex.atlases.len() {
|
|
|
|
let len = ATLAS_SIZE * ATLAS_SIZE * 4 * tex.atlases.len();
|
|
|
|
let mut data = Vec::with_capacity(len);
|
|
|
|
unsafe {
|
|
|
|
data.set_len(len);
|
|
|
|
}
|
|
|
|
self.gl_texture.get_pixels(gl::TEXTURE_2D_ARRAY,
|
|
|
|
0,
|
|
|
|
gl::RGBA,
|
|
|
|
gl::UNSIGNED_BYTE,
|
|
|
|
&mut data[..]);
|
|
|
|
self.gl_texture.image_3d(gl::TEXTURE_2D_ARRAY,
|
|
|
|
0,
|
|
|
|
ATLAS_SIZE as u32,
|
|
|
|
ATLAS_SIZE as u32,
|
|
|
|
tex.atlases.len() as u32,
|
|
|
|
gl::RGBA,
|
|
|
|
gl::UNSIGNED_BYTE,
|
|
|
|
&data[..]);
|
|
|
|
self.texture_layers = tex.atlases.len();
|
|
|
|
}
|
|
|
|
tex.pending_uploads.len()
|
|
|
|
};
|
|
|
|
if len > 0 {
|
2016-03-16 13:33:06 -04:00
|
|
|
// Upload pending changes
|
2015-10-07 14:36:59 -04:00
|
|
|
let mut tex = self.textures.write().unwrap();
|
|
|
|
for upload in &tex.pending_uploads {
|
|
|
|
let atlas = upload.0;
|
|
|
|
let rect = upload.1;
|
|
|
|
let img = &upload.2;
|
|
|
|
self.gl_texture.sub_image_3d(gl::TEXTURE_2D_ARRAY,
|
|
|
|
0,
|
|
|
|
rect.x as u32,
|
|
|
|
rect.y as u32,
|
|
|
|
atlas as u32,
|
|
|
|
rect.width as u32,
|
|
|
|
rect.height as u32,
|
|
|
|
1,
|
|
|
|
gl::RGBA,
|
|
|
|
gl::UNSIGNED_BYTE,
|
|
|
|
&img[..]);
|
|
|
|
}
|
|
|
|
tex.pending_uploads.clear();
|
|
|
|
}
|
2016-03-16 13:33:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update_textures(&mut self, delta: f64) {
|
|
|
|
self.gl_texture.bind(gl::TEXTURE_2D_ARRAY);
|
|
|
|
self.do_pending_textures();
|
2015-10-07 14:36:59 -04:00
|
|
|
|
|
|
|
for ani in &mut self.textures.write().unwrap().animated_textures {
|
|
|
|
if ani.remaining_time <= 0.0 {
|
|
|
|
ani.current_frame = (ani.current_frame + 1) % ani.frames.len();
|
|
|
|
ani.remaining_time += ani.frames[ani.current_frame].time as f64;
|
|
|
|
let offset = ani.texture.width * ani.texture.width *
|
|
|
|
ani.frames[ani.current_frame].index * 4;
|
|
|
|
let offset2 = offset + ani.texture.width * ani.texture.width * 4;
|
|
|
|
self.gl_texture.sub_image_3d(gl::TEXTURE_2D_ARRAY,
|
|
|
|
0,
|
|
|
|
ani.texture.get_x() as u32,
|
|
|
|
ani.texture.get_y() as u32,
|
|
|
|
ani.texture.atlas as u32,
|
|
|
|
ani.texture.get_width() as u32,
|
|
|
|
ani.texture.get_height() as u32,
|
|
|
|
1,
|
|
|
|
gl::RGBA,
|
|
|
|
gl::UNSIGNED_BYTE,
|
|
|
|
&ani.data[offset..offset2]);
|
|
|
|
} else {
|
|
|
|
ani.remaining_time -= delta / 3.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:33:06 -04:00
|
|
|
fn init_trans(&mut self, width: u32, height: u32) {
|
|
|
|
self.trans = None;
|
2016-03-18 11:19:21 -04:00
|
|
|
self.trans = Some(TransInfo::new(width, height, &self.trans_shader));
|
2016-03-16 13:33:06 -04:00
|
|
|
}
|
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
pub fn get_textures(&self) -> Arc<RwLock<TextureManager>> {
|
|
|
|
self.textures.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_textures_ref(&self) -> &RwLock<TextureManager> {
|
|
|
|
&self.textures
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_texture(&self, tex: Texture) -> Texture {
|
|
|
|
if tex.version == self.resource_version {
|
|
|
|
tex
|
|
|
|
} else {
|
|
|
|
let mut new = Renderer::get_texture(&self.textures, &tex.name);
|
|
|
|
new.rel_x = tex.rel_x;
|
|
|
|
new.rel_y = tex.rel_y;
|
|
|
|
new.rel_width = tex.rel_width;
|
|
|
|
new.rel_height = tex.rel_height;
|
|
|
|
new.is_rel = tex.is_rel;
|
|
|
|
new
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_texture(textures: &RwLock<TextureManager>, name: &str) -> Texture {
|
|
|
|
let tex = {
|
|
|
|
textures.read().unwrap().get_texture(name)
|
|
|
|
};
|
|
|
|
match tex {
|
|
|
|
Some(val) => val,
|
|
|
|
None => {
|
|
|
|
let mut t = textures.write().unwrap();
|
2016-03-16 13:33:06 -04:00
|
|
|
// Make sure it hasn't already been loaded since we switched
|
|
|
|
// locks.
|
2015-10-07 14:36:59 -04:00
|
|
|
if let Some(val) = t.get_texture(name) {
|
|
|
|
val
|
|
|
|
} else {
|
|
|
|
t.load_texture(name);
|
|
|
|
t.get_texture(name).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:33:06 -04:00
|
|
|
struct TransInfo {
|
|
|
|
main: gl::Framebuffer,
|
2016-03-16 14:01:33 -04:00
|
|
|
fb_color: gl::Texture,
|
|
|
|
fb_depth: gl::Texture,
|
2016-03-16 13:33:06 -04:00
|
|
|
trans: gl::Framebuffer,
|
|
|
|
accum: gl::Texture,
|
|
|
|
revealage: gl::Texture,
|
|
|
|
depth: gl::Texture,
|
|
|
|
|
|
|
|
array: gl::VertexArray,
|
|
|
|
buffer: gl::Buffer,
|
|
|
|
}
|
|
|
|
|
|
|
|
init_shader! {
|
|
|
|
Program TransShader {
|
|
|
|
vert = "trans_vertex",
|
|
|
|
frag = "trans_frag",
|
|
|
|
attribute = {
|
|
|
|
position => "aPosition",
|
|
|
|
},
|
|
|
|
uniform = {
|
|
|
|
accum => "taccum",
|
|
|
|
revealage => "trevealage",
|
|
|
|
color => "tcolor",
|
|
|
|
samples => "samples",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TransInfo {
|
2016-03-18 11:19:21 -04:00
|
|
|
pub fn new(width: u32, height: u32, shader: &TransShader) -> TransInfo {
|
2016-03-16 13:33:06 -04:00
|
|
|
let trans = gl::Framebuffer::new();
|
|
|
|
trans.bind();
|
|
|
|
|
|
|
|
let accum = gl::Texture::new();
|
|
|
|
accum.bind(gl::TEXTURE_2D);
|
2016-03-18 11:19:21 -04:00
|
|
|
accum.image_2d_ex(gl::TEXTURE_2D, 0, width, height, gl::RGBA16F, gl::RGBA, gl::FLOAT, None);
|
|
|
|
accum.set_parameter(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR);
|
|
|
|
accum.set_parameter(gl::TEXTURE_2D, gl::TEXTURE_MAX_LEVEL, gl::LINEAR);
|
|
|
|
trans.texture_2d(gl::COLOR_ATTACHMENT_0, gl::TEXTURE_2D, &accum, 0);
|
|
|
|
|
|
|
|
let revealage = gl::Texture::new();
|
|
|
|
revealage.bind(gl::TEXTURE_2D);
|
|
|
|
revealage.image_2d_ex(gl::TEXTURE_2D, 0, width, height, gl::R16F, gl::RED, gl::FLOAT, None);
|
|
|
|
revealage.set_parameter(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR);
|
|
|
|
revealage.set_parameter(gl::TEXTURE_2D, gl::TEXTURE_MAX_LEVEL, gl::LINEAR);
|
|
|
|
trans.texture_2d(gl::COLOR_ATTACHMENT_1, gl::TEXTURE_2D, &revealage, 0);
|
|
|
|
|
|
|
|
let trans_depth = gl::Texture::new();
|
|
|
|
trans_depth.bind(gl::TEXTURE_2D);
|
|
|
|
trans_depth.image_2d_ex(gl::TEXTURE_2D, 0, width, height, gl::DEPTH_COMPONENT24, gl::DEPTH_COMPONENT, gl::UNSIGNED_BYTE, None);
|
|
|
|
trans_depth.set_parameter(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR);
|
|
|
|
trans_depth.set_parameter(gl::TEXTURE_2D, gl::TEXTURE_MAX_LEVEL, gl::LINEAR);
|
|
|
|
trans.texture_2d(gl::DEPTH_ATTACHMENT, gl::TEXTURE_2D, &trans_depth, 0);
|
|
|
|
|
|
|
|
shader.program.use_program();
|
|
|
|
gl::bind_frag_data_location(&shader.program, 0, "accum");
|
|
|
|
gl::bind_frag_data_location(&shader.program, 1, "revealage");
|
|
|
|
gl::draw_buffers(&[gl::COLOR_ATTACHMENT_0, gl::COLOR_ATTACHMENT_1]);
|
|
|
|
|
|
|
|
|
|
|
|
let main = gl::Framebuffer::new();
|
|
|
|
main.bind();
|
|
|
|
|
|
|
|
let fb_color = gl::Texture::new();
|
|
|
|
fb_color.bind(gl::TEXTURE_2D_MULTISAMPLE);
|
|
|
|
fb_color.image_2d_sample(gl::TEXTURE_2D_MULTISAMPLE, NUM_SAMPLES, width, height, gl::RGBA8, false);
|
|
|
|
main.texture_2d(gl::COLOR_ATTACHMENT_0, gl::TEXTURE_2D_MULTISAMPLE, &fb_color, 0);
|
|
|
|
|
|
|
|
let fb_depth = gl::Texture::new();
|
|
|
|
fb_depth.bind(gl::TEXTURE_2D_MULTISAMPLE);
|
|
|
|
fb_depth.image_2d_sample(gl::TEXTURE_2D_MULTISAMPLE, NUM_SAMPLES, width, height, gl::DEPTH_COMPONENT24, false);
|
|
|
|
main.texture_2d(gl::DEPTH_ATTACHMENT, gl::TEXTURE_2D_MULTISAMPLE, &fb_depth, 0);
|
|
|
|
|
|
|
|
gl::unbind_framebuffer();
|
|
|
|
|
|
|
|
let array = gl::VertexArray::new();
|
|
|
|
array.bind();
|
|
|
|
let buffer = gl::Buffer::new();
|
|
|
|
buffer.bind(gl::ARRAY_BUFFER);
|
|
|
|
|
|
|
|
let mut data = vec![];
|
|
|
|
for f in [-1.0, 1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0].into_iter() {
|
|
|
|
data.write_f32::<NativeEndian>(*f).unwrap();
|
|
|
|
}
|
|
|
|
buffer.set_data(gl::ARRAY_BUFFER, &data, gl::STATIC_DRAW);
|
|
|
|
|
|
|
|
shader.position.enable();
|
|
|
|
shader.position.vertex_pointer(2, gl::FLOAT, false, 8, 0);
|
|
|
|
|
|
|
|
TransInfo {
|
|
|
|
main: main,
|
|
|
|
fb_color: fb_color,
|
|
|
|
fb_depth: fb_depth,
|
|
|
|
trans: trans,
|
|
|
|
accum: accum,
|
|
|
|
revealage: revealage,
|
|
|
|
depth: trans_depth,
|
|
|
|
|
|
|
|
array: array,
|
|
|
|
buffer: buffer,
|
|
|
|
}
|
|
|
|
}
|
2016-03-16 13:33:06 -04:00
|
|
|
|
2016-03-18 11:19:21 -04:00
|
|
|
fn draw(&mut self, shader: &TransShader) {
|
|
|
|
gl::active_texture(0);
|
|
|
|
self.accum.bind(gl::TEXTURE_2D);
|
|
|
|
gl::active_texture(1);
|
|
|
|
self.revealage.bind(gl::TEXTURE_2D);
|
|
|
|
gl::active_texture(2);
|
|
|
|
self.fb_color.bind(gl::TEXTURE_2D_MULTISAMPLE);
|
|
|
|
|
|
|
|
shader.program.use_program();
|
|
|
|
shader.accum.set_int(0);
|
|
|
|
shader.revealage.set_int(1);
|
|
|
|
shader.color.set_int(2);
|
|
|
|
shader.samples.set_int(NUM_SAMPLES);
|
|
|
|
self.array.bind();
|
|
|
|
gl::draw_arrays(gl::TRIANGLES, 0, 6);
|
2016-03-16 13:33:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 11:04:25 -04:00
|
|
|
pub struct TextureManager {
|
2015-10-07 14:36:59 -04:00
|
|
|
textures: HashMap<String, Texture>,
|
|
|
|
version: usize,
|
|
|
|
resources: Arc<RwLock<resources::Manager>>,
|
|
|
|
atlases: Vec<atlas::Atlas>,
|
2015-09-17 11:04:25 -04:00
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
animated_textures: Vec<AnimatedTexture>,
|
|
|
|
pending_uploads: Vec<(i32, atlas::Rect, Vec<u8>)>,
|
2015-09-25 09:00:49 -04:00
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
dynamic_textures: HashMap<String, (i32, atlas::Rect)>,
|
|
|
|
free_dynamics: Vec<(i32, atlas::Rect)>,
|
2015-09-17 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TextureManager {
|
2015-10-07 14:36:59 -04:00
|
|
|
fn new(res: Arc<RwLock<resources::Manager>>) -> TextureManager {
|
|
|
|
let mut tm = TextureManager {
|
|
|
|
textures: HashMap::new(),
|
|
|
|
version: 0xFFFF,
|
|
|
|
resources: res,
|
|
|
|
atlases: Vec::new(),
|
|
|
|
animated_textures: Vec::new(),
|
|
|
|
pending_uploads: Vec::new(),
|
|
|
|
|
|
|
|
dynamic_textures: HashMap::new(),
|
|
|
|
free_dynamics: Vec::new(),
|
|
|
|
};
|
|
|
|
tm.add_defaults();
|
|
|
|
tm
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_defaults(&mut self) {
|
|
|
|
self.put_texture("steven",
|
|
|
|
"missing_texture",
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
vec![
|
2016-03-16 13:33:06 -04:00
|
|
|
0, 0, 0, 255,
|
|
|
|
255, 0, 255, 255,
|
|
|
|
255, 0, 255, 255,
|
|
|
|
0, 0, 0, 255,
|
|
|
|
]);
|
2015-10-07 14:36:59 -04:00
|
|
|
self.put_texture("steven",
|
|
|
|
"solid",
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
vec![
|
2016-03-16 13:33:06 -04:00
|
|
|
255, 255, 255, 255,
|
|
|
|
]);
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update_textures(&mut self, version: usize) {
|
|
|
|
self.dynamic_textures.clear();
|
|
|
|
self.free_dynamics.clear();
|
|
|
|
self.pending_uploads.clear();
|
|
|
|
self.atlases.clear();
|
|
|
|
self.animated_textures.clear();
|
|
|
|
self.version = version;
|
|
|
|
let map = self.textures.clone();
|
|
|
|
self.textures.clear();
|
|
|
|
|
|
|
|
self.add_defaults();
|
|
|
|
|
|
|
|
for name in map.keys() {
|
|
|
|
self.load_texture(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_texture(&self, name: &str) -> Option<Texture> {
|
|
|
|
self.textures.get(name).map(|v| v.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_texture(&mut self, name: &str) {
|
|
|
|
let (plugin, name) = if let Some(pos) = name.find(':') {
|
|
|
|
(&name[..pos], &name[pos + 1..])
|
|
|
|
} else {
|
|
|
|
("minecraft", name)
|
|
|
|
};
|
|
|
|
let path = format!("textures/{}.png", name);
|
|
|
|
let res = self.resources.clone();
|
|
|
|
if let Some(mut val) = res.read().unwrap().open(plugin, &path) {
|
|
|
|
let mut data = Vec::new();
|
|
|
|
val.read_to_end(&mut data).unwrap();
|
|
|
|
if let Ok(img) = image::load_from_memory(&data) {
|
|
|
|
let (width, height) = img.dimensions();
|
2016-03-16 13:33:06 -04:00
|
|
|
// Might be animated
|
2015-10-07 14:36:59 -04:00
|
|
|
if (name.starts_with("blocks/") || name.starts_with("items/")) && width != height {
|
|
|
|
let id = img.to_rgba().into_vec();
|
|
|
|
let frame = id[..(width * width * 4) as usize].to_owned();
|
|
|
|
if let Some(mut ani) = self.load_animation(plugin, name, &img, id) {
|
|
|
|
ani.texture = self.put_texture(plugin, name, width, width, frame);
|
|
|
|
self.animated_textures.push(ani);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.put_texture(plugin, name, width, height, img.to_rgba().into_vec());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.insert_texture_dummy(plugin, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_animation(&mut self,
|
|
|
|
plugin: &str,
|
|
|
|
name: &str,
|
|
|
|
img: &image::DynamicImage,
|
|
|
|
data: Vec<u8>)
|
|
|
|
-> Option<AnimatedTexture> {
|
|
|
|
let path = format!("textures/{}.png.mcmeta", name);
|
|
|
|
let res = self.resources.clone();
|
|
|
|
if let Some(val) = res.read().unwrap().open(plugin, &path) {
|
|
|
|
let meta: serde_json::Value = serde_json::from_reader(val).unwrap();
|
|
|
|
let animation = meta.find("animation").unwrap();
|
|
|
|
let frame_time = animation.find("frameTime").and_then(|v| v.as_i64()).unwrap_or(1);
|
|
|
|
let interpolate = animation.find("interpolate")
|
|
|
|
.and_then(|v| v.as_boolean())
|
|
|
|
.unwrap_or(false);
|
|
|
|
let frames = if let Some(frames) = animation.find("frames")
|
|
|
|
.and_then(|v| v.as_array()) {
|
|
|
|
let mut out = Vec::with_capacity(frames.len());
|
|
|
|
for frame in frames {
|
|
|
|
if let Some(index) = frame.as_i64() {
|
|
|
|
out.push(AnimationFrame {
|
|
|
|
index: index as usize,
|
|
|
|
time: frame_time,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
out.push(AnimationFrame{
|
2016-03-16 13:33:06 -04:00
|
|
|
index: frame.find("index").unwrap().as_i64().unwrap() as usize,
|
|
|
|
time: frame_time * frame.find("frameTime").unwrap().as_i64().unwrap(),
|
|
|
|
})
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out
|
|
|
|
} else {
|
|
|
|
let (width, height) = img.dimensions();
|
|
|
|
let count = height / width;
|
|
|
|
let mut frames = Vec::with_capacity(count as usize);
|
|
|
|
for i in 0..count {
|
|
|
|
frames.push(AnimationFrame {
|
|
|
|
index: i as usize,
|
|
|
|
time: frame_time,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
frames
|
|
|
|
};
|
|
|
|
|
|
|
|
return Some(AnimatedTexture {
|
|
|
|
frames: frames,
|
|
|
|
data: data,
|
|
|
|
interpolate: interpolate,
|
|
|
|
current_frame: 0,
|
|
|
|
remaining_time: 0.0,
|
|
|
|
texture: self.get_texture("steven:missing_texture").unwrap(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn put_texture(&mut self,
|
|
|
|
plugin: &str,
|
|
|
|
name: &str,
|
|
|
|
width: u32,
|
|
|
|
height: u32,
|
|
|
|
data: Vec<u8>)
|
|
|
|
-> Texture {
|
|
|
|
let (atlas, rect) = self.find_free(width as usize, height as usize);
|
|
|
|
self.pending_uploads.push((atlas, rect, data));
|
|
|
|
|
|
|
|
let mut full_name = String::new();
|
|
|
|
if plugin != "minecraft" {
|
|
|
|
full_name.push_str(plugin);
|
|
|
|
full_name.push_str(":");
|
|
|
|
}
|
|
|
|
full_name.push_str(name);
|
|
|
|
|
|
|
|
let tex = Texture {
|
|
|
|
name: full_name.clone(),
|
|
|
|
version: self.version,
|
|
|
|
atlas: atlas,
|
|
|
|
x: rect.x,
|
|
|
|
y: rect.y,
|
|
|
|
width: rect.width,
|
|
|
|
height: rect.height,
|
|
|
|
rel_x: 0.0,
|
|
|
|
rel_y: 0.0,
|
|
|
|
rel_width: 1.0,
|
|
|
|
rel_height: 1.0,
|
|
|
|
is_rel: false,
|
|
|
|
};
|
|
|
|
self.textures.insert(full_name, tex.clone());
|
|
|
|
tex
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_free(&mut self, width: usize, height: usize) -> (i32, atlas::Rect) {
|
|
|
|
let mut index = 0;
|
|
|
|
for atlas in &mut self.atlases {
|
|
|
|
if let Some(rect) = atlas.add(width, height) {
|
|
|
|
return (index, rect);
|
|
|
|
}
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
let mut atlas = atlas::Atlas::new(ATLAS_SIZE, ATLAS_SIZE);
|
|
|
|
let rect = atlas.add(width, height);
|
|
|
|
self.atlases.push(atlas);
|
|
|
|
(index, rect.unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_texture_dummy(&mut self, plugin: &str, name: &str) -> Texture {
|
|
|
|
let missing = self.get_texture("steven:missing_texture").unwrap();
|
|
|
|
|
|
|
|
let mut full_name = String::new();
|
|
|
|
if plugin != "minecraft" {
|
|
|
|
full_name.push_str(plugin);
|
|
|
|
full_name.push_str(":");
|
|
|
|
}
|
|
|
|
full_name.push_str(name);
|
|
|
|
|
|
|
|
let t = Texture {
|
|
|
|
name: full_name.to_owned(),
|
|
|
|
version: self.version,
|
|
|
|
atlas: missing.atlas,
|
|
|
|
x: missing.x,
|
|
|
|
y: missing.y,
|
|
|
|
width: missing.width,
|
|
|
|
height: missing.height,
|
|
|
|
rel_x: 0.0,
|
|
|
|
rel_y: 0.0,
|
|
|
|
rel_width: 1.0,
|
|
|
|
rel_height: 1.0,
|
|
|
|
is_rel: false,
|
|
|
|
};
|
|
|
|
self.textures.insert(full_name.to_owned(), t.clone());
|
|
|
|
t
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn put_dynamic(&mut self, plugin: &str, name: &str, img: image::DynamicImage) -> Texture {
|
|
|
|
let (width, height) = img.dimensions();
|
|
|
|
let (width, height) = (width as usize, height as usize);
|
|
|
|
let mut rect = None;
|
|
|
|
let mut rect_pos = 0;
|
|
|
|
for (i, r) in self.free_dynamics.iter().enumerate() {
|
|
|
|
let (atlas, r) = *r;
|
|
|
|
if r.width == width && r.height == height {
|
|
|
|
rect_pos = i;
|
|
|
|
rect = Some((atlas, r));
|
|
|
|
break;
|
|
|
|
} else if r.width >= width && r.height >= height {
|
|
|
|
rect_pos = i;
|
|
|
|
rect = Some((atlas, r));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let data = img.to_rgba().into_vec();
|
|
|
|
let mut new = false;
|
|
|
|
let (atlas, rect) = if let Some(r) = rect {
|
|
|
|
self.free_dynamics.remove(rect_pos);
|
|
|
|
r
|
|
|
|
} else {
|
|
|
|
new = true;
|
|
|
|
self.find_free(width as usize, height as usize)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut full_name = String::new();
|
|
|
|
if plugin != "minecraft" {
|
|
|
|
full_name.push_str(plugin);
|
|
|
|
full_name.push_str(":");
|
|
|
|
}
|
|
|
|
full_name.push_str(name);
|
|
|
|
|
|
|
|
self.dynamic_textures.insert(full_name.clone(), (atlas, rect));
|
|
|
|
if new {
|
|
|
|
self.put_texture(plugin, name, width as u32, height as u32, data)
|
|
|
|
} else {
|
|
|
|
let t = Texture {
|
|
|
|
name: full_name.clone(),
|
|
|
|
version: self.version,
|
|
|
|
atlas: atlas,
|
|
|
|
x: rect.x,
|
|
|
|
y: rect.y,
|
|
|
|
width: rect.width,
|
|
|
|
height: rect.height,
|
|
|
|
rel_x: 0.0,
|
|
|
|
rel_y: 0.0,
|
|
|
|
rel_width: 1.0,
|
|
|
|
rel_height: 1.0,
|
|
|
|
is_rel: false,
|
|
|
|
};
|
|
|
|
let rect = atlas::Rect {
|
|
|
|
x: rect.x,
|
|
|
|
y: rect.y,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
};
|
|
|
|
self.pending_uploads.push((atlas, rect, data));
|
|
|
|
self.textures.insert(full_name.to_owned(), t.clone());
|
|
|
|
t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_dynamic(&mut self, plugin: &str, name: &str) {
|
|
|
|
let mut full_name = String::new();
|
|
|
|
if plugin != "minecraft" {
|
|
|
|
full_name.push_str(plugin);
|
|
|
|
full_name.push_str(":");
|
|
|
|
}
|
|
|
|
full_name.push_str(name);
|
|
|
|
|
|
|
|
let desc = self.dynamic_textures.remove(&full_name).unwrap();
|
|
|
|
self.free_dynamics.push(desc);
|
|
|
|
}
|
2015-09-17 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 10:20:55 -04:00
|
|
|
#[allow(dead_code)]
|
2015-09-17 11:04:25 -04:00
|
|
|
struct AnimatedTexture {
|
2015-10-07 14:36:59 -04:00
|
|
|
frames: Vec<AnimationFrame>,
|
|
|
|
data: Vec<u8>,
|
|
|
|
interpolate: bool,
|
|
|
|
current_frame: usize,
|
|
|
|
remaining_time: f64,
|
|
|
|
texture: Texture,
|
2015-09-17 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct AnimationFrame {
|
2015-10-07 14:36:59 -04:00
|
|
|
index: usize,
|
|
|
|
time: i64,
|
2015-09-17 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Texture {
|
2015-10-07 14:36:59 -04:00
|
|
|
name: String,
|
|
|
|
version: usize,
|
|
|
|
pub atlas: i32,
|
|
|
|
x: usize,
|
|
|
|
y: usize,
|
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
is_rel: bool, // Save some cycles for none relative textures
|
|
|
|
rel_x: f32,
|
|
|
|
rel_y: f32,
|
|
|
|
rel_width: f32,
|
|
|
|
rel_height: f32,
|
2015-09-17 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Texture {
|
2015-10-07 14:36:59 -04:00
|
|
|
pub fn get_x(&self) -> usize {
|
|
|
|
if self.is_rel {
|
|
|
|
self.x + ((self.width as f32) * self.rel_x) as usize
|
|
|
|
} else {
|
|
|
|
self.x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_y(&self) -> usize {
|
|
|
|
if self.is_rel {
|
|
|
|
self.y + ((self.height as f32) * self.rel_y) as usize
|
|
|
|
} else {
|
|
|
|
self.y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_width(&self) -> usize {
|
|
|
|
if self.is_rel {
|
|
|
|
((self.width as f32) * self.rel_width) as usize
|
|
|
|
} else {
|
|
|
|
self.width
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_height(&self) -> usize {
|
|
|
|
if self.is_rel {
|
|
|
|
((self.height as f32) * self.rel_height) as usize
|
|
|
|
} else {
|
|
|
|
self.height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn relative(&self, x: f32, y: f32, width: f32, height: f32) -> Texture {
|
|
|
|
Texture {
|
|
|
|
name: self.name.clone(),
|
|
|
|
version: self.version,
|
|
|
|
x: self.x,
|
|
|
|
y: self.y,
|
|
|
|
atlas: self.atlas,
|
|
|
|
width: self.width,
|
|
|
|
height: self.height,
|
|
|
|
is_rel: true,
|
|
|
|
rel_x: self.rel_x + x * self.rel_width,
|
|
|
|
rel_y: self.rel_y + y * self.rel_height,
|
|
|
|
rel_width: width * self.rel_width,
|
|
|
|
rel_height: height * self.rel_height,
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 11:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused_must_use)]
|
|
|
|
pub fn generate_element_buffer(size: usize) -> (Vec<u8>, gl::Type) {
|
2015-10-07 14:36:59 -04:00
|
|
|
let mut ty = gl::UNSIGNED_SHORT;
|
|
|
|
let mut data = if (size / 6) * 4 * 3 >= u16::max_value() as usize {
|
|
|
|
ty = gl::UNSIGNED_INT;
|
|
|
|
Vec::with_capacity(size * 4)
|
|
|
|
} else {
|
|
|
|
Vec::with_capacity(size * 2)
|
|
|
|
};
|
|
|
|
for i in 0..size / 6 {
|
|
|
|
for val in &[0, 1, 2, 2, 1, 3] {
|
|
|
|
if ty == gl::UNSIGNED_INT {
|
|
|
|
data.write_u32::<NativeEndian>((i as u32) * 4 + val);
|
|
|
|
} else {
|
|
|
|
data.write_u16::<NativeEndian>((i as u16) * 4 + (*val as u16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(data, ty)
|
|
|
|
}
|