OpenGL Matrix Manipulation - Cecs.csulb.edu

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

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

73 εμφανίσεις

OpenGL Transformation


Learn how to carry out transformations in OpenGL


Rotation


Translation


Scaling


Introduce OpenGL matrix modes


Model
-
view


Projection


Stages of Vertex Transformations


CTM in OpenGL


OpenGL has a
model
-
view

and a
projection

matrix in the
pipeline which are concatenated together to form the CTM


Can manipulate each by first setting the correct matrix mode

OpenGL MATRICES


In OpenGL matrices are part of the state


Multiple types


Model
-
View (
GL_MODELVIEW
)


Projection (
GL_PROJECTION
)


Texture (
GL_TEXTURE
) (ignore for now)


Color(
GL_COLOR
) (ignore for now)


Single set of functions for manipulation


Select which to manipulated by


glMatrixMode
(GL_MODELVIEW);


glMatrixMode
(GL_PROJECTION);


The
Modelview

Matrix


A 4x4 matrix M representing the transformed coordinate
system.


Setting the matrix mode to
modelview


glMatrixMode
(GL_MODELVIEW);


Each vertex V you provide is a single vector matrix.


Before the rendering process, Each V is multiplied by the
Modelview

matrix.


V

=M V

The Projection Matrix


A 4x4 matrix M representing the projection matrix.


Setting the matrix mode to projection


glMatrixMode(GL_PROJECTION);


Defines the clipping volume


object out of the clipping
volume will be culled.


Initializing functions


void glOrtho( GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble near, GLdouble far );


void glFrustum( GLdouble left, GLdouble right, GLdouble
bottom, GLdouble top, GLdouble znear, GLdouble zfar );


void gluPerspective( GLdouble fovy, GLdouble aspect,
GLdouble zNear, GLdouble zFar );

CTM


Conceptually there is a 4 x 4 homogeneous coordinate
matrix, the
current transformation matrix

(CTM) that is
part of the state and is applied to all vertices that pass
down the pipeline


The CTM is defined in the user program and loaded
into a transformation unit

Transformations


Rotation


glRotated
(
GLdouble

angle,
GLdouble

x,
GLdouble

y,
GLdouble

z)


Scaling


glScaled
(
GLdouble

x
,
GLdouble

y
,
GLdouble

z
);


Translation


void
glTranslated
(
GLdouble

x,
GLdouble

y,
GLdouble

z );


The active GL matrix C is updated with the transformation T as
follows:


C =
C

x T


Each transformation effects only the vertices defined
after
the
transformation call.


In general, you make the active matrix the
Modelview

matrix.

Rotation about a Fixed Point

Start with identity matrix:

C



I

Move fixed point to origin:
C



CT

Rotate:
C



CR

Move fixed point back:
C



CT
-
1


Result:

C = TR T

1

which is
backwards
.



Reversing the Order

We want
C = T

1

R T

so we must do the operations in the following order


C



I

C



CT
-
1

C



CR

C



CT


Each operation corresponds to one function call in the program.


Note that the last operation specified is the first executed in the
program, a consequence of doing
postmultiplications


Example


Rotation about z axis by 30 degrees with a fixed point of (1.0,
2.0, 3.0)






Remember that last matrix specified in the program is the first
applied

glMatrixMode
(GL_MODELVIEW);

glLoadIdentity
();

glTranslatef
(1.0, 2.0, 3.0);

glRotatef
(30.0, 0.0, 0.0, 1.0);

glTranslatef
(
-
1.0,
-
2.0,
-
3.0); // to origin

Other Matrix Operations


Selecting the matrix mode is done using


void
glMatrixMode
(
GLenum

mode );


mode can be one of


GL_MODELVIEW


GL_PROJECTION


GL_TEXTURE

OpenGL Matrix functions


To initialize an identity matrix use


LoadIdentity()


To load a given matrix use


void glLoadMatrixd( const GLdouble *m );


m is a pointer to a 4x4 matrix stored in column
-
major
order as 16 consecutive values


In order to directly multiply the active matrix with a
given matrix, use


void glMultMatrixd( const GLdouble *m );


m is a pointer to a 4x4 matrix stored in column
-
major
order as 16 consecutive values

OpenGL Matrix Stack


The user can save the current transformation matrix by
pushing

it onto the stack with
glPushMatrix
()


The user can later restore the most recently pushed matrix
with
glPopMatrix
()


These commands really only make sense when in
GL_MODELVIEW

matrix mode


OpenGL Matrix Stack Example

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(…);

// save translation matrix:

glPushMatrix();

glRotatef(…);

// render something translated & rotated:

glCallList(foo);

// restore pushed matrix, undoing rotation:

glPopMatrix();

// render something else, no rotation:

glCallList(bar);