Skip to content

Added model shader sample #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion samples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ path = "input.rs"

[[bin]]
name = "3d_camera_first_person"
path = "3d_camera_first_person.rs"
path = "3d_camera_first_person.rs"

[[bin]]
name = "model_shader"
path = "model_shader.rs"
62 changes: 62 additions & 0 deletions samples/model_shader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use raylib::prelude::*;

const WINDOW_WIDTH: i32 = 1280;
const WINDOW_HEIGHT: i32 = 720;

fn main() {
let (mut rl, thread) = raylib::init()
.size(WINDOW_WIDTH, WINDOW_HEIGHT)
.title("Model shader example")
.build();

let mut camera = Camera3D::perspective(
Vector3::new(4.0, 4.0, 4.0), // Position
Vector3::new(0.0, 1.0, -1.0), // Target
Vector3::new(0.0, 1.0, 0.0), // Up vector
45.0 // FOV
);

rl.set_camera_mode(&camera, CameraMode::CAMERA_FREE);
rl.set_target_fps(60);

// Load shader
let shader = rl.load_shader(&thread, None, Some("static/model_shader/grayscale.fs")).unwrap();

// Load model
let mut model = rl.load_model(&thread, "static/model_shader/watermill.obj").unwrap();

// Load texture and generate mipmaps
let texture = unsafe {
let mut t = rl.load_texture(&thread, "static/model_shader/watermill_diffuse.png").unwrap();
t.gen_texture_mipmaps();
t.unwrap()
};

let materials = model.materials_mut();
let material = &mut materials[0];

// Assign shader to model materials
material.shader = *shader.as_ref();

// Assign loaded texture to material albedo map
let mut maps = material.maps_mut();
maps[MaterialMapType::MAP_ALBEDO as usize].texture = texture;

let model_position = Vector3::new(0.0, 0.0, 0.0);

while !rl.window_should_close() {
rl.update_camera(&mut camera);

let mut drawing = rl.begin_drawing(&thread);
drawing.clear_background(Color::WHITE);
{
let mut mode_3d = drawing.begin_mode_3D(camera);

mode_3d.draw_model(&model, model_position, 0.2, Color::WHITE);
mode_3d.draw_grid(10, 1.0);
}

drawing.draw_text("(c) Watermill 3D model by Alberto Cano", WINDOW_WIDTH - 210, WINDOW_HEIGHT - 20, 10, Color::GRAY);
drawing.draw_fps(10, 10)
}
}
26 changes: 26 additions & 0 deletions samples/static/model_shader/grayscale.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 330

// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;

// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;

// Output fragment color
out vec4 finalColor;

// NOTE: Add here your custom variables

void main()
{
// Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord)*colDiffuse*fragColor;

// Convert texel color to grayscale using NTSC conversion weights
float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114));

// Calculate final fragment color
finalColor = vec4(gray, gray, gray, texelColor.a);
}
Loading