Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei...
-
Upload
lily-wilkins -
Category
Documents
-
view
232 -
download
0
Transcript of Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei...
Projections
Viewports
Windows can have separate viewports
void glViewport(GLint x, GLint y, GLsizei width, GLsizei height )
x, y - Specify the lower left corner of the viewport rectangle, in pixels
width, height - Specify the width and height of the viewport
Viewport Example Create two
viewports in one window
w=Window width h=Window height
//Left ViewportglViewport(0.0,0.0,w/2.0,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-w/4.0,w/4.0,-h/2.0,h/2.0,-1.0,1.0);glMatrixMode(GL_MODELVIEW);//Draw the scenedrawScene();
//Right ViewportglViewport(w/2.0,0.0,w/2.0,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-w/4.0,w/4.0,-h/2.0,h/2.0,-1.0,1.0);glMatrixMode(GL_MODELVIEW);//Draw the scenedrawScene();
Projections
Flattening a 3D virtual scene by projecting it onto a plane
The projected geometry is next rasterized into fragments
View Frustum
Frustum A portion of a solid, normally a cone or pyramid,
which lies between two parallel planes cutting the solid.
We are interested in Square frusta
Used to define the viewing volume
Near and Far Clipping Planes
Near and Far clipping planes “Clip out” geometry outside of them
This Geometry is not rendered
Orthographic Projection
Parallel projection where the view direction is orthogonal to the projection plane
Projected Objects do not get bigger/smaller when moved away/toward the viewer
Orthographic Example
//Left ViewportglViewport(0.0,0.0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-w/4.0,w/4.0,-h/2.0,h/2.0,0.0,1.0);
//Draw the sceneglMatrixMode(GL_MODELVIEW);drawScene();
void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);
Perspective Projection
Perspective projections have a viewpoint that is a finite distance from the projection plane
Objects get smaller as distance from viewer gets larger
Perspective Example
void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);
Perspective Example
Defined in “GL/glu.h” void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
//Left ViewportglViewport(0.0,0.0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0,w/h,1.0,20.0);
//Draw the sceneglMatrixMode(GL_MODELVIEW);drawScene();
Model -> View -> Projection
Modeling Transformation Used to position geometry in the scene
Viewing Transformation Used to position and orient the view of a scene
Projection Transformation Used to project a scene onto a viewing plane
glVertexModeling
Transformation
View Transformatio
n
Projection Transformatio
n
Setting up the view
Used to position and orient the view of a scene
Viewing transformation can be thought of as a camera viewing the scene Order of transformations that setup a camera
Position Orientation
gluLookAt
Defined in <gl/glu.h>
void gluLookAt( GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ )
eyeX, eyeY, eyeZ Specifies the position
of the eye point. centerX, centerY, centerZ
Specifies the position of the reference point.
upX, upY, upZ Specifies the direction
of the up vector.
gluLookAt continued
eyeX, eyeY, eyeZ Specifies the position
of the eye point. centerX, centerY, centerZ
Specifies the position of the reference point.
upX, upY, upZ Specifies the direction
of the up vector.
Depth Buffer
Additional component of the frame buffer
Holds the depth value of each fragment
Needs to be cleared like color buffer
Setting up Depth Test
OpenGL disables depth test by default glEnable(GL_DEPTH_TEST);
We have an additional part of the frame buffer that needs clearing glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
We need a drawing context that has a depth buffer glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
Depth Test Example
void display(void){ glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Setup state to start drawing glBegin(GL_TRIANGLES); glVertex3i(-100, 100, 1); glVertex3i(0, 100, 1); glVertex3i(-50, 50, 1);
glVertex3i(50, 50, 3); glVertex3i(100, 100, 3); glVertex3i(75, 20, 3); glEnd();}
int main(int argc, char **argv){ glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
glutCreateWindow("Depth Example"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0;}
Display code
GLUT initialization