Anda di halaman 1dari 62

Purely Functional 3D in Typed Racket

or

Spoon-Feeding For the Functional Purist

Neil Toronto
RacketCon 2014

3D Engines
High-level abstraction layer (usually a library)
between program and GPU

3D Engines
High-level abstraction layer (usually a library)
between program and GPU

Program

3D Engine

OpenGL /
Direct3D

GPU

3D Engines
High-level abstraction layer (usually a library)
between program and GPU

Program

3D Engine

OpenGL /
Direct3D

GPU

Examples: Unity, Unreal, Rage (rendering parts)

3D Engines
High-level abstraction layer (usually a library)
between program and GPU

Program

3D Engine

OpenGL /
Direct3D

GPU

Examples: Unity, Unreal, Rage (rendering parts)


Problem: All are built around mutation

What Does a 3D Engine Do?

What Does a 3D Engine Do?

Giant baby consumes homogenous, bland,


first-order vertex data

What Does a 3D Engine Do?

Giant baby consumes homogenous, bland,


first-order vertex data
Giant baby often must be tricked

What Does a 3D Engine Do?

Giant baby consumes homogenous, bland,


first-order vertex data
Giant baby often must be tricked
Giant baby is very hungry but can swallow only a
few hundred spoonfuls per frame
9

Imperative Engine API

3D Engine
Commands
Scene Database
(add-shape! ...)
(move-shape! ...)
(add-light! ...)
(draw-scene!)

Algorithms
High-Powered
Blender

10

Scene Databases
Scene database: a data structure that makes
spatial queries (e.g. point-inside) efficient

11

Scene Databases
Scene database: a data structure that makes
spatial queries (e.g. point-inside) efficient

12

Scene Databases
Scene database: a data structure that makes
spatial queries (e.g. point-inside) efficient

13

Scene Databases
Scene database: a data structure that makes
spatial queries (e.g. point-inside) efficient

14

Scene Databases
Scene database: a data structure that makes
spatial queries (e.g. point-inside) efficient

15

Scene Databases
Scene database: a data structure that makes
spatial queries (e.g. point-inside) efficient

16

Scene Databases
Scene database: a data structure that makes
spatial queries (e.g. point-inside) efficient

Most databases are bounding hierarchies, and


almost all are trees

17

Mostly Functional Engine API

Computations
(make-shape ...)
(make-light ...)
(scene-union ...)
(scene-transform ...)
(draw-scene! ...)

3D Engine
Combinators
Algorithms
High-Powered
Blender

18

Awesome! Lets Try It!


>

19

Awesome! Lets Try It!


> (define unit-sphere
(shape->scene
(make-sphere-shape identity-flt3
(flvector 1.0 1.0 1.0 1.0)
(flvector 0.0 1.0 0.0 2.0)
(material 0.01 0.29 0.7 0.1)
#f)))
>

20

Awesome! Lets Try It!


> (define unit-sphere
(shape->scene
(make-sphere-shape identity-flt3
(flvector 1.0 1.0 1.0 1.0)
(flvector 0.0 1.0 0.0 2.0)
(material 0.01 0.29 0.7 0.1)
#f)))
> (scene-union unit-sphere
(scene-transform
unit-sphere
(translate-flt3 (flvector 1.0 0.0 0.0))))

21

Awesome! Lets Try It!


> (define unit-sphere
(shape->scene
(make-sphere-shape identity-flt3
(flvector 1.0 1.0 1.0 1.0)
(flvector 0.0 1.0 0.0 2.0)
(material 0.01 0.29 0.7 0.1)
#f)))
> (scene-union unit-sphere
(scene-transform
unit-sphere
(translate-flt3 (flvector 1.0 0.0 0.0))))
(scene-node
(Nonempty-FlRect3 (flvector -1.0 -1.0 -1.0) (flvector 2.0 1.0 1.0))
2
(scene-leaf
(Nonempty-FlRect3 (flvector -1.0 -1.0 -1.0) (flvector 1.0 1.0 1.0))
1
(sphere-shape (flidentity3) (flvector 1.0 1.0 1.0 1.0) (flvector 0.0 1.0 0.0 2.0)
(material 0.01 0.29 0.7 0.1) #f))
(scene-tran
(Nonempty-FlRect3 (flvector 0.0 -1.0 -1.0) (flvector 2.0 1.0 1.0))
1
(flaffine3 (flvector 1.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0))
(flaffine3 (flvector 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0))
(scene-leaf
(Nonempty-FlRect3 (flvector -1.0 -1.0 -1.0) (flvector 1.0 1.0 1.0))
1
(sphere-shape (flidentity3) (flvector 1.0 1.0 1.0 1.0) (flvector 0.0 1.0 0.0 2.0)
(material 0.01 0.29 0.7 0.1) #f))))

22

A More Excellent Way


>

23

A More Excellent Way


> (require pict3d)
>

24

A More Excellent Way


> (require pict3d)
> (with-emitted '(0 1 0 2)
(combine (sphere '(0 0 0) 1)
(sphere '(1 0 0) 1)))

25

A More Excellent Way


> (require pict3d)
> (with-emitted '(0 1 0 2)
(combine (sphere '(0 0 0) 1)
(sphere '(1 0 0) 1)))

26

Demos

27

Design Goals

Make it fast
May require users to tweak, cache, give hints
Makes the high-powered blender unsafe and
impure

28

Design Goals

Make it fast
May require users to tweak, cache, give hints
Makes the high-powered blender unsafe and
impure
Use and allow modern rendering techniques (i.e.
make it cool)

29

Design Goals

Make it fast
May require users to tweak, cache, give hints
Makes the high-powered blender unsafe and
impure
Use and allow modern rendering techniques (i.e.
make it cool)
Minimize confusing surprises

30

Design Goals

Make it fast
May require users to tweak, cache, give hints
Makes the high-powered blender unsafe and
impure
Use and allow modern rendering techniques (i.e.
make it cool)
Minimize confusing surprises
Last two goals are often complementary

31

Default Depth Buffer


z-near = 0.1 and z-far = 10.0
Default Depth Values

32

Default Depth Buffer: Confusing Surprises


16 meters away

1/2 meter away

33

Default Depth Buffer: Confusing Surprises


32 meters away

1/2 meter away

34

Default Depth Buffer: Confusing Surprises


64 meters away

1/2 meter away

35

Default Depth Buffer: Confusing Surprises


128 meters away

1/2 meter away

36

Default Depth Buffer: Confusing Surprises


256 meters away

1/2 meter away

37

Default Depth Buffer: Confusing Surprises


512 meters away

1/2 meter away

38

Default Depth Buffer: Confusing Surprises


1024 meters away

1/2 meter away

39

Default Depth Buffer: Confusing Surprises


2048 meters away

1/2 meter away

40

Default Depth Buffer: Confusing Surprises


4096 meters away

1/2 meter away

41

Logarithmic Depth Buffer


z-near = 0.1 and z-far = 10.0
Logarithmic Depth Values

* http://tulrich.com/geekstuff/log_depth_buffer.txt

42

Logarithmic Depth Buffer: Results


2 million meters away

1 millimeter away

43

Multi-Pass Rendering

44

Multi-Pass Rendering
Material: draw shape normals

45

Multi-Pass Rendering
Material: draw shape normals

Lighting: draw illumination (diffuse and specular)

46

Multi-Pass Rendering
Material: draw shape normals

Lighting: draw illumination (diffuse and specular)

Color: draw shape colors

47

Multi-Pass Rendering
Material: draw shape normals

Color: draw shape colors

Lighting: draw illumination (diffuse and specular)

Bloom: extract overbright and blur

48

Multi-Pass Rendering
Material: draw shape normals

Color: draw shape colors

Lighting: draw illumination (diffuse and specular)

Bloom: extract overbright and blur

Final: combine and tone-map

49

Multi-Pass Rendering
Material: draw shape normals

Color: draw shape colors

Lighting: draw illumination (diffuse and specular)

Bloom: extract overbright and blur

Final: combine and tone-map

O(m+n) in the number of shapes and lights


50

Best Minimizer of Confusing Surprises

51

Best Minimizer of Confusing Surprises

It's purely functional!

52

Best Minimizer of Confusing Surprises

It's purely functional!


Dont need to worry about the state of the scene
or engine

53

Best Minimizer of Confusing Surprises

It's purely functional!


Dont need to worry about the state of the scene
or engine
Other advantages to full persistence
printf debugging is crazy useful
Store scenes to rewind a game or record a
demo

54

Now What?
Make pict3d work on the three major platforms

55

Now What?
Make pict3d work on the three major platforms
Make pict3d work in untyped Racket

56

Now What?
Make pict3d work on the three major platforms
Make pict3d work in untyped Racket
Game canvases and big-bang-3d (very close)

57

Now What?
Make pict3d work on the three major platforms
Make pict3d work in untyped Racket
Game canvases and big-bang-3d (very close)
Other shapes and shape combinators, textures,
user GPU programs

58

Now What?
Make pict3d work on the three major platforms
Make pict3d work in untyped Racket
Game canvases and big-bang-3d (very close)
Other shapes and shape combinators, textures,
user GPU programs
More modern techniques: shadow mapping,
screen-space ambient occlusion, dynamic
occlusion culling

59

Now What?
Make pict3d work on the three major platforms
Make pict3d work in untyped Racket
Game canvases and big-bang-3d (very close)
Other shapes and shape combinators, textures,
user GPU programs
More modern techniques: shadow mapping,
screen-space ambient occlusion, dynamic
occlusion culling
Cairo rendering (for plot3d output)

60

Now What?
Make pict3d work on the three major platforms
Make pict3d work in untyped Racket
Game canvases and big-bang-3d (very close)
Other shapes and shape combinators, textures,
user GPU programs
More modern techniques: shadow mapping,
screen-space ambient occlusion, dynamic
occlusion culling
Cairo rendering (for plot3d output)
Faster faster faster faster faster
61

Install It Today!
Let pict3d spoon-feed the giant baby for you

raco pkg install pict3d


62

Anda mungkin juga menyukai