constructive solid geometry · constructive solid geometry idea: • use a set of objects with...

49
320322: Graphics and Visualization 232 Visualization and Computer Graphics Lab Jacobs University Constructive solid geometry Idea: Use a set of objects with simple representation and a set of efficient operations to combine the simple objects to more complex ones. Only store pointers to simple objects and operations. Hence, this can also be considered as a (semi-)implicit representation.

Upload: others

Post on 19-Oct-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

  • 320322: Graphics and Visualization 232

    Visualization and Computer Graphics LabJacobs University

    Constructive solid geometry

    Idea:• Use a set of objects with simple representation and a

    set of efficient operations to combine the simple objects to more complex ones.

    • Only store pointers to simple objects and operations.• Hence, this can also be considered as a (semi-)implicit

    representation.

  • 320322: Graphics and Visualization 233

    Visualization and Computer Graphics LabJacobs University

    Constructive solid geometryBasis objects represented by their boundaries:• Polyhedra: cuboids, tetrahedra, …• Implicitly defined objects: quadrics, sweep geometry, …

    Operations operate on solids (volumetric objects):• Boolean operations: intersection, union, difference,

    xor,…

    Combining constructions:• Many basis objects can be successively combined using

    Boolean operations.• The combinations are stored in a binary tree.

  • 320322: Graphics and Visualization 234

    Visualization and Computer Graphics LabJacobs University

    Constructive solid geometry

  • 320322: Graphics and Visualization 235

    Visualization and Computer Graphics LabJacobs University

    Constructive solid geometry

    Transformations:• In addition to the operations, we need to store

    transformations for the objects that are appliedbefore the objects get combined.

  • 320322: Graphics and Visualization 236

    Visualization and Computer Graphics LabJacobs University

    Constructive solid geometry (CSG)

  • 320322: Graphics and Visualization 237

    Visualization and Computer Graphics LabJacobs University

    CSG

  • 320322: Graphics and Visualization 238

    Visualization and Computer Graphics LabJacobs University

    CSG

  • 320322: Graphics and Visualization 239

    Visualization and Computer Graphics LabJacobs University

    3. Raster Graphics

  • 320322: Graphics and Visualization 240

    Visualization and Computer Graphics LabJacobs University

    3.1 Display

  • 320322: Graphics and Visualization 241

    Visualization and Computer Graphics LabJacobs University

    Displays

    The most commonly used displays are• Cathode Ray Tube (CRT)• Liquid Crystal Display (LCD)

    – Thin-film transistor (TFT)

  • 320322: Graphics and Visualization 242

    Visualization and Computer Graphics LabJacobs University

    CRT1. Electron guns2. Electron beams3. Focusing coils4. Deflection coils5. Anode connection6. Separating mask7. Phosphor layer8. Close-up look

  • 320322: Graphics and Visualization 243

    Visualization and Computer Graphics LabJacobs University

    Masking

    • Separating red, blue, and green light beams:

    • Resolution = amount of pixels

    pixel

    rasterized screen

  • 320322: Graphics and Visualization 244

    Visualization and Computer Graphics LabJacobs University

    Interlacing

    • Interlaced row drawing

  • 320322: Graphics and Visualization 245

    Visualization and Computer Graphics LabJacobs University

    TFT-LCD• Two versions:

    – mirroring light– light-emitting

    emitted/reflected

    lighthorizontal

    polarizationhorizontal

    filterliquid

    crystallayer

    verticalfilter

    verticalpolarization

  • 320322: Graphics and Visualization 246

    Visualization and Computer Graphics LabJacobs University

    TFT-LCD

    • The liquid crystal layer is deployed by a 2D array of thinfilm transistors.

    • One has three transistors per pixel. • Polarization is changed by 90°, unless voltage is applied.• The higher the voltage, the darker the pixel.

  • 320322: Graphics and Visualization 247

    Visualization and Computer Graphics LabJacobs University

    Graphics pipeline: Rasterization

    CPUCPU DLDL

    Poly.Poly. PerVertex

    PerVertex

    RasterRaster FragFrag FBFB

    PixelPixel

    TextureTexture

    • During rasterization, the projected 2D scene is discretizedinto a raster image.

    • The raster image consists of pixels(called fragments in this context).

    • The raster image is composed in the frame buffer.• The input of the frame buffer is rendered on the screen.

  • 320322: Graphics and Visualization 248

    Visualization and Computer Graphics LabJacobs University

    Double buffering

    12

    48

    16

    12

    48

    16FrontBuffer

    BackBuffer

    Display

  • 320322: Graphics and Visualization 249

    Visualization and Computer Graphics LabJacobs University

    Double buffering in OpenGL

    1. Request a double buffered color bufferglutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );

    2. Clear color bufferglClear( GL_COLOR_BUFFER_BIT );

    3. Render scene4. Request swap of front and back buffers

    glutSwapBuffers();

    5. Repeat steps 2 - 4 for animation

  • 320322: Graphics and Visualization 250

    Visualization and Computer Graphics LabJacobs University

    Double buffering in OpenGL

    • void drawScene( void )• {

    GLfloat vertices[] = { … };GLfloat colors[] = { … };glClear( GL_COLOR_BUFFER_BIT);glBegin( GL_TRIANGLE_STRIP );

    /* calls to glColor*() and glVertex*() */glEnd();glutSwapBuffers();

    • }

    • void drawScene( void )• {

    GLfloat vertices[] = { … };GLfloat colors[] = { … };glClear( GL_COLOR_BUFFER_BIT);glBegin( GL_TRIANGLE_STRIP );

    /* calls to glColor*() and glVertex*() */glEnd();glutSwapBuffers();

    • }

  • 320322: Graphics and Visualization 251

    Visualization and Computer Graphics LabJacobs University

    3.2 Scan conversion

  • 320322: Graphics and Visualization 252

    Visualization and Computer Graphics LabJacobs University

    Scan conversion

    Definition:• Scan conversion is the process of mapping the

    screen-space projection of a 3D scene to the pixelraster of a screen.

  • 320322: Graphics and Visualization 253

    Visualization and Computer Graphics LabJacobs University

    Digital differential analyzer (DDA)• Scan conversion of edges.• Input: endpoints (x1,y1) and (x2,y2) of an edge as 2D

    Cartesian coordinates in the screen space coordinatesystem.

    • Output: discretized edge, i.e., framebuffer withthose pixels filled that are traversed by the edge.

    (x1,y1)

    (x2,y2)

  • 320322: Graphics and Visualization 254

    Visualization and Computer Graphics LabJacobs University

    DDA algorithm

    DDA (x1, y1, x2, y2){

    length = max (|x2-x1|, |y2-y1|);dx = (x2-x1)/length;dy = (y2-y1)/length;for (i=0; i

  • 320322: Graphics and Visualization 255

    Visualization and Computer Graphics LabJacobs University

    DDA algorithm

    length = 10dx = 1dy = 2/5

    (1.5,1.5), (2.5,1.9), (3.5,2.3), (4.5,2.7), (5.5,3.1), (6.5,3.5), (7.5,3.9), (8.5,4.3), (9.5,4.7), (10.5,5.1), (11.5,5.5)

    (1.5,1.5)

    (11.5,5.5)

  • 320322: Graphics and Visualization 256

    Visualization and Computer Graphics LabJacobs University

    Bresenham algorithm

    • Alternative approach to DDA

    • Input/output: as before.

    • Idea: local increments based on error term.

  • 320322: Graphics and Visualization 257

    Visualization and Computer Graphics LabJacobs University

    Bresenham algorithmBresenham (x1, y1, x2, y2){

    dx = x2-x1;dy = y2-y1;if (dx > dy > 0) // distinguish octants{

    (x,y) = (x1,y1); // start pointerror = dy/dx; // initialize with slopefor (i=1; i = 0.5) // if error has accumulated{

    y++; // step in y-directione--;

    }x++; // step in x-directione += dy/dx; // update error

    }}else … // treat other octants

  • 320322: Graphics and Visualization 258

    Visualization and Computer Graphics LabJacobs University

    Bresenham algorithm

    (1.5,1.5)

    (11.5,5.5)

    dx = 10dy = 4

    (x,y) error• (1.5,1.5) 0.4• (2.5,1.5) 0.8• (3.5,2.5) 0.2• (4.5,2.5) 0.6• (5.5,3.5) 0• (6.5,3.5) 0.4• (7.5,3.5) 0.8• (8.5,4.5) 0.2• (9.5,4.5) 0.6• (10.5,5.5) 0• (11.5,5.5)

  • 320322: Graphics and Visualization 259

    Visualization and Computer Graphics LabJacobs University

    Bresenham algorithm

    • The code can be restructured such that it only usesinteger operations.– idea:

    • multiply all fractional numbers with dx• (dy/dx > 0.5) equivalent to (0.5 dx – dy < 0)

    – faster– more accurate

    • The octants can be handled more efficiently byswapping x1, x2, y1, and y2 respectively.

  • 320322: Graphics and Visualization 260

    Visualization and Computer Graphics LabJacobs University

    Bresenham algorithm using integers

    Bresenham_integer (x1, y1, x2, y2){

    if (|y2-y1| > |x2-x1|) {

    swap(x1, y1);swap(x2, y2);

    }if (x0>x1){

    swap(x1, x2);swap(y1, y2);

    }

  • 320322: Graphics and Visualization 261

    Visualization and Computer Graphics LabJacobs University

    Bresenham algorithm using integersint dx = x2 – x1;int dy = |y2 – y1|;(int,int) (x,y) = (x1,y1); // start pointint error = dx / 2; // initializationint ystep = (y1 < y2)?1:-1; // step in + or – y-directionfor (i=1; i |x2-x1|) plot(y,x); else plot(x,y);error -= dy; // subtract dyx++;if (error < 0) // new error test{

    y += ystep; error += dx; // add dx

    }}}

  • 320322: Graphics and Visualization 262

    Visualization and Computer Graphics LabJacobs University

    Bresenham algorithm

    (1.5,1.5)

    (11.5,5.5)

    dx = 10dy = 4

    (x,y) error5

    • (1.5,1.5) 1• (2.5,1.5) 7• (3.5,2.5) 3• (4.5,2.5) 9• (5.5,3.5) 5• (6.5,3.5) 1• (7.5,3.5) 7• (8.5,4.5) 3• (9.5,4.5) 9• (10.5,5.5) 5• (11.5,5.5)

  • 320322: Graphics and Visualization 263

    Visualization and Computer Graphics LabJacobs University

    Generalization

    • The incremental structure of the algorithm allows forscan conversion of any curve.

    • What needs to be adapted is the adjustment of theerror term.

  • 320322: Graphics and Visualization 264

    Visualization and Computer Graphics LabJacobs University

    Bresenham algorithm for circles

    BresenhamCircle(int x0, int y0, int radius) {

    int f = 1 - radius; int ddF_x = 1; int ddF_y = -2 * radius; int x = 0; int y = radius;

    plot(x0 + x, y0 + y); plot(x0 + x, y0 - y); plot(x0 + y, y0 + x); plot(x0 - y, y0 + x);

  • 320322: Graphics and Visualization 265

    Visualization and Computer Graphics LabJacobs University

    Bresenham algorithm for circles…while(x < y) {

    if(f >= 0) {

    y--; ddF_y += 2; // adjusting slopef += ddF_y; // update error

    } x++; ddF_x += 2; // adjusting slopef += ddF_x; // update error

    plot(x0 + x, y0 + y); plot(x0 - x, y0 + y); plot(x0 + x, y0 - y); plot(x0 - x, y0 - y); plot(x0 + y, y0 + x); plot(x0 - y, y0 + x); plot(x0 + y, y0 - x); plot(x0 - y, y0 - x);

    }}

  • 320322: Graphics and Visualization 266

    Visualization and Computer Graphics LabJacobs University

    Problems

    • Line thickness depends on orientation:

    length = 4 length =#pixels = 5 #pixels = 5

    • Aliasing (see later)

  • Visualization and Computer Graphics LabJacobs University

    3.3 Polygon filling

  • 320322: Graphics and Visualization 268

    Visualization and Computer Graphics LabJacobs University

    Polygon filling

    • Polygon filling treats the drawing of polygonal facesafter being projected to the screen space.

    • The input is a (set of) polygon(s).

    • The output is a raster image stored in theframebuffer that represents a discrete version of the projected polygons.

  • 320322: Graphics and Visualization 269

    Visualization and Computer Graphics LabJacobs University

    Seed fill algorithm

    • The seed fill (also: flood fill) algorithm starts with a seed point inside the polygon and keeps on filling in all directions until the boundary of the polygon is hit.

    1. Scan convert all edges of the polygon.2. Choose a seed point inside the polygon and fill the

    respective pixel.3. Recursively fill all pixels of the 4-point neighborhood,

    if they are not already filled. Stop recursion at already filled pixels.

  • 320322: Graphics and Visualization 270

    Visualization and Computer Graphics LabJacobs University

    Seed fill algorithm

    polygonscan conversionseedingneighborsseed fill iterations

  • 320322: Graphics and Visualization 271

    Visualization and Computer Graphics LabJacobs University

    Seed fill algorithm

    • How to pick appropriate seed point?• It must lie inside the polygon

    Strategy:1. Use heuristic (e.g., barycenter of polygon) to pick

    any point.2. Check inside-property using a ray-intersection test.

  • 320322: Graphics and Visualization 272

    Visualization and Computer Graphics LabJacobs University

    Ray-intersection test

    • Let x be the chosen seed point.• Shoot a ray to infinity (typically along a coordinate axis)• Compute intersection of ray with all edges of the polygon• Iff number of intersections is odd, x lies inside the polygon

  • 320322: Graphics and Visualization 273

    Visualization and Computer Graphics LabJacobs University

    Scan line algorithm

    • The scan line algorithm is an alternative to the seedfill algorithm.

    • It does not require scan conversion of the edgesbefore filling the polygons

    • It can be applied simultaneously to a set of polygonsrather than filling each polygon individually.

  • 320322: Graphics and Visualization 274

    Visualization and Computer Graphics LabJacobs University

    Scan line algorithm

    • Use a horizontal scan line that traverses the scenetop-down.

    • Stop at each pixel row• For each pixel row

    – Compute the intersections of the polygon edges with thescan line.

    – Sort the intersections from left to right.– Start filling at first intersection.– Stop filling at second intersection– Start filling again at third intersection.…

  • 320322: Graphics and Visualization 275

    Visualization and Computer Graphics LabJacobs University

    Scan line algorithn

    • Horizontal scan lines scan scene top-down and stop at each row:

    (xk+1, yk+1)

    (xk , yk) Scan Line yk

    Scan Line yk +1

  • 320322: Graphics and Visualization 276

    Visualization and Computer Graphics LabJacobs University

    Scan line algorithm

    • Filling pixels between intersections in an alternatingfashion:

  • 320322: Graphics and Visualization 277

    Visualization and Computer Graphics LabJacobs University

    Scan line algorithm

    Example:

  • 320322: Graphics and Visualization 278

    Visualization and Computer Graphics LabJacobs University

    Scan line algorithm

    Example:

  • 320322: Graphics and Visualization 279

    Visualization and Computer Graphics LabJacobs University

    Data structure for scan line algorithm

    • The scan line algorithm is supported by a linked list data structure that stores all the edges that areintersected by the current position of the scan line in the correct order.

    • Linked list exploits spatial coherence betweensuccessive scan line positions.

    • The order of the edges stored in the linked list onlychanges at distinct points (event points).

  • 320322: Graphics and Visualization 280

    Visualization and Computer Graphics LabJacobs University

    Data structure for scan line algorithm

    • Event points are:– start/end of edge: insert/remove edge into sorted list.– edge crossings: flip order of successive edges in sorted list.

    (Only in case of multiple polygons!)• Start/end points are known from the beginning.• Edge crossings are checked only between neighboring

    edges in the sorted list. It is necessary whenever a new neighborhood emerges, i.e., at event points.