7/30/2019 Win Viewing
1/39
Window toViewport Mapping
7/30/2019 Win Viewing
2/39
Acknowledgements
Some of the material for the slides were adapted fromRick Parent
Some of the images were taken from Hearn, Baker, and Carithers, Computer Graphics with
OpenGL
7/30/2019 Win Viewing
3/39
Figure 3-1 The transformation sequence from modeling coordinates to device coordinates for athree-dimensional scene. Object shapes can be individually defined in modeling-coordinate referencesystems. Then the shapes are positioned within the world-coordinate scene. Next, world-coordinatespecifications are transformed through the viewing pipeline to viewing and projection coordinates andthen to normalized coordinates. At the final step, individual device drivers transfer the normalized-coordinate representation of the scene to the output devices for display.
7/30/2019 Win Viewing
4/39
Coordinate Systems
Four Cartesian co-ordinates systems in computer Graphics.
1. Modeling co-ordinates
2. World co-ordinates
3. Normalized device co-ordinates
4. Device co-ordinates
Modeling coordinate is also known as local coordinate.
Ex: where individual object in a scene within separate coordinate referenceframes.
Each object has an origin (0,0)So the part of the objects are placed with reference to the objects origin.
In term of scale it is user defined, so, coordinate values can be any size.
7/30/2019 Win Viewing
5/39
Coordinate Systems
World Coordinate
The world coordinate system describes the relative positionsand orientations of every generated objects.
The scene has an origin (0,0).
The object in the scene are placed with reference to the scenesorigin.
World co-ordinate scale may be the same as the modeling co-ordinate scale or it may be different.
However, the coordinates values can be any size (similar toMC)
7/30/2019 Win Viewing
6/39
Coordinate Systems
Normalized Device Coordinate
Output devices have their own co-ordinates.
Co-ordinates values:
The x and y axis range from 0 to 1 All the x and y co-ordinates are floating point numbers in the
range of 0 to 1
This makes the system independent of the various devices
coordinates. This is handled internally by graphic system without user
awareness.
7/30/2019 Win Viewing
7/39
Coordinate Systems
Device Coordinates
Specific co-ordinates used by a device.
Pixels on a monitor
Points on a laser printer.
mm on a plotter.
The transformation based on the individual device is handledby computer system without user concern.
7/30/2019 Win Viewing
8/39
Coordinate Systems
World Coordinate System - This is object space or the space inwhich the application model is defined.
Screen Coordinate System - The space in which the image isdisplayed.
World Window (or clipping) - This is the rectangle in the world
defining the region that is to be displayed. Interface Window - The window opened on the raster graphics
screen in which the image will be displayed. Viewport - The rectangular portion of the interface window that
defines where the image will actually appear (usually the entire
interface window but in some cases modified to be a portion of theinterface window). Viewing Transformation - The process of mapping a world
window in World Coordinates to the Viewport.
7/30/2019 Win Viewing
9/39
World Coordinate System
Application specific difficult to workdirectly in screen coordinates
20 feet
10 feet
7/30/2019 Win Viewing
10/39
Screen Coordinate System
- 2D Regular Cartesian Grid- Origin (0,0) at lower left
corner (OpenGL convention)- Horizontal axis xVertical axis y
- Pixels are defined at the grid
intersections- This coordinate system is definedrelative to the display window origin(OpenGL: the lower left cornerof the window)
(0,0)
y
x
(2,2)
7/30/2019 Win Viewing
11/39
Define a world window
7/30/2019 Win Viewing
12/39
World Window
World window a rectangular region in theworld 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)glOrtho(left,right, bottom, top,near, far)
7/30/2019 Win Viewing
13/39
Viewport
The rectangular region in the screen for displayingthe 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));
http://../Documents%20and%20Settings/hwshen/Local%20Settings/Temporary%20Internet%20Files/Graphics/myTests/lab1/lab1/Debug/lab1.exehttp://../Documents%20and%20Settings/hwshen/Local%20Settings/Temporary%20Internet%20Files/Graphics/myTests/lab1/lab1/Debug/lab1.exehttp://../Documents%20and%20Settings/hwshen/Local%20Settings/Temporary%20Internet%20Files/Graphics/myTests/lab1/lab1/Debug/lab1.exehttp://../Documents%20and%20Settings/hwshen/Local%20Settings/Temporary%20Internet%20Files/Graphics/myTests/lab1/lab1/Debug/lab1.exehttp://../Documents%20and%20Settings/hwshen/Local%20Settings/Temporary%20Internet%20Files/Graphics/myTests/lab1/lab1/Debug/lab1.exe7/30/2019 Win Viewing
14/39
To draw in world coordinate system
Two tasks need to be done
Define a rectangularworld window(call an OpenGL function)
Define a viewport (call an OpenGL function)
Perform window to viewport mapping(OpenGL internals will do this for you)
7/30/2019 Win Viewing
15/39
Window to viewport mapping
Window-to-Viewport mapping is the process ofmapping or transforming a two-dimensional,world-coordinate scene to device coordinates. In
particular, objects inside the world or clippingwindow are mapped to the viewport. Theviewport is displayed in the interface window onthe screen. In other words, the clipping window is
used to select the part of the scene that is to bedisplayed. The viewport then positions the sceneon the output device.
7/30/2019 Win Viewing
16/39
Window to viewport mapping
The objects in the world window will then
be drawn onto the viewport
(x,y)
(Sx, Sy)
World windowviewport
7/30/2019 Win Viewing
17/39
Window to viewport mapping
How to calculate (sx, sy) from (x,y)?
(x,y)
(Sx, Sy)
7/30/2019 Win Viewing
18/39
Window to viewport mapping
First thing to remember you dont need todo it by yourself. OpenGL will do it for you
You just need to define the viewport (withglViewport()), and the world window (withgluOrtho2D())
But we will look under the hood
7/30/2019 Win Viewing
19/39
Also, one thing to remember
A practical OpenGL issue
Before calling gluOrtho2D(), you need to havethe following two lines of code
glMatrixMode(GL_PROJECTION);
glLoadIdentity();gluOrtho2D(Left, Right, Bottom, Top);
7/30/2019 Win Viewing
20/39
Window to viewport mapping
Things that are given:
The world window (W_L, W_R, W_B, W_T) The viewport (V_L, V_R, V_B, V_T)
A point (x,y) in the world coordinate system
Calculate the corresponding point (sx, sy) inthe screen coordinate system
7/30/2019 Win Viewing
21/39
7/30/2019 Win Viewing
22/39
Window to viewport mapping
(x,y) (sx,sy)
(x W_L) / (W_R W_L) = (sx V_L) / (V_R V_L)
(y - W_B) / (W_T W_B) = (sy V_B) / (V_T V_B)
sx = x * (V_R-V_L)/(W_R-W_L) - W_L * (V_R V_L)/(W_R-W_L) + V_L
sy = y * (V_T-V_B)/(W_T-W_B) W_B * (V_T-V_B)/(W_T-W_B) + V_B
7/30/2019 Win Viewing
23/39
Some practical issues
How to set up an appropriate world window
automatically? How to zoom in the picture?
How to set up an appropriate viewport, so
that the picture is not going to be distorted?
7/30/2019 Win Viewing
24/39
World window setup
The basic idea is to see all the objects in the
world This can just be your initial view, and the user
can change it later
How to achieve it?
7/30/2019 Win Viewing
25/39
World window set up
Find the world coordinates extent that willcover the entire scene
min X max X
min Y
max Y
7/30/2019 Win Viewing
26/39
Zoom into the picture
Shrink your world window call gluOrtho2D() with a new range
Viewport
7/30/2019 Win Viewing
27/39
Non-distorted viewport setup
Distortion happens when
World window and display window havedifferent aspect ratios
Aspect ratio?
R = W / H
7/30/2019 Win Viewing
28/39
Compare aspect ratios
World window
Aspect Ratio = R
Display window
Aspect Ratio = W / H
W
H
R > W / H
7/30/2019 Win Viewing
29/39
Match aspect ratios
World window
Aspect Ratio = R
Display window
Aspect Ratio = W / H
W
H
R > W / H
R ?
7/30/2019 Win Viewing
30/39
Match aspect ratios
World window
Aspect Ratio = R
Display window
Aspect Ratio = W / H
W
H
R > W / H
R W/R
glViewport(0, 0, W, W/R)
7/30/2019 Win Viewing
31/39
Compare aspect ratios
World window
Aspect Ratio = R
Display window
Aspect Ratio = W / H
W
H
R < W / H
7/30/2019 Win Viewing
32/39
Match aspect ratios
World window
Aspect Ratio = R
Display window
Aspect Ratio = W / H
W
H
R < W / H
?
7/30/2019 Win Viewing
33/39
Match aspect ratios
World window
Aspect Ratio = R
Display window
Aspect Ratio = W / H
W
H
H * R
R < W / H glViewport(0, 0, H*R, H)
7/30/2019 Win Viewing
34/39
When to call glViewport() ?
Initialization
Default: same as the window size
When the user resizes the display window
Two places:
7/30/2019 Win Viewing
35/39
Resize (Reshape) window
Void main(int argc, char** argv){
glutDisplayFunc(display);glutReshapeFunc(resize);
glutKeyboardFunc(key);
}
void resize () a functionprovided by you. It will becalled when the windowchanges size.
7/30/2019 Win Viewing
36/39
Resize (reshape) window
Void resize(int W, int H){
glViewport(0,0,W, H);}
This is done by default in GLUT
You can use the call to makesure the aspect ratio isfixed that we just discussed.
7/30/2019 Win Viewing
37/39
An Example
#include
void drawSquare(void)
{
glBegin(GL_POLYGON); //initiates polygon and starts list of vertices
glVertex3f(5,5,0); //defines vertex at position x=5, y=5, z=0
glVertex3f(10,5,0); //defines vertex at position x=10, y=5, z=0
glVertex3f(10,10,0); //defines vertex at position x=10, y=10, z=0
glVertex3f(5,10,0); //defines vertex at position x=5, y=10, z=0
glEnd(); //terminates the list of vertices for polygon
} void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); //clears window to prepare it for drawing
//sets the Red, Green, Blue float values to full green for the color in//which to drawglColor3f(0.0, 1.0, 0.0);
//specifies a 100x100 viewport in pixels whose lower-left corner is at//(100, 0)measured from the origin of the window
glViewport(100, 0, 100, 100);
drawSquare();
glFlush(); /forces any buffered OpenGL commands to execute
}
7/30/2019 Win Viewing
38/39
An Example
int main(int argc, char **argv)
{
/*initializes the OpenGL utilities toolkit,taking as parameters the arguments from the main
function*/
glutInit(&argc, argv);
/* the buffering mode, the color, and the depth mode, for display */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);/*specifies the initial top-left corner of the window as 100x100 in pixels*/
glutInitWindowPosition(100,100);/*specifies the initial height and width of the window as 200x200 in pixels*/
glutInitWindowSize(200,200);/*creates a window on the display with title "square"*/
glutCreateWindow("square");
/*sets the Red, Green, Blue, Alpha clear color to full red to use when clearing the color buffer */
glClearColor(1.0, 0.0, 0.0, 0.0);
/*specifies that the projection matrix will be the matrix affected by subsequent transformations*/
glMatrixMode(GL_PROJECTION);
7/30/2019 Win Viewing
39/39
An Example
/*sets the current transformation matrix (the projection matrix) to an
identity matrix */
glLoadIdentity();
//defines orthographic two-dimensional viewing
glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
/*registers display() as the call back function to redraw the window */glutDisplayFunc(display);/*causes the program to enter an event-processing loop*/
glutMainLoop();
return 0;
}