Sei sulla pagina 1di 36

GROUP PRESENTATION

COMPUTER GRAPHICS

COURSE WORK 1

TOPIC: 2D and 3D Viewing
Coordinate Systems
Screen Coordinate system
World Coordinate system
World window
Viewport
Window to viewport mapping
min
xw
max
xw
min
yw
max
yw
Clipping Window
Screen Coordinate System
- 2D Regular Cartesian Grid
- Origin (0,0) at lower left
corner (OpenGL convention)
- Horizontal axis x
Vertical axis y
- Pixels are defined at the grid
intersections
- This coordinate system is defined
relative to the display window origin
(OpenGL: the lower left corner
of the window)

(0,0)
y
x
(2,2)
World Coordinate System
Application specific difficult to work directly in screen
coordinates
It would be nice if we can use application specific coordinates
20 feet
10 feet
World Window
World window a rectangular region in the world that is to be
displayed
Define by

W_L, W_R, W_B, W_T
W_L
W_R
W_B
W_T
Use OpenGL command:

gluOrtho2D(left,right,bottom, top)
Viewport
The rectangular region in the screen for displaying the
graphical objects defined in the world window
Defined in the screen coordinate system
V_L V_R
V_B
V_T
glViewport(int left, int bottom,
int (right-left),
int (top-bottom));

call this function before drawing
(calling glBegin() and
glEnd() )
To draw in world coordinate system
Two tasks need to be done
Define a rectangular world window
(call an OpenGL function)
Define a viewport (call an OpenGL function)
Perform window to viewport mapping
(OpenGL internals will do this for you)
2D VIEWING
Mapping of a 2D world coordinate system to device coordinates
is called a two-dimensional viewing transformation.
This mapping occurs in several steps






usually, in 2D, viewing coordinates and world coordinates are
the same.
Clipping is often done in normalized coordinates.


C
We use modeling coordinates for individual parts of a 2D scene. For
example, your polygon function put the polygon at the origin with
a radius of 1. Those were modeling coordinates.
World coordinates are used for organizing the parts into a scene.
View coordinates are the coordinates within the desired viewport
which is somewhere within the window you are using.
Normalized coordinates are between 0 and 1 or -1 and 1 in each
direction. They are used to make the viewing process independent of the
output device.
Device coordinates are relative to the actual display area on whatever
device you are using.
2D Coordinate Systems




The clipping window is the section of the 2D scene that is selected for
viewing.
The display window is where the scene will be viewed.
The viewport controls the placement of the scene within the display
window.
Clipping Window
The clipping window can, in principle, be any shape, size and
orientation.
Rectangular clipping areas that are aligned to the world coordinate axes
are the easiest to clip.
A rotated rectangular clipping area can be transformed by translation
and rotation to make it easy to clip.





