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

Feature: vertex_index_u32 #321

Merged
merged 4 commits into from
Apr 26, 2023
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ path = "src/lib.rs"

[features]
conrod = ["conrod_core"]

vertex_index_u32 = []

[dependencies]
bitflags = "1.2"
Expand Down
3 changes: 2 additions & 1 deletion examples/custom_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate nalgebra as na;
use kiss3d::camera::Camera;
use kiss3d::context::Context;
use kiss3d::light::Light;
use kiss3d::resource::vertex_index::VERTEX_INDEX_TYPE;
use kiss3d::resource::{Effect, Material, Mesh, ShaderAttribute, ShaderUniform};
use kiss3d::scene::ObjectData;
use kiss3d::window::Window;
Expand Down Expand Up @@ -97,7 +98,7 @@ impl Material for NormalMaterial {
Context::get().draw_elements(
Context::TRIANGLES,
mesh.num_pts() as i32,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0,
);

Expand Down
11 changes: 6 additions & 5 deletions src/builtin/object_material.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::camera::Camera;
use crate::context::Context;
use crate::light::Light;
use crate::resource::vertex_index::VERTEX_INDEX_TYPE;
use crate::resource::Material;
use crate::resource::{Effect, Mesh, ShaderAttribute, ShaderUniform};
use crate::scene::ObjectData;
Expand Down Expand Up @@ -122,7 +123,7 @@ impl Material for ObjectMaterial {
verify!(ctxt.draw_elements(
Context::TRIANGLES,
mesh.num_pts() as i32,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
}
Expand All @@ -138,15 +139,15 @@ impl Material for ObjectMaterial {
verify!(ctxt.draw_elements(
Context::TRIANGLES,
mesh.num_pts() as i32,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
} else {
mesh.bind_edges();
verify!(ctxt.draw_elements(
Context::LINES,
mesh.num_pts() as i32 * 2,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
}
Expand All @@ -162,14 +163,14 @@ impl Material for ObjectMaterial {
verify!(ctxt.draw_elements(
Context::TRIANGLES,
mesh.num_pts() as i32,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
} else {
verify!(ctxt.draw_elements(
Context::POINTS,
mesh.num_pts() as i32,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
}
Expand Down
11 changes: 6 additions & 5 deletions src/builtin/planar_object_material.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::context::Context;
use crate::planar_camera::PlanarCamera;
use crate::resource::vertex_index::VERTEX_INDEX_TYPE;
use crate::resource::PlanarMaterial;
use crate::resource::{Effect, PlanarMesh, ShaderAttribute, ShaderUniform};
use crate::scene::PlanarObjectData;
Expand Down Expand Up @@ -97,7 +98,7 @@ impl PlanarMaterial for PlanarObjectMaterial {
verify!(ctxt.draw_elements(
Context::TRIANGLES,
mesh.num_pts() as i32,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
}
Expand All @@ -113,15 +114,15 @@ impl PlanarMaterial for PlanarObjectMaterial {
verify!(ctxt.draw_elements(
Context::TRIANGLES,
mesh.num_pts() as i32,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
} else {
mesh.bind_edges();
verify!(ctxt.draw_elements(
Context::LINES,
mesh.num_pts() as i32 * 2,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
}
Expand All @@ -138,14 +139,14 @@ impl PlanarMaterial for PlanarObjectMaterial {
verify!(ctxt.draw_elements(
Context::TRIANGLES,
mesh.num_pts() as i32,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
} else {
verify!(ctxt.draw_elements(
Context::POINTS,
mesh.num_pts() as i32,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));
}
Expand Down
27 changes: 16 additions & 11 deletions src/loader/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::loader::mtl;
use crate::loader::mtl::MtlMaterial;
use crate::resource::vertex_index::VertexIndex;
use crate::resource::GPUVec;
use crate::resource::{AllocationType, BufferType, Mesh};
use na::{Point2, Point3, Vector3};
Expand Down Expand Up @@ -78,7 +79,7 @@ pub fn parse(
let mut normals: Vec<Normal> = Vec::new();
let mut uvs: Vec<UV> = Vec::new();
let mut groups: HashMap<String, usize> = HashMap::new();
let mut groups_ids: Vec<Vec<Point3<u16>>> = Vec::new();
let mut groups_ids: Vec<Vec<Point3<VertexIndex>>> = Vec::new();
let mut curr_group: usize = 0;
let mut ignore_normals = false;
let mut ignore_uvs = false;
Expand Down Expand Up @@ -172,7 +173,7 @@ fn parse_usemtl<'a>(
mtllib: &HashMap<String, MtlMaterial>,
group2mtl: &mut HashMap<usize, MtlMaterial>,
groups: &mut HashMap<String, usize>,
groups_ids: &mut Vec<Vec<Point3<u16>>>,
groups_ids: &mut Vec<Vec<Point3<VertexIndex>>>,
curr_mtl: &mut Option<MtlMaterial>,
) -> usize {
let mname: Vec<&'a str> = ws.collect();
Expand Down Expand Up @@ -276,7 +277,7 @@ fn parse_f<'a>(
normals: &[Vector3<f32>],
ignore_uvs: &mut bool,
ignore_normals: &mut bool,
groups_ids: &mut Vec<Vec<Point3<u16>>>,
groups_ids: &mut Vec<Vec<Point3<VertexIndex>>>,
curr_group: usize,
) {
// Four formats possible: v v/t v//n v/t/n
Expand Down Expand Up @@ -335,7 +336,11 @@ fn parse_f<'a>(
}

assert!(x >= 0 && y >= 0 && z >= 0);
groups_ids[curr_group].push(Point3::new(x as u16, y as u16, z as u16));
groups_ids[curr_group].push(Point3::new(
x as VertexIndex,
y as VertexIndex,
z as VertexIndex,
));

i += 1;
}
Expand Down Expand Up @@ -377,7 +382,7 @@ fn parse_g<'a>(
ws: Words<'a>,
prefix: &str,
groups: &mut HashMap<String, usize>,
groups_ids: &mut Vec<Vec<Point3<u16>>>,
groups_ids: &mut Vec<Vec<Point3<VertexIndex>>>,
) -> usize {
let suffix: Vec<&'a str> = ws.collect();
let suffix = suffix.join(" ");
Expand All @@ -402,17 +407,17 @@ fn reformat(
coords: Vec<Coord>,
normals: Option<Vec<Normal>>,
uvs: Option<Vec<UV>>,
groups_ids: Vec<Vec<Point3<u16>>>,
groups_ids: Vec<Vec<Point3<VertexIndex>>>,
groups: HashMap<String, usize>,
group2mtl: HashMap<usize, MtlMaterial>,
) -> Vec<(String, Mesh, Option<MtlMaterial>)> {
let mut vt2id: HashMap<Point3<u16>, u16> = HashMap::new();
let mut vertex_ids: Vec<u16> = Vec::new();
let mut vt2id: HashMap<Point3<VertexIndex>, VertexIndex> = HashMap::new();
let mut vertex_ids: Vec<VertexIndex> = Vec::new();
let mut resc: Vec<Coord> = Vec::new();
let mut resn: Option<Vec<Normal>> = normals.as_ref().map(|_| Vec::new());
let mut resu: Option<Vec<UV>> = uvs.as_ref().map(|_| Vec::new());
let mut resfs: Vec<Vec<Point3<u16>>> = Vec::new();
let mut allfs: Vec<Point3<u16>> = Vec::new();
let mut resfs: Vec<Vec<Point3<VertexIndex>>> = Vec::new();
let mut allfs: Vec<Point3<VertexIndex>> = Vec::new();
let mut names: Vec<String> = Vec::new();
let mut mtls: Vec<Option<MtlMaterial>> = Vec::new();

Expand All @@ -427,7 +432,7 @@ fn reformat(
None
}
None => {
let idx = resc.len() as u16;
let idx = resc.len() as VertexIndex;

resc.push(coords[point.x as usize]);

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/conrod_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct ConrodRenderer {
image_uvs: ShaderAttribute<Point2<f32>>,
image_texture: ShaderUniform<i32>,
points: GPUVec<f32>,
indices: GPUVec<Point3<u16>>,
indices: GPUVec<Point3<VertexIndex>>,
cache: GlyphCache<'static>,
texture: Texture,
resized_once: bool,
Expand Down Expand Up @@ -270,7 +270,7 @@ impl ConrodRenderer {
verify!(ctxt.draw_elements(
Context::TRIANGLES,
self.indices.len() as i32 * 3,
Context::UNSIGNED_SHORT,
VERTEX_INDEX_TYPE,
0
));

Expand Down
10 changes: 10 additions & 0 deletions src/resource/gl_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ unsafe impl GLPrimitive for Point3<u16> {
const GLTYPE: u32 = Context::UNSIGNED_SHORT;
}

unsafe impl GLPrimitive for Point3<u32> {
type Element = u32;
const GLTYPE: u32 = Context::UNSIGNED_INT;
}

unsafe impl GLPrimitive for Point2<u32> {
type Element = u32;
const GLTYPE: u32 = Context::UNSIGNED_INT;
}

/*
*
* Impl for tuples
Expand Down
15 changes: 8 additions & 7 deletions src/resource/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::iter;
use std::sync::{Arc, RwLock};

use crate::resource::gpu_vector::{AllocationType, BufferType, GPUVec};
use crate::resource::vertex_index::VertexIndex;
use crate::resource::ShaderAttribute;
use na::{self, Point2, Point3, Vector3};
use ncollide3d::procedural::{IndexBuffer, TriMesh};
Expand All @@ -16,10 +17,10 @@ mod error;
/// It also contains the GPU location of those buffers.
pub struct Mesh {
coords: Arc<RwLock<GPUVec<Point3<f32>>>>,
faces: Arc<RwLock<GPUVec<Point3<u16>>>>,
faces: Arc<RwLock<GPUVec<Point3<VertexIndex>>>>,
normals: Arc<RwLock<GPUVec<Vector3<f32>>>>,
uvs: Arc<RwLock<GPUVec<Point2<f32>>>>,
edges: Option<Arc<RwLock<GPUVec<Point2<u16>>>>>,
edges: Option<Arc<RwLock<GPUVec<Point2<VertexIndex>>>>>,
}

impl Mesh {
Expand All @@ -28,7 +29,7 @@ impl Mesh {
/// If the normals and uvs are not given, they are automatically computed.
pub fn new(
coords: Vec<Point3<f32>>,
faces: Vec<Point3<u16>>,
faces: Vec<Point3<VertexIndex>>,
normals: Option<Vec<Vector3<f32>>>,
uvs: Option<Vec<Point2<f32>>>,
dynamic_draw: bool,
Expand Down Expand Up @@ -177,7 +178,7 @@ impl Mesh {
/// Creates a new mesh. Arguments set to `None` are automatically computed.
pub fn new_with_gpu_vectors(
coords: Arc<RwLock<GPUVec<Point3<f32>>>>,
faces: Arc<RwLock<GPUVec<Point3<u16>>>>,
faces: Arc<RwLock<GPUVec<Point3<VertexIndex>>>>,
normals: Arc<RwLock<GPUVec<Vector3<f32>>>>,
uvs: Arc<RwLock<GPUVec<Point2<f32>>>>,
) -> Mesh {
Expand Down Expand Up @@ -263,7 +264,7 @@ impl Mesh {
}

/// This mesh faces.
pub fn faces(&self) -> &Arc<RwLock<GPUVec<Point3<u16>>>> {
pub fn faces(&self) -> &Arc<RwLock<GPUVec<Point3<VertexIndex>>>> {
&self.faces
}

Expand All @@ -285,7 +286,7 @@ impl Mesh {
/// Computes normals from a set of faces.
pub fn compute_normals_array(
coordinates: &[Point3<f32>],
faces: &[Point3<u16>],
faces: &[Point3<VertexIndex>],
) -> Vec<Vector3<f32>> {
let mut res = Vec::new();

Expand All @@ -297,7 +298,7 @@ impl Mesh {
/// Computes normals from a set of faces.
pub fn compute_normals(
coordinates: &[Point3<f32>],
faces: &[Point3<u16>],
faces: &[Point3<VertexIndex>],
normals: &mut Vec<Vector3<f32>>,
) {
let mut divisor: Vec<f32> = iter::repeat(0f32).take(coordinates.len()).collect();
Expand Down
1 change: 1 addition & 0 deletions src/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ mod planar_material_manager;
mod planar_mesh;
mod planar_mesh_manager;
mod texture_manager;
pub mod vertex_index;
11 changes: 6 additions & 5 deletions src/resource/planar_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::iter;
use std::sync::{Arc, RwLock};

use crate::resource::gpu_vector::{AllocationType, BufferType, GPUVec};
use crate::resource::vertex_index::VertexIndex;
use crate::resource::ShaderAttribute;
use na::{Point2, Point3};

Expand All @@ -14,9 +15,9 @@ mod error;
/// It also contains the GPU location of those buffers.
pub struct PlanarMesh {
coords: Arc<RwLock<GPUVec<Point2<f32>>>>,
faces: Arc<RwLock<GPUVec<Point3<u16>>>>,
faces: Arc<RwLock<GPUVec<Point3<VertexIndex>>>>,
uvs: Arc<RwLock<GPUVec<Point2<f32>>>>,
edges: Option<Arc<RwLock<GPUVec<Point2<u16>>>>>,
edges: Option<Arc<RwLock<GPUVec<Point2<VertexIndex>>>>>,
}

impl PlanarMesh {
Expand All @@ -25,7 +26,7 @@ impl PlanarMesh {
/// If the normals and uvs are not given, they are automatically computed.
pub fn new(
coords: Vec<Point2<f32>>,
faces: Vec<Point3<u16>>,
faces: Vec<Point3<VertexIndex>>,
uvs: Option<Vec<Point2<f32>>>,
dynamic_draw: bool,
) -> PlanarMesh {
Expand Down Expand Up @@ -57,7 +58,7 @@ impl PlanarMesh {
/// Creates a new mesh. Arguments set to `None` are automatically computed.
pub fn new_with_gpu_vectors(
coords: Arc<RwLock<GPUVec<Point2<f32>>>>,
faces: Arc<RwLock<GPUVec<Point3<u16>>>>,
faces: Arc<RwLock<GPUVec<Point3<VertexIndex>>>>,
uvs: Arc<RwLock<GPUVec<Point2<f32>>>>,
) -> PlanarMesh {
PlanarMesh {
Expand Down Expand Up @@ -125,7 +126,7 @@ impl PlanarMesh {
}

/// This mesh faces.
pub fn faces(&self) -> &Arc<RwLock<GPUVec<Point3<u16>>>> {
pub fn faces(&self) -> &Arc<RwLock<GPUVec<Point3<VertexIndex>>>> {
&self.faces
}

Expand Down
7 changes: 4 additions & 3 deletions src/resource/planar_mesh_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::f32;

use na::{Point2, Point3};

use crate::resource::vertex_index::VertexIndex;
use crate::resource::PlanarMesh;
use std::cell::RefCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -59,11 +60,11 @@ impl PlanarMeshManager {
circle_vtx.push(Point2::new(ang.cos(), ang.sin()) * 0.5);
circle_ids.push(Point3::new(
0,
circle_vtx.len() as u16 - 2,
circle_vtx.len() as u16 - 1,
circle_vtx.len() as VertexIndex - 2,
circle_vtx.len() as VertexIndex - 1,
));
}
circle_ids.push(Point3::new(0, circle_vtx.len() as u16 - 1, 1));
circle_ids.push(Point3::new(0, circle_vtx.len() as VertexIndex - 1, 1));

let circle = PlanarMesh::new(circle_vtx, circle_ids, None, false);
res.add(Rc::new(RefCell::new(circle)), "circle");
Expand Down
Loading