119 lines
3.4 KiB
Rust
119 lines
3.4 KiB
Rust
|
|
use macroquad::prelude as mq;
|
||
|
|
use rapier3d::prelude::*;
|
||
|
|
|
||
|
|
mod engine;
|
||
|
|
use engine::*;
|
||
|
|
|
||
|
|
mod graphics_util;
|
||
|
|
use graphics_util::*;
|
||
|
|
|
||
|
|
#[macroquad::main("3D")]
|
||
|
|
async fn main() {
|
||
|
|
// Graphics Initialization
|
||
|
|
|
||
|
|
let vertex = include_str!("shaders/shader.vert");
|
||
|
|
let fragment = include_str!("shaders/shader.frag");
|
||
|
|
|
||
|
|
let material = mq::load_material(
|
||
|
|
mq::ShaderSource::Glsl { vertex, fragment },
|
||
|
|
mq::MaterialParams {
|
||
|
|
pipeline_params: mq::PipelineParams {
|
||
|
|
depth_test: mq::Comparison::LessOrEqual,
|
||
|
|
depth_write: true,
|
||
|
|
..Default::default()
|
||
|
|
},
|
||
|
|
uniforms: vec![mq::UniformDesc::new(
|
||
|
|
"render_normals_bool",
|
||
|
|
mq::UniformType::Int1,
|
||
|
|
)],
|
||
|
|
..Default::default()
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.unwrap();
|
||
|
|
material.set_uniform("render_normals_bool", 0);
|
||
|
|
|
||
|
|
// Physics Initialization
|
||
|
|
let mut world = World::default();
|
||
|
|
world.register_free_collider(
|
||
|
|
ColliderBuilder::cuboid(5.0, 0.1, 5.0)
|
||
|
|
.restitution(0.5)
|
||
|
|
.build(),
|
||
|
|
None,
|
||
|
|
);
|
||
|
|
|
||
|
|
let body = world.register_body(
|
||
|
|
RigidBodyBuilder::dynamic()
|
||
|
|
.translation(vector![0.0, 10.0, 0.0])
|
||
|
|
.rotation(vector![std::f32::consts::PI / 4.2, 0.0, 0.0])
|
||
|
|
.build(),
|
||
|
|
);
|
||
|
|
|
||
|
|
let coll = world.register_collider(
|
||
|
|
ColliderBuilder::cuboid(0.5, 0.5, 0.5)
|
||
|
|
.restitution(0.5)
|
||
|
|
.build(),
|
||
|
|
body,
|
||
|
|
None,
|
||
|
|
);
|
||
|
|
|
||
|
|
// Main Loop
|
||
|
|
loop {
|
||
|
|
// Physics Simulation
|
||
|
|
world.step();
|
||
|
|
|
||
|
|
// Graphics Rendering
|
||
|
|
mq::clear_background(mq::LIGHTGRAY);
|
||
|
|
|
||
|
|
mq::set_camera(&mq::Camera3D {
|
||
|
|
position: mq::vec3(-20., 15., 0.),
|
||
|
|
up: mq::vec3(0., 1., 0.),
|
||
|
|
target: mq::vec3(0., 0., 0.),
|
||
|
|
..Default::default()
|
||
|
|
});
|
||
|
|
|
||
|
|
mq::draw_grid(20, 1., mq::BLACK, mq::GRAY);
|
||
|
|
|
||
|
|
for (handle, coll) in world.colliders.iter() {
|
||
|
|
let position: glam::Vec3 = world.position_of_collider(handle).unwrap().into();
|
||
|
|
let rotation = *coll.rotation();
|
||
|
|
unsafe {
|
||
|
|
let context = mq::get_internal_gl().quad_gl;
|
||
|
|
|
||
|
|
context.push_model_matrix(mq::Mat4::from_rotation_translation(
|
||
|
|
rotation.into(),
|
||
|
|
position,
|
||
|
|
));
|
||
|
|
}
|
||
|
|
let color = world.collider_data.get(&handle).unwrap().color;
|
||
|
|
match coll.shape().shape_type() {
|
||
|
|
ShapeType::Ball => {
|
||
|
|
mq::draw_sphere(
|
||
|
|
mq::vec3(0.0, 0.0, 0.0),
|
||
|
|
coll.shape().as_ball().unwrap().radius,
|
||
|
|
None,
|
||
|
|
color,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
ShapeType::Cuboid => {
|
||
|
|
mq::gl_use_material(&material);
|
||
|
|
draw_cuboid(
|
||
|
|
mq::vec3(0.0, 0.0, 0.0),
|
||
|
|
(coll.shape().as_cuboid().unwrap().half_extents * 2.0).into(),
|
||
|
|
color,
|
||
|
|
);
|
||
|
|
mq::gl_use_default_material();
|
||
|
|
}
|
||
|
|
_ => println!("Not implemented.. Skipping"),
|
||
|
|
}
|
||
|
|
unsafe {
|
||
|
|
let context = mq::get_internal_gl().quad_gl;
|
||
|
|
context.pop_model_matrix();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// draw_sphere(world.position_of_rb(body).unwrap().into(), 0.5, None, BLUE);
|
||
|
|
|
||
|
|
mq::next_frame().await
|
||
|
|
}
|
||
|
|
}
|