CS 470 Introduction to Computer Graphics
description
Transcript of CS 470 Introduction to Computer Graphics
![Page 1: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/1.jpg)
CS 470 CS 470 Introduction to Introduction to
Computer GraphicsComputer Graphics
Basic State ManagementBasic State Management
TransformationsTransformations
![Page 2: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/2.jpg)
OpenGL State ManagementOpenGL State Management
• Recall that OpenGL can be Recall that OpenGL can be conceptualized as a state machine.conceptualized as a state machine.
• The current state of the OpenGL The current state of the OpenGL state machine determines how state machine determines how actions are taken and how rendering actions are taken and how rendering is done.is done.
• The state of the OpenGL machine is The state of the OpenGL machine is defined by the values of several state defined by the values of several state variables and matricesvariables and matrices
![Page 3: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/3.jpg)
OpenGL State ManagementOpenGL State Management
• Two types of objects define the stateTwo types of objects define the state– state variablesstate variables– matrices *matrices *
![Page 4: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/4.jpg)
State VariablesState Variables
• A number of scalar or enumerated A number of scalar or enumerated type variables define many properties type variables define many properties of the current state.of the current state.
• When a state variable value has been When a state variable value has been set it effects all relevant subsequent set it effects all relevant subsequent operations until it is changed.operations until it is changed.
• You have seen at least one state You have seen at least one state variable-variable-– GL_CURRENT_COLORGL_CURRENT_COLOR
![Page 5: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/5.jpg)
State VariablesState Variables
• Some state variables are set by Some state variables are set by special functions…special functions…– glColor3f(r,g,b);glColor3f(r,g,b);
• Some can be set through assignmentSome can be set through assignment
• Some state variables represent Some state variables represent OpenGL functionality that is turned OpenGL functionality that is turned on or off (Boolean)on or off (Boolean)
![Page 6: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/6.jpg)
State VariablesState Variables
void glEnable(GLenum feature);void glEnable(GLenum feature);void glDisable(GLenum feature);void glDisable(GLenum feature);
glEnable enables the feature glEnable enables the feature featurefeatureglDisable disables the featureglDisable disables the feature
glEnable(GL_DEPTH_TEST);glEnable(GL_DEPTH_TEST);about 40 feature variables that can be about 40 feature variables that can be turned off.turned off.See appendix B of the Red BookSee appendix B of the Red Book
![Page 7: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/7.jpg)
State VariablesState Variables
• You can also query the state of the You can also query the state of the state variables –state variables –
GLboolean glIsEnabled(GLenum GLboolean glIsEnabled(GLenum feature);feature);
Returns GL_TRUE if the feature is Returns GL_TRUE if the feature is currently enabled, GL_FALSE if is notcurrently enabled, GL_FALSE if is not
![Page 8: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/8.jpg)
State VariablesState Variables
• More generic queries for non-boolean More generic queries for non-boolean features-features-
void glGetBooleanv(GLenum feature, GLboolean *p);void glGetBooleanv(GLenum feature, GLboolean *p);
void glGetIntegerv(GLenum feature, GLboolean *p);void glGetIntegerv(GLenum feature, GLboolean *p);
void glGetFloatv(GLenum feature, GLboolean *p);void glGetFloatv(GLenum feature, GLboolean *p);
void glGetDoublev(GLenum feature, GLboolean *p);void glGetDoublev(GLenum feature, GLboolean *p);
void glGetPointerv(GLenum feature, GLboolean *p);void glGetPointerv(GLenum feature, GLboolean *p);
![Page 9: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/9.jpg)
State VariablesState Variables
• For example, to query the current For example, to query the current colorcolor
float cur_color[4];float cur_color[4];
glGetFloatv(GL_CURRENT_COLOR, glGetFloatv(GL_CURRENT_COLOR, &curr_color);&curr_color);
![Page 10: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/10.jpg)
State VariablesState Variables• You can also save current state variables You can also save current state variables
for later reuse –for later reuse –
void glPushAttrib(GLbitfield mask);void glPushAttrib(GLbitfield mask);
void glPopAttrib();void glPopAttrib();
glPushAttrib() push current groups of attributes glPushAttrib() push current groups of attributes on to a stackon to a stack
glPopAttrib() pops them off the stackglPopAttrib() pops them off the stack
mask is a bit mask of attribute categories to mask is a bit mask of attribute categories to pushed onto the stackpushed onto the stack
![Page 11: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/11.jpg)
MatricesMatrices
• OpenGL has a number of matrices for OpenGL has a number of matrices for a number of purposesa number of purposes
• Two import matrices are Two import matrices are – GL_MODELVIEW matrix - for operating GL_MODELVIEW matrix - for operating
on the model objecton the model object– GL_PROJECTION matrix – use for GL_PROJECTION matrix – use for
projecting models in world space to projecting models in world space to displays in screen spacedisplays in screen space
![Page 12: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/12.jpg)
MatricesMatrices
• Consider that when perform Consider that when perform transformations to our model objects transformations to our model objects we are making changes to the we are making changes to the GL_MODELVIEW matrix.GL_MODELVIEW matrix.
• Recall from linear algebra that we Recall from linear algebra that we can combine matrix multiplication can combine matrix multiplication and create composite functions.and create composite functions.
![Page 13: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/13.jpg)
MatricesMatrices
• When we perform a transformation on the When we perform a transformation on the scene we modify the GL_MODELVIEW, …scene we modify the GL_MODELVIEW, …
• When we preform another transformation When we preform another transformation on the scene we again modify the on the scene we again modify the GL_MODELVIEW matrix…GL_MODELVIEW matrix…
• … … these modifications are cumulative…these modifications are cumulative…• … … therefore the transformations are therefore the transformations are
cumulativecumulative• SO, how do we stop this?SO, how do we stop this?
![Page 14: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/14.jpg)
MatricesMatrices
• Ans: store the matrix on a stack..Ans: store the matrix on a stack..
void glPushMatrix();void glPushMatrix();void glPopMatrix();void glPopMatrix();
glPushMatrix() pushes the current matrix glPushMatrix() pushes the current matrix on the stack.on the stack.glPopMatrix() pops a matrix from the top of glPopMatrix() pops a matrix from the top of the stack and makes it the current matrixthe stack and makes it the current matrix
![Page 15: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/15.jpg)
MatricesMatrices
• Normally, there areNormally, there are– about 32 slots on the MODELVIEW about 32 slots on the MODELVIEW
matrix stack.matrix stack.– about 2 slots on the PROJECTION matrix about 2 slots on the PROJECTION matrix
stackstack
![Page 16: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/16.jpg)
MatricesMatrices• by the way---by the way---
– void glMatrixMode(GLenum matmode);void glMatrixMode(GLenum matmode);
declares that the matrix will be of type declares that the matrix will be of type matmode.matmode.
matmode can be GL_MODEL_VIEW or matmode can be GL_MODEL_VIEW or GL_PROJECTION, GL_TEXTUREGL_PROJECTION, GL_TEXTURE
void glLoadIdentity();void glLoadIdentity();
Initializes the current matrix as an identity Initializes the current matrix as an identity matrixmatrix
![Page 17: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/17.jpg)
TransformationsTransformations
![Page 18: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/18.jpg)
TranslationTranslation
• Moving objects to a new location in 3D spaceMoving objects to a new location in 3D space
void glTranslate{f|d}(TYPE dx, TYPE dy, TYPE dz);void glTranslate{f|d}(TYPE dx, TYPE dy, TYPE dz);
Translates (moves) objects to new location based on Translates (moves) objects to new location based on offsets dx, dy, dz.offsets dx, dy, dz.
One was to view dx, dy, dz is as distances from the One was to view dx, dy, dz is as distances from the camera (i.e. +dz is behind the camera)camera (i.e. +dz is behind the camera)
![Page 19: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/19.jpg)
TranslationTranslation
• What does translation do?What does translation do?– Moves the object to the new location Moves the object to the new location
defined by x+dx, y+dy, z+dz ….defined by x+dx, y+dy, z+dz ….– Moves the origin of the coordinate Moves the origin of the coordinate
system by dx, dy, dzsystem by dx, dy, dz– … … can be viewed either way…can be viewed either way…
![Page 20: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/20.jpg)
RotationRotation
void glRotate{f|d}(TYPE angle, TYPE void glRotate{f|d}(TYPE angle, TYPE dx, dx, TYPE dy, TYPE dz);TYPE dy, TYPE dz);
Rotates the object(s) Rotates the object(s) angleangle degrees degrees about an axis defined by a vector about an axis defined by a vector from the origin to from the origin to dxdx, , dydy, , dzdz..angleangle, , dxdx, , dydy, and , and dzdz can be floats or can be floats or doublesdoubles
![Page 21: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/21.jpg)
RotationRotation
• For example to rotate a cube 45 For example to rotate a cube 45 degrees…degrees…
glMatrixMode(GL_MODELVIEW);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glLoadIdentity();glRotatef(45.0, 0.0, 0.0, 1.0);glRotatef(45.0, 0.0, 0.0, 1.0);glutWireCube(0.5);glutWireCube(0.5);
assumes the cube is centered on the originassumes the cube is centered on the origin
![Page 22: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/22.jpg)
RotationRotation• What it if is not centered on the What it if is not centered on the
origin?…origin?…
glMatrixMode(GL_MODELVIEW);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glLoadIdentity();glTranslate(cur_x, cur_y, cur_z);glTranslate(cur_x, cur_y, cur_z);glRotate(rot_angle, dx, dy, dz);glRotate(rot_angle, dx, dy, dz);glTranslate(-cur_x, -cur_y, -cur_z);glTranslate(-cur_x, -cur_y, -cur_z);
……what is going on in this code?what is going on in this code?
![Page 23: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/23.jpg)
ScalingScaling
void glScale{f|d}(TYPE sx, TYPE sy, void glScale{f|d}(TYPE sx, TYPE sy, TYPE sz);TYPE sz);
Scales or resizes objects based on sx, sy, Scales or resizes objects based on sx, sy, and sz scaling factors.and sz scaling factors.or…or…more techically… creates a transformation more techically… creates a transformation matrix to scale by sx, sy, and szmatrix to scale by sx, sy, and sz
![Page 24: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/24.jpg)
ScalingScaling
• Scaling assumes that the object is at Scaling assumes that the object is at the origin.the origin.
• If not at the origin use technique as If not at the origin use technique as was used in the rotation example to was used in the rotation example to translate, scale and translate back.translate, scale and translate back.
![Page 25: CS 470 Introduction to Computer Graphics](https://reader036.fdocuments.net/reader036/viewer/2022062322/568147ab550346895db4e653/html5/thumbnails/25.jpg)