550 likes | 761 Views
UBI 516 Advanced Computer Graphics. Graphics Output Primitives ( Chapter 3 ). Screen & Window Coordinates. Screen coordinates. Setting a pixel glBegin(GL_POINTS); glVertex2f(px,py); glEnd(); Setting Orthogonal Camera
E N D
UBI 516 Advanced Computer Graphics Graphics Output Primitives ( Chapter 3 )
Screen & Window Coordinates Screen coordinates • Setting a pixel • glBegin(GL_POINTS); glVertex2f(px,py); glEnd(); • Setting Orthogonal Camera • glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(xmin,xmax,ymin,ymax); (0,0) x y (399,299) xmax,ymax (px,py) x (0,0) xmin,ymin (799,599) y
. . . Curve=Polyline • GLU has many polynomials that are defined with discrete point sets (like spheres, cylinders)
Pixels and Object Geometry • Pixel Line Rectangle
Drawing a Circle • Midpoint and modified midpoint algorithm.
Splitting concave polygon E5 E5 E4 E4 <180o E6 E6 E3 E3 >180o E2 E2 E1 E1 Polygon Fill • Convex Concave (E1xE2)z>0 (E2xE3)z>0 (E3xE4)z<0 (E4xE5)z>0 (E5xE6)z>0
Slitting of a Polygon into Triangles • OpenGL guarentees correct rendering of polygons only if they are convex. A triangle is a convex polygon. • But a polygon which has >3 vertices can be concave. • So, we must tesselate a given polygon into triangles.The GLU library has a tesselator.
Inside-Outside Testing • Flat and convex polygons are guarenteed to be rendered correctly by OpenGL. • For nonflat polygons: • We can work with their projections. • Or we can use first three vertices to determine a flat plane to use for the interrior. • For nonsimple flat polygons, we must decide how to determine whether a given point is inside or outside of the polygon (Jordan theorem). We want fill inside of a polygon with constant color.
Odd-even rule (easy implementation) = crossing, odd parity rule, even-odd rule Crossed odd edges: inside point Crossed even edges : outside point Non-zero winding rule Clocwise edges : +1 Counterclocwise edges : –1 Don’t fill while total 0 +1 –1 –1 +1 Inside-Outside Testing
Polygon Tables VERTEX TABLE EDGE TABLE POLYGON-SURFACE TABLE V1: x1 y1 z1 V2: x2 y2 z2 V3: x3 y3 z3 V4: x4 y4 z4 V5: x5 y5 z5 E1 : V 1, V 2 E2 : V 2, V 3 E3 : V 3, V 1 E4 : V 3, V 4 E5 : V 4, V 5 E6 : V 5, V 1 S1 : E1, E 2, E3 S2 : E 3, E4, E5, E6 V1 E6 E1 E3 S1 S2 V5 V3 E2 E2 E5 E4 V2 V4
Vertex Arrays • typedef GLint vertex3[3]; vertex3 pt[8] = { (0,0,0), (0,1,0), (1,0,0), (1,1,0), (0,0,1), (0,1,1), (1,0,1), (1,1,1) } • void quad(GLint n1, GLint n2, GLint n3, GLint n4) { glBegin(GL_QUADS); glVertx3iv( pt[ n1 ]); glVertx3iv( pt[ n2 ]); glVertx3iv( pt[ n3 ]); glVertx3iv( pt[ n4 ]); }} • void cube( ) { quad( 6,2,3,7 ); quad( 5,1,0,4 ); quad( 7,3,1,5 ); quad( 4,0,2,6 ); quad( 2,0,1,3 ); quad( 7,5,4,6 );} 4 5 6 7 0 1 2 3
Plane Equations (x,y,z)
Surface Normal • Surface (plane) equation : Ax+By+Cz+D=0 • N = (V1-V2)x(V1-V3) = [ A B C]T • If P is any point on the plane : • N•P+D = 0 N•P = -D y V1 V2 V3 x z
OpenGL: Front/Back Rendering • Each polygon has two sides, front and back (Example)glFrontFace(GL_CCW); glCullFace(GL_BACK); glEnable(GL_CULL_FACE); • OpenGL can render the two differently • The ordering of vertices in the list determines which is the front side: • When looking at thefront side, the vertices go counterclockwise • This is basically the right-hand rule
Culling • For convex objects, such as the cube, we could simply remove all the faces pointing away from viewer. • glEnable(GL_CULL); • Normally, if we use only the z-buffer algorithm, we pass 6n polygons through the pipeline. • If we enable culling, only 3n polygons can pass throughthe pipeline.
OpenGL Pixel Operations • glReadPixels( xmin, ymin, width, height, dataFormat, dataType, array);glEnable( GL_COLOR_LOGIC_OP );glLogicOp( LogicOp );glDrawPixels( width, height, dataFormat, dataType, array ); dataFormat : GL_RGB, GL_LUMINANCE, ...dataType : GL_UNSIGNED_BYTE, LogicOp : GL_COPY, GL_AND, GL_XOR, ... with GL_COPY_INVERTED
Bitmap Fonts • Text in computer graphics is problematic. • OpenGL : no text primitive (use window primitives) • GLUT : minimal support glRasterPosition2i(x1, y1); // First Character PositionglutBitmapCharacter(font, character);glRasterPosition2i(x2, y2); // Second Character PositionglutBitmapCharacter(font, character); Font: GLUT_BITMAP_TIMES_ROMAN_10, GLUT_STROKE_ROMAN, ...
Text • Stroke Text • Use vertices to define line segments or curvesthat outline each character. • Advantages: • It is defined in the same way as otherobjects. • It can be manipulated as other objects(rotate, make bigger, smaller, etc.) • Disadvantages: • Take up too much memory and processingtime • Slow
Text • Raster Text • Characters are defined as rectangles of bitscalled bit blocks. • Each bit block defines a single character by thepattern of 0 and 1bits in the block.
Text • Advantages: • Simple and fast • A raster character can be placed in theframe bufferrapidlyby a bit-block-transferoperation (butblt) • Does not take up too much memory andprocessing time • Disadvantages: • It can not be defined in the same way asother objects. • It can not be manipulated as other objects.
Display Lists • Command block for often used objects. • Creation :GLuint blockNum; blockNum = glGenLists( 1 ); glNewList( blockNum, GL_COMPILE ); ... // OpenGL commands glEndList( ); • Execution:glCallList( blockNum ); • Deletion: glDeleteLists( blockNum, 1 );
UBI 516 Advanced omputer Graphics Attributes of Graphics Primitives ( Chapter 4 )
OpenGL is a State Machine • Use current attribute, change it, use new attribute. time1 : glBegin( ... ); ... // Present color ? glEnd( ); time2 : glColor3fv( Red ); // New color is red time3 : glBegin( ... ); ... glEnd( );
Light • Visible Light in Electromagnetic Spectrum red green yellow blue violet ultraviolet FM infrared AM microwave visible X-ray frequency (Hz) 104 106 108 1010 1012 1014 1016 1018 1020
Blue Red M Green Y C Color • C() : Strenght of wavelength in the color(energy distribution) • Additive color model • C() = Sum of 3 color’s energy • Three-color theory • C = T1R + T2G + T3B= Desired color • T1, T2, T3(Tristimulus Values): The numbers that specify the values ofR, G, B
Human Visual System • cones : red, green, bluereceptors • sensitivy curve of a cone : Si() • brain perception values • three-color theory works for human eye • (Ared, Agreen, Ablue) three parameter of color
Color Model • Color model • Colors are differenton a CRT, a film or a printer. • Color models : RGB, CMY, YIQ, CIE, … • Color gamut • The ranges of color produced by given system. • Color solid (= color cube) (Example) • Color model based on three primary colors. • Three primary color are three axis ofa cube. • Color gamut is set of all points on the cube.
Green C Blue Y Red M RGB color model • Red, Green, Blue • Tri-stimulus theory. • Additive system : Adding ligths to blank points. • RGB cube has 3 axis (0 to 1).
Yellow G Cyan R Magenta B CMY, CMYK color model • Hard copy systems : printing, paiting ... • Subtractive system : Addingcolors to white surface (ex:paper) • Convertion CMY to RGB, and RGB to CMY. • CMYK color model : K (black) for cartridge saving. • Normally, cyan + magenta + yellow = black • CMY can beused for dark gray. • But, (K) ink will be used for black.
RGB color system Additive primaries Adding ligths For monitor, datashows R, G, B lights. Graphics system generally use RGB color system CMY color system Subtractive primiaries Adding color pigments For printer, plotter ... C, M, Yinks. RGB vs. CMY
Direct color system • Video card characteristics : • Each pixelstored with RGB values on frame buffer • If frame buffer storesn bit for every values • 2n× 2n × 2ncolors = 23n colors • 3n = 24 : true color system • 8 bit for every RGB values (0~255). • Frame buffer wold have over 3MB of memory that would have to be redisplay at video rates.
Direct color system • OpenGL functions • Video card has 3n system • OpenGL always use 0.0 ~ 1.0 values for red, green, blue. • It means, OpenGL is independent of hardware. • glColor*(); • For setting colors.
Direct color system • RGBA color model • RGB + A (alpha channel = opacity value) • A = 1.0 is for invisible objects. • glColor4f(red, green, blue, alpha); • Clearing system to color (i.e. pure white with alphavalue of 0.0) • glClearColor(1.0, 1.0, 1.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);.
Indexed color system • If the frame buffer is limited in depth (i.e. eachpixel is only 8-bits deep), then we can select colorsby interpreting our limited depthpixels as indices,rather than as color values. • The indices point to a color-lookup table. • Example: • For a frame buffer that has k-bits per pixel, eachpixel index is a value from 0 to 2k-1. • Assume that the display can display colors with anaccuracy of m (2m reds, 2m greens, 2m blues). color-lookup table
Indexed color system • Why indexed color system ? • Image and frame buffer size is reduced. • Image file format : GIF, BMP use this system. • OpenGL functions : • Sets current color to LUT[index].glIndexi(index); • Change color at LUT[index].glutSetColor(index, red, green, blue);
opaque transparent OpenGL Blending • // destination • Draw the rectangle (dr, dg, db, da) glEnable(GL_BLEND);glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); • // source • Draw the triangle (sr, sg, sb, sa) • Don’t forget disabling !glDisable(GL_BLEND);
Point Attributes • Point color & size : Pixel size = 1
Line Attributes • Line color, width & style : • glLineWidth ( width ); • glLineStipple ( repeatFactor, pattern ); • // 0x1c47 for dash-dot pattern • // binary: 1110001000111 • glEnable ( GL_LINE_STIPPLE ); • ... • glDisable ( GL_LINE_STIPPLE ); • See page 192 for example code.
Line Attributes • Implementation of line width :
Fill-Area Attributes • Hollow, solid or patterned.GLubyte fillPattern[ ] = {0xff,0,0xff,0,...} • glPolygonStipple ( fillPattern ); • glEnable ( GL_POLYGON_STIPPLE );... • glDisable ( GL_POLYGON_STIPPLE );
OpenGL Polygon Modes • glPolygonMode( face, displayMode ); (Example) • Face:GL_FRONT, GL_BACK, GL_FRON_AND_BACK • Display Modes:GL_POINT (Only edge points) GL_LINE (Wireframe) GL_FILL (Default) • glFrontFace( faceOrder );// GL_CW or GL_CCWglEdgeFlag( flag ); // GL_TRUE or GL_FALSE for line mode
General Scan-Line Polygon-Fill Algorithm • Consider the following polygon: • How do we know whether a given pixel on the scanline is inside or outside the polygon? D B C A E F
Polygon Rasterization • Inside-Outside PointsPoint a, c : +1Point b, d : -1if zero then fill • For edge E ? • For edges A and C? Do not changes
Flood-Fill Algorithm • Set pixel to fill color value until bounds. • An interior point (x, y) • A boundary color • A fill color Fill color Boundary color Interior point (x, y)
4-connected 8-connected Start point 4-connected vs. 8-connected
Flood-Fill Algorithm • Rough algorithm for 4-connected case • void flood_fill4(int x, int y, Color fill, Color boundary) {Color current = getPixel(x, y); if (current boundary && current fill) {setPixel(x, y, fill);flood_fill4(x+1, y, fill, boundary); // recursion !flood_fill4(x–1, y, fill, boundary);flood_fill4(x, y+1, fill, boundary);flood_fill4(x, y–1, fill, boundary);} • }