-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Render to an intermediate canvas * Make the "world" threadsafe * Parallelize via Weave * Properly free the canvas
- Loading branch information
Showing
8 changed files
with
146 additions
and
46 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Trace of Radiance | ||
# Copyright (c) 2020 Mamy André-Ratsimbazafy | ||
# Licensed and distributed under either of | ||
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). | ||
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). | ||
# at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
|
||
import | ||
# Low-level | ||
system/ansi_c, | ||
# Stdlib | ||
std/math, | ||
# Internal | ||
./colors | ||
|
||
# Canvas | ||
# ------------------------------------------------------------------------ | ||
# A Canvas is a 2D Buffer | ||
|
||
type | ||
Canvas* = object | ||
## 2D Buffer | ||
## Images are stored in row-major order | ||
# Size 24 bytes | ||
pixels*: ptr UncheckedArray[Color] | ||
nrows*, ncols*: int32 | ||
samples_per_pixel*: int32 | ||
gamma_correction*: float32 | ||
|
||
|
||
proc newCanvas*( | ||
height, width, | ||
samples_per_pixel: SomeInteger, | ||
gamma_correction: SomeFloat): Canvas = | ||
result.nrows = int32 height | ||
result.ncols = int32 width | ||
result.samples_per_pixel = int32 samples_per_pixel | ||
result.pixels = cast[ptr UncheckedArray[Color]]( | ||
c_malloc(csize_t height * width * sizeof(Color)) | ||
) | ||
result.gamma_correction = float32(gamma_correction) | ||
|
||
proc delete*(canvas: var Canvas) {.inline.} = | ||
if not canvas.pixels.isNil: | ||
c_free(canvas.pixels) | ||
|
||
func draw*(canvas: var Canvas, row, col: SomeInteger, pixel: Color) {.inline.} = | ||
# Draw a gamma-corrected pixel in the canvas | ||
let scale = 1.0 / canvas.samples_per_pixel.float64 | ||
let gamma = 1.0 / canvas.gamma_correction.float64 | ||
let pos = row*canvas.ncols + col | ||
canvas.pixels[pos].x = pow(scale * pixel.x, gamma) | ||
canvas.pixels[pos].y = pow(scale * pixel.y, gamma) | ||
canvas.pixels[pos].z = pow(scale * pixel.z, gamma) | ||
|
||
proc `[]`*(canvas: Canvas, row, col: SomeInteger): Color {.inline.} = | ||
canvas.pixels[row*canvas.ncols + col] |
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
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,9 @@ | ||
# Package | ||
|
||
version = "0.1.9" | ||
author = "Mamy André-Ratsimbazafy" | ||
description = "An educational raytracer" | ||
license = "MIT or Apache License 2.0" | ||
|
||
# Dependencies | ||
requires "nim >= 1.2.0", "weave" |