Skip to content

Commit 75429f4

Browse files
committed
render: use left-handed coordinate system and y-up
1 parent 4ba2f72 commit 75429f4

File tree

24 files changed

+180
-115
lines changed

24 files changed

+180
-115
lines changed

assets/models/monkey/Monkey.bin

59.7 KB
Binary file not shown.

assets/models/monkey/Monkey.gltf

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"asset" : {
3-
"generator" : "Khronos glTF Blender I/O v1.1.46",
3+
"generator" : "Khronos glTF Blender I/O v1.2.75",
44
"version" : "2.0"
55
},
66
"scene" : 0,
@@ -37,63 +37,63 @@
3737
{
3838
"bufferView" : 0,
3939
"componentType" : 5126,
40-
"count" : 1968,
40+
"count" : 3321,
4141
"max" : [
42-
1.3671875,
43-
0.8515625,
44-
0.984375
42+
1.325934886932373,
43+
0.9392361640930176,
44+
0.8223199844360352
4545
],
4646
"min" : [
47-
-1.3671875,
48-
-0.8515625,
49-
-0.984375
47+
-1.325934886932373,
48+
-0.9704862236976624,
49+
-0.7782661318778992
5050
],
5151
"type" : "VEC3"
5252
},
5353
{
5454
"bufferView" : 1,
5555
"componentType" : 5126,
56-
"count" : 1968,
56+
"count" : 3321,
5757
"type" : "VEC3"
5858
},
5959
{
6060
"bufferView" : 2,
6161
"componentType" : 5126,
62-
"count" : 1968,
62+
"count" : 3321,
6363
"type" : "VEC2"
6464
},
6565
{
6666
"bufferView" : 3,
6767
"componentType" : 5123,
68-
"count" : 2904,
68+
"count" : 11808,
6969
"type" : "SCALAR"
7070
}
7171
],
7272
"bufferViews" : [
7373
{
7474
"buffer" : 0,
75-
"byteLength" : 23616,
75+
"byteLength" : 39852,
7676
"byteOffset" : 0
7777
},
7878
{
7979
"buffer" : 0,
80-
"byteLength" : 23616,
81-
"byteOffset" : 23616
80+
"byteLength" : 39852,
81+
"byteOffset" : 39852
8282
},
8383
{
8484
"buffer" : 0,
85-
"byteLength" : 15744,
86-
"byteOffset" : 47232
85+
"byteLength" : 26568,
86+
"byteOffset" : 79704
8787
},
8888
{
8989
"buffer" : 0,
90-
"byteLength" : 5808,
91-
"byteOffset" : 62976
90+
"byteLength" : 23616,
91+
"byteOffset" : 106272
9292
}
9393
],
9494
"buffers" : [
9595
{
96-
"byteLength" : 68784,
96+
"byteLength" : 129888,
9797
"uri" : "Monkey.bin"
9898
}
9999
]

