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

Add MeshLoader and SubMesh index. #41

Merged
merged 4 commits into from
Jan 18, 2018
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
7 changes: 4 additions & 3 deletions crayon-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ authors = ["Jingkai Mao <oammix@gmail.com>"]
travis-ci = { repository = "shawnscode/crayon", branch = "master" }

[dependencies]
rand = "0.3.18"
image = "0.18.0"
crayon = { path = "..", version = "0.1.0" }
crayon-imgui = { path = "../modules/imgui", version = "0.1.0" }
crayon-imgui = { path = "../modules/imgui", version = "0.1.0" }
image = "0.18.0"
obj = "0.8.1"
rand = "0.3.18"
146 changes: 146 additions & 0 deletions crayon-examples/assets/cornell_box.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# cornell_box.obj and cornell_box.mtl are grabbed from Intel's embree project.
# original cornell box data
# comment

# empty line including some space


mtllib cornell_box.mtl
mtllib cornell_box2.mtl

o floor
usemtl white
v 552.8 0.0 0.0
v 0.0 0.0 0.0
v 0.0 0.0 559.2
v 549.6 0.0 559.2

v 130.0 0.0 65.0
v 82.0 0.0 225.0
v 240.0 0.0 272.0
v 290.0 0.0 114.0

v 423.0 0.0 247.0
v 265.0 0.0 296.0
v 314.0 0.0 456.0
v 472.0 0.0 406.0

f 1 2 3 4
f 8 7 6 5
f 12 11 10 9

o light
usemtl light
v 343.0 548.0 227.0
v 343.0 548.0 332.0
v 213.0 548.0 332.0
v 213.0 548.0 227.0
f -4 -3 -2 -1

o ceiling
usemtl white
v 556.0 548.8 0.0
v 556.0 548.8 559.2
v 0.0 548.8 559.2
v 0.0 548.8 0.0
f -4 -3 -2 -1

o back_wall
usemtl white
v 549.6 0.0 559.2
v 0.0 0.0 559.2
v 0.0 548.8 559.2
v 556.0 548.8 559.2
f -4 -3 -2 -1

o front_wall
usemtl blue
v 549.6 0.0 0
v 0.0 0.0 0
v 0.0 548.8 0
v 556.0 548.8 0
#f -1 -2 -3 -4

o green_wall
usemtl green
v 0.0 0.0 559.2
v 0.0 0.0 0.0
v 0.0 548.8 0.0
v 0.0 548.8 559.2
f -4 -3 -2 -1

o red_wall
usemtl red
v 552.8 0.0 0.0
v 549.6 0.0 559.2
v 556.0 548.8 559.2
v 556.0 548.8 0.0
f -4 -3 -2 -1

o short_block
usemtl white

v 130.0 165.0 65.0
v 82.0 165.0 225.0
v 240.0 165.0 272.0
v 290.0 165.0 114.0
f -4 -3 -2 -1

v 290.0 0.0 114.0
v 290.0 165.0 114.0
v 240.0 165.0 272.0
v 240.0 0.0 272.0
f -4 -3 -2 -1

v 130.0 0.0 65.0
v 130.0 165.0 65.0
v 290.0 165.0 114.0
v 290.0 0.0 114.0
f -4 -3 -2 -1

v 82.0 0.0 225.0
v 82.0 165.0 225.0
v 130.0 165.0 65.0
v 130.0 0.0 65.0
f -4 -3 -2 -1

v 240.0 0.0 272.0
v 240.0 165.0 272.0
v 82.0 165.0 225.0
v 82.0 0.0 225.0
f -4 -3 -2 -1

o tall_block
usemtl white

v 423.0 330.0 247.0
v 265.0 330.0 296.0
v 314.0 330.0 456.0
v 472.0 330.0 406.0
f -4 -3 -2 -1

usemtl white
v 423.0 0.0 247.0
v 423.0 330.0 247.0
v 472.0 330.0 406.0
v 472.0 0.0 406.0
f -4 -3 -2 -1

v 472.0 0.0 406.0
v 472.0 330.0 406.0
v 314.0 330.0 456.0
v 314.0 0.0 456.0
f -4 -3 -2 -1

v 314.0 0.0 456.0
v 314.0 330.0 456.0
v 265.0 330.0 296.0
v 265.0 0.0 296.0
f -4 -3 -2 -1

v 265.0 0.0 296.0
v 265.0 330.0 296.0
v 423.0 330.0 247.0
v 423.0 0.0 247.0
f -4 -3 -2 -1

File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions crayon-examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extern crate crayon;
extern crate crayon_imgui;
extern crate image;
extern crate obj;
extern crate rand;

