Docstoc

opengl

Document Sample
opengl Powered By Docstoc
					OpenGL 라이브러리
 OpenGL 관련 사이트 및
 프로그래밍 실습 예제


http://www.opengl.org

http://www.cs.utah.edu/~narobins/opengl.html
OpenGL


       Part 1.
    Introduction
Prerequisites
   Event-driven programming 에 대한 이해
   C / C++ 언어 사용 능력

   Win32 Operating System
   Visual Studio

   & nothing…
What do I want to do ?
   Ex) 2차원 그래픽 드로잉 프로그램을 만들고 싶
    다.

       Mouse, keyboard, menu 등을 통한 interaction

       기본적인 drawing primitive 들 제공

       간단한 animation 기능 제공
You can use OpenGL, But…
   OpenGL 을 이용하지 않아도 이것들을 충분히 할
    수 있다.

       DirectX Graphics component

       Win32 GDI
Then, why OpenGL ?
   OpenGL 을 이용하면 이것보다 훨씬 더 많은 것
    들을 할 수 있다.

       OpenGL = 3D graphics rendering API

       But, DirectX Graphics ≈ OpenGL ?

       OpenGL = window system independent API

       OpenGL = operating system independent API
OpenGL solutions ( on win32 )

   일반적으로 다음의 세 가지 선택 가능성이 있다.

       Win32 API + OpenGL

       MFC + OpenGL

       GLUT + OpenGL → We’ll choose this !
What is GLUT ?
   Mark J. Kilgard 가 개발한 portable
    windowing API
       대부분의 window system 에 보편적인 기능들을
        wrapping 한 상위 interface 를 제공

       Win32 를 비롯한 많은 window system 에 대해
        implementation 이 이루어져 있음

       OpenGL 이 제공하는 범위보다 상위 수준의 utility
        function 들도 제공함
Then, why GLUT ?
   Pros
       Window system independent code
        → UNIX / X-window 에서 개발된 수많은 code 들
        을 그대로 재사용할 수 있다.

       매우 쓰기 쉽다
        → Win32 API 도, MFC 도, Xlib 도 전혀 알 필요 없
        다.

   Cons
       Window system 의 기능을 제한적으로 이용 가능
OpenGL


            Part 2.
First experience with OpenGL
Simplest OpenGL sample
Simplest OpenGL sample
#include <GL/glut.h>
void display()
{
    glClear( GL_COLOR_BUFFER_BIT );
    glBegin( GL_TRIANGLES );
        glColor3f( 1.0f, 1.0f, 1.0f );
        glVertex2f( -0.8f, -0.5f );
        glVertex2f( 0.0f, 0.8f );
        glVertex2f( 0.8f, -0.5f );
    glEnd();
    glFlush();
}
int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutCreateWindow( "Simplest OpenGL sample" );
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}
You’ll learn these, step by
step
1) OpenGL+GLUT downloading &
   installation
2) Project generation & setup on
   Visual Studio
3) Basic template of the general
   GLUT program
4) Callback functions of the general
   GLUT program
5) OpenGL Basic
Step 1 : OpenGL + GLUT
   OpenGL
    1)   You already have OpenGL dll on your Win32
         system
         X:\WINDOWS\SYSTEM32\OpenGL32.dll


    2)   You also have OpenGL lib & header in your
         Visual Studio directory
         X:\…\VisualStudio\VC98\Lib\OpenGL32.lib
         X:\…\VisualStudio\VC98\Include\GL\gl.h
Step 1 : OpenGL + GLUT
   GLUT
    1)   Perhaps, you don’t have GLUT library

    2)   You can download GLUT library at,
         http://reality.sgi.com/mjk/glut3/glut3.h
         tml

    3)   You should place the unzipped files at,
         X:\WINDOWS\SYSTEM32\glut32.dll
         X:\…\VisualStudio\VC98\Lib\glut32.lib
         X:\…\VisualStudio\VC98\Include\GL\glut.h
Step 2 : Project on VS

1)   File / New / Projects / Win32 Console Application

2)   Project / Settings / Link / Object&library
     modules
     append : opengl32.lib glu32.lib glut32.lib

    Make new files
     & add those to the project
     & compile, link
     & so on …
Step 3 : GLUT template
   Win32 API template
    1)   WinMain()
    2)   Message pump
    3)   Registers a Window class, including WndProc()
    4)   WndProc() receives & processes messages

   GLUT template
    1)   main()  normal C program
    2)   glutMainLoop()
    3)   Registers some callback functions
    4)   Callback functions are called with some
         values
