Skip to content

Commit

Permalink
[General] models
Browse files Browse the repository at this point in the history
  • Loading branch information
Dacode45 committed Sep 17, 2020
1 parent 0dda735 commit ef2a293
Show file tree
Hide file tree
Showing 57 changed files with 2,228 additions and 1,434 deletions.
1 change: 1 addition & 0 deletions raylib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub use ffi::GuiDropdownBoxProperty;
pub use ffi::GuiListViewProperty;
pub use ffi::GuiProgressBarProperty;
pub use ffi::GuiScrollBarProperty;
pub use ffi::GuiScrollBarSide;
pub use ffi::GuiSliderProperty;
pub use ffi::GuiSpinnerProperty;
pub use ffi::GuiTextAlignment;
Expand Down
1 change: 0 additions & 1 deletion raylib/src/core/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ impl Into<ffi::Camera2D> for &Camera2D {
}
}


impl Camera3D {
pub fn camera_type(&self) -> crate::consts::CameraType {
unsafe { std::mem::transmute(self.type_.clone()) }
Expand Down
8 changes: 5 additions & 3 deletions raylib/src/core/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,8 @@ pub trait RaylibDraw3D {

/// Draws a line in 3D world space.
#[inline]
fn draw_line_3d(
#[allow(non_snake_case)]
fn draw_line_3D(
&mut self,
start_pos: impl Into<ffi::Vector3>,
end_pos: impl Into<ffi::Vector3>,
Expand All @@ -1109,7 +1110,8 @@ pub trait RaylibDraw3D {

/// Draws a circle in 3D world space.
#[inline]
fn draw_circle_3d(
#[allow(non_snake_case)]
fn draw_circle_3D(
&mut self,
center: impl Into<ffi::Vector3>,
radius: f32,
Expand Down Expand Up @@ -1410,7 +1412,7 @@ pub trait RaylibDraw3D {
#[inline]
fn draw_billboard(
&mut self,
camera: Camera3D,
camera: impl Into<ffi::Camera3D>,
texture: &Texture2D,
center: impl Into<ffi::Vector3>,
size: f32,
Expand Down
6 changes: 5 additions & 1 deletion raylib/src/core/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ impl RaylibHandle {
}

// Loads model from a generated mesh
pub fn load_model_from_mesh(&mut self, _: &RaylibThread, mesh: &Mesh) -> Result<Model, String> {
pub fn load_model_from_mesh(
&mut self,
_: &RaylibThread,
mesh: WeakMesh,
) -> Result<Model, String> {
let m = unsafe { ffi::LoadModelFromMesh(mesh.0) };

if m.meshes.is_null() || m.materials.is_null() {
Expand Down
2 changes: 1 addition & 1 deletion showcase/src/example/audio/audio_raw_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
}

// Scale read cursor's position to minimize transition artifacts
readCursor = (readCursor * (waveLength / oldWavelength));
readCursor = readCursor * (waveLength / oldWavelength);
oldFrequency = frequency;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {

_exit_window = rl.window_should_close();

if rl.is_key_pressed(raylib::consts::KeyboardKey::KEY_ESCAPE) {
if rl.is_key_pressed(crate::EXIT_KEY) {
showMessageBox = !showMessageBox;
}

Expand Down
1 change: 0 additions & 1 deletion showcase/src/example/core/core_2d_camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
// Main game loop
// Detect window close button or ESC key
return Box::new(move |rl: &mut RaylibHandle, thread: &RaylibThread| -> () {

// Update
//----------------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions showcase/src/example/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ pub mod core;
pub mod image_exporter;
pub mod models;
pub mod others;
pub mod portable_window;
pub mod scroll_panel;
pub mod shaders;
pub mod textures;
14 changes: 14 additions & 0 deletions showcase/src/example/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
pub mod models_animation;
pub mod models_billboard;
pub mod models_box_collisions;
pub mod models_cubicmap;
pub mod models_first_person_maze;
pub mod models_geometric_shapes;
pub mod models_heightmap;
pub mod models_loading;
pub mod models_material_pbr;
pub mod models_mesh_generation;
pub mod models_mesh_picking;
pub mod models_orthographic_projection;
pub mod models_rlgl_solar_system;
pub mod models_skybox;
pub mod models_waving_cubes;
pub mod models_yaw_pitch_roll;
69 changes: 69 additions & 0 deletions showcase/src/example/models/models_billboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************************
*
* raylib [models] example - Drawing billboards
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

pub use raylib::prelude::*;

pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
// Initialization
//--------------------------------------------------------------------------------------
let screen_width = 800;
let screen_height = 450;

rl.set_window_title(thread, "raylib [models] example - drawing billboards");
rl.set_window_size(screen_width, screen_height);

// Define the camera to look into our 3d world
let mut camera = Camera3D::perspective(
rvec3(5.0, 4.0, 5.0),
rvec3(0.0, 2.0, 0.0),
rvec3(0.0, 1.0, 0.0),
45.0,
);

let bill = rl.load_texture(thread, "original/models/resources/billboard.png").unwrap(); // Our texture billboard
let billPosition = rvec3(0.0, 2.0, 0.0); // Position where draw billboard

rl.set_camera_mode(&camera, raylib::consts::CameraMode::CAMERA_ORBITAL); // Set an orbital camera mode

rl.set_target_fps(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
return Box::new(move |rl: &mut RaylibHandle, thread: &RaylibThread| -> ()
// Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
rl.update_camera(&mut camera); // Update camera
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
let mut d = rl.begin_drawing(thread);

d.clear_background(Color::RAYWHITE);

{

let mut d = d.begin_mode3D(&camera);

d.draw_grid(10, 1.0); // Draw a grid

d.draw_billboard(&camera, &bill, billPosition, 2.0, Color::WHITE);
}



d.draw_fps(10, 10);

//----------------------------------------------------------------------------------
});
}
115 changes: 115 additions & 0 deletions showcase/src/example/models/models_box_collisions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*******************************************************************************************
*
* raylib [models] example - Detect basic 3d collisions (box vs sphere vs box)
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

pub use raylib::prelude::*;

pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut
{
// Initialization
//--------------------------------------------------------------------------------------
let screen_width = 800;
let screen_height = 450;

rl.set_window_title(thread, "raylib [models] example - box collisions");
rl.set_window_size(screen_width, screen_height);

// Define the camera to look into our 3d world
let camera = Camera::perspective( rvec3( 0.0, 10.0, 10.0 ), rvec3( 0.0, 0.0, 0.0 ), rvec3( 0.0, 1.0, 0.0 ), 45.0 );

let mut playerPosition = rvec3( 0.0, 1.0, 2.0 );
let playerSize = rvec3( 1.0, 2.0, 1.0 );
let mut playerColor = Color::GREEN;

let enemyBoxPos = rvec3( -4.0, 1.0, 0.0 );
let enemyBoxSize = rvec3( 2.0, 2.0, 2.0 );

let enemySpherePos = rvec3( 4.0, 0.0, 0.0 );
let enemySphereSize = 1.5;

let mut collision = false;

rl.set_target_fps(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
return Box::new(move |rl: &mut RaylibHandle, thread: &RaylibThread| -> () // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------

// Move player
if (rl.is_key_down(raylib::consts::KeyboardKey::KEY_RIGHT)){ playerPosition.x += 0.2;}
else if (rl.is_key_down(raylib::consts::KeyboardKey::KEY_LEFT)) {playerPosition.x -= 0.2;}
else if (rl.is_key_down(raylib::consts::KeyboardKey::KEY_DOWN)) {playerPosition.z += 0.2;}
else if (rl.is_key_down(raylib::consts::KeyboardKey::KEY_UP)){ playerPosition.z -= 0.2;}

collision = false;

// Check collisions player vs enemy-box
if
BoundingBox::new(rvec3( playerPosition.x - playerSize.x/2.0,
playerPosition.y - playerSize.y/2.0,
playerPosition.z - playerSize.z/2.0 ),
rvec3( playerPosition.x + playerSize.x/2.0,
playerPosition.y + playerSize.y/2.0,
playerPosition.z + playerSize.z/2.0 )).check_collision_boxes(
BoundingBox::new(rvec3( enemyBoxPos.x - enemyBoxSize.x/2.0,
enemyBoxPos.y - enemyBoxSize.y/2.0,
enemyBoxPos.z - enemyBoxSize.z/2.0 ),
rvec3( enemyBoxPos.x + enemyBoxSize.x/2.0,
enemyBoxPos.y + enemyBoxSize.y/2.0,
enemyBoxPos.z + enemyBoxSize.z/2.0 ))) {collision = true;}

// Check collisions player vs enemy-sphere
if
BoundingBox::new(rvec3( playerPosition.x - playerSize.x/2.0,
playerPosition.y - playerSize.y/2.0,
playerPosition.z - playerSize.z/2.0 ),
rvec3( playerPosition.x + playerSize.x/2.0,
playerPosition.y + playerSize.y/2.0,
playerPosition.z + playerSize.z/2.0 )).check_collision_box_sphere(
enemySpherePos, enemySphereSize) {collision = true;}

if (collision){ playerColor = Color::RED;}
else {playerColor = Color::GREEN;}
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
let mut d = rl.begin_drawing(thread);

d.clear_background(Color::RAYWHITE);

{

let mut d = d.begin_mode3D(&camera);

// Draw enemy-box
d.draw_cube(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, Color::GRAY);
d.draw_cube_wires(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, Color::DARKGRAY);

// Draw enemy-sphere
d.draw_sphere(enemySpherePos, enemySphereSize, Color::GRAY);
d.draw_sphere_wires(enemySpherePos, enemySphereSize, 16, 16, Color::DARKGRAY);

// Draw player
d.draw_cube_v(playerPosition, playerSize, playerColor);

d.draw_grid(10, 1.0); // Draw a grid
}

d.draw_text("Move player with cursors to collide", 220, 40, 20, Color::GRAY);

d.draw_fps(10, 10);

//----------------------------------------------------------------------------------
});
}
102 changes: 102 additions & 0 deletions showcase/src/example/models/models_cubicmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*******************************************************************************************
*
* raylib [models] example - Cubicmap loading and drawing
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

pub use raylib::prelude::*;

pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
// Initialization
//--------------------------------------------------------------------------------------
let screen_width = 800;
let screen_height = 450;

rl.set_window_title(
thread,
"raylib [models] example - cubesmap loading and drawing",
);
rl.set_window_size(screen_width, screen_height);

// Define the camera to look into our 3d world
let mut camera = Camera3D::perspective(
rvec3(16.0, 14.0, 16.0),
rvec3(0.0, 0.0, 0.0),
rvec3(0.0, 1.0, 0.0),
45.0,
);

let image = Image::load_image("original/models/resources/cubicmap.png").unwrap(); // Load cubicmap image (RAM)
let cubicmap = rl.load_texture_from_image(thread, &image).unwrap(); // Convert image to texture to display (VRAM)

// Because model depends on mesh, we have to make sure mesh lives as long as the model. We make it weak
// to manually control it's lifespan.
let mesh = unsafe { Mesh::gen_mesh_cubicmap(thread, &image, rvec3(1.0, 1.0, 1.0)).make_weak() };
let mut model = rl.load_model_from_mesh(thread, mesh.clone()).unwrap();

// NOTE: By default each cube is mapped to one part of texture atlas
// make texture weak so it lives as long as the model
let texture = unsafe {
rl
.load_texture(thread, "original/models/resources/cubicmap_atlas.png")
.unwrap().make_weak() // Load map texture

};
model.materials_mut()[0].maps_mut()[raylib::consts::MaterialMapType::MAP_ALBEDO as usize]
.texture = *texture.as_ref(); // Set map diffuse texture
let mapPosition = rvec3(-16.0, 0.0, -8.0); // Set model position

rl.set_camera_mode(&camera, raylib::consts::CameraMode::CAMERA_ORBITAL); // Set an orbital camera mode

rl.set_target_fps(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
return Box::new(
move |rl: &mut RaylibHandle, thread: &RaylibThread| -> () // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
rl.update_camera(&mut camera); // Update camera
//----------------------------------------------------------------------------------

{
// Draw
//----------------------------------------------------------------------------------
let mut d = rl.begin_drawing(thread);

d.clear_background(Color::RAYWHITE);

{
let mut d = d.begin_mode3D(&camera);

d.draw_model(&model, mapPosition, 1.0, Color::WHITE);

}


d.draw_texture_ex(&cubicmap, rvec2( screen_width - cubicmap.width*4 - 20, 20 ), 0.0, 4.0, Color::WHITE);
d.draw_rectangle_lines(screen_width - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, Color::GREEN);

d.draw_text("cubicmap image used to", 658, 90, 10, Color::GRAY);
d.draw_text("generate map 3d model", 658, 104, 10, Color::GRAY);

d.draw_fps(10, 10);

//----------------------------------------------------------------------------------
}
if rl.is_key_pressed(crate::EXIT_KEY) {
unsafe {
rl.unload_texture(thread, texture.clone());
// Don't need to unload mesh because the model will.
// rl.unload_mesh(thread, mesh.clone());
}
}
},
);
}
Loading

0 comments on commit ef2a293

Please sign in to comment.