Lect1_Giới thiệu OpenGL

Embed Size (px)

Citation preview

  • 8/4/2019 Lect1_Gii thiu OpenGL

    1/35

    Gii thiu OpenGL

  • 8/4/2019 Lect1_Gii thiu OpenGL

    2/35

    Cc cng c HMT

    Cng c

    Phncng

    Thit b raThit b

    vo

    Phnmm

    Hm vc bn

    Hm viql ca s

    Hm vmy quay

  • 8/4/2019 Lect1_Gii thiu OpenGL

    3/35

    ha c lp thit b

    Th vin ha c lp thit b: cho php sdng tp hm ha chung chy trn nhiuh thng khc nhau.

    OpenGL

  • 8/4/2019 Lect1_Gii thiu OpenGL

    4/35

    ha c lp thit b: OpenGL

    Cho php cng chng trnh ha chy trnnhiu my khc nhau v cho ra kt qu hunh hon ton ging nhau.

    Dng cc tp .dll OpenGL nh l mt API: n iu khin phn

    cng bn ang s dujg v bn ch cn dngcc hm ca OpenGL thay v iu khin thitb phn cng trc tip.

    OpenGL l open source (free).

  • 8/4/2019 Lect1_Gii thiu OpenGL

    5/35

    Chng trnh hng s kin

    p ng cc s kit nh click, di chuynchut, bm phm, thay i kch c ca s. Hthng qun l s kin theo hng i.

    Ngi lp trnh cung cp cc hm call-back iu khin mi s kin.

    Hm call-back phi c ng k vi OpenGL

    OpenGL bit hm no iu khin s kinno.

    Hm ng k khng gi n!

  • 8/4/2019 Lect1_Gii thiu OpenGL

    6/35

    Ci t v cu hnh

    Files: .h, .lib, .dll

    Ton b th mc glc t trong th mcInclude ca Visual C++

    Ton b cc tp libc t trong th mc libca Visual C++

    Ton b cc tp dllc t trong

    C:\Windows\System32

  • 8/4/2019 Lect1_Gii thiu OpenGL

    7/35

    S dng OpenGL

    Includes:

    (nu s dng)

  • 8/4/2019 Lect1_Gii thiu OpenGL

    8/35

    Th vin thm vo th mc include

    GL, cu lnh bt u vi GL; GLUT, GL Utility Toolkit, m ca s, pht trin thc

    n v qun l s kin. GLU, GL Utility Library, cung cp cc hm mc cao

    iu khin cc thao tc v phc tp. GLUI, User Interface Library, c tch hp vo GLUT

    library. Hm GLUT phi c sn cho GLUI thao tc c chnh

    xc. GLUI cung cp cc iu khin v thc n phc tp cho

    cc ng dng OpenGL

  • 8/4/2019 Lect1_Gii thiu OpenGL

    9/35

    S dng OpenGL

    Thay i project settings: Visual C++ 6.0

    Project /Settings

    Trong Object/library modules: thm glui32.lib

    glut32.lib glu32.lib opengl32.lib(dng du cchphn cch)

    Trong Project Options: thm cng tp cc tp .lib

    ngthc n Project v lu workspace

  • 8/4/2019 Lect1_Gii thiu OpenGL

    10/35

    S dng OpenGL (2)

    Thay i project settings: Visual C++ .NET2003

    Project/Properties/Linker/Command Line

    Thm glui32.lib glut32.lib glu32.lib opengl32.lib

    ng thc nProject v lu s thay i

  • 8/4/2019 Lect1_Gii thiu OpenGL

    11/35

    Lnh v c bn

    setPixel(x, y, color)

    Pixel ti (x, y) c mu color

    Tn khc: putPixel(), SetPixel(),hoc drawPoint()

    line(x1, y1, x2, y2)

    ng thng gia (x1, y1) v (x2, y2)

    Tn khc: drawLine() or Line().

  • 8/4/2019 Lect1_Gii thiu OpenGL

    12/35

    Lnh v c bn (2)

    current position (cp), v tr hin ti.

    moveTo(x,y)chuyn v tr hin ti n (x, y).

    lineTo(x,y)v ng thng t v tr hin tin (x, y) v thay i cp thnh (x, y).

  • 8/4/2019 Lect1_Gii thiu OpenGL

    13/35

    V d: v hnh vung

    moveTo(4, 4); //move

    to starting corner

    lineTo(-2, 4);

    lineTo(-2, -2);

    lineTo(4, -2);

    lineTo(4, 4); //close

    the square

  • 8/4/2019 Lect1_Gii thiu OpenGL

    14/35

    Khung mt chng trnh// include OpenGL libraries

    void main()

    {

    glutDisplayFunc(myDisplay); // register the redraw function

    glutReshapeFunc(myReshape); // register the reshape function

    glutMouseFunc(myMouse); // register the mouse action functionglutMotionFunc(myMotionFunc); // register the mouse motionfunction

    glutKeyboardFunc(myKeyboard); // register the keyboard actionfunction

    perhaps initialize other thingsglutMainLoop(); // enter the unending main loop

    }

    all of the callback functions are defined here

  • 8/4/2019 Lect1_Gii thiu OpenGL

    15/35

    Hm Callback

    glutDisplayFunc(myDisplay);

    V li ca mn hnh khi ca s c m.

    glutReshapeFunc(myReshape);

    Khi thay i kch c ca s

    glutIdleFunc(myIdle);

    Khi khng c s kin g xy ra vic v li ch cn s dngvoid myIdle() {glutPostRedisplay();}

  • 8/4/2019 Lect1_Gii thiu OpenGL

    16/35

    Hm Callback (2)

    glutMouseFunc(myMouse);

    Handles mouse button presses. Knows mouse

    location and nature of button (up or down and

    which button).

    glutMotionFunc(myMotionFunc);

    Handles case when the mouse is moved with one

    or more mouse buttons pressed.

  • 8/4/2019 Lect1_Gii thiu OpenGL

    17/35

    Hm Callback (3)

    glutPassiveMotionFunc(myPassiveMotionFunc)

    iu khin khi chut i vo ca s nhng khng cnt no c bm.

    glutKeyboardFunc(myKeyboardFunc); iu khin n v nh phm.

    glutMainLoop()

    Chy lin tc v ch cc s kin xy ra. Khi c s kin xyra, th tham chiu n hm callback tng ng.

  • 8/4/2019 Lect1_Gii thiu OpenGL

    18/35

    Mt ct GL m mt ca s (2)// appropriate #includes go here see Appendix 1void main(int argc, char** argv){

    glutInit(&argc, argv); // initialize the toolkitglutInitDisplayMode(GLUT_SINGLE |

    GLUT_RGB); // set the display modeglutInitWindowSize(640,480); // set window sizeglutInitWindowPosition(100, 150);

    // set window upper left corner position on screen

    glutCreateWindow("my first attempt");// open the screen window (Title: my first attempt)

  • 8/4/2019 Lect1_Gii thiu OpenGL

    19/35

    Mt ct GL m mt ca s (3)

    // register the callback functions

    glutDisplayFunc(myDisplay);

    glutReshapeFunc(myReshape);

    glutMouseFunc(myMouse);glutKeyboardFunc(myKeyboard);

    myInit(); // additional initializations as necessary

    glutMainLoop(); // go into a perpetual loop

    } Terminate program by closing window(s) it is using.

  • 8/4/2019 Lect1_Gii thiu OpenGL

    20/35

    Gii thch

    glutInit (&argc, argv) khi to Open-GL Toolkit

    glutInitDisplayMode (GLUT_SINGLE |

    GLUT_RGB) nh v buffer hin th n v ch mu RGB v.

    glutInitWindowSize (640, 480) To ca s640x480.

  • 8/4/2019 Lect1_Gii thiu OpenGL

    21/35

    Gii thch (2)

    glutInitWindowPosition (100, 150) v tr cagc trn bn tri ca ca s.

    glutCreateWindow (my first attempt) Mca s c tn my first attempt

  • 8/4/2019 Lect1_Gii thiu OpenGL

    22/35

    Kt qu ca chng trnh

  • 8/4/2019 Lect1_Gii thiu OpenGL

    23/35

    V cc im trong OpenGL

    H trc ta 0 n 679 cho trc x v 0 n479 cho trc y.

    OpenGL v da trn cc nh (vertices hoc

    corners). v mt i tng trong OpenGL,t mt danh sch cc nh trong glBegin vglEnd Danh sch bt u vi glBegin(arg);v kt thc vi

    glEnd();

    Argxc nh i tng v. glEnd()Gi d liu v vo lung OpenGL .

  • 8/4/2019 Lect1_Gii thiu OpenGL

    24/35

    V d

    glBegin (GL_POINTS);

    glVertex2i (100, 50);

    glVertex2i (100, 130);

    glVertex2i (150, 130);

    glEnd();

    GL_POINTS hng Open-GL (GL_LINES,

    GL_POLYGON, )

  • 8/4/2019 Lect1_Gii thiu OpenGL

    25/35

    Hin th cc im

  • 8/4/2019 Lect1_Gii thiu OpenGL

    26/35

    Hm OpenGL

  • 8/4/2019 Lect1_Gii thiu OpenGL

    27/35

    V d v hm OpenGL

    glVertex2i () ly mt gi tr nguyn

    glVertex2d () ly mt gi tr thc

    OpenGL s dng kiu d liu ca OpenGL

  • 8/4/2019 Lect1_Gii thiu OpenGL

    28/35

    Kiu d liu OpenGL

    suffix data type C/C++ type OpenGL type name

    b 8-bit integer signed char GLbyte

    s 16-bit integer Short GLshort

    i32-bit integer int or long GLint, GLsizei

    f 32-bit float Float GLfloat, GLclampf

    d 64-bit float Double GLdouble,GLclampd

    ub

    8-bit unsignednumber unsigned char GLubyte,GLboolean

    us 16-bit unsignednumber

    unsigned short GLushort

    ui 32-bit unsigned

    number

    unsigned int or

    unsigned long

    GLuint,Glenum,GLbitfield

  • 8/4/2019 Lect1_Gii thiu OpenGL

    29/35

    Mu trong OpenGL

    glColor3f(red, green, blue);

    // set drawing color glColor3f(1.0, 0.0, 0.0); // red

    glColor3f(0.0, 1.0, 0.0); // green

    glColor3f(0.0, 0.0, 1.0); // blue

    glColor3f(0.0, 0.0, 0.0); // black

    glColor3f(1.0, 1.0, 1.0); // bright white

    glColor3f(1.0, 1.0, 0.0); // bright yellow

    glColor3f(1.0, 0.0, 1.0); // magenta

  • 8/4/2019 Lect1_Gii thiu OpenGL

    30/35

    Mu nn trong OpenGL

    glClearColor (red, green, blue, alpha);

    Thit lp mu nn.

    Alpha trong sut; ban u s dng 0.0 .

    glClear(GL_COLOR_BUFFER_BIT);

    Thit lp ca s thnh mu nn.

  • 8/4/2019 Lect1_Gii thiu OpenGL

    31/35

    Cu hnh h trc ta

    void myInit(void)

    {

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(0, 640.0, 0, 480.0);

    }

    // sets up coordinate system for window from(0,0) to (679, 479)

  • 8/4/2019 Lect1_Gii thiu OpenGL

    32/35

    V ng thng

    glBegin (GL_LINES); //draws one line

    glVertex2i (40, 100); // between 2 vertices

    glVertex2i (202, 96);

    glEnd (); glFlush();

    Nu c hn 2 nh trong cp

    glBegin(GL_LINES) v glEnd() chng cghp thnh cp v tng on thng tch ri sc v ni gia cc cp ny.

  • 8/4/2019 Lect1_Gii thiu OpenGL

    33/35

    Mi lin h gia cc thit b

  • 8/4/2019 Lect1_Gii thiu OpenGL

    34/35

    Mi lin h gia cc thit b (2)

    frame buffer

    pixel at address [x,y]

    0 639

    479

    0 x

    ydisplay surface

    scancontroller

    x

    y

    x

    y

    spot at (x,y)

    geometricposition

    logicaladdress

    at (639, 0)

    at (639, 479)

    convert pixelvalue to color

  • 8/4/2019 Lect1_Gii thiu OpenGL

    35/35

    Mi lin h gia cc thit b (3)

    Based on cathode ray tube (CRT).

    framebuffer(6 planes)

    DAC's

    scan controller

    x

    y

    y

    x

    deflectioncoils

    electronbeamguns

    red

    green

    bluespot

    pixelvalue