Skip to content

Commit

Permalink
Create new Vec3 package
Browse files Browse the repository at this point in the history
  • Loading branch information
MakisChristou committed May 6, 2024
1 parent 67dfc53 commit 42f2a30
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 47 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ all: build

# Build the project
build:
$(GO_BUILD) -o $(DIST_DIR)/$(BINARY_NAME) $(SRC_DIR)/*
$(GO_BUILD) -o $(DIST_DIR)/$(BINARY_NAME) $(SRC_DIR)/*.go

# Clean up the binaries
clean:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ make build

## Run
```bash
$ rtxon --width 1920 --samples 500 --depth 100 > file.ppm
$ rtxon --width 1920 --samples 500 --depth 100 --scene 1 > file.ppm
```

## Cli arguments
Expand Down
2 changes: 2 additions & 0 deletions src/aabb.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type Aabb struct {
X, Y, Z Interval
}
Expand Down
14 changes: 8 additions & 6 deletions src/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"math"

. "github.com/makischristou/rtxon/src/vec3"

"github.com/schollz/progressbar/v3"
)

Expand Down Expand Up @@ -45,19 +47,19 @@ func (c Camera) Render(world HittableList) {
}

pixelColor = pixelColor.MultiplyScalar(c.pixelSampleScale)
writeColor(Color(pixelColor))
WriteColor(Color(pixelColor))
}

bar.Add(1)
}
}

func SampleSquare() Vec3 {
return Vec3{randomDouble() - 0.5, randomDouble() - 0.5, 0.0}
return Vec3{RandomDouble() - 0.5, RandomDouble() - 0.5, 0.0}
}

func (c Camera) defocusDiskSample() Vec3 {
p := randomInUnitDisk()
p := RandomInUnitDisk()
return c.center.Add(c.defocusDiskU.MultiplyScalar(p.X)).Add(c.defocusDiskV.MultiplyScalar(p.Y))
}

Expand All @@ -74,7 +76,7 @@ func (c Camera) getRay(i int, j int) Ray {
}

rayDirection := pixelSample.Subtract(rayOrigin)
rayTime := randomDouble()
rayTime := RandomDouble()

return Ray{rayOrigin, rayDirection, rayTime}
}
Expand All @@ -91,7 +93,7 @@ func NewCamera(imageWidth int, aspectRatio, defocusAngle, focusDist, vFov, sampl
pixelSampleScale := 1.0 / float64(samplesPerPixel)

// focalLength := lookFrom.Subtract(lookAt).Length()
theta := degreesToRadians(vFov)
theta := DegreesToRadians(vFov)
h := math.Tan(theta / 2)
viewportHeight := 2.0 * h * focusDist
viewportWidth := viewportHeight * (float64(imageWidth) / float64(imageHeight))
Expand All @@ -113,7 +115,7 @@ func NewCamera(imageWidth int, aspectRatio, defocusAngle, focusDist, vFov, sampl
viewportUpperLeft := center.Subtract(w.MultiplyScalar(focusDist)).Subtract(viewportU.DivideScalar(2)).Subtract(viewportV.DivideScalar(2))
pixel00Loc := viewportUpperLeft.Add(pixelDeltaU.Add(pixelDeltaV).MultiplyScalar(0.5))

defocusRadius := focusDist * math.Tan(degreesToRadians(defocusAngle/2))
defocusRadius := focusDist * math.Tan(DegreesToRadians(defocusAngle/2))
defocusDiskU := u.MultiplyScalar(defocusRadius)
defocusDiskV := v.MultiplyScalar(defocusRadius)

Expand Down
6 changes: 5 additions & 1 deletion src/checker_texture.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "math"
import (
"math"

. "github.com/makischristou/rtxon/src/vec3"
)

type CheckerTexture struct {
invScale float64
Expand Down
28 changes: 16 additions & 12 deletions src/dielectric.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package main

import "math"
import (
"math"

. "github.com/makischristou/rtxon/src/vec3"
)

type Dielectric struct {
refractionIndex float64
RefractionIndex float64
}

func NewDielectric(refractionIndex float64) Dielectric {
return Dielectric{refractionIndex}
func NewDielectric(RefractionIndex float64) Dielectric {
return Dielectric{RefractionIndex}
}

func (d Dielectric) Scatter(in Ray, rec *HitRecord, attenuation *Vec3, scattered *Ray) bool {
Expand All @@ -16,9 +20,9 @@ func (d Dielectric) Scatter(in Ray, rec *HitRecord, attenuation *Vec3, scattered

var ri float64
if rec.FrontFace {
ri = 1.0 / d.refractionIndex
ri = 1.0 / d.RefractionIndex
} else {
ri = d.refractionIndex
ri = d.RefractionIndex
}

unitDirection := UnitVector(in.Direction())
Expand All @@ -28,10 +32,10 @@ func (d Dielectric) Scatter(in Ray, rec *HitRecord, attenuation *Vec3, scattered
cannotRefract := (ri * sinTheta) > 1.0
var direction Vec3

if cannotRefract || reflactance(cosTheta, ri) > randomDouble() {
direction = reflect(unitDirection, rec.Normal)
if cannotRefract || reflactance(cosTheta, ri) > RandomDouble() {
direction = Reflect(unitDirection, rec.Normal)
} else {
direction = refract(unitDirection, rec.Normal, ri)
direction = Refract(unitDirection, rec.Normal, ri)
}
*scattered = NewRayTime(rec.P, direction, in.Time())
return true
Expand All @@ -41,9 +45,9 @@ func (d Dielectric) Emmited(u, v float64, p Vec3) Vec3 {
return Vec3{0, 0, 0}
}

func reflactance(cosine float64, refractionIndex float64) float64 {
// Use Schlick's approximation for reflectance.
r0 := (1 - refractionIndex) / (1 + refractionIndex)
func reflactance(cosine float64, RefractionIndex float64) float64 {
// Use Schlick's approximation for Reflectance.
r0 := (1 - RefractionIndex) / (1 + RefractionIndex)
r0 = r0 * r0
return r0 + (1-r0)*math.Pow((1-cosine), 5)
}
2 changes: 2 additions & 0 deletions src/diffuse.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type DiffuseLight struct {
texture Texture
}
Expand Down
2 changes: 2 additions & 0 deletions src/hitable.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type HitRecord struct {
P Vec3
Normal Vec3
Expand Down
2 changes: 2 additions & 0 deletions src/hittable_list.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type HittableList struct {
Objects []Hittable
bbox Aabb
Expand Down
4 changes: 3 additions & 1 deletion src/lambertian.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type Lambertian struct {
texture Texture
}
Expand All @@ -13,7 +15,7 @@ func NewLambertianFromTexture(texture Texture) Lambertian {
}

func (l Lambertian) Scatter(in Ray, rec *HitRecord, attenuation *Vec3, scattered *Ray) bool {
scatterDirection := rec.Normal.Add(randomUnitVector())
scatterDirection := rec.Normal.Add(RandomUnitVector())

// Catch degenerate scatter direction
if scatterDirection.NearZero() {
Expand Down
7 changes: 4 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
. "github.com/makischristou/rtxon/src/vec3"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -34,8 +35,8 @@ func generateMainScene(imageWidth int, samplesPerPixel, maxDepth float64) (Hitta

for a := -11; a < 11; a++ {
for b := -11; b < 11; b++ {
chooseMat := randomDouble()
center := Vec3{float64(a) + 0.9*randomDouble(), 0.2, float64(b) + 0.9*randomDouble()}
chooseMat := RandomDouble()
center := Vec3{float64(a) + 0.9*RandomDouble(), 0.2, float64(b) + 0.9*RandomDouble()}

if center.Subtract(Vec3{4, 0.2, 0}).Length() > 0.9 {
var sphereMaterial Material
Expand All @@ -45,7 +46,7 @@ func generateMainScene(imageWidth int, samplesPerPixel, maxDepth float64) (Hitta
sphereMaterial = NewLambertian(albedo)
} else if chooseMat < 0.95 {
albedo := RandomVec3Range(0.5, 1)
fuzz := randomDoubleRange(0, 0.5)
fuzz := RandomDoubleRange(0, 0.5)
sphereMaterial = NewMetal(albedo, fuzz)
} else {
sphereMaterial = NewDielectric(1.5)
Expand Down
2 changes: 2 additions & 0 deletions src/material.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type Material interface {
Scatter(in Ray, rec *HitRecord, attenuation *Vec3, scattered *Ray) bool
Emmited(u, v float64, p Vec3) Vec3
Expand Down
8 changes: 5 additions & 3 deletions src/metal.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type Metal struct {
albedo Vec3
fuzz float64
Expand All @@ -10,9 +12,9 @@ func NewMetal(albedo Vec3, fuzz float64) Metal {
}

func (m Metal) Scatter(in Ray, rec *HitRecord, attenuation *Vec3, scattered *Ray) bool {
reflected := reflect(in.Direction(), rec.Normal)
reflected = UnitVector(reflected).Add(randomUnitVector().MultiplyScalar(m.fuzz))
*scattered = NewRayTime(rec.P, reflected, in.Time())
Reflected := Reflect(in.Direction(), rec.Normal)
Reflected = UnitVector(Reflected).Add(RandomUnitVector().MultiplyScalar(m.fuzz))
*scattered = NewRayTime(rec.P, Reflected, in.Time())
*attenuation = m.albedo
return true
}
Expand Down
6 changes: 5 additions & 1 deletion src/quad.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "math"
import (
"math"

. "github.com/makischristou/rtxon/src/vec3"
)

type Quad struct {
q Vec3
Expand Down
2 changes: 2 additions & 0 deletions src/ray.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type Ray struct {
origin Vec3
direction Vec3
Expand Down
2 changes: 2 additions & 0 deletions src/solid_color.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type SolidColor struct {
albedo Vec3
}
Expand Down
6 changes: 5 additions & 1 deletion src/sphere.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "math"
import (
"math"

. "github.com/makischristou/rtxon/src/vec3"
)

type Sphere struct {
center1 Vec3
Expand Down
2 changes: 2 additions & 0 deletions src/texture.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import . "github.com/makischristou/rtxon/src/vec3"

type Texture interface {
Value(u float64, v float64, p Vec3) Vec3
}
2 changes: 1 addition & 1 deletion src/interval.go → src/vec3/interval.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package vec3

import "math"

Expand Down
12 changes: 6 additions & 6 deletions src/utils.go → src/vec3/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package vec3

import (
"fmt"
Expand All @@ -15,7 +15,7 @@ func linearToGamma(linearComponent float64) float64 {
return 0
}

func writeColor(pixelColor Color) {
func WriteColor(pixelColor Color) {
r := pixelColor.X
g := pixelColor.Y
b := pixelColor.Z
Expand All @@ -32,18 +32,18 @@ func writeColor(pixelColor Color) {
fmt.Print(rByte, " ", gByte, " ", bByte, "\n")
}

func degreesToRadians(degrees float64) float64 {
func DegreesToRadians(degrees float64) float64 {
return degrees * math.Pi / 180
}

func randomDouble() float64 {
func RandomDouble() float64 {
// Returns a random real in [0,1).
return rand.Float64()
}

func randomDoubleRange(min, max float64) float64 {
func RandomDoubleRange(min, max float64) float64 {
// Returns a random real in [min,max).
return min + (max-min)*randomDouble()
return min + (max-min)*RandomDouble()
}

var Infinity = math.Inf(1)
Loading

0 comments on commit 42f2a30

Please sign in to comment.