1 / 54

OpenGL 라이브러리

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 ?.

lucie
Download Presentation

OpenGL 라이브러리

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OpenGL 라이브러리

  2. OpenGL 관련 사이트 및프로그래밍 실습 예제 http://www.opengl.org http://www.cs.utah.edu/~narobins/opengl.html

  3. OpenGL Part 1. Introduction

  4. Prerequisites • Event-driven programming 에 대한 이해 • C / C++ 언어 사용 능력 • Win32 Operating System • Visual Studio • & nothing…

  5. What do I want to do ? • Ex) 2차원 그래픽 드로잉 프로그램을 만들고 싶다. • Mouse, keyboard, menu 등을 통한 interaction • 기본적인 drawing primitive 들 제공 • 간단한 animation 기능 제공

  6. You can use OpenGL, But… • OpenGL 을 이용하지 않아도 이것들을 충분히 할 수 있다. • DirectX Graphics component • Win32 GDI

  7. Then, why OpenGL ? • OpenGL 을 이용하면 이것보다 훨씬 더 많은 것들을 할 수 있다. • OpenGL = 3D graphics rendering API • But, DirectX Graphics ≈ OpenGL ? • OpenGL = window system independent API • OpenGL = operating system independent API

  8. OpenGL solutions ( on win32 ) • 일반적으로 다음의 세 가지 선택 가능성이 있다. • Win32 API + OpenGL • MFC + OpenGL • GLUT + OpenGL → We’ll choose this !

  9. What is GLUT ? • Mark J. Kilgard 가 개발한 portable windowing API • 대부분의 window system 에 보편적인 기능들을 wrapping 한 상위 interface 를 제공 • Win32 를 비롯한 많은 window system 에 대해 implementation 이 이루어져 있음 • OpenGL 이 제공하는 범위보다 상위 수준의 utility function 들도 제공함

  10. Then, why GLUT ? • Pros • Window system independent code → UNIX / X-window 에서 개발된 수많은 code 들을 그대로 재사용할 수 있다. • 매우 쓰기 쉽다 → Win32 API 도, MFC 도, Xlib 도 전혀 알 필요 없다. • Cons • Window system 의 기능을 제한적으로 이용 가능

  11. OpenGL Part 2. First experience with OpenGL

  12. Simplest OpenGL sample

  13. 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; }

  14. You’ll learn these, step by step • OpenGL+GLUT downloading & installation • Project generation & setup on Visual Studio • Basic template of the general GLUT program • Callback functions of the general GLUT program • OpenGL Basic

  15. Step 1 : OpenGL + GLUT • OpenGL • You already have OpenGL dll on your Win32 system X:\WINDOWS\SYSTEM32\OpenGL32.dll • You also have OpenGL lib & header in your Visual Studio directory X:\…\VisualStudio\VC98\Lib\OpenGL32.lib X:\…\VisualStudio\VC98\Include\GL\gl.h

  16. Step 1 : OpenGL + GLUT • GLUT • Perhaps, you don’t have GLUT library • You can download GLUT library at, http://reality.sgi.com/mjk/glut3/glut3.html • 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

  17. Step 2 : Project on VS • File / New / Projects / Win32 Console Application • 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 …

  18. Step 3 : GLUT template • Win32 API template • WinMain() • Message pump • Registers a Window class, including WndProc() • WndProc() receives & processes messages • GLUT template • main()  normal C program • glutMainLoop() • Registers some callback functions • Callback functions are called with some values

  19. Step 3 : GLUT template • main() int main( int argc, char** argv ) { glutInit( &argc, argv ); initWindow(); initCallbackFunctions(); initMenu(); initOpenGL(); glutMainLoop(); }

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

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

  22. 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 ); }

  23. Step 3 : GLUT template • initOpenGL() • Initialize lights, materials, etc. …

  24. 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 !!! )

  25. 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 }

  26. 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()

  27. 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 }

  28. 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 }

  29. 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 )

  30. 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 }

  31. Step 4 : Callback functions • Idle ( PeekMessage(...)==0 ) void idle() { // GLUT calls this function when, // it doesn’t have nothing to do.. }

  32. 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 ?

  33. GL_LINES GL_POLYGON GL_LINE_STRIP GL_LINE_LOOP GL_POINTS GL_TRIANGLES GL_QUADS GL_TRIANGLE_FAN GL_TRIANGLE_STRIP GL_QUAD_STRIP Step 5 : OpenGL Basic

  34. 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(); }

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

  36. 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...

  37. 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(); }

  38. Step 5 : OpenGL Basic

  39. Step 5 : OpenGL Basic

  40. Step 5 : OpenGL Basic

  41. 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...

  42. 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

  43. 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_BUFFER_BIT); // ...from glBegin() to glEnd()... glutSwapBuffers();

  44. 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

  45. Step 5 : OpenGL Basic • Camera analogy viewing volume camera model tripod

  46. 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

  47. Step 5 : OpenGL Basic • 3D rendering pipeline

  48. Step 5 : OpenGL Basic • 3D rendering pipeline • Seeing is believing : Projection http://www.xmission.com/~nate/tutors.html

  49. 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

  50. 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();

More Related