Skip to content

Commit

Permalink
[General] shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Dacode45 committed Dec 5, 2020
1 parent e4f0da6 commit edffedf
Show file tree
Hide file tree
Showing 51 changed files with 620 additions and 560 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void main()
{
for (int i = 2; (i < max(2, sqrt(value) + 1)); i++)
{
if ((value - i*floor(value/i)) == 0)
if ((value - i*floor(float(value/i))) == 0)
{
color = Colorizer(float(i), scale);
//break; // Uncomment to color by the largest factor instead
Expand Down
5 changes: 2 additions & 3 deletions showcase/src/example/core/core_2d_camera_platformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*
********************************************************************************************/


pub use raylib::prelude::*;

const G: f32 = 400.0;
Expand Down Expand Up @@ -72,7 +71,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
};

// Store pointers to the multiple update camera functions
let camera_updaters = [
let mut camera_updaters = [
update_camera_center,
update_camera_center_inside_map,
update_camera_center_smooth_follow,
Expand All @@ -82,7 +81,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {

let mut camera_option = 0;

let camera_description = [
let mut camera_description = [
"Follow player center",
"Follow player center, but clamp to map edges",
"Follow player center; smoothed",
Expand Down
2 changes: 1 addition & 1 deletion showcase/src/example/core/core_basic_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* Welcome to raylib!
*
* To test examples in Notepad++, provided with default raylib installer package,
* To test examples in Notepad+=1, provided with default raylib installer package,
* just press F6 and run [raylib_compile_execute] script, it will compile and execute.
* Note that compiled executable is placed in the same folder as .c file
*
Expand Down
2 changes: 1 addition & 1 deletion showcase/src/example/models/models_box_collisions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut
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 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 );
Expand Down
2 changes: 1 addition & 1 deletion showcase/src/example/models/models_geometric_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut
rl.set_window_size(screen_width, screen_height);

// Define the camera to look into our 3d world
let camera = Camera3D::perspective(
let mut camera = Camera3D::perspective(
rvec3( 0.0, 10.0,10.0 ),
rvec3( 0.0, 0.0,0.0 ),
rvec3( 0.0, 1.0,0.0 ),
Expand Down
4 changes: 2 additions & 2 deletions showcase/src/example/models/models_material_pbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread)-> crate::SampleOut {
rl.update_camera(&mut camera); // Update camera

// Send to material PBR shader camera view position
let camera_pos: [f32; 3] = [camera.position.x, camera.position.y, camera.position.z];
let mut camera_pos: [f32; 3] = [camera.position.x, camera.position.y, camera.position.z];
let loc = model.materials()[0].shader().locs()
[raylib::consts::ShaderLocationIndex::LOC_VECTOR_VIEW as usize];
model.materials_mut()[0]
Expand Down Expand Up @@ -254,7 +254,7 @@ fn load_material_pbr(
// Set view matrix location
mat.shader_mut().locs_mut()[raylib::consts::ShaderLocationIndex::LOC_MATRIX_MODEL as usize] =
mat.shader().get_shader_location("matModel");
//mat.shader_mut().locs_mut()[raylib::consts::ShaderLocationIndex::LOC_MATRIX_VIEW] = mat.shader().get_shader_location( "view");
//mat.shader_mut().locs_mut()[raylib::consts::ShaderLocationIndex::LOC_MATRIX_VIEW as usize] = mat.shader().get_shader_location( "view");
mat.shader_mut().locs_mut()[raylib::consts::ShaderLocationIndex::LOC_VECTOR_VIEW as usize] =
mat.shader().get_shader_location("viewPos");

Expand Down
2 changes: 1 addition & 1 deletion showcase/src/example/models/models_waving_cubes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut
let scale = 2.0 + time.sin()*0.7;

// Move camera around the scene
let cameraTime = time*0.3;
let mut cameraTime = time*0.3;
camera.position.x = (cameraTime).cos()*40.0;
camera.position.z = (cameraTime).sin()*40.0;
//----------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion showcase/src/example/models/models_yaw_pitch_roll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
mats[raylib::consts::MaterialMapType::MAP_ALBEDO as usize].texture = texture;
}

let camera = Camera3D::perspective(
let mut camera = Camera3D::perspective(
Vector3::new(0.0, 60.0, -120.0),
Vector3::new(0.0, 12.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
Expand Down
3 changes: 2 additions & 1 deletion showcase/src/example/others/rlgl_standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
ffi::rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
}

let camera = Camera3D::perspective(rvec3(5.0, 5.0, 5.0), Vector3::zero(), Vector3::up(), 45.0);
let mut camera =
Camera3D::perspective(rvec3(5.0, 5.0, 5.0), Vector3::zero(), Vector3::up(), 45.0);

let cube_position = Vector3::zero(); // Cube default position (center)
//--------------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions showcase/src/example/shaders/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pub mod shaders_basic_lighting;
pub mod shaders_custom_uniform;
pub mod shaders_eratosthenes;
pub mod shaders_fog;
pub mod shaders_julia_set;
pub mod shaders_postprocessing;
2 changes: 1 addition & 1 deletion showcase/src/example/shaders/shaders_basic_lighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
modelA.set_transform(&(*modelA.transform() * Matrix::rotate_z(0.012)));

// Update the light shader with the camera view position
let cameraPos = Vector3::new(camera.position.x, camera.position.y, camera.position.z);
let mut cameraPos = Vector3::new(camera.position.x, camera.position.y, camera.position.z);
let loc = shader.locs_mut()[raylib::consts::ShaderLocationIndex::LOC_VECTOR_VIEW as usize];
shader.set_shader_value( loc, cameraPos);
//----------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,119 +23,118 @@ const GLSL_VERSION: i32 = 330;
#[cfg(target_arch = "wasm32")]
const GLSL_VERSION: i32 = 100;


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

SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)

rl.set_window_size(screen_width, screen_height);
rl.set_window_title(thread, "raylib [shaders] example - custom uniform variable");


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

let model = rl.load_model(&thread, "original/models/resources/models/barracks.obj"); // Load OBJ model
let texture = rl.load_texture(thread, "resources/models/barracks_diffuse.png"); // Load model texture (diffuse map)
model.materials_mut()[0].maps_mut()[raylib::consts::MaterialMapType::MAP_ALBEDO].texture = *texture.as_ref(); // Set model diffuse texture
let mut model = rl
.load_model(&thread, "original/shaders/resources/models/barracks.obj")
.unwrap(); // Load OBJ model
let texture = rl
.load_texture(
thread,
"original/shaders/resources/models/barracks_diffuse.png",
)
.unwrap(); // Load model texture (diffuse map)
model.materials_mut()[0].maps_mut()[raylib::consts::MaterialMapType::MAP_ALBEDO as usize]
.texture = *texture.as_ref(); // Set model diffuse texture

let position = Vector3::zero(); // Set model position

// Load postprocessing shader
// NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
let shader = rl.load_shader(thread,0, &format!("resources/shaders/glsl{}/swirl.fs", GLSL_VERSION));
let mut shader = rl
.load_shader(
thread,
None,
Some(&format!(
"original/shaders/resources/shaders/glsl{}/swirl.fs",
GLSL_VERSION
)),
)
.unwrap();

// Get variable (uniform) location on the shader to connect with the program
// NOTE: If uniform variable could not be found in the shader, function returns -1
int swirlCenterLoc = shader.get_shader_location( "center");
let swirlCenterLoc = shader.get_shader_location("center");

float swirlCenter[2] = {(float)screen_width / 2, (float)screen_height / 2};
let mut swirlCenter = Vector2::new(screen_width as f32 / 2.0, screen_height as f32 / 2.0);

// Create a RenderTexture2D to be used for render to texture
RenderTexture2D target = LoadRenderTexture(screen_width, screen_height);
let mut target = rl
.load_render_texture(thread, screen_width as u32, screen_height as u32)
.unwrap();

// Setup orbital camera
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
return Box::new(
move |rl: &mut RaylibHandle, thread: &RaylibThread| -> () // Detect window close button or ESC key
{
let _ = texture;
// Update
//----------------------------------------------------------------------------------
Vector2 mousePosition = rl.get_mouse_position();
let mousePosition = rl.get_mouse_position();

swirlCenter[0] = mousePosition.x;
swirlCenter[1] = screen_height - mousePosition.y;
swirlCenter.x = mousePosition.x;
swirlCenter.y = screen_height as f32 - mousePosition.y;

// Send new value to the shader to be used on drawing
shader.set_shader_value( swirlCenterLoc, swirlCenter, UNIFORM_VEC2);
shader.set_shader_value( swirlCenterLoc, swirlCenter);

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_texture_mode(thread, &target); // Enable drawing to texture
{
let mut d = d.begin_texture_mode(thread, &mut target); // Enable drawing to texture

d.clear_background(Color::RAYWHITE); // Clear texture background

{
let mut d = d.begin_mode3D(&camera); // Begin 3d mode drawing

d.draw_model(model, position, 0.5, Color::WHITE); // Draw 3d model with texture
d.draw_model(&model, position, 0.5, Color::WHITE); // Draw 3d model with texture

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

EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode
}

d.draw_text("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30,Color::RED);

EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)

BeginShaderMode(shader);
}
{
let mut d = d.begin_shader_mode(&shader);

// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
DrawTextureRec(target.texture, rrect(0, 0, target.texture.width, -target.texture.height), rvec2(0, 0), Color::WHITE);

EndShaderMode();
d.draw_texture_rec(target.texture(), rrect(0, 0, target.texture().width, -target.texture().height), rvec2(0, 0), Color::WHITE);

}
// Draw some 2d text over drawn texture
d.draw_text("(c) Barracks 3D model by Alberto Cano", screen_width - 220, screen_height - 20, 10, Color::GRAY);

d.draw_fps(10, 10);

EndDrawing();
//----------------------------------------------------------------------------------
}

// De-Initialization
//--------------------------------------------------------------------------------------
UnloadShader(shader); // Unload shader
UnloadTexture(texture); // Unload texture
UnloadModel(model); // Unload model
UnloadRenderTexture(target); // Unload render texture

CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
}
//----------------------------------------------------------------------------------
},
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ const GLSL_VERSION: i32 = 330;
#[cfg(target_arch = "wasm32")]
const GLSL_VERSION: i32 = 100;


pub fn run(rl
: &mut RaylibHandle, thread
: &RaylibThread)
->crate::SampleOut
{
pub fn run(rl: &mut RaylibHandle, thread: &RaylibThread) -> crate::SampleOut {
// Initialization
//--------------------------------------------------------------------------------------
let screen_width = 800;
Expand All @@ -44,18 +39,29 @@ pub fn run(rl
rl.set_window_size(screen_width, screen_height);
rl.set_window_title(thread, "raylib [shaders] example - Sieve of Eratosthenes");


RenderTexture2D target = LoadRenderTexture(screen_width, screen_height);
let mut target = rl
.load_render_texture(thread, screen_width as u32, screen_height as u32)
.unwrap();

// Load Eratosthenes shader
// NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
let shader = rl.load_shader(thread,0, &format!("resources/shaders/glsl{}/eratosthenes.fs", GLSL_VERSION));
let shader = rl
.load_shader(
thread,
None,
Some(&format!(
"original/shaders/resources/shaders/glsl{}/eratosthenes.fs",
GLSL_VERSION
)),
)
.unwrap();

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
return Box::new(
move |rl: &mut RaylibHandle, thread: &RaylibThread| -> () // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
Expand All @@ -67,33 +73,22 @@ pub fn run(rl
let mut d = rl.begin_drawing(thread);

d.clear_background(Color::RAYWHITE);

let mut d = d.begin_texture_mode(thread, &target); // Enable drawing to texture
{
let mut d = d.begin_texture_mode(thread, &mut target); // Enable drawing to texture
d.clear_background(Color::BLACK); // Clear the render texture

// Draw a rectangle in shader mode to be used as shader canvas
// NOTE: Rectangle uses font white character texture coordinates,
// so shader can not be applied here directly because input vertexTexCoord
// do not represent full screen coordinates (space where want to apply shader)
d.draw_rectangle(0, 0, rl.get_screen_width(), rl.get_screen_height(), Color::BLACK);
EndTextureMode(); // End drawing to texture (now we have a blank texture available for the shader)

BeginShaderMode(shader);
d.draw_rectangle(0, 0, d.get_screen_width(), d.get_screen_height(), Color::BLACK);
}
{
let mut d = d.begin_shader_mode(&shader);
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
DrawTextureRec(target.texture, rrect(0, 0, target.texture.width, -target.texture.height), rvec2(0.0, 0.0), Color::WHITE);
EndShaderMode();

EndDrawing();
d.draw_texture_rec(target.texture(), rrect(0, 0, target.texture().width, -target.texture().height), rvec2(0.0, 0.0), Color::WHITE);
}
//----------------------------------------------------------------------------------
}

// De-Initialization
//--------------------------------------------------------------------------------------
UnloadShader(shader); // Unload shader
UnloadRenderTexture(target); // Unload texture

CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
},
);
}
Loading

0 comments on commit edffedf

Please sign in to comment.