3D graphics with Perl Jonathan Chin <jon-techtalk@earth.li> - - PowerPoint PPT Presentation

3d graphics with perl
SMART_READER_LITE
LIVE PREVIEW

3D graphics with Perl Jonathan Chin <jon-techtalk@earth.li> - - PowerPoint PPT Presentation

3D graphics with Perl Jonathan Chin <jon-techtalk@earth.li> November 2005 OpenGL API for real-time 3D graphics OpenGL API for real-time 3D graphics Abstracts away the details of complicated rendering hardware/software OpenGL


slide-1
SLIDE 1

3D graphics with Perl

Jonathan Chin <jon-techtalk@earth.li> November 2005

slide-2
SLIDE 2

OpenGL

◮ API for real-time 3D graphics

slide-3
SLIDE 3

OpenGL

◮ API for real-time 3D graphics ◮ Abstracts away the details of complicated rendering

hardware/software

slide-4
SLIDE 4

OpenGL

◮ API for real-time 3D graphics ◮ Abstracts away the details of complicated rendering

hardware/software

◮ Standard on UNIX; competes with Direct3D on Windows.

slide-5
SLIDE 5

◮ OpenGL will: ◮ Render 3d graphics very quickly.

slide-6
SLIDE 6

◮ OpenGL will: ◮ Render 3d graphics very quickly. ◮ Get the hardware to do it, if possible.

slide-7
SLIDE 7

◮ OpenGL will: ◮ Render 3d graphics very quickly. ◮ Get the hardware to do it, if possible. ◮ OpenGL will not:

slide-8
SLIDE 8

◮ OpenGL will: ◮ Render 3d graphics very quickly. ◮ Get the hardware to do it, if possible. ◮ OpenGL will not: ◮ Raytrace

slide-9
SLIDE 9

◮ OpenGL will: ◮ Render 3d graphics very quickly. ◮ Get the hardware to do it, if possible. ◮ OpenGL will not: ◮ Raytrace ◮ Deal with windowing, input, mouse/keyboard interaction

slide-10
SLIDE 10

Some graphics hardware from 2002

slide-11
SLIDE 11

Equivalent hardware in 2005

slide-12
SLIDE 12

Talking to OpenGL from Perl

◮ OpenGL.pm

slide-13
SLIDE 13

Talking to OpenGL from Perl

◮ OpenGL.pm ◮ SDL::OpenGL

slide-14
SLIDE 14

Talking to OpenGL from Perl

◮ OpenGL.pm ◮ SDL::OpenGL ◮ OpenGL::Simple

slide-15
SLIDE 15

Step 1: Open a window.

◮ X11 (The OpenGL.pm way)

slide-16
SLIDE 16

Step 1: Open a window.

◮ X11 (The OpenGL.pm way) ◮ SDL

slide-17
SLIDE 17

Step 1: Open a window.

◮ X11 (The OpenGL.pm way) ◮ SDL ◮ GTK

slide-18
SLIDE 18

Step 1: Open a window.

◮ X11 (The OpenGL.pm way) ◮ SDL ◮ GTK ◮ GLUT

slide-19
SLIDE 19

Step 1: Open a window.

◮ X11 (The OpenGL.pm way) ◮ SDL ◮ GTK ◮ GLUT ◮ OpenGL::Simple::Viewer

slide-20
SLIDE 20

use OpenGL::Simple ’:all’; use OpenGL::Simple::GLUT ’:all’; glutInit; glutInitWindowSize(500,500); glutCreateWindow("Window title"); glutDisplayFunc(\&displayfunc); glutMainLoop; sub displayfunc { glClearColor(0.8, 0.8, 0.8, 0); glClear(GL_COLOR_BUFFER_BIT); glPointSize(5.0); glColor(0,0,0); glBegin(GL_POINTS); glVertex(-0.5, -0.5); glVertex(-0.5, 0.5); glVertex( 0.5, 0.5); glVertex( 0.5, -0.5); glEnd; glFlush; }

slide-21
SLIDE 21

