Basic C - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

Basic C

Description:

Exam questions will require knowledge of Data Structures and General Programming ... We chop each end-point of the line at the 'nearest' window boundary. 1. 2. 3. 4 ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 47
Provided by: johndin
Category:
Tags: basic | chop

less

Transcript and Presenter's Notes

Title: Basic C


1
Basic C
2
Points to Note
  • Exam questions will not be about C
  • Exam questions will require knowledge of Data
    Structures and General Programming (Pseudocode
    and an understanding of OpenGL instruction
    sequences)
  • Labs and Coursebooks deal largely with C/C
    examples
  • OpenGL unfortunately implemented as C API
  • If you absolutely must you can use any alternate
    programming language for the projects but please
    check with me first

3
Useful
  • C for Java Programmers
  • http//www.cs.wisc.edu/hasti/cs368/CppTutorial/
  • C How to Program, Fourth Edition (2003)
    Harvey Deitel
  • ISBN 0-13-038474-7
  • C How to Program, Third Edition (2001) -
    Deitel and Deitel
  • ISBN 0-13-089571-7
  • Other Authors Cay Horstman, Bjarne Stroustrup
  • For this course you wont need to be an expert!

4
Writing Classes in C
  • class MyClass
  • int value //a private member variable
  • public //everything after this is public
  • MyClass() //constructor
  • void draw() //a method

5
Files
  • Common practice in C is to separate the class
    declaration and the class implementation
  • Declaration (goes in the .H/.HPP file) shows all
    the member variables and method names of the
    class
  • Definition (.CPP) contains the code for the
    actual methods and functions this is what is
    actually compiled
  • You could also stick everything in the .H file
    (like you might have done in Java) but this is
    messy and not appreciated

6
JAVA-ish code for MyClass.H Everything done
inside the class statement. Undesireable when
class gets big enough.
class MyClass int value public MyClass()
values 0 void draw() for (int
i0 iltvalue i) //do something e.g.
drawPixel
7
Myclass Implementation in MyClass.CPP
Myclass Declaration in MyClass.H
include MyClass.h MyClassMyClass() value0
void MyClassdraw()
class MyClass int value public MyClass()
void draw()
Classname resolution operator tells compiler
that these methods are members of MyClass
8
Using Other Classes
include ltiostream.hgt //iostream is in default
include directory include WinSetup.h
//WinSetup is in the current directory
  • Include header files for classes/libraries that
    have already been written.
  • Never include a .cpp file (you need to link
    these to the project in Visual Studio).
  • Usually you will not need to see someone elses
    implementation even if youre using their library
    the Header should be enough

9
Arrays
int myArray18 1, 4, 3, 4, 5, 6, 7, 8 int
myArray28
for (int i0 ilt8 i) int v v
myArray1i //reading from array myArray2i
v //writing to array
10
Dynamically Allocated Arrays
int myDynamicArray int v 20 myDynamicArray
new intv delete myDynamicArray
11
Window to viewport Transformation
12
Window To Viewport Transformation
13
(No Transcript)
14
Global Vs. Local Coordinates
15
Global Coords
v0
v1
v4
v2
v3
v7
v5
v6
16
Local Coords
Q1 v1
Q1 v0
Q2 v0
Q1 v2
Q1 v3
Q1 scale by 2 translate by (2, 7) Q2 rotate
by 45 translate by (3, 5)
Q2 v3
Q2 v1
v0
v1
Q2 v2
v2
v3
17
CLIPPING
18
Clipping
  • A fundamental task in graphics is to keep those
    parts of an object that lie outside a selected
    view from being drawn
  • Clipping is the removal of all objects or part of
    objects in a modelled scene that are outside the
    real-world window.
  • Doing this on a pixel-by-pixel basis would be
    very slow, especially if most objects in the
    scene are outside the window
  • More practical techniques are necessary to speed
    up the task

19
Point Clipping
  • Simple for rectangular clip regions
  • Given rectangular extents (xwmin, xwmax, ywmin,
    ywmax) and a point (x, y) if it satisfies the
    following conditions it is accepted
  • else it is rejected

20
Line Clipping
  • Lines are defined by their endpoints, so it
    should be possible just to examine these (in a
    similar way to points) and determine whether or
    not to clip without considering every pixel on
    the line
  • We often have windows that are either very large,
    i.e. nearly the whole scene fits inside, or very
    small, i.e. most of the scene lies inside the
    window
  • Hence, most lines may be either trivially
    accepted or rejected

21
CASE STUDY Cohen-Sutherland
  • The Cohen-Sutherland line-clipping algorithm is
    particularly fast for trivial cases, i.e. lines
    completely inside or outside the window.
  • Non-trivial lines, i.e. ones that cross a
    boundary of the window, are clipped by computing
    the coordinates of the new boundary endpoint of
    the line where it crosses the edge of the window
  • Each point on all lines are first assigned an
    outcode defining their position relative to the
    clipping rectangle

22
Outcodes
  • The window edges are assumed to be extended so
    that the whole picture is divided into 9 regions
  • Each region is assigned a four-bit code, called
    an outcode

23
Cohen Sutherland cont.
  • The outcode for each end of the line is computed,
    giving codes c1 and c2.
  • One of the following cases will occur
  • Both c1 and c2 are 0000. This means that both
    endpoints lie inside the window, so the line may
    be trivially accepted
  • If c1 and c2 have one or more bits in common,
    then the line must be wholly outside the window.
    Hence if (c1 AND c2) is not equal to zero, the
    line may be trivially rejected
  • If (c1 AND c2) is equal to zero, further
    processing is necessary.

