Drawing and Coordinate Systems Coordinate Systems n n

  • Slides: 35
Download presentation
Drawing and Coordinate Systems

Drawing and Coordinate Systems

Coordinate Systems n n n Screen Coordinate system World window Viewport Window to viewport

Coordinate Systems n n n Screen Coordinate system World window Viewport Window to viewport mapping

Screen Coordinate System Glut Open. GL (0, 0)

Screen Coordinate System Glut Open. GL (0, 0)

Screen Coordinate System - 2 D Regular Cartesian Grid - Origin (0, 0) at

Screen Coordinate System - 2 D Regular Cartesian Grid - Origin (0, 0) at lower left corner (Open. GL 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 (Open. GL: the lower left corner of the window) y x (2, 2)

World Coordinate System n Screen coordinate system is not easy to use 10 feet

World Coordinate System n Screen coordinate system is not easy to use 10 feet 20 feet

World Coordinate System n Another example: plot a sinc function: sinc(x) = sin(PI*x)/PI*x x

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

World Coordinate System n It would be nice if we can use application specific

World Coordinate System n It would be nice if we can use application specific coordinates – world coordinate system gl. Begin(GL_LINE_STRIP); for (x = -4. 0; x <4. 0; x+=0. 1){ Glfloat y = sin(3. 14 * x) / (3. 14 * x); gl. Vertex 2 f (x, y); } gl. End();

Define a world window

Define a world window

World Window n World window – a rectangular region in the world that limits

World Window n 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 Open. GL command: W_L W_R glu. Ortho 2 D(left, right, bottom, top)

Viewport n n The rectangular region in the screen that maps to our world

Viewport n n The rectangular region in the screen that maps to our world window Defined in the window’s (or control’s) coordinate system gl. Viewport(int left, int bottom, int (right-left), int (top-bottom)); V_T V_B V_L V_R

To draw in world coordinate system Two tasks need to be done n n

To draw in world coordinate system Two tasks need to be done n n Define a rectangular world window (call an Open. GL function) Define a viewport (call an Open. GL function) Perform window to viewport mapping (Open. GL internals will do this for you)

A simple example Draw. Quad() { (300, 200) gl. Viewport(0, 0, 300, 200); gl.

A simple example Draw. Quad() { (300, 200) gl. Viewport(0, 0, 300, 200); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Indentity(); gl. Ortho 2 D(-1, 1, -1, 1); gl. Begin(GL_QUADS); gl. Color 3 f(1, 1, 0); gl. Vertex 2 i(-0. 5, -0. 5); (0, 0) gl. Vertex 2 i(+0. 5, -0. 5); viewport gl. Vertex 2 i(+0. 5, +0. 5); gl. Vertex 2 i(-0. 5, +0. 5); How big is the quad? gl. End(); }

Window to viewport mapping n The objects in the world window will then be

Window to viewport mapping n The objects in the world window will then be drawn onto the viewport (x, y) World window viewport (Sx, Sy)

Window to viewport mapping n How to calculate (sx, sy) from (x, y)? (x,

Window to viewport mapping n How to calculate (sx, sy) from (x, y)? (x, y) (Sx, Sy)

Window to viewport mapping n First thing to remember – you don’t need to

Window to viewport mapping n First thing to remember – you don’t need to do it by yourself. Open. GL will do it for you n n You just need to define the viewport (with gl. Viewport()), and the world window (with glu. Ortho 2 D()) But we will look ‘under the hood’

Also, one thing to remember … n A practical Open. GL issue n Before

Also, one thing to remember … n A practical Open. GL issue n Before calling glu. Ortho 2 D(), you need to have the following two lines of code – gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); glu. Ortho 2 D(Left, Right, Bottom, Top);

Window to viewport mapping n Things that are given: n n The world window

Window to viewport mapping n Things that are given: n n 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

Window to viewport mapping n Basic principle: the mapping should be proportional (sx, sy)

Window to viewport mapping n Basic principle: the mapping should be proportional (sx, sy) (x, y) (x – W_L) / (W_R – W_L) (y - W_B) / (W_T – W_B) = = (sx – V_L) / (V_R – V_L) (sy – V_B) / (V_T – V_B)

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

Window to viewport mapping (sx, sy) (x, y) (x – W_L) / (W_R – W_L) (y - W_B) / (W_T – W_B) = = (sx – V_L) / (V_R – V_L) (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

Some practical issues n n n How to set up an appropriate world window

Some practical issues n n n 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?

World window setup n The basic idea is to see all the objects in

World window setup n The basic idea is to see all the objects in the world n n This can just be your initial view, and the user can change it later How to achieve it?

World window set up n Find the world coordinates extent that will cover the

World window set up n Find the world coordinates extent that will cover the entire scene (the bounding box) max Y min X max X

Zoom into the picture Shrink your world window – call glu. Ortho 2 D()

Zoom into the picture Shrink your world window – call glu. Ortho 2 D() with a new range Viewport

Non-distorted viewport setup n n Distortion happens when … World window and display window

Non-distorted viewport setup n n Distortion happens when … World window and display window have different aspect ratios Aspect ratio? R=W/H

Fixing the aspect ratio n Method I – Fixed camera view n n Limit

Fixing the aspect ratio n Method I – Fixed camera view n n 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. n We will cover this when we look at 3 D.

Compare aspect ratios H W World window Display window Aspect Ratio = R Aspect

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 =

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

Match aspect ratios H R World window Aspect Ratio = R R> W/H W/R W Display window Aspect Ratio = W / H gl. Viewport(0, 0, W, W/R)

Compare aspect ratios H W World window Display window Aspect Ratio = R Aspect

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

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

Match aspect ratios H*R H World window Aspect Ratio = R R< W/H W Display window Aspect Ratio = W / H gl. Viewport(0, 0, H*R, H)

When to call gl. Viewport() ? n n n Initialization When the user resizes

When to call gl. Viewport() ? n n n Initialization When the user resizes the display window. New type of camera? 35 mm, 70 mm, … Note: Resize event is actually called on initialization, but your callback may not have been connected at this time.

Resize event Void resize(int W, int H) { gl. Viewport(0, 0, W, H); }

Resize event Void resize(int W, int H) { gl. Viewport(0, 0, W, H); } You can provide your own to make sure the aspect ratio is fixed.

Put it all together Draw. Quad() { (300, 200) gl. Viewport(0, 0, 300, 200);

Put it all together Draw. Quad() { (300, 200) gl. Viewport(0, 0, 300, 200); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Indentity(); gl. Ortho 2 D(-1, 1, -1, 1); gl. Begin(GL_QUADS); gl. Color 3 f(1, 1, 0); gl. Vertex 2 i(-0. 5, -0. 5); (0, 0) gl. Vertex 2 i(+0. 5, -0. 5); viewport gl. Vertex 2 i(+0. 5, +0. 5); gl. Vertex 2 i(-0. 5, +0. 5); How big is the quad? gl. End(); }

More Viewports n Viewports can also be thought of as clip windows. This can

More Viewports n Viewports can also be thought of as clip windows. This can be useful for: n n n User interaction Static camera – small moving object Limited field-of-view Occlusion culling Selection (picking)