-
Notifications
You must be signed in to change notification settings - Fork 0
/
oldstuff.txt
295 lines (285 loc) · 9.42 KB
/
oldstuff.txt
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
//func groups() {
// w := mat.NewWorld()
// w.Light = append(w.Light, mat.NewLight(mat.NewPoint(-3, 2.5, -3), mat.NewColor(1, 1, 1)))
//
// camera := mat.NewCamera(640, 480, math.Pi/3)
// //camera := mat.NewCamera(320, 240, math.Pi/3)
// viewTransform := mat.ViewTransform(mat.NewPoint(-1.3, 2, -5), mat.NewPoint(0, 0.5, 0), mat.NewVector(0, 1, 0))
// camera.Transform = viewTransform
// camera.Inverse = mat.Inverse(viewTransform)
//
// gr := mat.NewGroup()
//
// s1 := mat.NewSphere()
// //s1.SetTransform(mat.Multiply(mat.Translate(-2, 0.25, -1), mat.Scale(1.25, 0.25, 0.25)))
// s1.SetTransform(mat.Translate(-2, -1, 0))
// mat1 := mat.NewMaterialWithReflectivity(mat.NewColor(1, 0.1, 0.1), 0.1, 0.5, 0.8, 220.0, 0.4)
// s1.SetMaterial(mat1)
// gr.AddChild(s1)
//
// s4 := mat.NewSphere()
// //s4.SetTransform(mat.Multiply(mat.Translate(1, 0.25, -1), mat.Scale(0.25, 0.25, 0.25)))
// s4.SetTransform(mat.Translate(0, 0, 0))
// mat4 := mat.NewMaterialWithReflectivity(mat.NewColor(0.1, 0.1, 1.0), 0.1, 0.5, 0.8, 220.0, 0.4)
// s4.SetMaterial(mat4)
// gr.AddChild(s4)
//
// w.Objects = append(w.Objects, gr)
//
// canvas := mat.Render(camera, w)
// mat.RenderReferenceAxises(canvas, camera)
//
// // write
// data := canvas.ToPPM()
// err := ioutil.WriteFile("world-group.ppm", []byte(data), os.FileMode(0755))
// if err != nil {
// fmt.Println(err.Error())
// }
//}
//
//func renderworld() {
// w := mat.NewWorld()
// w.Light = append(w.Light, mat.NewLight(mat.NewPoint(-10, 1, -10), mat.NewColor(1, 1, 1)))
// w.Light = append(w.Light, mat.NewLight(mat.NewPoint(1, 13, 1), mat.NewColor(0.5, 0.5, 0.5)))
//
// camera := mat.NewCamera(480, 320, math.Pi/3)
// viewTransform := mat.ViewTransform(mat.NewPoint(0, 1.5, -5), mat.NewPoint(0, 1, 0), mat.NewVector(0, 1, 0))
// camera.Transform = viewTransform
//
// // Create floor
// floor := mat.NewSphere()
// floor.Transform = mat.Scale(10, 0.01, 10)
// floor.Material = mat.NewDefaultMaterial()
// floor.Material.Color = mat.NewColor(1, 0.9, 0.9)
// floor.Material.Specular = 0.0
// floor.Material.Reflectivity = 0.2
// w.Objects = append(w.Objects, floor)
//
// // create left wall
// leftWall := mat.NewSphere()
//
// scaleM := mat.Scale(10, 0.01, 10)
// rotXM := mat.RotateX(math.Pi / 2)
// rotYM := mat.RotateY(-math.Pi / 4)
// transM := mat.Translate(0, 0, 5)
//
// m1 := mat.Multiply(transM, rotYM)
// m2 := mat.Multiply(m1, rotXM)
// m3 := mat.Multiply(m2, scaleM)
// leftWall.Transform = m3
// leftWall.Material = floor.Material
// w.Objects = append(w.Objects, leftWall)
//
// // create right wall
// rightWall := mat.NewSphere()
//
// scaleM = mat.Scale(10, 0.01, 10)
// rotXM = mat.RotateX(math.Pi / 2)
// rotYM = mat.RotateY(math.Pi / 4)
// transM = mat.Translate(0, 0, 5)
//
// m1 = mat.Multiply(transM, rotYM)
// m2 = mat.Multiply(m1, rotXM)
// m3 = mat.Multiply(m2, scaleM)
// rightWall.Transform = m3
// rightWall.Material = floor.Material
// w.Objects = append(w.Objects, rightWall)
//
// // middle sphere
// middle := mat.NewSphere()
// middle.Transform = mat.Translate(-0.5, 1, 0.5)
// middle.Material = mat.NewDefaultMaterial()
// middle.Material.Color = mat.NewColor(0.1, 1, 0.5)
// middle.Material.Diffuse = 0.7
// middle.Material.Specular = 0.3
// w.Objects = append(w.Objects, middle)
//
// // right sphere
// right := mat.NewSphere()
// right.Transform = mat.Multiply(mat.Translate(1.5, 0.5, -0.5), mat.Scale(0.5, 0.5, 0.5))
// right.Material = mat.NewDefaultMaterial()
// right.Material.Color = mat.NewColor(0.5, 1, 0.1)
// right.Material.Diffuse = 0.7
// right.Material.Specular = 0.3
// w.Objects = append(w.Objects, right)
//
// // left sphere
// left := mat.NewSphere()
// left.Transform = mat.Multiply(mat.Translate(-1.5, 0.33, -0.75), mat.Scale(0.33, 0.33, 0.33))
// left.Material = mat.NewDefaultMaterial()
// left.Material.Color = mat.NewColor(1, 0.8, 0.1)
// left.Material.Diffuse = 0.7
// left.Material.Specular = 0.3
// w.Objects = append(w.Objects, left)
//
// // cube
// cube := mat.NewCube()
// cube.Transform = mat.Multiply(mat.Translate(-.6, 0.25, -1.5), mat.Scale(0.25, 0.25, 0.25))
// cube.Material = mat.NewDefaultMaterial()
// cube.Material.Color = mat.NewColor(1, 0.6, 0.2)
// cube.Material.Transparency = 0.0
// cube.Material.Diffuse = 0.7
// cube.Material.Specular = 0.3
// cube.Material.Reflectivity = 0.0
// w.Objects = append(w.Objects, cube)
//
// canvas := mat.RenderThreaded(camera, w)
// // write
// data := canvas.ToPPM()
// err := ioutil.WriteFile("world1.ppm", []byte(data), os.FileMode(0755))
// if err != nil {
// fmt.Println(err.Error())
// }
//}
//
//func shadedSphereDemo() {
// c := mat.NewCanvas(512, 512)
//
// // this is our eye starting 15 units "in front" of origo.
// rayOrigin := mat.NewPoint(0, 0, -15.0)
//
// // Note!! If I understand this correctly, the "wall" in this case is actually an abstraction
// // of the "far" wall of the view frustum, giving something to cast our ray against, forming a vector between
// // the eye and a point in world space.
// wallZ := 20.0
// wallSize := 7.0
// pixelSize := wallSize / float64(c.W)
// half := wallSize / 2
// sphere := mat.NewSphere()
// //mat.SetTransform(&sphere, mat.Translate(1, 1, 1))
// material := mat.NewDefaultMaterial()
// material.Color = mat.NewColor(1, 0.2, 1)
// sphere.SetMaterial(material)
//
// lightPos := mat.NewPoint(-10, 10, -10)
// lightColor := mat.NewColor(1, 1, 1)
// light := mat.NewLight(lightPos, lightColor)
//
// for row := 0; row < c.W; row++ {
// worldY := half - pixelSize*float64(row)
//
// for col := 0; col < c.H; col++ {
// worldX := -half + pixelSize*float64(col)
// posOnWall := mat.NewPoint(worldX, worldY, wallZ)
//
// // Build a ray (origin + direction)
// rayFromOriginToPosOnWall := mat.NewRay(rayOrigin, mat.Normalize(mat.Sub(posOnWall, rayOrigin)))
//
// // check if our ray intersects the sphere
// intersections := mat.IntersectRayWithShape(sphere, rayFromOriginToPosOnWall)
// intersection, found := mat.Hit(intersections)
//
// if found {
// pointOfHit := mat.Position(rayFromOriginToPosOnWall, intersection.T)
// normalAtHit := mat.NormalAt(sphere, pointOfHit, nil)
// minusEyeRayVector := mat.Negate(rayFromOriginToPosOnWall.Direction)
// color := mat.Lighting(sphere.Material, sphere, light, pointOfHit, minusEyeRayVector, normalAtHit, false)
//
// c.WritePixel(col, c.H-row, color)
// }
// }
// }
// // write
// data := c.ToPPM()
// err := ioutil.WriteFile("shadedcircle.ppm", []byte(data), os.FileMode(0755))
// if err != nil {
// fmt.Println(err.Error())
// }
//}
//
//func circleDemo() {
// c := mat.NewCanvas(100, 100)
//
// rayOrigin := mat.NewPoint(0, 0, -15.0)
// wallZ := 20.0
// wallSize := 7.0
// pixelSize := wallSize / float64(c.W)
// half := wallSize / 2
// color := mat.NewColor(1, 0, 0)
// sphere := mat.NewSphere()
//
// //mat.SetTransform(sphere, mat.Scale(1, 0.5, 1))
// //mat.SetTransform(sphere, mat.Multiply(mat.RotateZ(math.Pi/4), mat.Scale(0.5, 1, 1)))
//
// for row := 0; row < c.W; row++ {
// worldY := half - pixelSize*float64(row)
//
// for col := 0; col < c.H; col++ {
// worldX := -half + pixelSize*float64(col)
// posOnWall := mat.NewPoint(worldX, worldY, wallZ)
//
// rayFromOriginToPosOnWall := mat.NewRay(rayOrigin, mat.Normalize(mat.Sub(posOnWall, rayOrigin)))
//
// // check if our ray intersects the sphere
// intersections := mat.IntersectRayWithShape(sphere, rayFromOriginToPosOnWall)
// _, found := mat.Hit(intersections)
// if found {
// c.WritePixel(col, c.H-row, color)
// }
// }
// }
// // write
// data := c.ToPPM()
// err := ioutil.WriteFile("circle.ppm", []byte(data), os.FileMode(0755))
// if err != nil {
// fmt.Println(err.Error())
// }
//}
func clockDemo() {
c := mat.NewCanvas(80, 80)
center := (c.W/2 + c.H/2) / 2
white := mat.NewColor(1, 1, 1)
point := mat.NewPoint(0, 1, 0)
for i := 0; i < 12; i++ {
rotation := float64(i) * (2 * math.Pi) / 12
rotMat := mat.RotateZ(rotation)
p2 := mat.MultiplyByTuple(rotMat, point)
p2 = mat.MultiplyByScalar(p2, 30.0)
c.WritePixel(center+int(p2.Get(0)), center-int(p2.Get(1)), white)
}
// write
data := c.ToPPM()
err := ioutil.WriteFile("clock.ppm", []byte(data), os.FileMode(0755))
if err != nil {
fmt.Println(err.Error())
}
}
func projectileDemo() {
prj := NewProjectile(mat.NewPoint(0, 1, 0), mat.MultiplyByScalar(mat.Normalize(mat.NewVector(1, 1.8, 0)), 11.25))
env := NewEnvironment(mat.NewVector(0, -0.1, 0), mat.NewVector(-0.01, 0, 0))
c := mat.NewCanvas(900, 550)
red := mat.NewColor(1, 1, 1)
for prj.pos.Get(1) > 0.0 {
tick(prj, env)
//time.Sleep(time.Millisecond * 100)
fmt.Printf("Projectile pos %v at height %v with velocity %v\n", mat.Magnitude(prj.pos), prj.pos.Get(1), prj.velocity)
fmt.Printf("Drawing at: %d %d\n", int(prj.pos.Get(0)), c.H-int(prj.pos.Get(1)))
c.WritePixel(int(prj.pos.Get(0)), c.H-int(prj.pos.Get(1)), red)
}
fmt.Printf("Projectile flew %v\n", mat.Magnitude(prj.pos))
data := c.ToPPM()
err := ioutil.WriteFile("pic.ppm", []byte(data), os.FileMode(0755))
if err != nil {
fmt.Println(err.Error())
}
}
func tick(prj *Projectile, env *Environment) {
prj.pos = mat.Add(prj.pos, prj.velocity)
prj.velocity = mat.Add(prj.velocity, env.gravity)
prj.velocity = mat.Add(prj.velocity, env.wind)
}
type Environment struct {
gravity mat.Tuple4
wind mat.Tuple4
}
func NewEnvironment(gravity mat.Tuple4, wind mat.Tuple4) *Environment {
return &Environment{gravity: gravity, wind: wind}
}
type Projectile struct {
pos mat.Tuple4
velocity mat.Tuple4
}
func NewProjectile(pos mat.Tuple4, velocity mat.Tuple4) *Projectile {
return &Projectile{pos: pos, velocity: velocity}
}