GLUT&ps+pi+alls alexandrizavodny GLUTbarebones - - PowerPoint PPT Presentation

glut ps pi alls
SMART_READER_LITE
LIVE PREVIEW

GLUT&ps+pi+alls alexandrizavodny GLUTbarebones - - PowerPoint PPT Presentation

GLUT&ps+pi+alls alexandrizavodny GLUTbarebones WhatisthesimplestGLUTappyoucanwrite? GLUTbarebones GLUTbarebones GLUTbarebones


slide-1
SLIDE 1

GLUT
&ps
+
pi+alls


alexandri
zavodny


slide-2
SLIDE 2

GLUT
barebones


What
is
the
simplest
GLUT
app
you
can
write?


slide-3
SLIDE 3

GLUT
barebones


slide-4
SLIDE 4

GLUT
barebones


slide-5
SLIDE 5

GLUT
barebones


What
is
the
simplest
GLUT
app
you’d
WANT
to
write?


slide-6
SLIDE 6

GLUT
barebones


  • What
is
the
simplest
GLUT
app
you’d
WANT
to
write?

  • Leave
nothing
to
fate:


– Set
the
window
size
+
default
posi&on
 – Set
the
projec&on
and
modelview
matrices
 – Set
the
object’s
color
 – Request
double
buffering,
depth
buffer,

RGBA
color
buffer
 – Make
sure
that
depth
tes&ng
is
on
(GL_DEPTH_TEST)
 – Clear
the
color
buffer
/
depth
buffer!
 – Swap
the
buffers
/
flush
to
buffer 


slide-7
SLIDE 7

GLUT
barebones


  • Not
much
longer:

slide-8
SLIDE 8

Matrix
Maintenance


  • Keep
track
of
your
GL_PROJECTION
and


GL_MODELVIEW
matrices!


– glLoadIden&ty()
when
appropriate


  • Know
your
mode
/
pick
a
conven&on


– Single
projec&on:
update
in
resize
callback
and
 forget
about
it!
 – Mul&
projec&on:
update
every
frame!


slide-9
SLIDE 9

Matrix
Maintenance


  • glOrtho
/
gluOrtho2D

  • glTranslate
/
glRotate
/
glScale

  • glFrustum

  • gluPerspec&ve

  • gluLookAt

slide-10
SLIDE 10

Matrix
Maintenance


  • Know
which
func&ons
are
appropriate

  • GL_PROJECTION


– glOrtho
/
gluOrtho2D
 – gluPerspec&ve
 – glFrustum


  • GL_MODELVIEW


– gluLookAt
 – glTranslate
/
glRotate
/
glScale


slide-11
SLIDE 11

Display
Lists


  • Why?


– Efficiency.


  • Why
not?


– Too
sta&c.


  • How?


– GLuint
displaylist
=
glGenLists(1);
 – glNewList(displaylist,
GL_COMPILE);


  • //
drawing
code


– glEndList();



slide-12
SLIDE 12

Ligh&ng
Checklist


  • To
get
ligh&ng:


– 1)
Ligh&ng
must
be
enabled
 – 2)
Objects
must
have
materials


  • 2a)
Set
material
proper&es
with
glMaterial*()

  • 2b)
Override
material
proper&es
and
use
glColor()


– 3)
Objects
must
have
normals!


  • All
GLUT
primi&ves
and
GLU
quadrics
have
them
yay

  • If
you
use
glScale,
use
glEnable(GL_NORMALIZE)!

slide-13
SLIDE 13

Ligh&ng
Checklist
(cont’d)


  • Step
1:
Enabling
ligh&ng


– glEnable(GL_LIGHTING);


  • But
there
are
no
lights,
so…


– glEnable(GL_LIGHT0); 
 
//or
1,
2,
…
up
to
7*


  • And
that
light
has
no
proper&es,
so…


– glLigh+v(GL_LIGHT0,
GL_POSITION,
posi&on);
 – glLigh+v(GL_LIGHT0,
GL_DIFFUSE,
diffuseColor);
 – glLigh+v(GL_LIGHT0,
GL_AMBIENT,
ambientColor);


*Some
versions
of
OpenGL
have
fewer!


slide-14
SLIDE 14

Ligh&ng
Checklist
(cont’d)


  • Step
2:

Use
materials
or
setup
color
tracking


– If
using
materials:


  • glColor
won’t
do
anything
;
use
glMaterialfv()
instead

  • Example:


– Glfloat
red[]
=
{1,
0,
0,
1};
 – glMaterialfv(GL_FRONT_AND_BACK,

 
 
 
 
 

 
 
 
 
GL_AMBIENT_AND_DIFFUSE,
red);


– If
using
color
tracking:


  • glEnable(GL_COLOR_MATERIAL);

  • glColorMaterial(GL_FRONT,




 
 
 
 

 
 
 
 
 
 












GL_AMBIENT_AND_DIFFUSE);


slide-15
SLIDE 15

Ligh&ng
Checklist
(cont’d)


  • Step
3:
Normals


– GLUT
primi&ves
&
GLU
quadrics
have
them
 – Inside
of
GL_QUADS,
GL_TRIANGLES,
etc.:


  • Call
glNormal3f(nx,
ny,
nz)
before
each
glVertex3f.

  • Step
4:
PROFIT

slide-16
SLIDE 16

Misc
Ligh&ng
Tips


  • When
using
glMaterialfv
or
glLigh+v,
make


sure
your
arrays
have
4
elements!!


– For
light
posi&on,
the
last
component
should
be
1


  • Light
posi&on
gets
transformed
by
the


modelview
matrix
just
like
a
vertex!


slide-17
SLIDE 17

Misc.
Tips


  • Follow
protocol!

  • Create
a
“debug
mode”
that
draws:


– A
grid
over
the
ground
plane
 – The
axes,
showing
+/‐
XYZ
direc&ons


slide-18
SLIDE 18

Misc.
Tips


  • All
of
the
gl*
func&ons
allow
you
to
set…


glGet()
allows
you
to
get
info
back!


– Check
if
state
variables
are
enabled
/
disabled
 – Great
for
debugging


  • glGetError()
returns
OpenGL‐specific
‘errno’

  • Sketch
out
your
app
ahead
of
&me!