24
  • Having decided that a line is non-trivial, it
    must be clipped
  • We chop each end-point of the line at the
    nearest window boundary

It may be necessary to repeat this process
several times until the line may be trivially
accepted or rejected.
1
2
3
4
25
  • When a line needs to be clipped, an endpoint
    outside the window is chosen (there will always
    be one)
  • The algorithm chooses one of the set bits, and
    uses this to determine which window edge to clip
    to.
  • In this case, The outcode of point d is 0000, and
    of point a is 1010 (bits for above and right
    are set.)
  • Algorithm first works on the above bit, and
    pushes the line to the top edge of the window,
    I.e. point b.

26
  • The outcodes of the line are again computed. This
    time, point b has outcode 0010 (I.e. bit for
    right is set), so the line is pushed to the
    right edge of the window (point c)
  • The outcodes of the endpoints are again computed,
    both are 0000, so the line is now accepted

27
Computing intersection points
p2 (x2,y2)
wymax
(Slope)
A
ax wxmax
ay y1 (wxmax-x1) m
p1 (x1,y1)
wymin
NOTE Vertical lines are a special case
wxmin
wxmax
28
Other Clipping algorithms
  • The Cohen-Sutherland algorithm requires the
    window to be a rectangle, with edges aligned with
    the co-ordinate axes
  • It is sometimes necessary to clip to any convex
    polygonal window, e.g. triangular, hexagonal, or
    rotated.
  • The Cyrus-Beck, and Liang-Barsky line clippers
    better optimise the intersection calculations for
    clipping to window boundary
  • Nicholl-Lee-Nicholl reducing redundant boundary
    clipping by identifying edge and corner regions

29
Polygon Clipping
  • A polygon is usually defined by a sequence of
    vertices and edges
  • If the polygons are un-filled, line-clipping
    techniques are sufficient
  • However, if the polygons are filled, the process
    in more complicated

30
Clipping Filled Polygons
  • Polygons may be fragmented into several polygons
    during clipping
  • Polygon clipper needs to generate one or more
    closed and then associate the original colour
    with each one
  • Output should be a series of new polygon
    boundaries

31
Polygon Clipping Algorithms
  • The Sutherland-Hodgeman clipping algorithm clips
    any polygon against a convex clip polygon.
  • The Weiler-Atherton clipping algorithm will clip
    any polygon against any clip polygon. The
    polygons may even have holes

32
Viewport Mapping Summary
  • Map from rectangular to rectangular
  • Convert continuous Real-world coordinates to
    discrete Screen/Device Coordinates
  • Possibly rescale image dimensions

33
Clipping Summary
  • Cohen Sutherland Line-clipping
  • Fast Elimination of Trivial Cases

1001
1000
1010
0000
0010
0001
0110
0100
0101
34
Why Points ?
  • Weve defined our viewing and modelling functions
    in terms of points
  • This is because most of our objects will be
    defined also in terms of points

35
2D Display Pipeline
  • We have a slight problem.
  • Our primitive drawLine function takes screen
    coordinates
  • We really need to apply the relevant
    transformations before we pass the arguments to
    drawLine

Model
Image
36
drawPolyline(Polyline L) for all points in
L apply relevant transformations //intermediate
data transformed polyline L for all Lines in
L clip to selected Window //intermediate data
clipped Polyline L for all points in L map
to defined Window //intermediate data Polyline
L mapped to //screen coordinates L.draw()

37
Vertex Transformation Pipeline
Local coordinates (pi.x, pi.y, 1)
TRANSFORM
Global coordinates(qi.x, qi.y, 1)
VIEW
Viewport coordinates (vi.x, vi.y, 1)
CLIP
38
Lines
  • DDA Line Drawing, Antialiasing

39
Line Drawing
  • How does a machine go about drawing a line
    defined by a mathematical representation (e.g.
    vector, endpoints, parametric, etc.)?
  • How does the continuous line description get
    converted to the discrete pixel representation.
  • Important part of Rasterization process
  • Some common algorithms
  • DDA
  • Bresenhams

40
Lines
  • Line equation
  • Where m is the slope/gradient
  • b is the y-intercept
  • For any given interval

41
Line Drawing
  • For analog (vector) devices the slope can be used
    directly - deflection voltages proportional to ?x
    and ?y used in the CRT deflectors
  • For raster systems, line must be discretely
    sampled - or scan converted to pixels.

42
DDA Algorithm
  • Digital Differential Analyser a scan conversion
    line algorithm
  • Based on calculating the rate of change of x or y
    coordinates (Dx or Dy respectively)
  • Given two endpoints
  • Step for the larger of length(x) or length(y)
  • E.g.
  • Starting at first endpoint
  • increment by Dx,
  • calculate y rounding off to the nearest pixel
    coordinate each time.

43
void lineDDA (int xa, int ya, int xa, int xb)
int dx xb - xa, dy yb - ya, steps, k float
xIncr, yIncr, x xa, y ya if (
abs(dx)gtabs(dy) ) steps abs(dx) else steps
abs(dy) xIncr dx / (float) steps yIncr
dy / (float) steps drawPixel ( ROUND(x),
ROUND(y) ) for (k0 kltsteps k) x
xIncr y yIncr drawPixel(
ROUND(x), ROUND(y) )
44
Antialiasing
  • Aliasing Distortion of information due to
    low-frequency sampling
  • In raster images leads to jagged edges with
    staircase effect
  • We can reduce effects by antialiasing methods to
    compensate for undersampling

45
Antialiasing by supersampling Example
Supersample rasterised line by 200
46
Now undersample the raster to get the raster of
required resolution. However, we now get average
intensities from clusters of four pixels.
Write a Comment
User Comments (0)
About PowerShow.com