I am trying to get the culling mechanism to work, but regardless of what culling option I choose I don’t see any difference in what is being rendered. Perhaps i am understanding culling wrong, or maybe the cause is elsewhere.
Below follows a test program using the three_d crate, in which i try to get culling to work.
The program is very simple, and pretty much only renders a cube and a triangle.
I have not bothered defining the vertices in any order, as i have implemented functionality allowing for moving the camera to look at both faces (move using w/s, also exit with ctrl+w).
In the example i try to cull both front and back sides, but this does not yield anything for me. The same goes for the other culling options, being front, back and none.
In other words, no matter the option i choose, i get the behaviour as if i choose to cull none of the sides.
use three_d::*;
pub fn main() {
// Create a window (a canvas on web)
let window = Window::new(WindowSettings {
title: "Cull test!".to_string(),
min_size: (10, 10),
max_size: Some((1280, 720)),
borderless: false,
surface_settings: Default::default(),
})
.unwrap();
// Get the graphics context from the window
let context = window.gl();
context.set_cull(Cull::FrontAndBack);
// this is what context.set_cull() calls essentially.
// unsafe {
// //context.enable(context::CULL_FACE);
// context.cull_face(context::FRONT_AND_BACK);
// }
dbg!(&context);
let mut camera = Camera::new_perspective(
window.viewport(),
vec3(0.0, 0.0, 20.0),
vec3(0.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
degrees(45.0),
0.1,
1000.0,
);
let mut cube_trimesh = CpuMesh::cube();
cube_trimesh.colors = Some(Vec::from([Srgba::RED; 36]));
let cube_obj = Gm::new(Mesh::new(&context, &cube_trimesh), PhysicalMaterial::default());
// Create a CPU-side mesh consisting of a single colored triangle
let positions = vec![
vec3(5.0, -5.0, 5.0), // bottom right
vec3(-5.0, -5.0, 5.0), // bottom left
vec3(0.0, 5.0, 5.0), // top
];
dbg!(vec3(0.5, -0.5, 0.0).cross(vec3(-0.5, -0.5, 0.0)));
let colors = vec![
Srgba::BLUE, // bottom right
Srgba::BLUE, // bottom left
Srgba::BLUE, // top
];
let mut triangle_mesh = CpuMesh {
positions: Positions::F32(positions),
colors: Some(colors),
..Default::default()
};
triangle_mesh.compute_normals();
let blue_triangle = Gm::new(Mesh::new(&context, &triangle_mesh), PhysicalMaterial::default());
let mut directional_light =
renderer::light::DirectionalLight::new(&context, 1.0, Srgba::WHITE, &vec3(1.0, 0.0, -1.0));
let ambient_light = renderer::light::AmbientLight::new(&context, 0.05, Srgba::WHITE);
// Start the main render loop
window.render_loop(
move |frame_input| // Begin a new frame with an updated frame input
{
dbg!(&context);
// Ensure the viewport matches the current window viewport which changes if the window is resized
camera.set_viewport(frame_input.viewport);
directional_light.generate_shadow_map(128, &cube_obj);
//unsafe {context.cull_face(Cull::FrontAndBack as u32);}
for ev in &frame_input.events{
if let Event::KeyPress { kind, modifiers, handled: _ } = ev {
if *kind == Key::W && modifiers.ctrl {
println!("Ctrl + W pressed. Exiting application...");
std::process::exit(0);
}
match *kind {
Key::W => {
let mut pos = camera.position().clone();
pos.z += 3.0;
let target = camera.target().clone();
let up = camera.up().clone();
camera.set_view(pos, target, up)
},
Key::S => {
let mut pos = camera.position().clone();
pos.z += -3.0;
let target = camera.target().clone();
let up = camera.up().clone();
camera.set_view(pos, target, up)
},
_ =>(),
}
}
};
// Get the screen render target to be able to render something on the screen
frame_input.screen()
// Clear the color and depth of the screen render target
.clear(ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0))
.render(
&camera, &[&cube_obj,&blue_triangle], &[&directional_light,&ambient_light]
);
// Returns default frame output to end the frame
FrameOutput::default()
},
);
}
Am i using the three_d crate wrongly? Or could this be a platform or hardware-specific thing? Or could it be a bug in the three_d crate?
I am on Ubuntu 22.04.4. Any help is appreciated 🙂
I think i explained this in the previous section.
lobster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.