glBegin(GL_POINTS); glVertex(-0.5, -0.5); glVertex(-0.5, 0.5); glVertex( 0.5, 0.5); glVertex( 0.5, -0.5); glEnd;

slide-22
SLIDE 22

glBegin(GL_LINES); glVertex(-0.5, -0.5); glVertex(-0.5, 0.5); glVertex( 0.5, 0.5); glVertex( 0.5, -0.5); glEnd;

slide-23
SLIDE 23

glBegin(GL_LINE_STRIP); glVertex(-0.5, -0.5); glVertex(-0.5, 0.5); glVertex( 0.5, 0.5); glVertex( 0.5, -0.5); glEnd;

slide-24
SLIDE 24

glBegin(GL_LINE_LOOP); glVertex(-0.5, -0.5); glVertex(-0.5, 0.5); glVertex( 0.5, 0.5); glVertex( 0.5, -0.5); glEnd;

slide-25
SLIDE 25

glBegin(GL_POLYGON); glVertex(-0.5, -0.5); glVertex(-0.5, 0.5); glVertex( 0.5, 0.5); glVertex( 0.5, -0.5); glEnd;

slide-26
SLIDE 26

glBegin(GL_POINTS); glColor(1,0,0); glVertex(-0.5,-0.5); glColor(0,1,0); glVertex(-0.5,0.5); glColor(0,0,1); glVertex(0.5,0.5); glColor(0,1,1); glVertex(0.5,-0.5); glEnd;

slide-27
SLIDE 27

glBegin(GL_LINES); glColor(1,0,0); glVertex(-0.5,-0.5); glColor(0,1,0); glVertex(-0.5,0.5); glColor(0,0,1); glVertex(0.5,0.5); glColor(0,1,1); glVertex(0.5,-0.5); glEnd;

slide-28
SLIDE 28

glBegin(GL_LINE_STRIP); glColor(1,0,0); glVertex(-0.5,-0.5); glColor(0,1,0); glVertex(-0.5,0.5); glColor(0,0,1); glVertex(0.5,0.5); glColor(0,1,1); glVertex(0.5,-0.5); glEnd;

slide-29
SLIDE 29

glBegin(GL_LINE_LOOP); glColor(1,0,0); glVertex(-0.5,-0.5); glColor(0,1,0); glVertex(-0.5,0.5); glColor(0,0,1); glVertex(0.5,0.5); glColor(0,1,1); glVertex(0.5,-0.5); glEnd;

slide-30
SLIDE 30

glBegin(GL_POLYGON); glColor(1,0,0); glVertex(-0.5,-0.5); glColor(0,1,0); glVertex(-0.5,0.5); glColor(0,0,1); glVertex(0.5,0.5); glColor(0,1,1); glVertex(0.5,-0.5); glEnd;

slide-31
SLIDE 31

But what about 3D graphics?

slide-32
SLIDE 32

But what about 3D graphics?

slide-33
SLIDE 33

use OpenGL::Simple ’:all’; use OpenGL::Simple::GLUT ’:all’; use OpenGL::Simple::Viewer; glutInit; my $v = new OpenGL::Simple::Viewer( draw_geometry => sub { glBegin(GL_POLYGON); glColor(1,0,0); glVertex(-1,-1,0); glColor(0,1,0); glVertex( 1,-1,0); glColor(0,0,1); glVertex( 1, 1,0); glColor(1,0,1); glVertex(-1, 1,0); glEnd; }, ); glutMainLoop;

slide-34
SLIDE 34

use OpenGL::Simple ’:all’; use OpenGL::Simple::GLUT ’:all’; use OpenGL::Simple::Viewer; glutInit; my $v = new OpenGL::Simple::Viewer( draw_geometry => sub { glutSolidTeapot(1.0); }, ); glutMainLoop; exit;

slide-35
SLIDE 35

