Skip to content
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

Implementation of functions to draw cylinders and cones #733

Merged
merged 2 commits into from
Jun 14, 2024
Merged
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
189 changes: 189 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,192 @@ pub fn draw_sphere_ex(
}
}
}

#[derive(Debug, Clone)]
pub struct DrawCylinderParams {
pub sides: usize,
pub draw_mode: DrawMode,
}

impl Default for DrawCylinderParams {
fn default() -> DrawCylinderParams {
DrawCylinderParams {
sides: 16,
draw_mode: DrawMode::Triangles,
}
}
}

pub fn draw_cylinder(
position: Vec3,
radius_top: f32,
radius_bottom: f32,
height: f32,
texture: Option<&Texture2D>,
color: Color,
) {
draw_cylinder_ex(
position,
radius_top,
radius_bottom,
height,
texture,
color,
Default::default(),
);
}

pub fn draw_cylinder_wires(
position: Vec3,
radius_top: f32,
radius_bottom: f32,
height: f32,
texture: Option<&Texture2D>,
color: Color,
) {
let params = DrawCylinderParams {
draw_mode: DrawMode::Lines,
..Default::default()
};
draw_cylinder_ex(
position,
radius_top,
radius_bottom,
height,
texture,
color,
params,
);
}

//Note: can also be used to draw a cone by setting radius_top or radius_bottom to 0
pub fn draw_cylinder_ex(
position: Vec3,
radius_top: f32,
radius_bottom: f32,
height: f32,
texture: Option<&Texture2D>,
color: Color,
params: DrawCylinderParams,
) {
let context = get_context();

let sides = params.sides;

let color: [f32; 4] = color.into();

context.gl.texture(texture.into());
context.gl.draw_mode(params.draw_mode);

use std::f32::consts::PI;
let angle_step = PI * 2.0 / sides as f32;
//draw body
for i in 0..sides + 1 {
let i = i as f32;
//bottom left
let v1 = vec3(
(i * angle_step).sin() * radius_bottom,
0.0,
(i * angle_step).cos() * radius_bottom,
);
//bottom right
let v2 = vec3(
((i + 1.0) * angle_step).sin() * radius_bottom,
0.0,
((i + 1.0) * angle_step).cos() * radius_bottom,
);
//top right
let v3 = vec3(
((i + 1.0) * angle_step).sin() * radius_top,
height,
((i + 1.0) * angle_step).cos() * radius_top,
);

context.gl.geometry(
&[
((v1 + position).into(), [0.0, 0.0], color),
((v2 + position).into(), [1.0, 0.0], color),
((v3 + position).into(), [1.0, 1.0], color),
],
&[0, 1, 2],
);

//top left
let v1 = vec3(
(i * angle_step).sin() * radius_top,
height,
(i * angle_step).cos() * radius_top,
);
//bottom left
let v2 = vec3(
(i * angle_step).sin() * radius_bottom,
0.0,
(i * angle_step).cos() * radius_bottom,
);
//top right
let v3 = vec3(
((i + 1.0) * angle_step).sin() * radius_top,
height,
((i + 1.0) * angle_step).cos() * radius_top,
);

context.gl.geometry(
&[
((v1 + position).into(), [0.0, 0.0], color),
((v2 + position).into(), [1.0, 0.0], color),
((v3 + position).into(), [1.0, 1.0], color),
],
&[0, 1, 2],
);
}

//draw cap
for i in 0..sides + 1 {
let i = i as f32;
let v1 = vec3(0.0, height, 0.0);
let v2 = vec3(
(i * angle_step).sin() * radius_top,
height,
(i * angle_step).cos() * radius_top,
);
let v3 = vec3(
((i + 1.0) * angle_step).sin() * radius_top,
height,
((i + 1.0) * angle_step).cos() * radius_top,
);

context.gl.geometry(
&[
((v1 + position).into(), [0.0, 0.0], color),
((v2 + position).into(), [1.0, 0.0], color),
((v3 + position).into(), [1.0, 1.0], color),
],
&[0, 1, 2],
);
}

//draw base
for i in 0..sides + 1 {
let i = i as f32;
let v1 = vec3(0.0, 0.0, 0.0);
let v2 = vec3(
(i * angle_step).sin() * radius_bottom,
0.0,
(i * angle_step).cos() * radius_bottom,
);
let v3 = vec3(
((i + 1.0) * angle_step).sin() * radius_bottom,
0.0,
((i + 1.0) * angle_step).cos() * radius_bottom,
);

context.gl.geometry(
&[
((v1 + position).into(), [0.0, 0.0], color),
((v2 + position).into(), [1.0, 0.0], color),
((v3 + position).into(), [1.0, 1.0], color),
],
&[0, 1, 2],
);
}
}
Loading