Display multiple instances of OBJs
This commit is contained in:
@@ -7,4 +7,5 @@ edition = "2021"
|
|||||||
glium = "0.32.1"
|
glium = "0.32.1"
|
||||||
image = "0.24.4"
|
image = "0.24.4"
|
||||||
glutin = "0.29.1"
|
glutin = "0.29.1"
|
||||||
|
obj = "0.10.2"
|
||||||
load_file = "1.0.1"
|
load_file = "1.0.1"
|
||||||
+70
-6
@@ -13,6 +13,12 @@ use glium::{glutin, Surface};
|
|||||||
|
|
||||||
mod support;
|
mod support;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
struct PerInstance {
|
||||||
|
pub w_position: (f32, f32, f32),
|
||||||
|
}
|
||||||
|
implement_vertex!(PerInstance, w_position);
|
||||||
|
|
||||||
const BIOMES_NUMBER: usize = 19;
|
const BIOMES_NUMBER: usize = 19;
|
||||||
const BIOMES: [&str; BIOMES_NUMBER] = [
|
const BIOMES: [&str; BIOMES_NUMBER] = [
|
||||||
"dirt",
|
"dirt",
|
||||||
@@ -46,7 +52,7 @@ fn main() {
|
|||||||
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
|
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
|
||||||
|
|
||||||
// building the vertex and index buffers
|
// building the vertex and index buffers
|
||||||
let vertex_buffer = support::load_ground(&display);
|
let ground_vertex_buffer = support::load_ground(&display);
|
||||||
|
|
||||||
let textures: [SrgbTexture2d; BIOMES_NUMBER] = BIOMES.map(|biome| {
|
let textures: [SrgbTexture2d; BIOMES_NUMBER] = BIOMES.map(|biome| {
|
||||||
println!("{}", biome);
|
println!("{}", biome);
|
||||||
@@ -113,7 +119,7 @@ fn main() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// the program
|
// the program
|
||||||
let program = program!(&display,
|
let ground_program = program!(&display,
|
||||||
140 => {
|
140 => {
|
||||||
vertex: "
|
vertex: "
|
||||||
#version 140
|
#version 140
|
||||||
@@ -175,12 +181,51 @@ fn main() {
|
|||||||
|
|
||||||
let mut camera = support::camera::CameraState::new();
|
let mut camera = support::camera::CameraState::new();
|
||||||
|
|
||||||
|
let vertex_buffer = support::load_wavefront(&display, &load_bytes!("../Extensions/LemnosLife/Map/Common/Structures/24.obj"));
|
||||||
|
|
||||||
|
let structures_program = program!(&display,
|
||||||
|
140 => {
|
||||||
|
vertex: "
|
||||||
|
#version 140
|
||||||
|
|
||||||
|
uniform mat4 persp_matrix;
|
||||||
|
uniform mat4 view_matrix;
|
||||||
|
|
||||||
|
in vec3 w_position;
|
||||||
|
in vec3 position;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = persp_matrix * view_matrix * vec4(position + w_position, 1.0);
|
||||||
|
}
|
||||||
|
",
|
||||||
|
|
||||||
|
fragment: "
|
||||||
|
#version 140
|
||||||
|
|
||||||
|
out vec4 f_color;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
f_color = vec4(1.0, 1.0, 1.0, 1.0);
|
||||||
|
}
|
||||||
|
",
|
||||||
|
},
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let per_instance = vec![
|
||||||
|
PerInstance {
|
||||||
|
w_position: (0.0, 0.0, 0.0),
|
||||||
|
},
|
||||||
|
PerInstance {
|
||||||
|
w_position: (1.0, 0.0, 0.0),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
// the main loop
|
// the main loop
|
||||||
support::start_loop(event_loop, move |events| {
|
support::start_loop(event_loop, move |events| {
|
||||||
camera.update();
|
camera.update();
|
||||||
|
|
||||||
// building the uniforms
|
// building the uniforms
|
||||||
let uniforms = uniform! {
|
let ground_uniforms = uniform! {
|
||||||
persp_matrix: camera.get_perspective(),
|
persp_matrix: camera.get_perspective(),
|
||||||
view_matrix: camera.get_view(),
|
view_matrix: camera.get_view(),
|
||||||
tex0: &textures[0],
|
tex0: &textures[0],
|
||||||
@@ -204,6 +249,11 @@ fn main() {
|
|||||||
tex18: &textures[18],
|
tex18: &textures[18],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let structures_uniforms = uniform! {
|
||||||
|
persp_matrix: camera.get_perspective(),
|
||||||
|
view_matrix: camera.get_view(),
|
||||||
|
};
|
||||||
|
|
||||||
// draw parameters
|
// draw parameters
|
||||||
let params = glium::DrawParameters {
|
let params = glium::DrawParameters {
|
||||||
depth: glium::Depth {
|
depth: glium::Depth {
|
||||||
@@ -216,19 +266,33 @@ fn main() {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let per_instance_buffer =
|
||||||
|
glium::vertex::VertexBuffer::new(&display, &per_instance).unwrap();
|
||||||
|
|
||||||
// drawing a frame
|
// drawing a frame
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let mut target = display.draw();
|
let mut target = display.draw();
|
||||||
target.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
|
target.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
|
||||||
target
|
target
|
||||||
.draw(
|
.draw(
|
||||||
&vertex_buffer,
|
&ground_vertex_buffer,
|
||||||
&glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip),
|
&glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip),
|
||||||
&program,
|
&ground_program,
|
||||||
&uniforms,
|
&ground_uniforms,
|
||||||
¶ms,
|
¶ms,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
target
|
||||||
|
.draw(
|
||||||
|
(&vertex_buffer, per_instance_buffer.per_instance().unwrap()),
|
||||||
|
&glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
|
||||||
|
&structures_program,
|
||||||
|
&structures_uniforms,
|
||||||
|
¶ms,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
target.finish().unwrap();
|
target.finish().unwrap();
|
||||||
let duration = start.elapsed();
|
let duration = start.elapsed();
|
||||||
println!("Time elapsed is: {:?}", duration);
|
println!("Time elapsed is: {:?}", duration);
|
||||||
|
|||||||
@@ -13,14 +13,15 @@ pub struct CameraState {
|
|||||||
rotate_right: bool,
|
rotate_right: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
const SPEED: f32 = 100.0; //1.0;
|
const SPEED: f32 = 1.0; //1.0;
|
||||||
|
|
||||||
impl CameraState {
|
impl CameraState {
|
||||||
pub fn new() -> CameraState {
|
pub fn new() -> CameraState {
|
||||||
CameraState {
|
CameraState {
|
||||||
aspect_ratio: 1_024.0 / 768.0,
|
aspect_ratio: 1_024.0 / 768.0,
|
||||||
// The second coordinate is for the altitude.
|
// The second coordinate is for the altitude.
|
||||||
position: (3_646.41, 12.3622, 13_113.7),
|
//position: (3_646.41, 12.3622, 13_113.7),
|
||||||
|
position: (0.0, 0.0, 0.0),
|
||||||
direction: (0.0, 0.0, -1.0),
|
direction: (0.0, 0.0, -1.0),
|
||||||
moving_up: false,
|
moving_up: false,
|
||||||
moving_left: false,
|
moving_left: false,
|
||||||
@@ -36,7 +37,7 @@ impl CameraState {
|
|||||||
pub fn get_perspective(&self) -> [[f32; 4]; 4] {
|
pub fn get_perspective(&self) -> [[f32; 4]; 4] {
|
||||||
let fov: f32 = std::f32::consts::PI / 2.0;
|
let fov: f32 = std::f32::consts::PI / 2.0;
|
||||||
let zfar = 53_209.0;
|
let zfar = 53_209.0;
|
||||||
let znear = 1.0; //0.01;
|
let znear = 0.01;
|
||||||
|
|
||||||
let f = 1.0 / (fov / 2.0).tan();
|
let f = 1.0 / (fov / 2.0).tan();
|
||||||
|
|
||||||
|
|||||||
@@ -196,3 +196,54 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn load_wavefront(display: &Display, data: &[u8]) -> VertexBufferAny {
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
struct Vertex {
|
||||||
|
position: [f32; 3],
|
||||||
|
texture: [f32; 2],
|
||||||
|
}
|
||||||
|
|
||||||
|
implement_vertex!(Vertex, position, texture);
|
||||||
|
|
||||||
|
let mut data = ::std::io::BufReader::new(data);
|
||||||
|
let data = obj::ObjData::load_buf(&mut data).unwrap();
|
||||||
|
|
||||||
|
let mut vertex_data = Vec::new();
|
||||||
|
|
||||||
|
for object in data.objects.iter() {
|
||||||
|
for polygon in object.groups.iter().flat_map(|g| g.polys.iter()) {
|
||||||
|
match polygon {
|
||||||
|
obj::SimplePolygon(indices) => {
|
||||||
|
for i in 0..=2 {
|
||||||
|
let v = indices[i];
|
||||||
|
let position = data.position[v.0];
|
||||||
|
let texture = v.1.map(|index| data.texture[index]);
|
||||||
|
|
||||||
|
let texture = texture.unwrap_or([0.0, 0.0]);
|
||||||
|
|
||||||
|
vertex_data.push(Vertex {
|
||||||
|
position,
|
||||||
|
texture,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for i in [0, 2, 3] {
|
||||||
|
let v = indices[i];
|
||||||
|
let position = data.position[v.0];
|
||||||
|
let texture = v.1.map(|index| data.texture[index]);
|
||||||
|
|
||||||
|
let texture = texture.unwrap_or([0.0, 0.0]);
|
||||||
|
|
||||||
|
vertex_data.push(Vertex {
|
||||||
|
position,
|
||||||
|
texture,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
glium::vertex::VertexBuffer::new(display, &vertex_data)
|
||||||
|
.unwrap()
|
||||||
|
.into()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user