crates/bevy_core/src/transform/face_toward.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub trait FaceToward {
66

77
impl FaceToward for Mat4 {
88
fn face_toward(eye: Vec3, center: Vec3, up: Vec3) -> Self {
9-
let forward = (eye - center).normalize();
9+
let forward = (center - eye).normalize();
1010
let right = up.cross(forward).normalize();
1111
let up = forward.cross(right);
1212
Mat4::from_cols(

crates/bevy_pbr/src/pipelines/forward/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub const FORWARD_PIPELINE_HANDLE: Handle<PipelineDescriptor> =
1717
pub fn build_forward_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
1818
PipelineDescriptor {
1919
rasterization_state: Some(RasterizationStateDescriptor {
20-
front_face: FrontFace::Ccw,
20+
front_face: FrontFace::Cw,
2121
cull_mode: CullMode::Back,
2222
depth_bias: 0,
2323
depth_bias_slope_scale: 0.0,

crates/bevy_render/src/camera/projection.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ pub struct PerspectiveProjection {
1717

1818
impl CameraProjection for PerspectiveProjection {
1919
fn get_projection_matrix(&self) -> Mat4 {
20-
let projection = Mat4::perspective_rh_gl(self.fov, self.aspect_ratio, self.near, self.far);
21-
projection
20+
Mat4::perspective_lh(self.fov, self.aspect_ratio, self.near, self.far)
2221
}
2322
fn update(&mut self, width: usize, height: usize) {
2423
self.aspect_ratio = width as f32 / height as f32;
@@ -56,15 +55,14 @@ pub struct OrthographicProjection {
5655

5756
impl CameraProjection for OrthographicProjection {
5857
fn get_projection_matrix(&self) -> Mat4 {
59-
let projection = Mat4::orthographic_rh(
58+
Mat4::orthographic_lh(
6059
self.left,
6160
self.right,
6261
self.bottom,
6362
self.top,
6463
self.near,
6564
self.far,
66-
);
67-
projection
65+
)
6866
}
6967
fn update(&mut self, width: usize, height: usize) {
7068
match self.window_origin {
@@ -93,8 +91,8 @@ impl Default for OrthographicProjection {
9391
right: 0.0,
9492
bottom: 0.0,
9593
top: 0.0,
96-
near: -1.0,
97-
far: 0.1,
94+
near: 0.0,
95+
far: 1000.0,
9896
window_origin: WindowOrigin::Center,
9997
}
10098
}

crates/bevy_render/src/mesh.rs

Lines changed: 102 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@ pub mod shape {
249249

250250
pub struct Quad {
251251
pub size: Vec2,
252+
pub flip: bool,
253+
}
254+
255+
impl Quad {
256+
pub fn new(size: Vec2) -> Self {
257+
Self { size, flip: false }
258+
}
259+
260+
pub fn flipped(size: Vec2) -> Self {
261+
Self { size, flip: true }
262+
}
252263
}
253264

254265
impl From<Quad> for Mesh {
@@ -260,28 +271,53 @@ pub mod shape {
260271
let north_east = vec2(extent_x, extent_y);
261272
let south_west = vec2(-extent_x, -extent_y);
262273
let south_east = vec2(extent_x, -extent_y);
263-
let vertices = &[
264-
(
265-
[south_west.x(), south_west.y(), 0.0],
266-
[0.0, 0.0, 1.0],
267-
[0.0, 1.0],
268-
),
269-
(
270-
[north_west.x(), north_west.y(), 0.0],
271-
[0.0, 0.0, 1.0],
272-
[0.0, 0.0],
273-
),
274-
(
275-
[north_east.x(), north_east.y(), 0.0],
276-
[0.0, 0.0, 1.0],
277-
[1.0, 0.0],
278-
),
279-
(
280-
[south_east.x(), south_east.y(), 0.0],
281-
[0.0, 0.0, 1.0],
282-
[1.0, 1.0],
283-
),
284-
];
274+
let vertices = if quad.flip {
275+
[
276+
(
277+
[south_west.x(), south_west.y(), 0.0],
278+
[0.0, 0.0, 1.0],
279+
[0.0, 1.0],
280+
),
281+
(
282+
[north_west.x(), north_west.y(), 0.0],
283+
[0.0, 0.0, 1.0],
284+
[0.0, 0.0],
285+
),
286+
(
287+
[north_east.x(), north_east.y(), 0.0],
288+
[0.0, 0.0, 1.0],
289+
[1.0, 0.0],
290+
),
291+
(
292+
[south_east.x(), south_east.y(), 0.0],
293+
[0.0, 0.0, 1.0],
294+
[1.0, 1.0],
295+
),
296+
]
297+
} else {
298+
[
299+
(
300+
[south_east.x(), south_east.y(), 0.0],
301+
[0.0, 0.0, 1.0],
302+
[1.0, 1.0],
303+
),
304+
(
305+
[north_east.x(), north_east.y(), 0.0],
306+
[0.0, 0.0, 1.0],
307+
[1.0, 0.0],
308+
),
309+
(
310+
[north_west.x(), north_west.y(), 0.0],
311+
[0.0, 0.0, 1.0],
312+
[0.0, 0.0],
313+
),
314+
(
315+
[south_west.x(), south_west.y(), 0.0],
316+
[0.0, 0.0, 1.0],
317+
[0.0, 1.0],
318+
),
319+
]
320+
};
285321

286322
let indices = vec![0, 2, 1, 0, 3, 2];
287323

@@ -312,10 +348,51 @@ pub mod shape {
312348

313349
impl From<Plane> for Mesh {
314350
fn from(plane: Plane) -> Self {
315-
Quad {
316-
size: Vec2::new(plane.size, plane.size),
351+
let extent = plane.size / 2.0;
352+
353+
let vertices = [
354+
(
355+
[extent, 0.0, -extent],
356+
[0.0, 1.0, 0.0],
357+
[1.0, 1.0],
358+
),
359+
(
360+
[extent, 0.0, extent],
361+
[0.0, 1.0, 0.0],
362+
[1.0, 0.0],
363+
),
364+
(
365+
[-extent, 0.0, extent],
366+
[0.0, 1.0, 0.0],
367+
[0.0, 0.0],
368+
),
369+
(
370+
[-extent, 0.0, -extent],
371+
[0.0, 1.0, 0.0],
372+
[0.0, 1.0],
373+
),
374+
];
375+
376+
let indices = vec![0, 2, 1, 0, 3, 2];
377+
378+
let mut positions = Vec::new();
379+
let mut normals = Vec::new();
380+
let mut uvs = Vec::new();
381+
for (position, normal, uv) in vertices.iter() {
382+
positions.push(position.clone());
383+
normals.push(normal.clone());
384+
uvs.push(uv.clone());
385+
}
386+
387+
Mesh {
388+
primitive_topology: PrimitiveTopology::TriangleList,
389+
attributes: vec![
390+
VertexAttribute::position(positions),
391+
VertexAttribute::normal(normals),
392+
VertexAttribute::uv(uvs),
393+
],
394+
indices: Some(indices),
317395
}
318-
.into()
319396
}
320397
}
321398
}

crates/bevy_render/src/pipeline/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl PipelineDescriptor {
7272
sample_mask: !0,
7373
alpha_to_coverage_enabled: false,
7474
rasterization_state: Some(RasterizationStateDescriptor {
75-
front_face: FrontFace::Ccw,
75+
front_face: FrontFace::Cw,
7676
cull_mode: CullMode::Back,
7777
depth_bias: 0,
7878
depth_bias_slope_scale: 0.0,

crates/bevy_render/src/pipeline/state_descriptors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub enum FrontFace {
7474

7575
impl Default for FrontFace {
7676
fn default() -> Self {
77-
FrontFace::Ccw
77+
FrontFace::Cw
7878
}
7979
}
8080

crates/bevy_sprite/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ impl AppPlugin for SpritePlugin {
5050
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
5151
meshes.set(
5252
QUAD_HANDLE,
53-
Mesh::from(shape::Quad {
54-
size: Vec2::new(1.0, 1.0),
55-
}),
53+
// Use a flipped quad because the camera is facing "forward" but quads should face backward
54+
Mesh::from(shape::Quad::flipped(Vec2::new(1.0, 1.0))),
5655
);
5756

5857
let mut color_materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap();

crates/bevy_sprite/src/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub const SPRITE_SHEET_PIPELINE_HANDLE: Handle<PipelineDescriptor> =
2121
pub fn build_sprite_sheet_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
2222
PipelineDescriptor {
2323
rasterization_state: Some(RasterizationStateDescriptor {
24-
front_face: FrontFace::Ccw,
24+
front_face: FrontFace::Cw,
2525
cull_mode: CullMode::None,
2626
depth_bias: 0,
2727
depth_bias_slope_scale: 0.0,
@@ -66,7 +66,7 @@ pub fn build_sprite_sheet_pipeline(shaders: &mut Assets<Shader>) -> PipelineDesc
6666
pub fn build_sprite_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
6767
PipelineDescriptor {
6868
rasterization_state: Some(RasterizationStateDescriptor {
69-
front_face: FrontFace::Ccw,
69+
front_face: FrontFace::Cw,
7070
cull_mode: CullMode::None,
7171
depth_bias: 0,
7272
depth_bias_slope_scale: 0.0,

0 commit comments

Comments
 (0)