Most graphics packages allow you to specify a standard, rectangular
clipping window.
Normalization
2D Viewing in OpenGL
The basic library does everything in 3D.
The 3D routines can be adapted to 2D
A viewport function is provided.
To define clipping windows and viewports, first go to
projection mode.
glMatrixMode( GL_PROJECTION);
If necessary, use glLoadIdentity to initialize the matrix.
A 2D clipping window can be defined with
gluOtrho2D( xwmin,xwmax, ywmin,ywmax)
Normalized coordinates are from 1 to 1 in OpenGL. This is
the default
clipping window.
Viewports in OpenGL
To specify the viewport parameters, do
glViewport( xvmin, yvmin, vpWidth, vpHeight)
with parameters given in integer screen coordinates. The
first two give the position of the lower left corner relative to
the lower left corner of the display window.
By default, the viewport is the entire window. window.
You can create multiple viewports within a window.
To get the parameters for the active viewport, use
glGetIntegerv( GL_VIEWPORT, vpArray);
where vpArray is a 4 element integer array. The contents
of vpArray after the function returns will be the same as the
arguments given to glViewport in the same order.
Display Windows in OpenGL
Dont forget to initialize GLUT with glutInit.
Create a window of specified size in a particular position on
the screen with
glutInitWindowPosition( xTopLeft, yTopLeft);
glutInitWindowSize( dwWidth, dwHeight);
glutCreateWindow( "Title");
Default position is (1,1) which allows the window system
to choose the
position.
Default size is 300 pixels square.
glutCreateWindow returns an integer id for the window. You
only need this
if you are displaying more than one window.
Display Mode and Color
Use glutInitDisplayMode( mode) to set the color mode,
number of buffers, depth mode, etc.
The constants for the various modes are OR-ed together (use
|) to make the mode argument.
Use glClearColor to specify the background color of the
window.
Using Multiple Windows
Create multiple windows by doing the same sequence of
steps for each window.
You need to keep track of the window ID to be able to go
back to a particular window.
glutSetWindow(windowID);
is used to make a particular window active.
You can destroy a window when you are done with it.
glutDestroyWindow( windowID);
The current window ID can be obtained using
glutGetWindow();
Window Behavior
Other behavior that can be controlled
Screen space
glutFullScreen();
glutIconifyWindow();
glutSetIconTitle();
Visibility
glutHideWindow();
glutShowWindow();
Depth in window order
glutPushWindow(); // send to back
glutPopWindow(); // bring to front
Modifying Window Properties
You can control how the window is reshaped by defining a
reshape function and setting it as the reshape callback using
glutReshapeFunc( winReshapeFunc);
You can modify the size and shape of the current window
using
glutPositionWindow( xNewTopLeft, yNewTopLeft);
glutReshapeWindow( newWidth, newHeight);
Change the window title with
glutSetWindowTitle( newTitle);
Specify the cursor with
glutSetCursorShape( shape);
Use symbolic constants like GLUT_CURSOR_UP_DOWN
to specify the shapes. (Exact
shapes available are system specific.)
Clipping Algorithms
Clipping eliminates the parts of a scene that arent going to be visible in
the
final display.
Clipping can be applied to various types of graphics objects.
Points this is basically trivial; check to see that the point
coordinates are
between the clipping boundaries.
Lines
Fill Areas
Curves
Text
The first three are standard in graphics packages.
Most of the algorithms apply to rectangular clipping regions.
Introduction to 3D viewing
3D is just like taking a photograph!
Position and orient your camera
Projection Transformation
Control the lens of the camera
Project the object from 3D world to 2D screen


Viewing Transformation (2)
Important camera parameters to specify
Camera (eye) position (Ex,Ey,Ez) in world coordinate
system
Center of interest (coi) (cx, cy, cz)
Orientation (which way is up?) View-up vector (Up_x,
Up_y, Up_z)
world
(cx, cy, cz)
(ex, ey, ez)
view up vector
(Up_x, Up_y, Up_z)
Viewing Transformation (3)
Transformation?
Form a camera (eye) coordinate frame












Transform objects from world to eye space
world
u
v
n
x
y
z
Eye coordinate frame
coi
world
u
v
n
x
y
z
(0,0,0)
coi
Viewing Transformation (4)
Eye space?






Transform to eye space can simplify many downstream
operations (such as projection) in the pipeline
(1,0,0)
(0,1,0)
(0,0,1)
Projection Transformation
Important things to control
Perspective or Orthographic
Field of view and image aspect ratio
Near and far clipping planes
Perspective Projection
Characterized by object foreshortening
- Objects appear to be larger if they are closer to the
camera
- This is what happens in the real world
Need:
Projection center
Projection plane
Projection: Connecting the object
to the projection center
projection plane
camera
Orthographic Projection
No foreshortening effect distance from camera
does not matter
The projection center is at infinite




Projection calculation just drop z coordinates
Field of View
Determine how much of the world is taken into the picture







The larger is the field view, the smaller is the object
projection size
x
y
z
y
z
q
field of view
center of projection
Near and Far Clipping Planes
Only objects between near and far planes are
drawn




Near plane + far plane + field of view =
Viewing Frustum





x
y
z
Near plane
Far plane
Viewing Frustum
3D counterpart of 2D world clip window






Objects outside the frustum are clipped
x
y
z
Near plane
Far plane
Viewing Frustum
gluPerspective(fovy, aspect, near, far)
Aspect ratio is used to calculate the window width
x
y
z
y
z
fovy
eye
near far
Aspect = w / h
w
h
glFrustum(left, right, bottom, top, near, far)
Or You can use this function in place of
gluPerspective()
x
y
z
left
right
bottom
top
near far
glOrtho(left, right, bottom, top, near, far)
For orthographic projection
x
y
z
left
right
bottom
top
near
far
3D viewing under the hood
Modeling Viewing Projection
Transformation Transformation Transformation


Viewport
Transformation


Display



Flexible Camera Control
gluLookAt() does not let you to control pitch
and yaw
you need to compute/maintain the vector by
yourself
And then calculate COI = Eye + (x,y,z)
before you can call gluLookAt().

Potrebbero piacerti anche