Skip to content

Commit

Permalink
Merge pull request #7 from bwasty/v0.1
Browse files Browse the repository at this point in the history
v0.1
  • Loading branch information
bwasty authored Sep 23, 2017
2 parents 5188436 + 4ad5938 commit f4fc598
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 97 deletions.
12 changes: 1 addition & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Benjamin Wasty <[email protected]>"]
categories = ["rendering"]
description = "Simple glTF 2.0 viewer (WIP)"
description = "Simple glTF 2.0 viewer"
include = [
"Cargo.toml",
"Cargo.lock", # Note: currently ignored by `cargo package`, see rust-lang/cargo#2263
Expand Down Expand Up @@ -37,23 +37,13 @@ itertools = "0.6.1"

[dependencies.gltf]
version = "0.9.2"
# git = "https://github.com/alteous/gltf"
# branch = "simplify"
features = ["names"]

[dependencies.gltf-importer]
version = "0.9.2"
# git = "https://github.com/alteous/gltf"
# branch = "simplify"

[dependencies.gltf-utils]
version = "0.9.2"
# git = "https://github.com/alteous/gltf"
# branch = "simplify"

# [patch.crates-io]
# gltf = { git = "https://github.com/bwasty/gltf", branch = "bounds"}
# gltf-json = { git = "https://github.com/bwasty/gltf", branch = "bounds"}

# [profile.dev]
# opt-level = 2
Expand Down
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
[![GitHub release](https://img.shields.io/github/release/bwasty/gltf-viewer.svg)](https://github.com/bwasty/gltf-viewer/releases/latest)
[![](https://tokei.rs/b1/github/bwasty/gltf-viewer)](https://github.com/Aaronepower/tokei)
[![Build Status](https://travis-ci.org/bwasty/gltf-viewer.svg?branch=master)](https://travis-ci.org/bwasty/gltf-viewer)
[![Build status](https://ci.appveyor.com/api/projects/status/51ukh02thpb0r9cf/branch/master?svg=true)](https://ci.appveyor.com/project/bwasty/gltf-viewer/branch/master)<br>
[![Build status](https://ci.appveyor.com/api/projects/status/51ukh02thpb0r9cf/branch/master?svg=true)](https://ci.appveyor.com/project/bwasty/gltf-viewer/branch/master) <br>
<!--
[![Crates.io](https://img.shields.io/crates/d/gltf-viewer.svg)](https://crates.io/crates/gltf-viewer)
[![Github All Releases](https://img.shields.io/github/downloads/bwasty/gltf-viewer/total.svg)](https://github.com/bwasty/gltf-viewer/releases)
-->

glTF Viewer written in Rust (WIP).
[glTF](https://github.com/KhronosGroup/glTF) 2.0 Viewer written in Rust.

**Current state** (master): All sample models can be loaded, but there is no lighting yet:
**Current state** : All sample models can be loaded:

<img width="432" alt="gltf-viewer-fish" src="https://user-images.githubusercontent.com/1647415/29146607-4e8fd2e0-7d62-11e7-902f-18718c140135.png">
<img width="412" alt="gltf-viewer-0.1" src="https://user-images.githubusercontent.com/1647415/30771307-d70dbd26-a044-11e7-9ed1-b0e2ba80198c.png">

Some glTF features are not implemented yet, most notably animations. See [#3](https://github.com/bwasty/gltf-viewer/issues/3) for details.

## Installation
### Binaries (Win/Linux/macOS)
Expand All @@ -26,33 +30,36 @@ git clone https://github.com/bwasty/gltf-viewer.git
cd gltf-viewer
cargo install gltf-viewer
```

<!--
#### Additional dependencies (Ubuntu)
`sudo apt-get install libssl-dev`
-->

## Usage
```shell
USAGE:
gltf-viewer [OPTIONS] <FILE/URL>
gltf-viewer [OPTIONS] <FILE>

OPTIONS:
-s, --screenshot <FILE> Create screenshot (PNG)
-v, --verbose Enable verbose logging.
-v, --verbose Enable verbose logging (log level INFO). Can be repeated multiple times to increase log level to DEBUG/TRACE
-w, --width <WIDTH> Width in pixels [default: 800]
-h, --height <HEIGHT> Height in pixels [default: 600]
--help Prints help information
-V, --version Prints version information

ARGS:
<FILE/URL> glTF file name or URL
<FILE> glTF file name
```
Both .gltf and .glb files are supported.
Navigate the scene with `WASD` + Mouse.

### Example
```
gltf-viewer https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/BarramundiFish/glTF/BarramundiFish.gltf
$ curl -O https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Box/glTF-Binary/Box.glb
$ gltf-viewer Box.glb
```
For more models, see [KhronosGroup/glTF-Sample-Models](https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0).

## Goals
* Complete gltF 2.0 support
Expand All @@ -61,6 +68,7 @@ gltf-viewer https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/ma
* Platforms: Windows, Linux, macOS, Browser (via WebAssembly)
* Graphics backends:
- OpenGL ES 3.0 (-> WebGL 2.0 via WebAssembly)
- currently OpenGL 3.3 is used
- Vulkan?
* VR support
* Focus: OpenVR (HTC Vive)
Expand Down
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ notifications:

# Building is done in the test phase, so we disable Appveyor's build phase.
build: false

skip_branch_with_pr: true
3 changes: 3 additions & 0 deletions ci/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ main() {
| grep -E '^v[0.1.0-9.]+$' \
| $sort --version-sort \
| tail -n1)
# TODO!: temporary workaround for https://github.com/japaric/cross/issues/126
tag=v0.1.11

curl -LSfs https://japaric.github.io/trust/install.sh | \
sh -s -- \
--force \
Expand Down
1 change: 0 additions & 1 deletion rls.toml

This file was deleted.

20 changes: 13 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(dead_code)]
#![allow(unknown_lints)]
// #![allow(unused_features)]
// #![feature(test)]
#[macro_use] extern crate clap;
Expand Down Expand Up @@ -62,10 +63,10 @@ pub fn main() {
.version(crate_version!())
.setting(AppSettings::UnifiedHelpMessage)
.setting(AppSettings::DeriveDisplayOrder)
.arg(Arg::with_name("FILE/URL")
.arg(Arg::with_name("FILE") // TODO!: re-add URL when fixed...
.required(true)
.takes_value(true)
.help("glTF file name or URL"))
.help("glTF file name"))
.arg(Arg::with_name("screenshot")
.long("screenshot")
.short("s")
Expand All @@ -75,7 +76,7 @@ pub fn main() {
.long("verbose")
.short("v")
.multiple(true)
.help("Enable verbose logging (log level INFO). Can be repeated multiple times to increase to log level DEBUG/TRACE)"))
.help("Enable verbose logging (log level INFO). Can be repeated multiple times to increase log level to DEBUG/TRACE)"))
.arg(Arg::with_name("WIDTH")
.long("width")
.short("w")
Expand All @@ -89,7 +90,7 @@ pub fn main() {
.help("Height in pixels")
.validator(|value| value.parse::<u32>().map(|_| ()).map_err(|err| err.to_string())))
.get_matches();
let source = args.value_of("FILE/URL").unwrap();
let source = args.value_of("FILE").unwrap();
let width: u32 = args.value_of("WIDTH").unwrap().parse().unwrap();
let height: u32 = args.value_of("HEIGHT").unwrap().parse().unwrap();

Expand Down Expand Up @@ -249,8 +250,11 @@ impl GltfViewer {
let (gltf, buffers) = match gltf_importer::import_with_config(source, config) {
Ok((gltf, buffers)) => (gltf, buffers),
Err(err) => {
error!("glTF import failed: {}", err);
std::process::exit(1);
error!("glTF import failed: {:?}", err);
if let gltf_importer::Error::Io(_) = err {
error!("Hint: Are the .bin file(s) referenced by the .gltf file available?")
}
std::process::exit(1)
},
};

Expand Down Expand Up @@ -297,7 +301,7 @@ impl GltfViewer {
loop {
// per-frame time logic
// NOTE: Deliberately ignoring the seconds of `elapsed()`
self.delta_time = (self.last_frame.elapsed().subsec_nanos() as f64) / 1_000_000_000.0;
self.delta_time = f64::from(self.last_frame.elapsed().subsec_nanos()) / 1_000_000_000.0;
self.last_frame = Instant::now();

// events
Expand Down Expand Up @@ -355,6 +359,7 @@ impl GltfViewer {
}
}

#[allow(too_many_arguments)]
fn process_events(
events_loop: &mut glutin::EventsLoop,
gl_window: &glutin::GlWindow,
Expand All @@ -366,6 +371,7 @@ fn process_events(
height: &mut u32) -> bool
{
let mut keep_running = true;
#[allow(single_match)]
events_loop.poll_events(|event| {
match event {
glutin::Event::WindowEvent{ event, .. } => match event {
Expand Down
4 changes: 2 additions & 2 deletions src/render/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ fn load_texture(
assert_eq!(tex_coord, 0, "not yet implemented: tex coord set must be 0 (Material::from_gltf)");

if let Some(tex) = scene.textures.iter().find(|tex| (***tex).index == g_texture.index()) {
return tex.clone()
return Rc::clone(tex)
}

let texture = Rc::new(Texture::from_gltf(g_texture, tex_coord, buffers, base_path));
scene.textures.push(texture.clone());
scene.textures.push(Rc::clone(&texture));
texture
}
6 changes: 4 additions & 2 deletions src/render/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ pub struct Node {
pub bounds: Bounds,
}


impl Node {
// TODO!: refactor transformations using mint and non-deprecated functions
#[allow(deprecated)]
pub fn from_gltf(
g_node: gltf::Node,
scene: &mut Scene,
Expand All @@ -45,7 +48,7 @@ impl Node {
let mut mesh = None;
if let Some(g_mesh) = g_node.mesh() {
if let Some(existing_mesh) = scene.meshes.iter().find(|mesh| (***mesh).index == g_mesh.index()) {
mesh = Some(existing_mesh.clone());
mesh = Some(Rc::clone(existing_mesh));
}

if mesh.is_none() { // not using else due to borrow-checking madness
Expand All @@ -62,7 +65,6 @@ impl Node {
matrix,
mesh,
rotation,
// TODO!!: use of deprecated item: Use `transform().decomposed()` instead.
scale: g_node.scale().into(),
translation: g_node.translation().into(),
name: g_node.name().map(|s| s.into()),
Expand Down
8 changes: 4 additions & 4 deletions src/render/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ impl Primitive {

let mut material = None;
if let Some(mat) = scene.materials.iter().find(|m| (***m).index == g_material.index()) {
material = mat.clone().into()
material = Rc::clone(mat).into()
}

if material.is_none() { // no else due to borrow checker madness
let mat = Rc::new(Material::from_gltf(&g_material, scene, buffers, base_path));
scene.materials.push(mat.clone());
scene.materials.push(Rc::clone(&mat));
material = Some(mat);
};
let material = material.unwrap();
Expand All @@ -198,15 +198,15 @@ impl Primitive {
let mut new_shader = false; // borrow checker workaround
let shader =
if let Some(shader) = scene.shaders.get(&shader_flags) {
shader.clone()
Rc::clone(shader)
}
else {
new_shader = true;
PbrShader::new(shader_flags).into()

};
if new_shader {
scene.shaders.insert(shader_flags, shader.clone());
scene.shaders.insert(shader_flags, Rc::clone(&shader));
}

Primitive::new(bounds.into(), vertices, indices, material, shader)
Expand Down
6 changes: 3 additions & 3 deletions src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ impl Shader {
/// utility function for checking shader compilation/linking errors.
/// ------------------------------------------------------------------------
unsafe fn check_compile_errors(&self, shader: u32, type_: &str) {
let mut success = gl::FALSE as GLint;
let mut success = i32::from(gl::FALSE);
let mut info_log = Vec::with_capacity(1024);
info_log.set_len(1024 - 1); // subtract 1 to skip the trailing null character
if type_ != "PROGRAM" {
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success);
let log_type = if success == gl::TRUE as GLint { "WARNING" } else { "ERROR" };
let log_type = if success == i32::from(gl::TRUE) { "WARNING" } else { "ERROR" };
let mut length = 0;
gl::GetShaderInfoLog(shader, 1024, &mut length, info_log.as_mut_ptr() as *mut GLchar);
if length == 0 { return }
Expand All @@ -162,7 +162,7 @@ impl Shader {

} else {
gl::GetProgramiv(shader, gl::LINK_STATUS, &mut success);
let log_type = if success == gl::TRUE as GLint { "WARNING" } else { "ERROR" };
let log_type = if success == i32::from(gl::TRUE) { "WARNING" } else { "ERROR" };
let mut length = 0;
gl::GetProgramInfoLog(shader, 1024, &mut length, info_log.as_mut_ptr() as *mut GLchar);
if length == 0 { return }
Expand Down
29 changes: 0 additions & 29 deletions src/shaders/simple-frag.glsl

This file was deleted.

27 changes: 0 additions & 27 deletions src/shaders/simple-vert.glsl

This file was deleted.

4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn elapsed(start_time: &Instant) -> String {
fn format_duration(duration: Duration) -> String {
let secs = duration.as_secs();
let nanos = duration.subsec_nanos();
let ms = nanos as f64 / 1_000_000.0;
let ms = f64::from(nanos) / 1_000_000.0;
if secs > 0 {
let secs = secs as f64 + ms / 1000.0;
format!("{:<4.*} s", 1, secs)
Expand All @@ -22,7 +22,7 @@ fn format_duration(duration: Duration) -> String {
if ms >= 20.0 { 0 }
else if ms >= 1.0 { 1 }
else {
let micros = nanos as f64 / 1000.0;
let micros = f64::from(nanos) / 1000.0;
let places = if micros >= 10.0 { 0 } else { 2 };
return format!("{:>3.*} µs", places, micros)
};
Expand Down

0 comments on commit f4fc598

Please sign in to comment.