Step 3 : GLUT template
   main()
    int main( int argc, char** argv )
    {
       glutInit( &argc, argv );
       initWindow();
       initCallbackFunctions();
       initMenu();
       initOpenGL();
       glutMainLoop();
    }
Step 3 : GLUT template
   initWindow()

    void initWindow()
    {
       glutInitWindowSize( 512, 512 );
       glutInitWindowPosition( 0, 0 );
       glutInitDisplayMode(
             GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
       glutCreateWindow( “Simple OpenGL sample" );
    }
Step 3 : GLUT template
   initCallbackFunctions()

    void initCallbackFunctions()
    {
       glutReshapeFunc( reshape );
       glutDisplayFunc( display );
       glutKeyboardFunc( keyboard );
       glutMouseFunc( mouse );
       // & many other callback functions can be
       added
    }
Step 3 : GLUT template
   initMenu()

    void initMenu()
    {
       int h_submenu = glutCreateMenu( submenu );
       int h_menu = glutCreateMenu( menu );
       glutAddSubMenu( “submenu”, h_submenu );
       glutAddMenuEntry( “exit”, 0 );
       glutAttachMenu( GLUT_RIGHT_BUTTON );
    }
Step 3 : GLUT template
   initOpenGL()

       Initialize lights, materials, etc. …
Step 4 : Callback functions
   Frequently used callback functions
    void   reshape( int w, int h );
    void   display();
    void   keyboard( unsigned char key, int x, int
       y   );
    void   mouse( int btn, int state, int x, int y );
    void   timer( int timer_id );
    void   menu( int value );
    void   idle();

    ( You should refer to the GLUT API
       spec !!! )
Step 4 : Callback functions
   Reshape ( WM_SIZE )

    void reshape( int w, int h )
    {
       // GLUT calls this function when,
       // window size has changed..
       // - w : window width
       // - h : window height
    }
Step 4 : Callback functions
   Display ( WM_PAINT )
    void display()
    {
       // GLUT calls this function when,
       // window contents need to be re-drawn..
    }

       Related APIs
           glutPostRedisplay()
           glutSwapBuffers()
Step 4 : Callback functions
   Keyboard ( WM_KEYDOWN )

    void keyboard( unsigned char key, int x, int y )
    {
       // GLUT calls this function when,
       // user presses keyboard..
       // - key : ASCII character code
       // - x, y : mouse location when key is pressed
    }
Step 4 : Callback functions
   Mouse ( WM_[ L/R ]BUTTON[ DOWN/UP ] )

    void mouse( int button, int state, int x, int y )
    {
       // GLUT calls this function when,
       // user presses or releases mouse buttons..
       // - button : GLUT_[ RIGHT/MIDDLE/LEFT ]_BUTTON
       // - state : GLUT_DOWN | GLUT_UP
       // - x, y : mouse location when the state
    changed
    }
Step 4 : Callback functions
   Timer ( WM_TIMER )

    void timer( int value )
    {
       // GLUT calls this function when,
       // the registered timer has expired..
       // - value : registered timer ID
    }

       glutTimerFunc( unsigned int msecs, void
        (*func)(int value), int value )
Step 4 : Callback functions
   Menu ( WM_COMMAND )

    void menu( int value )
    {
       // GLUT calls this function when,
       // a menu entry is selected from the menu..
       // - value : selected menu entry’s ID
    }
Step 4 : Callback functions
   Idle ( PeekMessage(...)==0 )

    void idle()
    {
       // GLUT calls this function when,
       // it doesn’t have nothing to do..
    }
Step 5 : OpenGL Basic
   Drawing Primitives
       You can draw “ready-made” primitives
        which OpenGL provides

       In fact, you only gives “vertices” to OpenGL

       & inform that “what the vertices compose”

       Then, what kind of “Primitive” s ?
  Step 5 : OpenGL Basic



            GL_LINES
                                                         GL_POLYGON
                       GL_LINE_STRIP   GL_LINE_LOOP
GL_POINTS



                GL_TRIANGLES

                                            GL_QUADS
                                                       GL_QUAD_STRIP
GL_TRIANGLE_STRIP         GL_TRIANGLE_FAN
Step 5 : OpenGL Basic
   How to code ?

    void display()
    {
       glBegin( GL_xxxx );
       glVertex3d( 0.0, 1.0, 0.0 );
       // ...
       glVertex3d( -1.0, 1.0, 0.0 );
       glEnd();
    }
 Step 5 : OpenGL Basic


                glVertex3fv( v )



Number of              Data Type                 Vector
components        b    -   byte                omit “v” for
                  ub   -   unsigned byte
2 - (x,y)                                       scalar form
                  s    -   short
3 - (x,y,z)
                  us   -   unsigned short
4 - (x,y,z,w)                               glVertex2f( x, y )
                  i    -   int
                  ui   -   unsigned int
                  f    -   float
                  d    -   double
Step 5 : OpenGL Basic
   But, I want to

       control the shape of the primitive
        ( points ? lines ? or fill ? )
       control the size of the point ( or vertices )
       control the width of the line
       control the color of the filling
       & much more, much more...

   You should know this !

       OpenGL is a State Machine...
Step 5 : OpenGL Basic
   Then, how to code ?
    void display()
    {
       glBegin( GL_XXXX );
       glColor3d( 1.0, 0.0, 0.0 );
       glVertex3d( 0.0, 0.0, 0.0 );
       // ...
       glColor3d( 0.0, 0.0, 1.0 );
       glVertex3d( -1.0, 1.0, 0.0 );
       glEnd();
    }
Step 5 : OpenGL Basic
Step 5 : OpenGL Basic
Step 5 : OpenGL Basic
Step 5 : OpenGL Basic
   Is that all ?

       Complie, and link, and execute...
       Of course, nothing appears...


   Why ?

       Where do I draw them ?
       You should know some OpenGL buffers !
       Color buffer, Depth buffer, and Double
        buffering...
Step 5 : OpenGL Basic
   OpenGL Buffers

       Color buffer ; stores color values, to
        appear in screen

       Depth buffer ; stores depth values, to
        accomplish hidden surface removal

       Double buffering ; front & back buffer,
        chaining, to accomplish flicker-free
        animation
Step 5 : OpenGL Basic
   So, how to code ?
       Remember the function initWindow()
        glutInitDisplayMode(
             GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE
        );

       Then, insert & append next 2 lines in
        display()
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUF
        FER_BIT);
        // ...from glBegin() to glEnd()...
        glutSwapBuffers();
Step 5 : OpenGL Basic
   We did it !!! but...

       Resizing, minimizing or maximizing window
        makes the image ugly...


   Why ?

       You should remember that OpenGL is a 3D API,
        not 2D
       It means, you don’t draw directly on the window
       Rather, a camera takes a picture of the 3D space
Step 5 : OpenGL Basic
    Camera analogy


                      viewing
                      volume

camera


                          model
    tripod
Step 5 : OpenGL Basic
   3D rendering pipeline
       We’ll play with 2D objects for a while

       But, OpenGL is a 3D rendering API in itself,
        never a 2D drawing API

       You’ll learn the complete 3D rendering pipeline
        in on-going class

       At now, I’ll introduce it very simply, so you can
        understand some OpenGL commands
Step 5 : OpenGL Basic
   3D rendering pipeline
Step 5 : OpenGL Basic
   3D rendering pipeline
       Seeing is believing : Projection
        http://www.xmission.com/~nate/tutors.htm
        l
Step 5 : OpenGL Basic
   Camera analogy & Transformations
       Projection transformations
        adjust the lens of the camera
       Viewing transformations
        tripod–define position and orientation
        of the viewing volume in the world
       Modeling transformations
        moving the model
       Viewport transformations
        enlarge or reduce the physical film
Step 5 : OpenGL Basic
   Transformation
       Common senses about OpenGL
        transformation
           Transformation = Matrix
           Matrix multiplication = Matrix stack
           Two matrix mode : Projection / Model-View

       Common OpenGL commands about Matrices
        glMatrixMode( GL_PROJECTION|GL_MODELVIEW );
        glLoadIdentity();
        glPushMatrix();
        glPopMatrix();
Step 5 : OpenGL Basic
   Projection Transformation
       Orthographic projection
          Adequate for our 2D examples
        glOrtho( left, right, bottom, top, near,
           far );
        gluOrtho2D( left, right, bottom, top );

       Perspective projection
          Adequate for realistic 3D examples
        glFrustum( left, right, bottom, top, near,
           far );
        gluPerspective( fovy, aspect, near, far );
Step 5 : OpenGL Basic
   Question ?

       We’ve got a little understanding of “ 3D
        rendering pipeline ”

       Then, how can we solve the previous image
        “disappearance” & “distortion” problem ?

       Hint : reshape() callback function
Step 5 : OpenGL Basic
   Answer !

    void reshape( int w, int h )
    {
       // next line solves “disappearance” problem

        glViewport( 0, 0, w, h );

        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glOrtho( -w/2, w/2, -h/2, h/2, -1, 1);
    }
Reference

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:50
posted:5/3/2011
language:Korean
pages:54