Skip to content

[WIP] Ray casting #43

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

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1607747
Basic OBJ loading
kvark Jun 5, 2017
44b6b4f
Moved test data to a separate folder
kvark Jun 5, 2017
0d739c8
Proper normals/UV from OBJ files
kvark Jun 6, 2017
1fbb3ec
Basic OBJ material loading
kvark Jun 6, 2017
5561022
LRU-indexing of OBJ meshes
kvark Jun 6, 2017
cb7a814
Improved OBJ material loading with a new example model
kvark Jun 7, 2017
d1ac277
Texture cache
kvark Jun 7, 2017
f2db026
Texturing for basic material
kvark Jun 7, 2017
89473e9
WIP mint integration
kvark Jun 8, 2017
71e0562
Mint integration - examples
kvark Jun 9, 2017
23d6fc7
Filled up cargo manifest
kvark Jun 9, 2017
6b29ac7
Removal of local transform state
kvark Jun 10, 2017
11e6991
Removal of VisualObject
kvark Jun 11, 2017
e9beb6a
Removal of LightObject
kvark Jun 11, 2017
fdf2c07
Flat Lambertian shading
kvark Jun 12, 2017
ebf9af0
Removal of Object::visible flag
kvark Jun 12, 2017
8246590
NodeInfo logic
kvark Jun 12, 2017
8f74755
Quick fix for int/uint mismatch with MAX_LIGHTS
kvark Jun 12, 2017
2c5cf94
Gitter integration
kvark Jun 16, 2017
3fce33d
[WIP] Documentation (#29)
vitvakatu Jun 16, 2017
c9ece22
Update graph in sync
vitvakatu Jun 18, 2017
3ad7954
Merge #32
bors[bot] Jun 18, 2017
bf87746
New input API
kvark Jun 19, 2017
f629eb3
Merge #33
bors[bot] Jun 20, 2017
4de4e84
Native projection types
kvark Jun 19, 2017
6cf6abe
Merge #34
bors[bot] Jun 20, 2017
9eb84eb
Group demo
kvark Jun 21, 2017
6da42be
Vertex buffer sharing
kvark Jun 21, 2017
058a696
Exposing Pointer<Node> publically as NodePointer
sunjay Jun 24, 2017
cf7a555
Merge #39
bors[bot] Jun 24, 2017
719a0c8
Added macro for implementing conversion traits on structs that wrap t…
sunjay Jun 24, 2017
c94a970
changing # to :: in macro syntax and ignoring doctest which will not …
sunjay Jun 24, 2017
99df2ef
Merge #40
bors[bot] Jun 24, 2017
034d941
WIP bounds
kvark Jun 20, 2017
0ddf67b
WIP collision-rs integration
kvark Jun 20, 2017
e235fff
AABB backwards pass
kvark Jun 25, 2017
4996482
Shape trait
kvark Jun 26, 2017
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
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ cache: cargo
rust:
- nightly
- stable

notifications:
webhooks:
urls:
- https://webhooks.gitter.im/e/fc87a993d7683ec8b279
on_success: change
on_failure: always
on_start: never

script:
- cargo build
- cargo doc
- cargo test
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then (cargo bench); fi
# - if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then (cargo bench); fi
56 changes: 37 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
[package]
name = "three"
version = "0.1.0"
version = "0.2.0"
authors = ["Dzmitry Malyshau <[email protected]>"]
license = "Apache-2.0"
description = "Three.js inspired 3D engine in Rust"
categories = ["graphics", "game-engines"]
keywords = ["gamedev", "graphics", "engine", "3D"]
homepage = "https://github.com/kvark/three-rs"
repository = "https://github.com/kvark/three-rs"
documentation = "https://docs.rs/three/"
exclude = ["doc", "bors.toml", ".travis.yml", "test_data"]

[lib]

[[example]]
name = "lights"

[[example]]
name = "materials"

[[example]]
name = "shapes"

[[example]]
name = "sprite"

[[example]]
name = "aviator"
path = "examples/aviator/main.rs"

[lib]

[features]
default = ["opengl"]
"opengl" = ["gfx_device_gl", "gfx_window_glutin", "glutin"]

[dependencies]
cgmath = "0.14"
froggy = "0.3"
collision = {path = "../collision"}
froggy = {path = "../froggy"}
genmesh = "0.5"
gfx = "0.16"
image = "0.13"
log = "0.3"
obj = { version = "0.6", features = ["genmesh"] }
mint = "0.4.2"
winit = "0.6"
# OpenGL
gfx_device_gl = { version = "0.14", optional = true }
Expand All @@ -42,3 +37,26 @@ glutin = { version = "0.8", optional = true }
[dev-dependencies]
env_logger = "0.4"
rand = "0.3"


[[example]]
name = "lights"

[[example]]
name = "materials"

[[example]]
name = "obj"

[[example]]
name = "shapes"

[[example]]
name = "sprite"

[[example]]
name = "group"

[[example]]
name = "aviator"
path = "examples/aviator/main.rs"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
[![Build Status](https://travis-ci.org/kvark/three-rs.svg)](https://travis-ci.org/kvark/three-rs)
[![Docs](https://docs.rs/three/badge.svg)](https://docs.rs/three)
[![Crates.io](https://img.shields.io/crates/v/three.svg?maxAge=2592000)](https://crates.io/crates/three)
[![Gitter](https://badges.gitter.im/kvark/three-rs.svg)](https://gitter.im/three-rs/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

Totally not inspired Rust 3D library! Ok, maybe, just a tiny bit... by [Three.JS](http://threejs.org).

If you a looking for something simple to prototype 3D graphics with, you found it.

## Screenshots

![Aviator](examples/aviator/shot.png)
![CarObj](test_data/obj-car.png)

## Motivation and Goals

Expand Down
5 changes: 4 additions & 1 deletion data/shaders/basic_ps.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#version 150 core
#include locals

in vec2 v_TexCoord;
out vec4 Target0;

uniform sampler2D t_Map;

void main() {
Target0 = u_Color;
Target0 = u_Color * texture(t_Map, v_TexCoord);
}
3 changes: 3 additions & 0 deletions data/shaders/basic_vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

in vec4 a_Position;
in vec4 a_Normal;
in vec2 a_TexCoord;
out vec2 v_TexCoord;

uniform b_Globals {
mat4 u_ViewProj;
};

void main() {
v_TexCoord = mix(u_UvRange.xy, u_UvRange.zw, a_TexCoord);
gl_Position = u_ViewProj * u_World * a_Position;
}
9 changes: 6 additions & 3 deletions data/shaders/gouraud_ps.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#version 150 core

in vec4 v_ResultColor;
flat in vec4 v_ResultColorFlat;
flat in float v_Smooth;
in vec4 v_LightEval[2];
flat in vec4 v_LightEvalFlat[2];
in vec4 v_ShadowCoord[2];

out vec4 Target0;
Expand All @@ -10,15 +13,15 @@ uniform sampler2DShadow t_Shadow0;
uniform sampler2DShadow t_Shadow1;

void main() {
Target0 = v_ResultColor;
Target0 = mix(v_ResultColorFlat, v_ResultColor, v_Smooth);
if (v_ShadowCoord[0].w != 0.0) {
vec3 coord = v_ShadowCoord[0].xyz / v_ShadowCoord[0].w;
float shadow = texture(t_Shadow0, 0.5 * coord + 0.5);
Target0 += shadow * v_LightEval[0];
Target0 += shadow * mix(v_LightEvalFlat[0], v_LightEval[0], v_Smooth);
}
if (v_ShadowCoord[1].w != 0.0) {
vec3 coord = v_ShadowCoord[1].xyz / v_ShadowCoord[1].w;
float shadow = texture(t_Shadow1, 0.5 * coord + 0.5);
Target0 += shadow * v_LightEval[1];
Target0 += shadow * mix(v_LightEvalFlat[1], v_LightEval[1], v_Smooth);
}
}
20 changes: 15 additions & 5 deletions data/shaders/gouraud_vs.glsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#version 150 core
#include locals lights

#define MAX_SHADOWS 2

in vec4 a_Position;
in vec4 a_Normal;
out vec4 v_ResultColor;
out vec4 v_LightEval[2];
out vec4 v_ShadowCoord[2];
flat out vec4 v_ResultColorFlat;
flat out float v_Smooth;
out vec4 v_LightEval[MAX_SHADOWS];
flat out vec4 v_LightEvalFlat[MAX_SHADOWS];
out vec4 v_ShadowCoord[MAX_SHADOWS];

uniform b_Globals {
mat4 u_ViewProj;
Expand All @@ -15,9 +20,12 @@ uniform b_Globals {
void main() {
vec4 world = u_World * a_Position;
vec3 normal = normalize(mat3(u_World) * a_Normal.xyz);
v_ShadowCoord[0] = v_ShadowCoord[1] = vec4(0.0);
v_LightEval[0] = v_LightEval[1] = vec4(0.0);
for(int i=0; i<MAX_SHADOWS; ++i) {
v_ShadowCoord[i] = vec4(0.0);
v_LightEval[i] = v_LightEvalFlat[i] = vec4(0.0);
}
v_ResultColor = vec4(0.0);
v_Smooth = u_MatParams.x;

for(uint i=0U; i < min(MAX_LIGHTS, u_NumLights); ++i) {
Light light = u_Lights[i];
Expand All @@ -33,13 +41,15 @@ void main() {
vec4 color = light.intensity.y * max(0.0, dot_nl) * u_Color * light.color;
// compute shadow coordinates
int shadow_index = light.shadow_params[0];
if (0 <= shadow_index && shadow_index < 2) {
if (0 <= shadow_index && shadow_index < MAX_SHADOWS) {
v_ShadowCoord[shadow_index] = light.projection * world;
v_LightEval[shadow_index] = color;
v_LightEvalFlat[shadow_index] = color;
} else {
v_ResultColor += color;
}
}

v_ResultColorFlat = v_ResultColor;
gl_Position = u_ViewProj * world;
}
2 changes: 1 addition & 1 deletion data/shaders/lights.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define MAX_LIGHTS 4
#define MAX_LIGHTS 4U

struct Light {
mat4 projection;
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/phong_ps.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main() {
// hemisphere light test
if (dot(light.color_back, light.color_back) > 0.0) {
vec4 irradiance = mix(light.color_back, light.color, dot_nl*0.5 + 0.5);
color += shadow * light.intensity.y * u_Color * irradiance;
color += shadow * light.intensity.x * u_Color * irradiance;
} else {
float kd = light.intensity.x + light.intensity.y * max(0.0, dot_nl);
color += shadow * kd * u_Color * light.color;
Expand Down
47 changes: 25 additions & 22 deletions examples/aviator/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate env_logger;
extern crate cgmath;
extern crate mint;
extern crate rand;
extern crate three;

Expand All @@ -24,8 +25,8 @@ fn main() {
let mut win = three::Window::new("Three-rs Aviator demo", "data/shaders");
win.scene.background = three::Background::Color(COLOR_BACKGROUND);

let mut cam = win.factory.perspective_camera(60.0, 0.0, 1.0, 1000.0);
cam.transform_mut().disp = three::Vector::new(0.0, 100.0, 200.0);
let mut cam = win.factory.perspective_camera(60.0, 1.0, 1000.0);
cam.set_position([0.0, 100.0, 200.0]);
win.scene.add(&cam);

//TODO: win.scene.fog = Some(three::Fog::new(...));
Expand All @@ -34,46 +35,48 @@ fn main() {
let hemi_light = win.factory.hemisphere_light(0xaaaaaa, 0x000000, 0.9);
win.scene.add(&hemi_light);
let mut dir_light = win.factory.directional_light(0xffffff, 0.9);
dir_light.transform_mut().look_at(three::Position::new(150.0, 350.0, 350.0),
three::Position::new(0.0, 0.0, 0.0));
dir_light.look_at([150.0, 350.0, 350.0], [0.0, 0.0, 0.0], None);
let shadow_map = win.factory.shadow_map(2048, 2048);
dir_light.set_shadow(shadow_map, 800.0, 800.0, 1.0, 1000.0);
dir_light.set_shadow(shadow_map, 400.0, 1.0, 1000.0);
win.scene.add(&dir_light);
let ambient_light = win.factory.ambient_light(0xdc8874, 0.5);
win.scene.add(&ambient_light);

let mut sea = {
let geo = three::Geometry::new_cylinder(600.0, 600.0, 800.0, 40);
let material = three::Material::MeshLambert{ color: COLOR_BLUE };
let material = three::Material::MeshLambert{ color: COLOR_BLUE, flat: true };
win.factory.mesh(geo, material)
};
*sea.transform_mut() = three::Transform {
scale: 1.0,
rot: three::Orientation::from_angle_x(-cgmath::Rad::turn_div_4()),
disp: cgmath::vec3(0.0, -600.0, 0.0),
};
let sea_base_q = cgmath::Quaternion::from_angle_x(-cgmath::Rad::turn_div_4());
sea.set_transform([0.0, -600.0, 0.0],
[sea_base_q.v.x, sea_base_q.v.y, sea_base_q.v.z, sea_base_q.s],
1.0);
win.scene.add(&sea);

let mut sky = sky::Sky::new(&mut rng, &mut win.factory);
sky.group.transform_mut().disp.y = -600.0;
sky.group.set_position([0.0, -600.0, 0.0]);
win.scene.add(&sky.group);

let mut airplane = plane::AirPlane::new(&mut win.factory);
*airplane.group.transform_mut() = three::Transform {
scale: 0.25,
rot: three::Orientation::one(),
disp: cgmath::vec3(0.0, 100.0, 0.0),
};
airplane.group.set_transform([0.0, 100.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
0.25);
win.scene.add(&airplane.group);

while let Some(events) = win.update() {
let timer = win.input.time();
while win.update() && !three::KEY_ESCAPE.is_hit(&win.input) {
use cgmath::{Quaternion, Rad};
// assume the original velocities are given for 60fps
let dt = events.time_delta * 60.0;
let time = 60.0 * timer.get(&win.input);

airplane.update(dt, events.mouse_pos);
airplane.update(time, win.input.get_mouse_pos());

sea.transform_mut().rotate(0.0, 0.0, 0.005 * dt);
sky.group.transform_mut().rotate(0.0, 0.0, 0.01 * dt);
let sea_angle = Rad(0.005 * time);
let sea_q = Quaternion::from_angle_z(sea_angle) * sea_base_q;
sea.set_orientation([sea_q.v.x, sea_q.v.y, sea_q.v.z, sea_q.s]);
let sky_angle = Rad(0.01 * time);
let sky_q = Quaternion::from_angle_z(sky_angle);
sky.group.set_orientation([sky_q.v.x, sky_q.v.y, sky_q.v.z, sky_q.s]);

win.render(&cam);
}
Expand Down
30 changes: 16 additions & 14 deletions examples/aviator/plane.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use cgmath::{Quaternion, Rad, Rotation3};
use mint;
use three;

use {COLOR_RED, COLOR_WHITE, COLOR_BROWN, COLOR_BROWN_DARK};
Expand Down Expand Up @@ -26,40 +28,40 @@ impl AirPlane {
v.y += if v.y > 0.0 {-10.0} else {30.0};
}
}
factory.mesh(geo, three::Material::MeshLambert{ color: COLOR_RED })
factory.mesh(geo, three::Material::MeshLambert{ color: COLOR_RED, flat: false })
};
group.add(&cockpit);
let mut engine = factory.mesh(
three::Geometry::new_box(20.0, 50.0, 50.0),
three::Material::MeshLambert{ color: COLOR_WHITE }
three::Material::MeshLambert{ color: COLOR_WHITE, flat: false }
);
engine.transform_mut().disp.x = 40.0;
engine.set_position([40.0, 0.0, 0.0]);
group.add(&engine);
let mut tail = factory.mesh(
three::Geometry::new_box(15.0, 20.0, 5.0),
three::Material::MeshLambert{ color: COLOR_RED }
three::Material::MeshLambert{ color: COLOR_RED, flat: false }
);
tail.transform_mut().disp = three::Vector::new(-35.0, 25.0, 0.0);
tail.set_position([-35.0, 25.0, 0.0]);
group.add(&tail);
let wing = factory.mesh(
three::Geometry::new_box(40.0, 8.0, 150.0),
three::Material::MeshLambert{ color: COLOR_RED }
three::Material::MeshLambert{ color: COLOR_RED, flat: false }
);
group.add(&wing);

let mut propeller_group = factory.group();
propeller_group.transform_mut().disp = three::Vector::new(50.0, 0.0, 0.0);
propeller_group.set_position([50.0, 0.0, 0.0]);
group.add(&propeller_group);
let propeller = factory.mesh(
three::Geometry::new_box(20.0, 10.0, 10.0),
three::Material::MeshLambert{ color: COLOR_BROWN }
three::Material::MeshLambert{ color: COLOR_BROWN, flat: false }
);
propeller_group.add(&propeller);
let mut blade = factory.mesh(
three::Geometry::new_box(1.0, 100.0, 20.0),
three::Material::MeshLambert{ color: COLOR_BROWN_DARK }
three::Material::MeshLambert{ color: COLOR_BROWN_DARK, flat: false }
);
blade.transform_mut().disp = three::Vector::new(8.0, 0.0, 0.0);
blade.set_position([8.0, 0.0, 0.0]);
propeller_group.add(&blade);

AirPlane {
Expand All @@ -74,9 +76,9 @@ impl AirPlane {
}
}

pub fn update(&mut self, dt: f32, target: (f32, f32)) {
self.propeller_group.transform_mut().rotate(0.3 * dt, 0.0, 0.0);
self.group.transform_mut().disp =
three::Vector::new(0.0 + target.0 * 100.0, 100.0 + target.1 * 75.0, 0.0);
pub fn update(&mut self, time: f32, target: mint::Point2<f32>) {
let q = Quaternion::from_angle_x(Rad(0.3 * time));
self.propeller_group.set_orientation([q.v.x, q.v.y, q.v.z, q.s]);
self.group.set_position([0.0 + target.x * 100.0, 100.0 + target.y * 75.0, 0.0]);
}
}
Loading