Skip to content

Commit

Permalink
Merge pull request #59 from quephird/fix_antialiasing
Browse files Browse the repository at this point in the history
Fix antialiasing
  • Loading branch information
quephird committed Nov 29, 2023
2 parents 0f6717a + 72dcd96 commit 724e6a4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
45 changes: 22 additions & 23 deletions README.md
Expand Up @@ -831,37 +831,36 @@ If you've done all that, you now have a bona fide application and should be able
You can also optionally render a scene with antialiasing. In the image above, you can see that the various edges of the object are pretty jagged and take away from the verisimilitude of the image. By adding a property modifier to the `World` object, `.antialiasing(true)`, we can improve its quality:

```
import Darwin
import ScintillaLib

@available(macOS 12.0, *)
@main
struct CSGExample: ScintillaApp {
struct Cavatappi: ScintillaApp {
var world = World {
Camera(width: 400,
height: 400,
viewAngle: PI/3,
from: Point(0, 1.5, -2),
to: Point(0, 0, 0),
up: Vector(0, 1, 0))
from: Point(0, 7, -15),
to: Point(0, 7, 0),
up: Vector(0, 1, 0),
antialiasing: true)
PointLight(position: Point(-10, 10, -10))
Sphere()
.material(.solidColor(0, 0, 1))
.intersection {
Cube()
.material(.solidColor(1, 0, 0))
.scale(0.8, 0.8, 0.8)
}
.difference {
for (thetaX, thetaZ) in [(0, 0), (0, PI/2), (PI/2, 0)] {
Cylinder()
.material(.solidColor(0, 1, 0))
.scale(0.5, 0.5, 0.5)
.rotateX(thetaX)
.rotateZ(thetaZ)
}
}
.rotateY(PI/6)
PointLight(position: Point(10, 10, -10))
ParametricSurface(bottomFrontLeft: (-3.5, 0, -3.5),
topBackRight: (3.5, 15.0, 3.5),
uRange: (0, 2*PI),
vRange: (0, 7*PI),
accuracy: 0.001,
maxGradient: 1.0,
fx: { (u, v) in (2 + cos(u) + 0.1*cos(8*u))*cos(v) },
fy: { (u, v) in 2 + sin(u) + 0.1*sin(8*u) + 0.5*v },
fz: { (u, v) in (2 + cos(u) + 0.1*cos(8*u))*sin(v) })
.material(.solidColor(1.0, 0.8, 0))
Plane()
.material(.solidColor(1, 1, 1))
.translate(0, -3.0, 0)
}
.antialiasing(true)
}
```

Expand All @@ -871,7 +870,7 @@ struct CSGExample: ScintillaApp {

You should be able to see that it is far less "jaggy" than the orignal image shown further up in this README.

Because rendering times are much slower with antialiasing turned out, you should make sure that the run configuration is set to Release in order to run Swift in the fastest fashion. To get there, go to Product -> Scheme -> Edit Scheme...
Because rendering times are much slower with antialiasing turned on, you should make sure that the run configuration is set to Release in order to run Swift in the fastest fashion. To get there, go to Product -> Scheme -> Edit Scheme...

![](./images/SchemeSettings.png)

Expand Down
11 changes: 8 additions & 3 deletions Sources/ScintillaLib/Camera.swift
Expand Up @@ -16,24 +16,28 @@ public struct Camera {
var halfWidth: Double
var halfHeight: Double
@_spi(Testing) public var pixelSize: Double
var antialiasing: Bool

public init(width horizontalSize: Int,
height verticalSize: Int,
viewAngle fieldOfView: Double,
from: Point,
to: Point,
up: Vector) {
up: Vector,
antialiasing: Bool = false) {
let viewTransform = Matrix4.view(from, to, up)
self.init(width: horizontalSize,
height: verticalSize,
viewAngle: fieldOfView,
viewTransform: viewTransform)
viewTransform: viewTransform,
antialiasing: antialiasing)
}

public init(width horizontalSize: Int,
height verticalSize: Int,
viewAngle fieldOfView: Double,
viewTransform: Matrix4) {
viewTransform: Matrix4,
antialiasing: Bool = false) {
self.horizontalSize = horizontalSize
self.verticalSize = verticalSize
self.fieldOfView = fieldOfView
Expand All @@ -56,5 +60,6 @@ public struct Camera {
self.halfWidth = halfWidth
self.halfHeight = halfHeight
self.pixelSize = pixelSize
self.antialiasing = antialiasing
}
}
8 changes: 1 addition & 7 deletions Sources/ScintillaLib/World.swift
Expand Up @@ -14,7 +14,6 @@ public actor World {
@_spi(Testing) public var camera: Camera
@_spi(Testing) public var lights: [Light]
@_spi(Testing) public var shapes: [Shape]
var antialiasing: Bool = false

var totalPixels: Int

Expand Down Expand Up @@ -68,11 +67,6 @@ public actor World {
self.totalPixels = camera.horizontalSize * camera.verticalSize
}

public func antialiasing(_ antialiasing: Bool) -> Self {
self.antialiasing = antialiasing
return self
}

@_spi(Testing) public func intersect(_ ray: Ray) -> [Intersection] {
var intersections = self.shapes.flatMap({shape in shape.intersect(ray)})
intersections
Expand Down Expand Up @@ -278,7 +272,7 @@ public actor World {
for x in 0..<self.camera.horizontalSize {
let color: Color

if self.antialiasing {
if self.camera.antialiasing {
let subpixelSamplesX = 4
let subpixelSamplesY = 4

Expand Down
Binary file modified images/Antialiasing.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 724e6a4

Please sign in to comment.