250 likes | 337 Views
CS 470 Introduction to Computer Graphics. Basic State Management Transformations. OpenGL State Management. Recall that OpenGL can be conceptualized as a state machine. The current state of the OpenGL state machine determines how actions are taken and how rendering is done.
E N D
CS 470 Introduction to Computer Graphics Basic State Management Transformations
OpenGL State Management • Recall that OpenGL can be conceptualized as a state machine. • The current state of the OpenGL state machine determines how actions are taken and how rendering is done. • The state of the OpenGL machine is defined by the values of several state variables and matrices
OpenGL State Management • Two types of objects define the state • state variables • matrices *
State Variables • A number of scalar or enumerated type variables define many properties of the current state. • When a state variable value has been set it effects all relevant subsequent operations until it is changed. • You have seen at least one state variable- • GL_CURRENT_COLOR
State Variables • Some state variables are set by special functions… • glColor3f(r,g,b); • Some can be set through assignment • Some state variables represent OpenGL functionality that is turned on or off (Boolean)
State Variables void glEnable(GLenum feature); void glDisable(GLenum feature); glEnable enables the feature feature glDisable disables the feature glEnable(GL_DEPTH_TEST); about 40 feature variables that can be turned off. See appendix B of the Red Book
State Variables • You can also query the state of the state variables – GLboolean glIsEnabled(GLenum feature); Returns GL_TRUE if the feature is currently enabled, GL_FALSE if is not
State Variables • More generic queries for non-boolean features- void glGetBooleanv(GLenum feature, GLboolean *p); void glGetIntegerv(GLenum feature, GLboolean *p); void glGetFloatv(GLenum feature, GLboolean *p); void glGetDoublev(GLenum feature, GLboolean *p); void glGetPointerv(GLenum feature, GLboolean *p);
State Variables • For example, to query the current color float cur_color[4]; glGetFloatv(GL_CURRENT_COLOR, &curr_color);
State Variables • You can also save current state variables for later reuse – void glPushAttrib(GLbitfield mask); void glPopAttrib(); glPushAttrib() push current groups of attributes on to a stack glPopAttrib() pops them off the stack mask is a bit mask of attribute categories to pushed onto the stack
Matrices • OpenGL has a number of matrices for a number of purposes • Two import matrices are • GL_MODELVIEW matrix - for operating on the model object • GL_PROJECTION matrix – use for projecting models in world space to displays in screen space
Matrices • Consider that when perform transformations to our model objects we are making changes to the GL_MODELVIEW matrix. • Recall from linear algebra that we can combine matrix multiplication and create composite functions.
Matrices • When we perform a transformation on the scene we modify the GL_MODELVIEW, … • When we preform another transformation on the scene we again modify the GL_MODELVIEW matrix… • … these modifications are cumulative… • … therefore the transformations are cumulative • SO, how do we stop this?
Matrices • Ans: store the matrix on a stack.. void glPushMatrix(); void glPopMatrix(); glPushMatrix() pushes the current matrix on the stack. glPopMatrix() pops a matrix from the top of the stack and makes it the current matrix
Matrices • Normally, there are • about 32 slots on the MODELVIEW matrix stack. • about 2 slots on the PROJECTION matrix stack
Matrices • by the way--- • void glMatrixMode(GLenum matmode); declares that the matrix will be of type matmode. matmode can be GL_MODEL_VIEW or GL_PROJECTION, GL_TEXTURE void glLoadIdentity(); Initializes the current matrix as an identity matrix
Translation • Moving objects to a new location in 3D space void glTranslate{f|d}(TYPE dx, TYPE dy, TYPE dz); Translates (moves) objects to new location based on offsets dx, dy, dz. One was to view dx, dy, dz is as distances from the camera (i.e. +dz is behind the camera)
Translation • What does translation do? • Moves the object to the new location defined by x+dx, y+dy, z+dz …. • Moves the origin of the coordinate system by dx, dy, dz • … can be viewed either way…
Rotation void glRotate{f|d}(TYPE angle, TYPE dx, TYPE dy, TYPE dz); Rotates the object(s) angle degrees about an axis defined by a vector from the origin to dx, dy, dz. angle, dx, dy, and dz can be floats or doubles
Rotation • For example to rotate a cube 45 degrees… glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(45.0, 0.0, 0.0, 1.0); glutWireCube(0.5); assumes the cube is centered on the origin
Rotation • What it if is not centered on the origin?… glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslate(cur_x, cur_y, cur_z); glRotate(rot_angle, dx, dy, dz); glTranslate(-cur_x, -cur_y, -cur_z); …what is going on in this code?
Scaling void glScale{f|d}(TYPE sx, TYPE sy, TYPE sz); Scales or resizes objects based on sx, sy, and sz scaling factors. or… more techically… creates a transformation matrix to scale by sx, sy, and sz
Scaling • Scaling assumes that the object is at the origin. • If not at the origin use technique as was used in the rotation example to translate, scale and translate back.