Graphics Programming Basic of computer graphics with OpenGL.

download Graphics Programming Basic of computer graphics with OpenGL.

of 51

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Graphics Programming Basic of computer graphics with OpenGL.

  • Graphics ProgrammingBasic of computer graphics with OpenGL

  • Handful graphics functionOpenGL : by silicon graphicsPHIGS : Programmers Hierarchical Graphics SystemGKS : Graphics Kernel SystemJAVA-3D/JOGLBy Sun micro-systemDirectX:Microsoft corp.

  • Target Coordinate SystemsCG system is unable define exactly unit like cm, inch etcCG is a device independent systemCurrent coordinate is user coordinate = world coordinateIt should be match with Display coordinate system (Raster coordinate)

  • Graphics function properties7 groups of functionPrimitive: What is object ?low level objects or atomic entities, ex. point, polygon etc,AttributeHow the appear: fill, bold characterViewingHow we saw the imageTransformationTransform of object: rotate, moveInputDeal with the devices: keyboard, mouse etc.Control functionMultiwindow, multiprocessing environment handling.Inquiry functionInformation providing for different API

  • Pipeline and State MachineEntire graphics system thinking as a state machineThere are 2 types of Graphics functions Things that define primitivesThings that changes the state

  • The OpenGL InterfaceBegin with glStored in library and referred to as GLThere are Graphics Utility Library (GLU) GLU Toolkit (GLUT)GLX or WGL : glue for GL to OS Defined in standard header folder GL filename glut.h

  • Primitives and AttributesAPI should contain small set of primitives that every hardware can be supportedEx. Line, polygons, text Variety of primitive such as circle, curves, surface and solids able to build sophisticated object but few hardware supportedOpenGL takes an intermediateSupport 2 classes of primitives Geometric primitives : pass through a geometric pipelineRaster primitives: pass through pixel pipeline

  • GeometricAble to manipulatedRaster Lack of geometric properties

  • The 2D ModelingSpecial case of 3DSuppose z=0, every point refer to (x, y,0)Generally object created from set pointsIn graphics system , the word vertex more preferred that pointOpenGL function formglVertex*();where*: nt or ntv , 2 or 3 characters formn : number of dimension ( 2, 3 or 4)t : data type (ingeter, float, double, v for pointer)Ex. glVertex2i(); /* vertex for 2D integer type*/The data type may change to GL type instead of C Ex. GLfloat = float in C Note: All of them have already defined in header fine

  • OpenGL Object formDefined object in glBegin-glEnd loop2 kinds of primitives that is used to defined objectNo interior, eg. points, lineHave surface, eg. polygon

    glBegin(type); glVertex*(); . . .glEnd();Difference type of object formC command for defining object

  • Polygon BasicsClose object that has interiorAble to use as curve surfaceNumber of generated polygons per time is used as graphics performanceDisplay either only edges or fillCorrect properties should be simple, convex, and flat3D polygon is unnecessarily flatFilled objectspolygons displayingsimple polygonnonsimple polygonconvex property

  • Convex object properties3D convex object: 3 vertices are not collinearSafe for rendering if use triangle Hardware and software often support

  • Polygon display modeGL_POLYGONSEdges are perform line loop and closeEdges has no withdefine either fill or edges using glPolygonModeIf both, draw twice

  • Special types polygonTriangles and Quadrilaterals(GL_TRIANGLES, GL_QUADS)Strips and Fans (GL_TRIANGLE_STRIP, GL_QUAD_STRIP, GL_TRIANGLE_FAN)

  • Sample object:Generating a Sphereassign to be polygons and used GL_QUAD_STRIPUse longitude and latitude schemes for the middle body For pole usesGL_TRIANGLE_FANC=M_PI/180.0; //degrees to radians, M_PI = 3.14159for (phi = -80.0; phi
  • x=y=0; // North pole modelingz = 1;glBegin(GL_TRIANGLE_FAN); glVertex3d(x,y,z); c=M_Pi/180.0; z=sin(c*80.0); for(theta=-180.0; theta
  • Text2 types of text Stroke TextConstructed via using graphic primitivesAble to transform like other primitivesRaster TextCharacter are defined as rectangle of bits block

  • Stroke textConsume a lot of memoriesPostscript as an exampleRaster textRapidly be placed in buffer by using bit-block-transfer (bitblt) operationOperate only character sizing Often store in ROM (hardware)Portability is limited by particular fontGLUT provide 8x8 pixels functionglutBitmapCharacter(GLUT_BITMAP_8_BY_13, C)C: ASCII character numberCharacter is placed in the present position of screen

  • Curved ObjectsCreate by using 2 approachUse the primitive except pointsn side polygon instead of circleApproximate sphere with polyhedronCurved surface by a mesh of convex polygonUse mathematical definitionQuadric surfaces and parametric polynomial curved and surfacesexample: Define sphere by center and a point on surfaceCubic Polynomial is defined by 4 pointsOpenGL able to do both

  • AttributesAbout how primitive displayLine : display color, type of line (dash, solid)Concern with immediate mode: display as soon as they are defined

  • ColorMost interesting of perception and computer graphicsBase on three color theory If using additive color model- c = T1R+T2G+T3BC: color that we trying to matchT1, T2, T3: strength of intensity, the tristimulus value

  • Human Visual SystemOur visual system do a continuous perceptionDepends on 3 types of cone cell Visually indistinguishable if they have the same tristimulus valueCRT is an example of additive color systemAi: brain perception valueSi: cone cell sensitivityViewing a point as a color solid cube

  • Subtractive color modelThe complementary of additive color modelStart with white surfaceIf white light hit the surface, color will be absorb except the object color which are reflectEx. painting and printingComplementary color: cyan, magenta, yellow

    additive color modelsubtractive color model

  • RGB-color modelUse separate buffer for each colorEach pixel has 3 bytes (24 bits) for each color16 Million shade of colorOpenGL function glColor3f(r, g, b);ex. Red glColor3f(1.0, 0.0, 0.0);

  • RGBA, the 4 color modelA: Alpha channelStore in frame buffer like RGBFor creating effect ex. fog, combining images.OpenGL treat as opacity or transparencyEx. OpenGL command for 4 color modelglClearColor(1.0, 1.0, 1.0, 1.0);White color and opaque

  • Indexed ColorDifficult to support in hardwareHigher memory requirements but now memory is cheaperUse color tray of artist as principleInfinite color can be produced from different quantity of primary colors

  • OpenGL indexed color functionglIndex(element);Select color out of tableglutSetcolor(int color, GLfloat red, GLfloat blue, GLfloat green);Set color entry to map the color table

  • Color AttributesFor RGB modeglClearColor(1.0, 1.0, 1.0); /* clear to white */glColor3f(1.0, 0.0, 0.0); /* setting point to red */glPointSize(2.0); /* 2 pixel wide */

    Note: If 2 display differ in pixel size, rendered images may appear slightly different

  • ViewingMethod for objects appear on screenUse synthetic camera conceptFix lens and fix locationPicture would be distort like real world If we need to take an elephant picture, camera should far enough to take all information

  • 2D ViewingBase on the 2D rectangular areaKnow as viewing rectangle or clipping rectangleBe a special case of 3D viewing ex. plane at z=0Default in 2x2x2 volume, origin at the center and bottom-left corner is at (-1.0, -1.0)

  • Orthographic View2D view the orthographic projection of 3DFunction void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);// near, far: distance which are measured from camera/* orthographic projection from 3D */void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);/* 2D equivalent to glOrtho but near and far set to -1.0, 1.0 */Unlike camera, it is able to view behind object

  • Matrix ModesBetween graphic pipeline states, any transformation2 important matrices: model-view Projection

    glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0, 500.0, 0.0, 500.0);glMatrixMode(GL_MODELVIEW);

  • Control functionConcern about software environment between software and platformDifferent platform will have different interfacingGLUT also provide the utility : see further

  • Windows interfacingWindow : A rectangular area of our display, max = ScreenWindow default origin (0,0) at lower-left corner like Screen but mouse at top-leftOpenGL function (GLUT function) for windowglutInit (int *argcp, char **argv);glutCreateWindow(char *title); /* given the window title */

  • Display setupglutInitDisplayMode(GLUT_RGB| GLUT_DEPTH | GLUT_DOUBLE);GLUT_RGB: define RGB color modeGLUT_DEPTH: a depth buffer for hidden-surface removalGLUT_DOUBLE: number of buffer Double/Single

    default:RGB color, no hidden surface removal, single buffering

    glutInitWindowSize(480, 640);glutInitWindowPosition(0,0)

  • Aspect ratioRatio of rectangles width to its heightIf glOrtho and glutInitWindowSize are not specified the same size, object are distort.

  • View portA rectangular area of the display windowSetting a view portvoid glViewport(GLint x, GLint y, GLsizei w, GLsizei h);

  • Service function: main, display and myinitglutMainLoop(); /* begin an event-processing loop, let the window waiting for kill process */ void glutDisplayFunc(void *(func)(void));/* call to the redisplay function name func */


    void main(int argc, char **argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow("Simple OpenGL example"); glutDisplayFunc(display); myinit(); g