glutInit; my $v = new OpenGL::Simple::Viewer( draw_geometry => sub { glBegin(GL_POLYGON); glTexCoord(0,1); glVertex(-1,-1,0); glTexCoord(1,1); glVertex( 1,-1,0); glTexCoord(1,0); glVertex( 1, 1,0); glTexCoord(0,0); glVertex(-1, 1,0); glEnd; }, ); my $i = new Imager; $i->read(file=>’texture.png’); glTexImage2D(image=>$i); glEnable(GL_TEXTURE_2D); glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR); glutMainLoop;

slide-36
SLIDE 36

glBegin(GL_QUADS); glTexCoord(0.0,1.0); glVertex(-1.0,-1.0, 1.0); glTexCoord(1.0,1.0); glVertex( 1.0,-1.0, 1.0); ... glEnd;

slide-37
SLIDE 37

glBegin(GL_QUADS); glColor( 0.0, -1.0, 1.0); glTexCoord(0.0, 1.0); glVertex(-1.0, -1.0, 1.0); glColor( 1.0, 0.0, 1.0); glTexCoord(1.0, 1.0); glVertex( 1.0, -1.0, 1.0); ... glEnd;

slide-38
SLIDE 38

How to draw a simple fractal

Define three fixed points.

slide-39
SLIDE 39

How to draw a simple fractal

Put a dot somewhere in the centre.

slide-40
SLIDE 40

How to draw a simple fractal

Choose one of the fixed points, at random.

slide-41
SLIDE 41

How to draw a simple fractal

Draw another dot, half-way between the previous dot and the fixed point.

slide-42
SLIDE 42

How to draw a simple fractal

Repeat.

slide-43
SLIDE 43

How to draw a simple fractal

Repeat many times.

slide-44
SLIDE 44

How to draw a simple fractal

Repeat many many times with smaller dots.

slide-45
SLIDE 45

A 3D fractal

glBegin(GL_POINTS); for (1..$npoints) { my $j = int(rand(4)); # 0 <= $j <= 3 $x += 0.5*( $fixp[$j]->[0] - $x ); $y += 0.5*( $fixp[$j]->[1] - $y ); $z += 0.5*( $fixp[$j]->[2] - $z ); $cr+= 0.5*( $col[$j]->[0] - $cr ); $cg+= 0.5*( $col[$j]->[1] - $cg ); $cb+= 0.5*( $col[$j]->[2] - $cb ); glColor($cr,$cg,$cb); glVertex($x,$y,$z); } glEnd;

slide-46
SLIDE 46

use Chemistry::File::PDB; use Chemistry::Bond::Find ’:all’; .. my $mol = Chemistry::MacroMol->read(’dna.pdb;); .. sub make_model { my $i = 0; recenter(); my @atoms = $mol->atoms; for my $atom ( @atoms ) { my $mass = log( 1 + $atom->mass ) / $mass_scale; my $color = $element_colours{ $atom->symbol } || $colour{cyan}; my @coords = $atom->coords->array; push @ballpoints, [ $color, $mass, @coords ]; } for my $bond ( $mol->bonds ) { my ( $from, $to ) = $bond->atoms; my @from = $from->coords->array; my @to = $to->coords->array; push @ballsticks, [ \@from, \@to ]; } }

slide-47
SLIDE 47
slide-48
SLIDE 48

use OpenGL::Simple ’:all’; use OpenGL::Simple::GLUT ’:all’; use OpenGL::Simple::Viewer; use OpenGL::GLM; glutInit; my ($model,$list); my $v = new OpenGL::Simple::Viewer( draw_geometry => sub { unless ($model) { $model = new OpenGL::GLM(’camel.obj’); $model->Unitize; $list = $model->List(GLM_SMOOTH); } glColor(0.6,0.5,0.3); glCallList($list); }, ); glutMainLoop;

slide-49
SLIDE 49

glCallList($list); glTranslate(1,0,0); glCallList($list);

slide-50
SLIDE 50

glCallList($list); glTranslate(1,0,0); glScale(1.5,1.5,1.5); glCallList($list);

slide-51
SLIDE 51

glCallList($list); glTranslate(1,0,0); glScale(1.5,1.5,1.5); glRotate(25,0,1,0); glCallList($list);

slide-52
SLIDE 52
slide-53
SLIDE 53