main
_ 2021-12-19 22:57:09 +00:00
parent 0ace7a7dfa
commit 48304de5e6
3 changed files with 100 additions and 29 deletions

View File

@ -260,6 +260,14 @@ impl Graphics {
None => continue,
Some (x) => x,
};
let uv_coords = match prim.get (&Semantic::TexCoords (0)) {
None => continue,
Some (x) => x,
};
let uv_view = match uv_coords.view () {
None => continue,
Some (x) => x,
};
let indices = match prim.indices () {
None => continue,
Some (x) => x,
@ -278,6 +286,14 @@ impl Graphics {
4 * 3,
&level.buffer [positions.offset () + pos_view.offset ()] as *const u8 as *const c_void,
);
gl::VertexAttribPointer (
attrs [a::UV].unwrap (),
2,
gl::FLOAT,
0,
4 * 2,
&level.buffer [uv_coords.offset () + uv_view.offset ()] as *const u8 as *const c_void,
);
gl::DrawElements (
gl::TRIANGLES,
indices.count ().try_into ().unwrap (),
@ -291,10 +307,10 @@ impl Graphics {
glezz::uniform_3fv (unis [&u::ALBEDO], &shadow_blue);
if false {
if true {
// Raycast for player shadow
let coll = opengl_rust::physics::get_candidate (
&[], &state.aabbs,
&state.phys_tris, &state.aabbs,
state.player.pos,
state.player.pos + Vec3::new (0.0, 0.0, -100.0),
0.0

View File

@ -21,33 +21,11 @@ mod graphics;
use graphics::Graphics;
#[derive (Default)]
pub struct GameState {
player: PhysicsBody,
aabbs: Vec <opengl_rust::physics::Aabb>,
}
impl Default for GameState {
fn default () -> Self {
let player = Default::default ();
let aabbs = [
((-4.0, -4.0, -3.0), (4.0, 4.0, -1.0)),
((-1.5, 1.0, -1.0), (-0.5, 2.0, 0.0)),
((-0.5, 1.0, 0.0), (0.5, 2.0, 1.0)),
((0.5, 1.0, 1.0), (1.5, 2.0, 2.0)),
].into_iter ()
.map (|(min, max)| {
opengl_rust::physics::Aabb {
min: min.into (),
max: max.into (),
}
})
.collect ();
Self {
player,
aabbs,
}
}
phys_tris: Vec <opengl_rust::physics::Triangle>,
}
impl GameState {
@ -66,7 +44,7 @@ async fn main () -> Result <()> {
let sdl_context = sdl2::init ().map_err (|e| anyhow! ("Can't init SDL: {}", e))?;
let video_subsystem = sdl_context.video ().map_err (|e| anyhow! ("Can't get SDL video subsystem: {}", e))?;
let window = video_subsystem.window ("3D platformer", 320, 240)
let window = video_subsystem.window ("3D platformer", 320 * 2, 240 * 2)
.position_centered ()
.opengl ()
.build ()
@ -89,6 +67,82 @@ async fn main () -> Result <()> {
let mut time_step = TimeStep::new (60, 1000);
let level = LoadedLevel::from_path ("gltf/level-00.glb")?;
let scene = level.level.scenes ().next ().unwrap ();
let mut phys_tris: Vec <opengl_rust::physics::Triangle> = Vec::new ();
for node in scene.nodes () {
let mesh = match node.mesh () {
None => continue,
Some (x) => x,
};
let m = Mat4::from_cols_array_2d (&node.transform ().matrix ());
for prim in mesh.primitives () {
use gltf::Semantic;
let positions = match prim.get (&Semantic::Positions) {
None => continue,
Some (x) => x,
};
let pos_view = match positions.view () {
None => continue,
Some (x) => x,
};
let indices = match prim.indices () {
None => continue,
Some (x) => x,
};
let indices_view = match indices.view () {
None => continue,
Some (x) => x,
};
let idx_start = indices.offset () + indices_view.offset ();
let idx_slice = &level.buffer [idx_start..idx_start + indices.count () * 2];
let vert_start = positions.offset () + pos_view.offset ();
let vert_slice = &level.buffer [vert_start..vert_start + positions.count () * 4 * 3];
for chunk in idx_slice.chunks_exact (2 * 3) {
let read_idx = |i: usize| u16::from_le_bytes ([chunk [i * 2 + 0], chunk [i * 2 + 1]]);
let idxs = [
read_idx (0),
read_idx (1),
read_idx (2),
];
let read_pos_coord = |i| f32::from_le_bytes ([
vert_slice [i * 4 + 0],
vert_slice [i * 4 + 1],
vert_slice [i * 4 + 2],
vert_slice [i * 4 + 3],
]);
let read_pos = move |i| {
let i = usize::try_from (i).unwrap ();
m.transform_point3 (Vec3::new (
read_pos_coord (i * 3 + 0),
read_pos_coord (i * 3 + 1),
read_pos_coord (i * 3 + 2),
))
};
// glTF triangle winding is backwards from what I expected
// no biggie
let verts = [
read_pos (idxs [0]),
read_pos (idxs [2]),
read_pos (idxs [1]),
];
phys_tris.push (opengl_rust::physics::Triangle {
verts,
});
}
}
}
let mut graphics = Graphics::new ();
@ -105,6 +159,7 @@ async fn main () -> Result <()> {
let mut player_jump_vec: Option <Vec3> = None;
game_state.reset_level (&level);
game_state.phys_tris = phys_tris;
'running: loop {
let _frames_to_do = time_step.step ();
@ -157,7 +212,7 @@ async fn main () -> Result <()> {
}
}
let phys_result = opengl_rust::physics::step (&phys_params, &[], &game_state.aabbs, 0.5, &game_state.player);
let phys_result = opengl_rust::physics::step (&phys_params, &game_state.phys_tris, &[], 0.5, &game_state.player);
game_state.player = phys_result.body;
// tracing::debug! ("player pos: {}", game_state.player.pos);

View File

@ -14,7 +14,7 @@ pub struct PhysicsResult {
pub kill: bool,
}
#[derive (Copy, Clone)]
#[derive (Copy, Clone, Debug)]
pub struct Triangle {
pub verts: [Vec3; 3],
}