# Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2

date post

13-Dec-2015Category

## Documents

view

220download

2

Embed Size (px)

### Transcript of Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2

- Slide 1

Computer Graphics Rendering 2D Geometry CO2409 Computer Graphics Week 2 Slide 2 Todays Lecture 1.2D Geometry 2.Coordinate Systems & Viewports 3.Rendering Points and Lines 4.Polygons and Circles 5.Anti-Aliasing Slide 3 2D Geometry Basics Will look at 2D geometry represented by points and lines in two dimensional Cartesian space Represented on a graph with two axes, usually X (horizontal) and Y (vertical) Origin of the graph and of the 2D space is where the axes cross (X = Y = 0) Slide 4 2D Geometry Definitions I will often use informal English to describe 2D geometry. However, here are a couple of specific definitions that will be used: A vertex: a single point (plural = vertices) defined by coordinates on the axes An edge: a straight line joining two vertices A polygon: a single closed loop of edges Slide 5 Specifying 2D Geometry Example vertices: A(10, 20), B(30, 30), C(35, 15) Edges: AB, BC, AC Edges with direction (a form of vector, see later lectures),, Polygons: ABCA, or ABC (implies the final edge to A) Slide 6 Coordinate Systems / Spaces Define a coordinate system as a particular choices for: The location of the origin Orientation and scale of the axes This is also known as a space Can view the same geometry in different spaces: World space: an overview of all the geometry Viewport space: where the geometry is relative to the viewing window (see next slide) Will see more spaces later, an important concept Slide 7 Viewports A viewport (or window) is a rectangle of pixels representing a view into the world space A viewport has its own coordinate system, which may not match that of the geometry it shows The axes will usually be X horizontal & Y vertical But dont have to be rotated viewports The scale of the axes may be different The direction of the Y axis may differ. E.g. the geometry may be stored with +ve Y up, but the viewport has +ve Y down. The origin (usually in the corners or centre of the viewport) may not match the geometry origin. Slide 8 Viewport Example Example of changing coordinate system from world space to viewport space: P = (20,15) in world space. Where is P in viewport space? Slide 9 Rendering Points Rendering is the process of converting 2D or 3D geometry into pixels To render a vertex (a point): Convert vertex coordinates into viewport space Set the colour of the pixel at those coordinates The colour might be stored with the geometry, or we can use a fixed colour (e.g. black) In the previous diagram the vertex P would be rendered by setting the colour of the pixel at coordinates (266, 450) in the viewport Slide 10 Rendering Lines: 1 st Attempt A possible method to render an edge (a line): Convert the start and end vertex into viewport space Trace along this converted edge Colour each pixel traced over Unfortunately, this method creates thick lines. We need to colour fewer pixels to get a cleaner result. Slide 11 Rendering Lines: 2 nd Attempt Consider drawing a horizontal line with previous method: Trace along horizontally adjacent pixels, setting the colour of each Result is fine Adapt this for a diagonal line: Start at the left end of the line Move rightwards, colouring each pixel Periodically move up or down one pixel to create steps of the correct gradient Use variations of above for lines in different directions To go from start to end need to move right 9 pixels and down 4 pixels. So move down one pixel about every 9/4 pixels right Slide 12 Rendering Lines: Algorithms We will use a basic version of this algorithm to render lines in the lab Using floating point calculations A widely used method is Bresenhams Line Algorithm Wikipedia versionWikipedia version Highly efficient - only uses integer calculations We wont cover this, or other efficient 2D algorithms in this module as we will concentrate on higher level graphics work Many 2D algorithms like this are implemented in hardware now Slide 13 A polygon is a sequence of edges, so we render a polygon outline by simply rendering each edge We only need the set of vertices defining the polygon Axis-aligned squares and rectangles are special cases of a polygon and can be rendered more efficiently. Horizontal / vertical lines are simple cases But how to render a filled polygon? Rendering Polygons Slide 14 Case of a triangle: Simultaneously render the two polygon edges starting from the top corner Each time we move down a pixel connect left & right edges Horizontal line is quick to render Partway down the triangle one edge changes direction Left edge in this example Process is relatively simple extension of the line algorithm This is called Rasterisation Filling Polygons Slide 15 Split more complex polygons into triangles Always possible Need to identify whether dealing with a convex or concave polygon A concave polygon has indentations What are the algorithms used here? Filling Polygons Slide 16 Rendering Circles? The coordinates of a circle can be written as: X = A + R sin(), Y = B + R cos(), for 0 < 360 where (A, B) is the circle centre and R the radius We could use this to render a circle: Step from 0 to 360 and calculate X & Y Convert to viewport space and colour the pixels Not ideal though why not? Try it in the lab Again there is a better integer-based method Called the midpoint circle algorithm (derived from Bresenhams line algorithm, but not by him) (Wikipedia article)Wikipedia article Slide 17 Regular Polygons Can use the circle equations to create regular polygons A regular polygon has all its points on a circle Can calculate the polygon vertices using the circle equations Conversely - can we use regular polygons to draw a circle? Slide 18 Anti-Aliasing Returning to the naive line algorithm: Again we colour every pixel passed through This time consider how close the centre of the pixel is to the line we are drawing The colour used is more intense for pixels that lie nearer the line This is an anti-aliased line Aliasing is the jagged look of pixel edges This method smoothes these effects hence the name: anti- aliasing Note: one example of anti-aliasing, there are several other approaches