Drawing and Coordinate Systems
Coordinate Systems






Screen Coordinate system
World Coordinate system
World window
Viewport
Window to viewport ma...
Screen Coordinate System
Glut

OpenGL
(0,0)
Screen Coordinate System
- 2D Regular Cartesian Grid
- Origin (0,0) at lower left
corner (OpenGL convention)
- Horizontal ...
World Coordinate System


Screen coordinate system is not easy to
use

10 feet
20 feet
World Coordinate System


Another example:
plot a sinc function:
sinc(x) = sin(PI*x)/PI*x
x = -4 .. +4
World Coordinate System


It would be nice if we can use
application specific coordinates –
world coordinate system
glBeg...
Define a world window
World Window


World window – a rectangular region in
the world that limits our view
Define by

W_T

W_L, W_R, W_B, W_T

...
Viewport




The rectangular region in the screen that
maps to our world window
Defined in the window’s (or control’s)
c...
To draw in world coordinate system
Two tasks need to be done








Define a rectangular world window
(call an OpenGL...
A simple example
DrawQuad()
(300,200)
{
glViewport(0,0,300,200);
glMatrixMode(GL_PROJECTION);
glLoadIndentity();
glOrtho2D...
Window to viewport mapping


The objects in the world window will
then be drawn onto the viewport
(x,y)

World window

vi...
Window to viewport mapping


How to calculate (sx, sy) from (x,y)?

(x,y)
(Sx, Sy)
Window to viewport mapping


First thing to remember – you don’t
need to do it by yourself. OpenGL will
do it for you


...
Also, one thing to remember …


A practical OpenGL issue


Before calling gluOrtho2D(), you need to
have the following t...
Window to viewport mapping


Things that are given:






The world window (W_L, W_R, W_B, W_T)
The viewport (V_L, V_...
Window to viewport mapping


Basic principle: the mapping should be
proportional
(sx,sy)

(x,y)

(x – W_L) / (W_R – W_L)
...
Window to viewport mapping
(sx,sy)

(x,y)

(x – W_L) / (W_R – W_L)

=

(sx – V_L) / (V_R – V_L)

(y - W_B) / (W_T – W_B)

...
Some practical issues





How to set up an appropriate world
window automatically?
How to zoom into the picture?
How t...
World window setup


The basic idea is to see all the objects
in the world




This can just be your initial view, and ...
World window set up


Find the world coordinates extent that will
cover the entire scene (the bounding box )
max Y

min Y...
Zoom into the picture
Shrink your world window – call gluOrtho2D() with a new range

Viewport
Non-distorted viewport setup






Distortion happens when …
World window and display window have
different aspect rat...
Fixing the aspect ratio


Method I – Fixed camera view







Limit the viewport to a portion of the
window. (covered...
Compare aspect ratios
H

W

World window

Display window

Aspect Ratio = R

Aspect Ratio = W / H

R> W/H
Match aspect ratios
H

R

?
W

World window

Display window

Aspect Ratio = R

Aspect Ratio = W / H

R> W/H
Match aspect ratios
H

R
World window
Aspect Ratio = R

R> W/H

W/R
W

Display window
Aspect Ratio = W / H

glViewport(0, ...
Compare aspect ratios
H

W

World window

Display window

Aspect Ratio = R

Aspect Ratio = W / H

R< W/H
Match aspect ratios
?
H

W

World window

Display window

Aspect Ratio = R

Aspect Ratio = W / H

R< W/H
Match aspect ratios
H*R
H

World window
Aspect Ratio = R

R< W/H

W
Display window
Aspect Ratio = W / H

glViewport(0, 0, ...
When to call glViewport() ?





Initialization
When the user resizes the display
window.
New type of camera? 35mm, 70m...
Resize event
Void resize(int W, int H)
{
glViewport(0,0,W, H);
}
You can provide your own to make sure the
aspect ratio is...
Put it all together
DrawQuad()
(300,200)
{
glViewport(0,0,300,200);
glMatrixMode(GL_PROJECTION);
glLoadIndentity();
glOrth...
More Viewports


Viewports can also be thought of as clip
windows. This can be useful for:






User interaction
St...
Upcoming SlideShare
Loading in...5
×

computer graphics

368

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
368
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

computer graphics

  1. 1. Drawing and Coordinate Systems
  2. 2. Coordinate Systems      Screen Coordinate system World Coordinate system World window Viewport Window to viewport mapping
  3. 3. Screen Coordinate System Glut OpenGL (0,0)
  4. 4. 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 (0,0) relative to the display window origin (OpenGL: the lower left corner of the window) y x (2,2)
  5. 5. World Coordinate System  Screen coordinate system is not easy to use 10 feet 20 feet
  6. 6. World Coordinate System  Another example: plot a sinc function: sinc(x) = sin(PI*x)/PI*x x = -4 .. +4
  7. 7. World Coordinate System  It would be nice if we can use application specific coordinates – world coordinate system glBegin(GL_LINE_STRIP); for (x = -4.0; x <4.0; x+=0.1){ Glfloat y = sin(3.14 * x) / (3.14 * x); glVertex2f (x,y); } glEnd();
  8. 8. Define a world window
  9. 9. World Window  World window – a rectangular region in the world that limits our view Define by W_T W_L, W_R, W_B, W_T W_B Use OpenGL command: W_L W_R gluOrtho2D(left, right, bottom, top)
  10. 10. Viewport   The rectangular region in the screen that maps to our world window Defined in the window’s (or control’s) coordinate system glViewport(int left, int bottom, int (right-left), int (top-bottom)); V_T V_B V_L V_R
  11. 11. 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)
  12. 12. A simple example DrawQuad() (300,200) { glViewport(0,0,300,200); glMatrixMode(GL_PROJECTION); glLoadIndentity(); glOrtho2D(-1,1,-1,1); glBegin(GL_QUADS); glColor3f(1,1,0); glVertex2i(-0.5,-0.5); (0,0) glVertex2i(+0.5,-0.5); viewport glVertex2i(+0.5,+0.5); glVertex2i(-0.5,+0.5); How big is the quad? glEnd(); }
  13. 13. Window to viewport mapping  The objects in the world window will then be drawn onto the viewport (x,y) World window viewport (Sx, Sy)
  14. 14. Window to viewport mapping  How to calculate (sx, sy) from (x,y)? (x,y) (Sx, Sy)
  15. 15. Window to viewport mapping  First thing to remember – you don’t need to do it by yourself. OpenGL will do it for you   You just need to define the viewport (with glViewport()), and the world window (with gluOrtho2D()) But we will look ‘under the hood’
  16. 16. Also, one thing to remember …  A practical OpenGL issue  Before calling gluOrtho2D(), you need to have the following two lines of code – glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(Left, Right, Bottom, Top);
  17. 17. 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) in the screen coordinate system
  18. 18. Window to viewport mapping  Basic principle: the mapping should be proportional (sx,sy) (x,y) (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)
  19. 19. Window to viewport mapping (sx,sy) (x,y) (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 - W_L) * (V_R-V_L)/(W_R-W_L) + V_L sy = (y - W_B) * (V_T-V_B)/(W_T-W_B) + V_B
  20. 20. Some practical issues    How to set up an appropriate world window automatically? How to zoom into the picture? How to set up an appropriate viewport, so that the picture is not going to be distorted?
  21. 21. 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?
  22. 22. World window set up  Find the world coordinates extent that will cover the entire scene (the bounding box ) max Y min Y min X max X
  23. 23. Zoom into the picture Shrink your world window – call gluOrtho2D() with a new range Viewport
  24. 24. Non-distorted viewport setup     Distortion happens when … World window and display window have different aspect ratios Aspect ratio? R=W/H
  25. 25. Fixing the aspect ratio  Method I – Fixed camera view     Limit the viewport to a portion of the window. (covered next) Constrain the user’s resizing ability. Adjust the window (or control) size. Method II – Adjusting the scale to compensate for a non-square window.  We will cover this when we look at 3D.
  26. 26. Compare aspect ratios H W World window Display window Aspect Ratio = R Aspect Ratio = W / H R> W/H
  27. 27. Match aspect ratios H R ? W World window Display window Aspect Ratio = R Aspect Ratio = W / H R> W/H
  28. 28. Match aspect ratios H R World window Aspect Ratio = R R> W/H W/R W Display window Aspect Ratio = W / H glViewport(0, 0, W, W/R)
  29. 29. Compare aspect ratios H W World window Display window Aspect Ratio = R Aspect Ratio = W / H R< W/H
  30. 30. Match aspect ratios ? H W World window Display window Aspect Ratio = R Aspect Ratio = W / H R< W/H
  31. 31. Match aspect ratios H*R H World window Aspect Ratio = R R< W/H W Display window Aspect Ratio = W / H glViewport(0, 0, H*R, H)
  32. 32. When to call glViewport() ?    Initialization When the user resizes the display window. New type of camera? 35mm, 70mm, … Note: Resize event is actually called on initialization, but your callback may not have been connected at this time.
  33. 33. Resize event Void resize(int W, int H) { glViewport(0,0,W, H); } You can provide your own to make sure the aspect ratio is fixed.
  34. 34. Put it all together DrawQuad() (300,200) { glViewport(0,0,300,200); glMatrixMode(GL_PROJECTION); glLoadIndentity(); glOrtho2D(-1,1,-1,1); glBegin(GL_QUADS); glColor3f(1,1,0); glVertex2i(-0.5,-0.5); (0,0) glVertex2i(+0.5,-0.5); viewport glVertex2i(+0.5,+0.5); glVertex2i(-0.5,+0.5); How big is the quad? glEnd(); }
  35. 35. More Viewports  Viewports can also be thought of as clip windows. This can be useful for:      User interaction Static camera – small moving object Limited field-of-view Occlusion culling Selection (picking)
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×