Presentation is loading. Please wait.

Presentation is loading. Please wait.

2D Coordinate Systems and Drawing

Similar presentations


Presentation on theme: "2D Coordinate Systems and Drawing"— Presentation transcript:

1 2D Coordinate Systems and Drawing

2 Coordinate Systems Screen coordinate system World coordinate system
World window Viewport Window to viewport mapping

3 Screen Coordinate System
2D regular Cartesian grid Origin (0, 0) at the lower left (OpenGL convention) Pixels are defined at intersections Defined relatively to the display window y Your Monitor Screen x (0, 0) (2, 2)

4 Screen Coordinate System
Not easy to use in practice Window size can vary

5 Screen Coordinate System
Not easy to use in practice Window size can vary People prefer to specify objects in their actual sizes

6 Objects should be specified independent of the screen coordinate system.

7 Objects (in world coordinate system)
2D Drawing World Objects (in world coordinate system) World Window Screen Window Screen

8 Define a world window

9 Define a world window A rectangular region in the world that is to be displayed (in world coordinate system) gluOrtho2D(W_L, W_R, W_B, W_T) OpenGL function: 2D orthogonal projection

10 Viewport A rectangular region in the screen for display (in screen coordinate system) glViewport(V_L, V_B, V_R-V_L, V_T-V_B)

11 An Example (1, 1) (-1, -1) (400, 300) (350, 250) (50, 50) (0, 0)
(1, 1) void DrawQuad() { glViewport(50, 50, 300, 200); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1, 1, -1, 1); glBegin(GL_QUADS); glVertex2f(-0.5, -0.5); glVertex2f( 0.5, -0.5); glVertex2f( 0.5, 0.5); glVertex2f(-0.5, 0.5); glEnd(); } (-0.5, 0.5) (0.5, 0.5) (-1, -1) (400, 300) (350, 250) (-0.5, -0.5) (0.5, -0.5) (50, 50) (0, 0)

12 Remember to… Remember to specify the matrix type: void DrawQuad() {
glViewport(50, 50, 300, 200); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1, 1, -1, 1); glBegin(GL_QUADS); glVertex2f(-0.5, -0.5); glVertex2f( 0.5, -0.5); glVertex2f( 0.5, 0.5); glVertex2f(-0.5, 0.5); glEnd(); }

13 How to achieve this mapping?
gluOrtho2D(-1, 1, -1, 1); glViewport(50, 50, 300, 200); No need to do mapping by yourself, just call those two functions!

14 The problem Input: Output: World window: W_L, W_R, W_B, W_T
Viewport: V_L, V_R, V_B, V_T Some point (x, y) in the world coordinate system Output: (sx, sy) on the screen

15 Basic Information V_T – V_B W_T – W_B W_R - W_L V_R - V_L

16 Keep the Same Ratio sx - V_L x - W_L W_R - W_L V_R - V_L
(x - W_L) / (W_R - W_L) = (sx - V_L) / (V_R - V_L) sx = (x - W_L)(V_R - V_L) / (W_R - W_L) +V_L

17 Keep the Same Ratio V_T – V_B W_T – W_B y – W_B sy – V_B
(y – W_B) / (W_T – W_B) = (sy – V_B) / (V_T – V_B) sy = (y – W_B)(V_T - V_B) / (W_T - W_B) +V_B

18 Practical Questions How to initialize How to transform
The world window The viewport How to transform Translation Zoom in, zoom out…

19 A simple way to initialize the world window
Cover everything

20 Zoom In/Out Call gluOrtho2D() with new ranges

21 Distortion Example (1, 1) (-1, -1) (400, 300) (350, 250) (50, 50)
(1, 1) void DrawQuad() { glViewport(50, 50, 300, 200); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1, 1, -1, 1); glBegin(GL_QUADS); glVertex2f(-0.5, -0.5); glVertex2f( 0.5, -0.5); glVertex2f( 0.5, 0.5); glVertex2f(-0.5, 0.5); glEnd(); } (-0.5, 0.5) (0.5, 0.5) (-1, -1) (400, 300) (350, 250) (-0.5, -0.5) (0.5, -0.5) (50, 50) (0, 0)

22 Aspect Ratio r= width/height (0.5, 0.5) height width

23 Distortion happens when aspect ratios are not consistent
(1, 1) void DrawQuad() { glViewport(50, 50, 300, 200); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1, 1, -1, 1); glBegin(GL_QUADS); glVertex2f(-0.5, -0.5); glVertex2f( 0.5, -0.5); glVertex2f( 0.5, 0.5); glVertex2f(-0.5, 0.5); glEnd(); } (-0.5, 0.5) (0.5, 0.5) (-1, -1) (400, 300) (350, 250) (-0.5, -0.5) (0.5, -0.5) (50, 50) (0, 0)

24 Two solutions (-0.5, 0.5) (0.5, 0.5) (-0.5, 0.5) (0.5, 0.5)
(-0.5, -0.5) (0.5, -0.5) (-0.5, -0.5) (0.5, -0.5)

25 Where to define viewport?
Two places Initialization: the same size as the whole window Every time the user resizes the window Call Viewport in your resize callback function

26 Example world_height (center_x, center_y) world_width
world_height=world_width*view_port_height/view_port_width

27 Example world_height (center_x, center_y) world_width
W_L=center_x-world_width/2 W_B=center_y+world_height/2 W_R=center_x+world_width/2 W_T=center_y+world_height/2


Download ppt "2D Coordinate Systems and Drawing"

Similar presentations


Ads by Google