OpenGL Notes a - Pittsburgh Supercomputing Center Staff Pages

blareweyrSoftware and s/w Development

Dec 13, 2013 (3 years and 6 months ago)

54 views

OpenGLNotes
a
StuPomerantz
smp@psc.edu
http://www.psc.edu/~smp
September15,2004
a
Allmaterialisadaptedfrom:OpenGLARB,et.al,\TheOpenGLPro-
grammingGuide",ThirdEd.,Reading:Addison-Wesley,1999
1
TheCameraMetaphore
²ItisconvenienttoimaginethatOpenGLhasa(virtual)
camerawhichistakingapictureofthe(virtual)world.
Ifso,then...
²Mustdelimitthepartoftheworldthatthecamerawillsee.
{Can't¯ttheentireworldinsidethecomputer.
{Can't¯ttheentireworldonthescreen.
{Themathematicsforcesusto(fornearclippingplane).
{Realisticviewsdon'tseeeverything.
²Mustspecifyhowthecameraseestheworld
{Camerascanhavedi®erentlenses.
48
TheCameraMetaphore
²Mustspecifythepositionandorientationofthecamera.
²Mustalsospecifypositionandorientationofobjectsinthe
worldthatthecamerawillbe\photographing"
²TheOpenGLprojectiontransformationisconcernedwith:
{Delimitingthepartoftheworldthatthecamerawillsee.
{Howthecameraseestheworld.
²TheOpenGLprojectiontransformationisnotconcernedwith:
{Thepositionandorientationofthecamera.
{Thepositionandorientationofobjectsintheworld.
{Thisisthejobofthemodelviewtransformation
49
OpenGLProjectionTransformations
²Recall:Theprojectiontransformationde¯neshowapoint,or
vertex,istransformedfromworldspacetothe2Dplaneofthe
screen,orscreenspace.
{Worldspacecouldbean8
1
2
by11sheetofpaperina2D
drawingprogramorthesolarsysteminaspace°ight
simulator.
{Worldspaceisthetwo-orthree-dimensionspacewhichyou
aremodellinginthecomputer.
²InOpenGL,worldcoordinatenumbers(vertices)areunitless.
Thatis,glVertex2i(2,3)purposelydoesnotsaywhetherthe
2isinmm,cm,inch,m,km,miles,orlightyears.The
interpretationisuptotheapplication.
50
OpenGLProjectionTransformations
²OpenGLrepresentstheprojectiontransformasa4x4matrix.
0
B
B
B
B
B
@
x11
x12
x13
x14
x21
x22
x23
x24
x31
x32
x33
x34
x41
x42
x43
x44
1
C
C
C
C
C
A
²Manykindsofprojectiontransformationsthatcanbe
representedinOpenGL.
²Thereisspecialsupportfortwocommonlyusedtranformations.
{Orthographic
{Perspective
51
OpenGLProjectionTransformations
²Projectiontransformationscanbethoughtofasde¯ningthe
shapeofaviewingvolume.
²Anorthographicviewingvolume:
voidglOrtho(GLdoubleleft,GLdoubleright,
GLdoublebottom,GLdoubletop,
GLdoublezNear,GLdoublezFar);
52
OpenGLProjectionTransformations
²Aperspectiveviewingvolume:
voidgluPerspective(GLdoublefovy,GLdoubleaspect,
GLdoublezNear,GLdoublezFar);
²fovyisthe¯eldofviewindegrees.
²aspectistheaspectratiothatdeterminesthe¯eldofviewin
thexdirection.Theratioofx(width)toy(height).
53
OpenGLProjectionTransformations
²Thegeneralviewingvolumecall:
voidglFrustum(GLdoubleleft,GLdoubleright,
GLdoublebottom,GLdoubletop,
GLdoublezNear,GLdoublezFar);
54
OpenGLProjectionTransformations
²WhenglOrtho()gluPerspective()orglFrustumis
executed,twothingsoccur:
{Amatrixwhichproducesaprojectiontransformationfor
therequestedviewingvolumeisgenerated.
{ThatmatrixismultipliedintothecurrentOpenGLstate.
¤Thisisanimportantpointwhichwillbedetailedinafew
slides.
55
TheOpenGLModelviewTransformation
Recall,themodelviewtransformationisconcernedwith:
²Thepositionandorientationofthecamera.
²Thepositionandorientationofobjectsintheworld.
Liketheprojectiontransformation,themodelviewtranformationis
a4x4matrix.
ButWait...
²Whynotusetwoseparatetransformationmatrices,oneforthe
camera(aviewtransformation)andonefortheobjectsinthe
world(amodeltransformation)insteadofonecomposite
modelviewtransformation?
56
TheOpenGLModelviewTransformation
Trythisthoughtexperiment:
Supposethecamera,oreye,andtheobjectthecameraistoview
areinthesamelocation,theorigin.
Inordertoviewtheobjecteitherthecameraortheobjectmustbe
moved.
57
TheOpenGLModelviewTransformation
Assumingaconstantcolorbackground,isthereadi®erenceinwhat
thecamerasees:
Movingthecameraawayfromtheorigin:
Movingtheobjectawayfromtheorigin:
58
TheOpenGLModelviewTransformation
NoDi®erence!
²Theprogrammercanthinkofthemodelviewmatrixas
transformingthevieworthemodelsthatarebeingviewed.
²Hencethenamemodelview.
²OpenGL'sdefaultstateismodelviewmode.Thatis{OpenGL
matrixmultiplicationsa®ectthemodelviewtransformation.
59
TheOpenGLModelviewTransformation
Pointingthecameraisprettysimple:
voidgluLookAt(GLdoubleeyeX,//positionoftheeye
GLdoubleeyeY,
GLdoubleeyeZ,
GLdoublecenterX,//wheretheeye
GLdoublecenterY,//islooking
GLdoublecenterZ,
GLdoubleupX,//whichway
GLdoubleupY,//isup
GLdoubleupZ);
60
TheOpenGLModelviewTransformation
Forexample:
voidgluLookAt(0,0,-5,0,0,0,0,1,0);
Putsthecameraat¡5onthezaxis,lookingattheorigin,and
choosesuptobeconincidentwiththepositiveyaxis.Thisisa
standardview.
Anotherexample:
voidgluLookAt(10,0,0,0,0,0,0,0,1);
Putsthecameraat10onthexaxis,lookingattheorigin,and
choosesuptobeconincidentwiththepositivezaxis.
61
SpecifyingtheView
InordertospecifyaviewtoOpenGL:
²Settheviewport.
²Settheprojectiontransformation
²Setthemodelviewtranformation
Inordertodothesetasks,theremustalsobeawayto:
²Setthetransformationstatefortheprojectionmodeandthe
modelviewmodebacktoadefault'notransformation'state.
(sincedoingatransformationcallmultipliesthecurrentmatrix
bythenewmatrixratherthanreplacingit).
²Changebetweentheprojectionandmodelviewstates.
62
SpecifyingtheView
Usually,whenthewindowisresized(e.g.glutglutReshapeFunc),
these3tasksareperformed:
²Settheviewport.
²Setsomedefaultprojectiontransformation
²Setsomedefaultmodelviewtranformation
63
SpecifyingtheView
TotellOpenGLtochangeitsstatesothatOpenGLmatrix
multipliesa®ecttheprojectiontransformationcall:
glMatrixMode(GL_PROJECTION);
TotellOpenGLtochangeitsstatesothatOpenGLmatrix
multipliesa®ectthemodelviewtransformation(whatisusually
desired)call:
glMatrixMode(GL_MODELVIEW);
64
SpecifyingtheView
Nomatterwhichmatrixmodeisinuse,theremustbeawayof
gettingbacktoadefault,notransformation,state.
Toreturntothedefaultstatecall:
glLoadIdentity();
0
B
B
B
B
B
@
1000
0100
0010
0001
1
C
C
C
C
C
A
Whichsetsthetransformationtotheidentitymatrix.
65
SpecifyingtheView
²NotethatglLoadIdentity();doesnotmultiplythecurrent
matrixbytheidentitymatrix.
²Ratheritsetsthecurrentmatrix(ineitherprojectionmatrix
modeormodelviewmatrixmode)totheidentity,whichisthe
default'notransformation'state.
²IftheidentitymatrixIismultipliedbytransformationa
matrixM,thenI£M=M
²So,settingthetransformationtotheidentityandthen
multiplinginanewtransformationisequivilanttosimply
settingthatnewtransformation.
66
SpecifyingtheView
Thedefaultprojectionis:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
whichisequivilantto
glMatrixMode(GL_PROJECTION);
glOrtho(-1,1,-1,1,1,1);
Acommonexample:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//setprojectionmatrixtoidentity
gluPerspective(45,1,5,100);//multiplyina
//perspectivetransform
67
SpecifyingtheView
Thedefaultmodelviewis:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
whichisequivilantto
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,0,0,0,-1,0,1,0);
Acommonexample:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();//setmodelviewmatrixtoidentity
gluLookAt(0,0,10,0,0,-1,0,1,0);//multiplyina
//cameratransform
68
SpecifyingtheView
Alltogethernowinareshapecallbackexample:
voidreshape(intx,inty){
glViewPort(x,y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1,5,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,10,0,0,-1,0,1,0);
}
69
SpecifyingtheView
Beware!Althoughyouknowhowtodoit,you
don'tyetknowthemathematicsbehindwhyit
works.
70
ModelviewTransformations
²OpenGLtransformationsareallspeci¯edinthreedimensional
space.
²Examplesherewillbeintwodimensions(fornow).
²Onlythetranslationandrotationtransformationswillbe
discussed(fornow).
²TransformationcallsinOpenGLmultiplyamatrixwhich
representsthetransformationintothecurrentOpenGLstate.
²Thisapproachcouldeasilygetoutofhandiftherewerenoway
tosaveandrestorethemodelviewmatrixstate.Fortunately,
thereis.
71
ModelviewTransformations
²SavingtheModelviewstate:
glPushMatrix()pushesthecurrentmatrixontoastack,
duplicatingthecurrentmatrix.Thatis,afteraglPushMatrix
call,thematrixontopofthestackisidenticaltotheonebelow
it.
²RestoringtheModelviewstate:
glPopMatrix()popsthecurrentmatrixstack,replacingthe
currentmatrixwiththeonebelowitonthestack.
Commonlyusedtomaketemporarychangestothemodelview
transformationsoaparticularobjectcanbefurthertransformed
withouta®ectingtheoverallmodelviewstate.
72
ModelviewTransformations
²Recallthearrowexample:
//arrowvertices
floatpt[6][2]={{0,0},{1,0},
{1,0},{0.9,0.1},
{1,0},{0.9,-0.1}};
//drawingagreenarrow
glColor3f(0,.6,0);
glBegin(GL_LINES);
for(i=0;i<6;i++)
glVertex3f(pt[i][0],pt[i][1],1);
glEnd();
73
ModelviewTransformations
Drawsthis¯gure:
74
ModelviewTransformations
²Translation:
glTranslatef(GLfloatx,GLfloaty,GLfloatz)
Example(usinggluLookAt(0,0,7,0,0,0,0,1,0)):
Notranslationvs.glTranslatef(-1,-1,0)
75
ModelviewTransformations
²Translationisalwaysinthreedimensionalspace.Itiseasyto
holdonedimensionconstantanddoatwodimensional
translationasthepreviousexampledid.
²glTranslatef()calculatesatranslationmatrixwhichhasthe
desireda®ect.
²Thismatrixismultipliedintothecurrentmatrix
transformationmode(usuallythemodelviewmode).
76
ModelviewTransformations
²Rotation:
glRotatef(GLfloatangle,GLfloatx,GLfloaty,GLfloatz)
Example(usinggluLookAt(0,0,7,0,0,0,0,1,0)):
Norotationvs.glRotatef(45,0,0,1);Notethatthecamerais
facingthex-yplane,sorotationinthex-yplaneisaboutthezaxis.
77
ModelviewTransformations
²Rotation:
glRotatef(GLfloatangle,GLfloatx,GLfloaty,GLfloatz)
²anglespeci¯estheangleofrotation,indegrees.
²x,y,zspecifythex,y,andzcoordinatesofthevector,about
whichyouarerotation.
78
ModelviewTransformations
²TranslationfollowedbyRotation:
Example(usinggluLookAt(0,0,7,0,0,0,0,1,0)):
glTranslatef(-1,0,0);
glTranslatef(-1,0,0);
glRotatef(60,0,0,1);
TranslationvsTranslationfollowedbyRotation
79
ModelviewTransformations
²RotationfollowedbyTranslation:
Example(usinggluLookAt(0,0,7,0,0,0,0,1,0)):
glRotatef(60,0,0,1);
glRotatef(60,0,0,1);
glTranslatef(-1,0,0);
RotationvsRotationfollowedbyTranslation
80
ModelviewTransformations
²TranslationandRotationareorderdependant!
Example(usinggluLookAt(0,0,7,0,0,0,0,1,0)):
glTranslatef(-1,0,0);
glRotatef(60,0,0,1);
glRotatef(60,0,0,1);
glTranslatef(-1,0,0);
TranslationthenRotationvsRotationthenTranslation
81
ModelviewTransformations
²Rotationaboutanarbitrarypoint(centerofthearrow)
Example(usinggluLookAt(0,0,7,0,0,0,0,1,0)):
glTranslatef(-0.5,0,0);
glRotatef(35,0,0,1);
glTranslatef(0.5,0,0);
glTranslatef(0.5,0,0);
glRotatef(35,0,0,1);
glTranslatef(-0.5,0,0);
82
ModelviewTransformations
Beware!Althoughyouknowhowtodoit,you
don'tyetknowthemathematicsbehindwhyit
works.
83