1 / 25

CS 470 Introduction to Computer Graphics

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.

edan
Download Presentation

CS 470 Introduction to Computer Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 470 Introduction to Computer Graphics Basic State Management Transformations

  2. 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

  3. OpenGL State Management • Two types of objects define the state • state variables • matrices *

  4. 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

  5. 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)

  6. 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

  7. 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

  8. 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);

  9. State Variables • For example, to query the current color float cur_color[4]; glGetFloatv(GL_CURRENT_COLOR, &curr_color);

  10. 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

  11. 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

  12. 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.

  13. 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?

  14. 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

  15. Matrices • Normally, there are • about 32 slots on the MODELVIEW matrix stack. • about 2 slots on the PROJECTION matrix stack

  16. 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

  17. Transformations

  18. 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)

  19. 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…

  20. 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

  21. 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

  22. 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?

  23. 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

  24. 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.

More Related