Display multiple instances of OBJs

This commit is contained in:
2022-10-31 20:12:46 +01:00
parent 89387d82fd
commit e4ad531a7e
4 changed files with 126 additions and 9 deletions
+70 -6
View File
@@ -13,6 +13,12 @@ use glium::{glutin, Surface};
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: [&str; BIOMES_NUMBER] = [
"dirt",
@@ -46,7 +52,7 @@ fn main() {
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
// 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| {
println!("{}", biome);
@@ -113,7 +119,7 @@ fn main() {
);
// the program
let program = program!(&display,
let ground_program = program!(&display,
140 => {
vertex: "
#version 140
@@ -175,12 +181,51 @@ fn main() {
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
support::start_loop(event_loop, move |events| {
camera.update();
// building the uniforms
let uniforms = uniform! {
let ground_uniforms = uniform! {
persp_matrix: camera.get_perspective(),
view_matrix: camera.get_view(),
tex0: &textures[0],
@@ -204,6 +249,11 @@ fn main() {
tex18: &textures[18],
};
let structures_uniforms = uniform! {
persp_matrix: camera.get_perspective(),
view_matrix: camera.get_view(),
};
// draw parameters
let params = glium::DrawParameters {
depth: glium::Depth {
@@ -216,19 +266,33 @@ fn main() {
..Default::default()
};
let per_instance_buffer =
glium::vertex::VertexBuffer::new(&display, &per_instance).unwrap();
// drawing a frame
let start = Instant::now();
let mut target = display.draw();
target.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
target
.draw(
&vertex_buffer,
&ground_vertex_buffer,
&glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip),
&program,
&uniforms,
&ground_program,
&ground_uniforms,
&params,
)
.unwrap();
target
.draw(
(&vertex_buffer, per_instance_buffer.per_instance().unwrap()),
&glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
&structures_program,
&structures_uniforms,
&params,
)
.unwrap();
target.finish().unwrap();
let duration = start.elapsed();
println!("Time elapsed is: {:?}", duration);