18
Projections

Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

Embed Size (px)

Citation preview

Page 1: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

Projections

Page 2: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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

Page 3: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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();

Page 4: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

Projections

Flattening a 3D virtual scene by projecting it onto a plane

The projected geometry is next rasterized into fragments

Page 5: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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

Page 6: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

Near and Far Clipping Planes

Near and Far clipping planes “Clip out” geometry outside of them

This Geometry is not rendered

Page 7: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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

Page 8: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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

Page 9: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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

Page 10: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

Perspective Example

void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);

Page 11: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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();

Page 12: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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

Page 13: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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

Page 14: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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.

Page 15: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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.

Page 16: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

Depth Buffer

Additional component of the frame buffer

Holds the depth value of each fragment

Needs to be cleared like color buffer

Page 17: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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

Page 18: Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower

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