ROTATION WORKS!!!!

This commit is contained in:
CDaut 2023-11-24 18:39:39 +01:00
parent 053a69e5b7
commit ab590a4844
7 changed files with 15 additions and 541 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

Before After
Before After

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
scenes/cube_on_plane.glb Normal file

Binary file not shown.

View file

@ -1,526 +0,0 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.1.34",
"version":"2.0"
},
"extensionsUsed":[
"KHR_lights_punctual"
],
"extensionsRequired":[
"KHR_lights_punctual"
],
"extensions":{
"KHR_lights_punctual":{
"lights":[
{
"color":[
1,
1,
1
],
"intensity":54351.41306588226,
"type":"point",
"name":"Light"
}
]
}
},
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
0,
1,
2,
3,
4,
5,
6
]
}
],
"nodes":[
{
"mesh":0,
"name":"Cube"
},
{
"extensions":{
"KHR_lights_punctual":{
"light":0
}
},
"name":"Light",
"rotation":[
-0.28416627645492554,
0.7269423007965088,
0.34203392267227173,
0.5232754945755005
],
"translation":[
4.076245307922363,
5.903861999511719,
-1.0054539442062378
]
},
{
"camera":0,
"name":"Camera",
"rotation":[
0,
1,
0,
0
],
"translation":[
2,
10,
-1.9999992847442627
]
},
{
"mesh":1,
"name":"Cube.001"
},
{
"mesh":2,
"name":"Cube.002",
"translation":[
2,
10,
-10
]
},
{
"mesh":3,
"name":"Cube.003",
"rotation":[
0,
0.3826834559440613,
0,
0.9238795638084412
],
"translation":[
2,
10,
10
]
},
{
"mesh":4,
"name":"Cube.004",
"translation":[
-10,
10,
0
]
}
],
"cameras":[
{
"name":"Camera",
"perspective":{
"aspectRatio":1,
"yfov":0.7853981852531433,
"zfar":100,
"znear":0.0010000000474974513
},
"type":"perspective"
}
],
"materials":[
{
"doubleSided":true,
"name":"Material",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.800000011920929,
0.800000011920929,
0.800000011920929,
1
],
"metallicFactor":0,
"roughnessFactor":0.5
}
},
{
"doubleSided":true,
"name":"Material.001",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.8002041578292847,
0,
0,
1
],
"metallicFactor":0,
"roughnessFactor":0.5
}
},
{
"doubleSided":true,
"name":"Material.003",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.019036345183849335,
0,
0.8002663850784302,
1
],
"metallicFactor":0,
"roughnessFactor":0.5
}
},
{
"doubleSided":true,
"name":"Material.004",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.0012476131087169051,
0.8003553152084351,
0,
1
],
"metallicFactor":0,
"roughnessFactor":0.5
}
}
],
"meshes":[
{
"name":"Cube",
"primitives":[
{
"attributes":{
"POSITION":0,
"NORMAL":1,
"TEXCOORD_0":2
},
"indices":3,
"material":0
}
]
},
{
"name":"Cube.001",
"primitives":[
{
"attributes":{
"POSITION":4,
"NORMAL":5,
"TEXCOORD_0":6
},
"indices":3,
"material":0
}
]
},
{
"name":"Cube.002",
"primitives":[
{
"attributes":{
"POSITION":7,
"NORMAL":8,
"TEXCOORD_0":9
},
"indices":10,
"material":1
}
]
},
{
"name":"Cube.003",
"primitives":[
{
"attributes":{
"POSITION":11,
"NORMAL":12,
"TEXCOORD_0":13
},
"indices":10,
"material":2
}
]
},
{
"name":"Cube.004",
"primitives":[
{
"attributes":{
"POSITION":14,
"NORMAL":15,
"TEXCOORD_0":16
},
"indices":10,
"material":3
}
]
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":14,
"max":[
1.4142135381698608,
2,
1.4142135381698608
],
"min":[
-1.4142135381698608,
0,
-1.4142135381698608
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":14,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":14,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5123,
"count":36,
"type":"SCALAR"
},
{
"bufferView":4,
"componentType":5126,
"count":14,
"max":[
-0.5857864618301392,
2,
-0.5857864618301392
],
"min":[
-3.4142136573791504,
0,
-3.4142136573791504
],
"type":"VEC3"
},
{
"bufferView":5,
"componentType":5126,
"count":14,
"type":"VEC3"
},
{
"bufferView":6,
"componentType":5126,
"count":14,
"type":"VEC2"
},
{
"bufferView":7,
"componentType":5126,
"count":14,
"max":[
1,
1,
1
],
"min":[
-1,
-1,
-1
],
"type":"VEC3"
},
{
"bufferView":8,
"componentType":5126,
"count":14,
"type":"VEC3"
},
{
"bufferView":9,
"componentType":5126,
"count":14,
"type":"VEC2"
},
{
"bufferView":10,
"componentType":5123,
"count":36,
"type":"SCALAR"
},
{
"bufferView":11,
"componentType":5126,
"count":14,
"max":[
1,
1,
1
],
"min":[
-1,
-1,
-1
],
"type":"VEC3"
},
{
"bufferView":12,
"componentType":5126,
"count":14,
"type":"VEC3"
},
{
"bufferView":13,
"componentType":5126,
"count":14,
"type":"VEC2"
},
{
"bufferView":14,
"componentType":5126,
"count":14,
"max":[
1,
1,
1
],
"min":[
-1,
-1,
-1
],
"type":"VEC3"
},
{
"bufferView":15,
"componentType":5126,
"count":14,
"type":"VEC3"
},
{
"bufferView":16,
"componentType":5126,
"count":14,
"type":"VEC2"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":168,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":168,
"byteOffset":168,
"target":34962
},
{
"buffer":0,
"byteLength":112,
"byteOffset":336,
"target":34962
},
{
"buffer":0,
"byteLength":72,
"byteOffset":448,
"target":34963
},
{
"buffer":0,
"byteLength":168,
"byteOffset":520,
"target":34962
},
{
"buffer":0,
"byteLength":168,
"byteOffset":688,
"target":34962
},
{
"buffer":0,
"byteLength":112,
"byteOffset":856,
"target":34962
},
{
"buffer":0,
"byteLength":168,
"byteOffset":968,
"target":34962
},
{
"buffer":0,
"byteLength":168,
"byteOffset":1136,
"target":34962
},
{
"buffer":0,
"byteLength":112,
"byteOffset":1304,
"target":34962
},
{
"buffer":0,
"byteLength":72,
"byteOffset":1416,
"target":34963
},
{
"buffer":0,
"byteLength":168,
"byteOffset":1488,
"target":34962
},
{
"buffer":0,
"byteLength":168,
"byteOffset":1656,
"target":34962
},
{
"buffer":0,
"byteLength":112,
"byteOffset":1824,
"target":34962
},
{
"buffer":0,
"byteLength":168,
"byteOffset":1936,
"target":34962
},
{
"buffer":0,
"byteLength":168,
"byteOffset":2104,
"target":34962
},
{
"buffer":0,
"byteLength":112,
"byteOffset":2272,
"target":34962
}
],
"buffers":[
{
"byteLength":2384,
"uri":"cube_on_plane.bin"
}
]
}

