# OpenGL Notes a - Pittsburgh Supercomputing Center Staff Pages

Λογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 4 χρόνια και 7 μήνες)

141 εμφανίσεις

OpenGLNotes
a
StuPomerantz
smp@psc.edu
http://www.psc.edu/~smp
September15,2004
a
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
modelviewtransformation?
56
TheOpenGLModelviewTransformation
Trythisthoughtexperiment:
Supposethecamera,oreye,andtheobjectthecameraistoview
areinthesamelocation,theorigin.
Inordertoviewtheobjecteitherthecameraortheobjectmustbe
moved.
57
TheOpenGLModelviewTransformation
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
²Setthetransformationstatefortheprojectionmodeandthe
(sincedoingatransformationcallmultipliesthecurrentmatrix
bythenewmatrixratherthanreplacingit).
²Changebetweentheprojectionandmodelviewstates.
62
SpecifyingtheView
Usually,whenthewindowisresized(e.g.glutglutReshapeFunc),
²Settheviewport.
²Setsomedefaultprojectiontransformation
²Setsomedefaultmodelviewtranformation
63
SpecifyingtheView
TotellOpenGLtochangeitsstatesothatOpenGLmatrix
multipliesa®ecttheprojectiontransformationcall:
glMatrixMode(GL_PROJECTION);
TotellOpenGLtochangeitsstatesothatOpenGLmatrix
multipliesa®ectthemodelviewtransformation(whatisusually
desired)call:
glMatrixMode(GL_MODELVIEW);
64
SpecifyingtheView
Nomatterwhichmatrixmodeisinuse,theremustbeawayof
Toreturntothedefaultstatecall:
0
B
B
B
B
B
@
1000
0100
0010
0001
1
C
C
C
C
C
A
Whichsetsthetransformationtotheidentitymatrix.
65
SpecifyingtheView
matrixbytheidentitymatrix.
²Ratheritsetsthecurrentmatrix(ineitherprojectionmatrix
modeormodelviewmatrixmode)totheidentity,whichisthe
default'notransformation'state.
²IftheidentitymatrixIismultipliedbytransformationa
matrixM,thenI£M=M
²So,settingthetransformationtotheidentityandthen
multiplinginanewtransformationisequivilanttosimply
settingthatnewtransformation.
66
SpecifyingtheView
Thedefaultprojectionis:
glMatrixMode(GL_PROJECTION);
whichisequivilantto
glMatrixMode(GL_PROJECTION);
glOrtho(-1,1,-1,1,1,1);
Acommonexample:
glMatrixMode(GL_PROJECTION);
gluPerspective(45,1,5,100);//multiplyina
//perspectivetransform
67
SpecifyingtheView
Thedefaultmodelviewis:
glMatrixMode(GL_MODELVIEW);
whichisequivilantto
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,0,0,0,-1,0,1,0);
Acommonexample:
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,10,0,0,-1,0,1,0);//multiplyina
//cameratransform
68
SpecifyingtheView
Alltogethernowinareshapecallbackexample:
voidreshape(intx,inty){
glViewPort(x,y);
glMatrixMode(GL_PROJECTION);
gluPerspective(45,1,5,100);
glMatrixMode(GL_MODELVIEW);
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
77
ModelviewTransformations
²Rotation:
glRotatef(GLfloatangle,GLfloatx,GLfloaty,GLfloatz)
²anglespeci¯estheangleofrotation,indegrees.
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