/
main.go
183 lines (166 loc) · 5.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"flag"
"fmt"
"image/png"
"math"
"os"
"runtime"
)
const (
MAX_DIST = 1999999999
PI_180 = 0.017453292
SMALL = 0.000000001
)
func calcShadow(r *Ray, collisionObj int) float64 {
shadow := 1.0 //starts with no shadow
for i, obj := range scene.objectList {
r.interObj = -1
r.interDist = MAX_DIST
if obj.Intersect(r, i) && i != collisionObj {
shadow *= scene.materialList[obj.Material()].transmitCol
}
}
return shadow
}
func trace(r *Ray, depth int) (c Color) {
for i, obj := range scene.objectList {
obj.Intersect(r, i)
}
if r.interObj >= 0 {
matIndex := scene.objectList[r.interObj].Material()
interPoint := r.origin.Add(r.direction.Mul(r.interDist))
incidentV := interPoint.Sub(r.origin)
originBackV := r.direction.Mul(-1.0)
originBackV = originBackV.Normalize()
vNormal := scene.objectList[r.interObj].getNormal(interPoint)
for _, light := range scene.lightList {
switch light.kind {
case "ambient":
c = c.Add(light.color)
case "point":
lightDir := light.position.Sub(interPoint)
lightDir = lightDir.Normalize()
lightRay := Ray{interPoint, lightDir, MAX_DIST, -1}
shadow := calcShadow(&lightRay, r.interObj)
NL := vNormal.Dot(lightDir)
if NL > 0.0 {
if scene.materialList[matIndex].difuseCol > 0.0 { // ------- Difuso
difuseColor := light.color.Mul(scene.materialList[matIndex].difuseCol).Mul(NL)
difuseColor.r *= scene.materialList[matIndex].color.r * shadow
difuseColor.g *= scene.materialList[matIndex].color.g * shadow
difuseColor.b *= scene.materialList[matIndex].color.b * shadow
c = c.Add(difuseColor)
}
if scene.materialList[matIndex].specularCol > 0.0 { // ----- Especular
R := (vNormal.Mul(2).Mul(NL)).Sub(lightDir)
spec := originBackV.Dot(R)
if spec > 0.0 {
spec = scene.materialList[matIndex].specularCol * math.Pow(spec, scene.materialList[matIndex].specularD)
specularColor := light.color.Mul(spec).Mul(shadow)
c = c.Add(specularColor)
}
}
}
}
}
if depth < scene.traceDepth {
if scene.materialList[matIndex].reflectionCol > 0.0 { // -------- Reflexion
T := originBackV.Dot(vNormal)
if T > 0.0 {
vDirRef := (vNormal.Mul(2).Mul(T)).Sub(originBackV)
vOffsetInter := interPoint.Add(vDirRef.Mul(SMALL))
rayoRef := Ray{vOffsetInter, vDirRef, MAX_DIST, -1}
c = c.Add(trace(&rayoRef, depth+1.0).Mul(scene.materialList[matIndex].reflectionCol))
}
}
if scene.materialList[matIndex].transmitCol > 0.0 { // ---- Refraccion
RN := vNormal.Dot(incidentV.Mul(-1.0))
incidentV = incidentV.Normalize()
var n1, n2 float64
if vNormal.Dot(incidentV) > 0.0 {
vNormal = vNormal.Mul(-1.0)
RN = -RN
n1 = scene.materialList[matIndex].IOR
n2 = 1.0
} else {
n2 = scene.materialList[matIndex].IOR
n1 = 1.0
}
if n1 != 0.0 && n2 != 0.0 {
par_sqrt := math.Sqrt(1 - (n1*n1/n2*n2)*(1-RN*RN))
refactDirV := incidentV.Add(vNormal.Mul(RN).Mul(n1 / n2)).Sub(vNormal.Mul(par_sqrt))
vOffsetInter := interPoint.Add(refactDirV.Mul(SMALL))
refractRay := Ray{vOffsetInter, refactDirV, MAX_DIST, -1}
c = c.Add(trace(&refractRay, depth+1.0).Mul(scene.materialList[matIndex].transmitCol))
}
}
}
}
return c
}
func renderPixel(line chan int, done chan bool) {
for y := range line { // 1: 1, 5: 2, 8: 3,
for x := 0; x < scene.imgWidth; x++ {
var c Color
yo := y * scene.oversampling
xo := x * scene.oversampling
for i := 0; i < scene.oversampling; i++ {
for j := 0; j < scene.oversampling; j++ {
var dir Vector
dir.x = float64(xo)*scene.Vhor.x + float64(yo)*scene.Vver.x + scene.Vp.x
dir.y = float64(xo)*scene.Vhor.y + float64(yo)*scene.Vver.y + scene.Vp.y
dir.z = float64(xo)*scene.Vhor.z + float64(yo)*scene.Vver.z + scene.Vp.z
dir = dir.Normalize()
r := Ray{scene.cameraPos, dir, MAX_DIST, -1}
c = c.Add(trace(&r, 1.0))
yo += 1
}
xo += 1
}
srq_oversampling := float64(scene.oversampling * scene.oversampling)
c.r /= srq_oversampling
c.g /= srq_oversampling
c.b /= srq_oversampling
scene.image.SetRGBA(x, y, c.ToPixel())
//fmt.Println("check")
}
if y%10 == 0 {
fmt.Printf("%d ", y)
}
}
done <- true
}
var scene *Scene
func main() {
var sceneFilename string
var numWorkers int
flag.StringVar(&sceneFilename, "file", "samples/scene.txt", "Scene file to render.")
flag.IntVar(&numWorkers, "workers", runtime.NumCPU(), "Number of worker threads.")
flag.Parse()
scene = NewScene(sceneFilename)
done := make(chan bool, numWorkers)
line := make(chan int)
// launch the workers
for i := 0; i < numWorkers; i++ {
go renderPixel(line, done)
}
fmt.Println("Rendering: ", sceneFilename)
fmt.Printf("Line (from %d to %d): ", scene.startline, scene.endline)
for y := scene.startline; y < scene.endline; y++ {
line <- y
}
close(line)
// wait for all workers to finish
for i := 0; i < numWorkers; i++ {
<-done
}
output, err := os.Create(sceneFilename[0:len(sceneFilename)-4] + ".png")
if err != nil {
panic(err)
}
if err = png.Encode(output, scene.image); err != nil {
panic(err)
}
fmt.Println(" DONE!")
}