use std::env;
Expand Down
18 changes: 9 additions & 9 deletions crayon-examples/src/render_target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Window {
let mut label = graphics::RAIIGuard::new(video);

let attributes = graphics::AttributeLayoutBuilder::new()
.with(graphics::VertexAttribute::Position, 2)
.with(graphics::Attribute::Position, 2)
.finish();

//
Expand All @@ -39,8 +39,8 @@ impl Window {

// Create vertex buffer object.
let mut setup = graphics::MeshSetup::default();
setup.num_vertices = 3;
setup.num_indices = 3;
setup.num_verts = 3;
setup.num_idxes = 3;
setup.layout = Vertex::layout();

let mesh = label
Expand Down Expand Up @@ -69,8 +69,8 @@ impl Window {
// Create shader state.
let mut setup = graphics::ShaderSetup::default();
setup.layout = attributes;
setup.vs = include_str!("../../resources/render_target_p1.vs").to_owned();
setup.fs = include_str!("../../resources/render_target_p1.fs").to_owned();
setup.vs = include_str!("../../assets/render_target_p1.vs").to_owned();
setup.fs = include_str!("../../assets/render_target_p1.fs").to_owned();
let shader = label.create_shader(setup)?;

(Pass {
Expand All @@ -89,8 +89,8 @@ impl Window {
let idxes: [u16; 6] = [0, 1, 2, 0, 2, 3];

let mut setup = graphics::MeshSetup::default();
setup.num_vertices = 4;
setup.num_indices = 6;
setup.num_verts = 4;
setup.num_idxes = 6;
setup.layout = Vertex::layout();

let mesh = label
Expand All @@ -104,8 +104,8 @@ impl Window {

let mut setup = graphics::ShaderSetup::default();
setup.layout = attributes;
setup.vs = include_str!("../../resources/render_target_p2.vs").to_owned();
setup.fs = include_str!("../../resources/render_target_p2.fs").to_owned();
setup.vs = include_str!("../../assets/render_target_p2.vs").to_owned();
setup.fs = include_str!("../../assets/render_target_p2.fs").to_owned();
setup.uniform_variables.push("renderedTexture".into());
setup.uniform_variables.push("time".into());
let shader = label.create_shader(setup)?;
Expand Down
12 changes: 6 additions & 6 deletions crayon-examples/src/texture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Window {
fn new(engine: &mut Engine) -> errors::Result<Self> {
engine
.resource
.mount("std", resource::filesystem::DirectoryFS::new("resources")?)?;
.mount("std", resource::filesystem::DirectoryFS::new("assets")?)?;

let ctx = engine.context();
let video = ctx.shared::<GraphicsSystem>().clone();
Expand All @@ -33,13 +33,13 @@ impl Window {
let idxes: [u16; 6] = [0, 1, 2, 0, 2, 3];

let attributes = graphics::AttributeLayoutBuilder::new()
.with(graphics::VertexAttribute::Position, 2)
.with(graphics::Attribute::Position, 2)
.finish();

// Create vertex buffer object.
let mut setup = graphics::MeshSetup::default();
setup.num_vertices = 4;
setup.num_indices = 6;
setup.num_verts = 4;
setup.num_idxes = 6;
setup.layout = Vertex::layout();

let mesh = label
Expand All @@ -54,8 +54,8 @@ impl Window {
// Create shader state.
let mut setup = graphics::ShaderSetup::default();
setup.layout = attributes;
setup.vs = include_str!("../../resources/texture.vs").to_owned();
setup.fs = include_str!("../../resources/texture.fs").to_owned();
setup.vs = include_str!("../../assets/texture.vs").to_owned();
setup.fs = include_str!("../../assets/texture.fs").to_owned();
setup.uniform_variables.push("renderedTexture".into());
let shader = label.create_shader(setup)?;

Expand Down
66 changes: 66 additions & 0 deletions crayon-examples/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use crayon::prelude::*;
use image;
use image::GenericImage;

use obj;

use std;
use std::io;
use std::time::Duration;

pub struct TextureParser {}
Expand All @@ -20,6 +24,68 @@ impl graphics::TextureParser for TextureParser {
}
}

impl_vertex!{
OBJVertex {
position => [Position; Float; 3; false],
}
}
// texcoord => [Texcoord0; Float; 3; false],
// normal => [Normal; Float; 3; false],

pub struct OBJParser {}

impl graphics::MeshParser for OBJParser {
type Error = io::Error;

fn parse(bytes: &[u8]) -> io::Result<graphics::MeshData> {
let data: obj::Obj<obj::SimplePolygon> =
obj::Obj::load_buf(&mut std::io::BufReader::new(bytes))?;

let mut verts = Vec::new();
for v in data.position {
verts.push(OBJVertex::new(v));
}

let mut idxes = Vec::new();
let mut meshes = Vec::new();
for o in data.objects {
for mesh in o.groups {
meshes.push(idxes.len());
for poly in mesh.polys {
match poly.len() {
3 => {
idxes.push(poly[0].0 as u32);
idxes.push(poly[1].0 as u32);
idxes.push(poly[2].0 as u32);
}
4 => {
idxes.push(poly[0].0 as u32);
idxes.push(poly[1].0 as u32);
idxes.push(poly[2].0 as u32);

idxes.push(poly[0].0 as u32);
idxes.push(poly[2].0 as u32);
idxes.push(poly[3].0 as u32);
}
_ => unreachable!(),
};
}
}
}

Ok(graphics::MeshData {
layout: OBJVertex::layout(),
index_format: graphics::IndexFormat::U32,
primitive: graphics::Primitive::Triangles,
num_verts: verts.len(),
num_idxes: idxes.len(),
sub_mesh_offsets: meshes,
verts: Vec::from(OBJVertex::as_bytes(&verts)),
idxes: Vec::from(graphics::IndexFormat::as_bytes(&idxes)),
})
}
}

pub fn to_ms(duration: Duration) -> f32 {
duration.as_secs() as f32 * 1000.0 + duration.subsec_nanos() as f32 / 1_000_000.0
}
File renamed without changes.
File renamed without changes.
Loading