Drawing and Coordinate Systems. Coordinate Systems Screen Coordinate system World Coordinate system World window Viewport Window to viewport mapping.

Slides:



Advertisements
Similar presentations
CGPage: 1 We can define a window as a rectangular region of the world coordinate space, and the viewport as a rectangular region of the device coordinate.
Advertisements

Coordinate System.
Okay, you have learned … OpenGL drawing Viewport and World Window setup main() { glViewport(0,0,300,200); glMatrixMode(GL_PROJECTION); glLoadIndentity();
Okay, you have learned … OpenGL drawing Viewport and World Window setup main() { glViewport(0,0,300,200); glMatrixMode(GL_PROJECTION); glLoadIndentity();
Graphics Pipeline.
Computer Graphics Viewing.
H331: Computer Graphics Philip Dutré Department of Computer Science Wednesday, February 25.
CS 4731: Computer Graphics Lecture 4: 2D Graphic Systems
2D Viewing and Projection
© 2004, Tom Duff and George Ledin Jr1 Lectures OpenGL Introduction By Tom Duff Pixar Animation Studios Emeryville, California and George Ledin Jr Sonoma.
University of North Carolina at Greensboro
Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley Chapter 3 Additional Drawing Tools Ureerat Suksawatchon Department of Computer.
Informationsteknologi Monday, November 12, 2007Computer Graphics - Class 71 Today’s class Viewing transformation Menus Mandelbrot set and pixel drawing.
Viewing and Transformation
1 Computer Graphics Chapter 4 2D Viewing Algorithms.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
Sierpinski Gasket Program
Introduction to 3D viewing 3D is just like taking a photograph!
Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003.
CS 376 Introduction to Computer Graphics 02 / 12 / 2007 Instructor: Michael Eckmann.
1 Working with Callbacks Yuanfeng Zhou Shandong University.
WORKING WITH CALLBACKS Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Angel: Interactive.
The Viewing Pipeline (Chapter 4) 5/26/ Overview OpenGL viewing pipeline: OpenGL viewing pipeline: – Modelview matrix – Projection matrix Parallel.
Image Synthesis Rabie A. Ramadan, PhD 2. 2 Java OpenGL Using JOGL: Using JOGL: Wiki: You can download JOGL from.
GRAFIKA KOMPUTER ~ M. Ali Fauzi. Drawing 2 D Graphics.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 1 1.
2D Coordinate Systems and Drawing
OpenGL The Viewing Pipeline: Definition: a series of operations that are applied to the OpenGL matrices, in order to create a 2D representation from 3D.
Basic Perspective Projection Watt Section 5.2, some typos Define a focal distance, d, and shift the origin to be at that distance (note d is negative)
Computer Graphics I, Fall 2010 Working with Callbacks.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
Chap 3 Viewing and Transformation
Chi-Cheng Lin, Winona State University CS430 Computer Graphics Rendering Pipeline and Primitive Rasterization.
CS552: Computer Graphics Lecture 6: Viewing in 2D.
Coordinate Systems Lecture 1 Fri, Sep 2, The Coordinate Systems The points we create are transformed through a series of coordinate systems before.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley Chapter 3 Additional Drawing Tools PART I.
Chapter #03 More Drawing Tools 1. Viewport 2 Do not have use the entire window for the image: glViewport(x,y,w,h) Values in pixels (screen coordinates)
Introduction to Graphics Programming. Graphics API.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: Runs on everything, including.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.
OpenGL LAB III.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Working with Callbacks.
Working with Callbacks
Graphing in Calculator
Introduction to the Mouse
CSCE 441 Computer Graphics 3-D Viewing
Programming with OpenGL Part 2: Complete Programs
Chapter #03 More Drawing Tools.
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
Class 1: First Program.
WINDOWING AND CLIPPING
Working with Callbacks
Coordinate Systems and Transforming the Coordinates
WINDOWING AND CLIPPING
Last Time Canonical view pipeline Projection Local Coordinate Space
OpenGL program.
2D Graphics Lecture 4 Fri, Aug 31, 2007.
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Computer Graphics 3Practical Lesson
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Drawing and Coordinate Systems

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

Screen Coordinate System (0,0) OpenGL Glut

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 Screen coordinate system is not easy to use 20 feet 10 feet

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

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();

Define a world window

World Window World window – a rectangular region in the world that limits our view 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 that maps to our world window Defined in the window’s (or control’s) coordinate system V_L V_R V_B V_T glViewport(int left, int bottom, int (right-left), int (top-bottom));

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)

A simple example DrawQuad() { 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); glVertex2i(+0.5,-0.5); glVertex2i(+0.5,+0.5); glVertex2i(-0.5,+0.5); glEnd(); } (0,0) (300,200) viewport How big is the quad?

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

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 You just need to define the viewport (with glViewport()), and the world window (with gluOrtho2D()) But we will look ‘under the hood’

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);

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

Window to viewport mapping Basic principle: the mapping should be proportional (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)

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 - 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 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 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?

World window set up Find the world coordinates extent that will cover the entire scene (the bounding box) min Xmax X min Y max 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 ratios Aspect ratio? R = W / H

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.

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

Match aspect ratios World window Aspect Ratio = R Display window Aspect Ratio = W / H W H R > W / H R ?

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)

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

Match aspect ratios World window Aspect Ratio = R Display window Aspect Ratio = W / H W H R < W / H ?

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)

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.

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.

Put it all together DrawQuad() { 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); glVertex2i(+0.5,-0.5); glVertex2i(+0.5,+0.5); glVertex2i(-0.5,+0.5); glEnd(); } (0,0) (300,200) viewport How big is the quad?

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)