-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Miou-zora/transform-rotation
Add rotation
- Loading branch information
Showing
4 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const Ray = @import("Ray.zig").Ray; | ||
const HitRecord = @import("HitRecord.zig").HitRecord; | ||
const Vec3 = @import("Vec3.zig").Vec3; | ||
const Plane = @import("Plane.zig").Plane; | ||
|
||
pub const Rotation = struct { | ||
const Self = @This(); | ||
|
||
x: f32, | ||
y: f32, | ||
z: f32, | ||
|
||
pub fn ray_global_to_object(self: *const Self, ray: Ray, object: Plane) Ray { | ||
var ray_in_object_space = Ray{ | ||
.direction = ray.direction, | ||
.origin = ray.origin.subVec3(object.origin), | ||
}; | ||
ray_in_object_space.direction.rotateX(self.x); | ||
ray_in_object_space.direction.rotateY(self.y); | ||
ray_in_object_space.direction.rotateZ(self.z); | ||
ray_in_object_space.origin.rotateX(self.x); | ||
ray_in_object_space.origin.rotateY(self.y); | ||
ray_in_object_space.origin.rotateZ(self.z); | ||
return Ray{ | ||
.direction = ray_in_object_space.direction, | ||
.origin = ray_in_object_space.origin.addVec3(object.origin), | ||
}; | ||
} | ||
|
||
pub fn hitRecord_object_to_global(self: *const Self, hitrecord: HitRecord, object: Plane) HitRecord { | ||
var hitrecord_in_object_space = HitRecord{ | ||
.intersection_point = hitrecord.intersection_point.subVec3(object.origin), | ||
.normal = hitrecord.normal, | ||
.hit = hitrecord.hit, | ||
}; | ||
hitrecord_in_object_space.normal.rotateZ(-self.z); | ||
hitrecord_in_object_space.normal.rotateY(-self.y); | ||
hitrecord_in_object_space.normal.rotateX(-self.x); | ||
hitrecord_in_object_space.intersection_point.rotateZ(-self.z); | ||
hitrecord_in_object_space.intersection_point.rotateY(-self.y); | ||
hitrecord_in_object_space.intersection_point.rotateX(-self.x); | ||
return HitRecord{ | ||
.intersection_point = hitrecord_in_object_space.intersection_point.addVec3(object.origin), | ||
.normal = hitrecord_in_object_space.normal, | ||
.hit = hitrecord_in_object_space.hit, | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
const Translation = @import("Translation.zig").Translation; | ||
const Rotation = @import("Rotation.zig").Rotation; | ||
const Ray = @import("Ray.zig").Ray; | ||
const HitRecord = @import("HitRecord.zig").HitRecord; | ||
const Plane = @import("Plane.zig").Plane; | ||
|
||
pub const Transformation = union(enum) { | ||
translation: Translation, | ||
rotation: Rotation, | ||
}; | ||
|
||
pub fn ray_global_to_object(ray: Ray, transformation: Transformation) Ray { | ||
pub fn ray_global_to_object(ray: Ray, transformation: Transformation, object: Plane) Ray { | ||
switch (transformation) { | ||
Transformation.translation => |value| { | ||
return value.ray_global_to_object(ray); | ||
}, | ||
Transformation.rotation => |value| { | ||
return value.ray_global_to_object(ray, object); | ||
}, | ||
} | ||
} | ||
|
||
pub fn hitRecord_object_to_global(ray: HitRecord, transformation: Transformation) HitRecord { | ||
pub fn hitRecord_object_to_global(ray: HitRecord, transformation: Transformation, object: Plane) HitRecord { | ||
switch (transformation) { | ||
Transformation.translation => |value| { | ||
return value.hitRecord_object_to_global(ray); | ||
}, | ||
Transformation.rotation => |value| { | ||
return value.hitRecord_object_to_global(ray, object); | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters