Creating the Graphics of the DeLorean Digital Dashboard
Michael Conrad
mike@nrdvana.net
http://nrdvana.net/
CPAN: NERDVANA
Recap
C++ too time consuming
Experiment with Inline::CPP
Rewrote in Perl on a whim
Worked great
Told story, but no code
Started packaging up
2011-2014 - Graphics in C++
2014 - Rewrite in Perl
2015 - Presented Story at YAPC
New Modules
Most useful stuff shared now
X11/GLX cause no window manager
Sandbox wraps misc utilities
This is deeper dive
X11::Xlib
X11::GLX
OpenGL::Sandbox
But more Practically...
Even though talk about graphics,
unlikely useful
(unless dashboard
,secret aspirations game, and simple)
Will focus on prototyping
and high-performance perl
Rapid Prototyping
High-Performance Perl
before code
want to talk about building blocks
Legos
These are Legos.
convenient versatile useful
as a kid
Erector
This is erector
made of metal
assemble screws
annoying long to assemble
fiddling screws, hold in place
nice metal smell
rock solid
And this matters some times.
Science Olympiad
high school
county-wide
Science Olympiad.
event, build vehicle, simple machanics. spring falling weight
one year, travel then launch pingpong
I volunteered build
decided Legos
target worth most points
focused launching aiming.
no picture, terrible hoarder
this revised version
last 20 years.
original extra stuff ping-pong balls
plan propelling roll down a ramp
testing last minute
not enough speed, size ramp
friction lego wheels
The lego axles weren't strong / rubber bands
push by hand, lose points
last-minute strategy / launch down ramp
live at the event, attach rubber band
, pulled back, let go, exploded
frantic re-assemble
not in time
time negative, maximum negative score
Looking back
fatal mistake / Legos
potential mechanical energy
impractical for Legos
maybe possible
higher chance if used Erector
spent 4x time
Software
good parallel to sw
High-level rapid / complex programs quickly,
certain tasks explode
horrible disaster
Low-level extreme efficient
take forever complicated
High Level
Rapid Development
Limited Performance
Low Level
Extreme Efficiency
Complex => DNF
Use the Right Tool
early phase dashboard story
counterexample to the science olympiad story
I assumed / need low-level / graphics, wrote it in C.
4 years "the right way" with "the right language",
didn't finish.
Leads to next point,
Dashboard story would indicate otherwise
Tried "right way", but didn't have time
Middle Gound?
trapped, supposed to do?
fastest way sw written
assemble modules
packaged up others / Lego bricks
unit tests most of your debugging,
simplified interfaces
plug together
Use efficient foundation blocks
Build upper layers with glue
Don't expect foundation to reach to the top
Don't expect glue to be a foundation
Things to Glue with Perl
Perl, most high-level
link to libraries / efficient languages
problem is library in C horrible
can't just drop to C
while rapid prototyping Perl
but actually, can, Inline
solves exactly that problem!
(apologies Will Braswell; rPerl
said I was going to give it a try
still haven't made time
For more details,
can attend his talk.
Inline::C Inline::CPP magic sauce
discovered in 2014
convinced me to
graphics try in Perl
as previous talk,
worked flying colors,
got prototype
running in just
few months after to perl
use Inline C => '
double add_doubles(double d1, double d2) {
return d1 * d2;
}
';
The end. Auto-compiles and caches.
main purpose presentation
and OpenGL::Sandbox
show off techniques Perl with C.
But, interrupt self again
opengl disclaimer
Disclaimer...
Classic OpenGL API
Modern OpenGL API
OpenGL, Classic (1.x)
old OpenGL API
refer 1.x
based idea 3D coordinte system,
alter with translations rotate or scale
plot points in 3D
describe flat polygon surfaces
enable or disable
color or lighting or texturing
control how polygons painted
define polygons one at a time
sequence - build frame of animation.
swap to screen, start over
next frame animation
Since slow, ask OpenGL compile sequences
"Display List", stored effeciently
probably in graphics memory
play back on demand.
Plot out geometry with function calls
Maneuver through space by changing coordinate systems
Control the pixel math with provided options
OpenGL, Modern (2.x-4.x)
Later, new API (2.x - 4.x)
where allocate buffers hold geometry
ship to card / ask render everything instructions
provide custom math
coordinate spaces / color fill polygons.
No more "plotting", Pack geometry into buffers
No more "maneuvering", pack coordinate spaces into buffers
Describe your own pixel math with "Shaders"
Needs?
Lots of polygons?
Fancy lighting?
Shadows?
Moving Meshes?
Modern:
Old API can't give you this
Modern:
or this
these examples shadertoy.com
shader name for program on card
but...
Disadvantages:
API requires - plan out geometry ahead of time
and if animate - which vertices or cs in which buffer
planning takes time
learning API more time
(I might be biased, learned old first)
and so we come back to:
Plan the geometry
Plan the storage
Learn the shader process
Learn the shader language
Time
In particular, *don't* need render many
or goal is 2D - textures
really nothing wrong with classic
get the job done / less dev time
perfectly capable of
Classic works fine for:
Classic works fine for:
or this
Classic works fine for:
or this
Classic works fine for:
or this!
Classic works fine for:
or even this
Other Relevance:
concepts also apply to other , like
HTML5 canvas / SVG
3D Modeling
and if out & bought 3D printer
all the rage
then realized no idea CAD
no time to learn
no thousands of spare dollars
pro CAD software,
apply these old API concepts
modeling tool OpenSCAD
which uses all same brain cells
trained on OpenGL.
anyway
show code snippets
keep in mind
isn't how - write game
secretly dream of writing.
Unless was simple game and
graphics learning curve reason
never wrote it.
Triangle Example
Window Setup, Check Errors
Animate
Animate
Animate
Rotate A Static Model
Cache The Model
Type Less, Isolate Bugs
Display list is an allocated resource
glNewList/glEndList need matched
glBegin/glEnd need matched
Awkward names and constants
Display List Simplification
Display List Simplification
glBegin/glEnd pairing
Vertex simplification
Display List Wrapper
Begin/End Wrapper
Vertex Convenience
Main Loop Cleanup
Main Loop Cleanup
Main Loop Cleanup
Rotate Wrapper
Matrix Wrapper
All Together
Syntactic Sugar
Rapid prototyping for the win
But wait...
Speed got worse
What about Inline?
Inline Microbenchmak
function cost?
small stuff
worthwhile?
Inline Microbenchmark
Benchmark Results
my 5.20
no reason not to inline
Inline vertex
Inline vertex
manual for Inline::C
macros "Inline_Stack_Vars"
"Inline_Stack_Items
Inline_Stack_Item(i)"
Inline_Stack_Void
args, and ...
SvNV
perldoc perlapi
croak
multiple return
Inline rotate
interesting first arg number/letter
Inline rotate
declare first SV
Inline triangles
Now for something really hard.
do triangles
takes coderef
between glBegin/glEnd.
Inline triangles
wasnt hard
read docs
call_sv can eval
errsv is $@
no stack macro needed
Inline triangles
would have been fun
Inline::CPP, and Fonts
also reason gl 1.0
ftgl
nice api
setup face size
call render
Wrapping FTGL
also reason gl 1.0
ftgl
Wrapping FTGL
also reason gl 1.0
ftgl
Wrapping the Wrapper
painful if all attributes in C++
combine with Moo
Add Sugar to ->Render
FTGL render ok
want more layout
wrote in perl, but slow
construct/free hash options