View file

@ -1,5 +1,5 @@
use std::ops::Mul;
use cgmath::{Angle, InnerSpace, Matrix4, SquareMatrix, Vector3, Vector4};
use cgmath::{Angle, InnerSpace, Matrix3, Matrix4, Point3, SquareMatrix, Vector3, Vector4};
use easy_gltf::{Camera, Projection};
pub struct Ray {
@ -15,24 +15,25 @@ pub fn construct_primary_rays(camera: &Camera,
//TODO: ignoring aspect ratio here
let (fovy, aspect_ratio) = match camera.projection {
Projection::Perspective { yfov, aspect_ratio } => {
(yfov, aspect_ratio) }
(yfov, aspect_ratio)
}
Projection::Orthographic { .. } => { panic!("Orthographic rendering not supported.") }
};
//ray origin in world space
let origin_world_space = camera.position();
//dbg!(camera.transform);
//use a custom transform matrix because the one from easy_gltf is fucked
let transform_matrix = Matrix4::from_cols(
camera.right().extend(0.0),
-camera.up().extend(0.0),
-camera.forward().extend(0.0),
origin_world_space.extend(1.0),
);
// the distance from the camera origin to the view plane
let z: f32 = height as f32 / (2.0 * fovy.mul(0.5).tan());
//obtain the inverse transformation Matrix
let inverse_transform = camera.transform.invert().unwrap_or_else(||
panic!("Non invertible transform Matrix. giving up.")
);
dbg!(camera.transform);
//TODO: take ray multiplier per pixel into account here
let mut rays: Vec<Ray> = Vec::with_capacity(height * width);
@ -41,7 +42,7 @@ pub fn construct_primary_rays(camera: &Camera,
rays.push(generate_single_primary_ray(
width,
height,
&inverse_transform,
&transform_matrix,
z,
pixel_x_coord,
pixel_y_coord,
@ -52,7 +53,7 @@ pub fn construct_primary_rays(camera: &Camera,
fn generate_single_primary_ray(image_width: usize,
image_height: usize,
inverse_transform: &Matrix4<f32>,
cam_to_world_transform: &Matrix4<f32>,
focal_length: f32,
u: usize,
v: usize,
@ -63,9 +64,8 @@ fn generate_single_primary_ray(image_width: usize,
v as f32 - (image_height as f32 / 2.0),
focal_length,
0.0);
//TODO: Rotation is fucked
//x rotation has sign wrong, y and z are flipped
let direction_world_space = inverse_transform * direction_view_space.normalize();
let direction_world_space = cam_to_world_transform * direction_view_space.normalize();
Ray { source: ray_origin, direction: direction_world_space.truncate().normalize() }
}