Open GL http www opengl org http www

  • Slides: 54
Download presentation

Open. GL 관련 사이트 및 프로그래밍 실습 예제 http: //www. opengl. org http: //www.

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

Open. GL Part 1. Introduction

Open. GL Part 1. Introduction

Prerequisites Event-driven programming 에 대한 이해 ã C / C++ 언어 사용 능력 ã

Prerequisites Event-driven programming 에 대한 이해 ã C / C++ 언어 사용 능력 ã Win 32 Operating System ã Visual Studio ã ã & nothing…

What do I want to do ? ã Ex) 2차원 그래픽 드로잉 프로그램을 만들고

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

You can use Open. GL, But… ã Open. GL 을 이용하지 않아도 이것들을 충분히

You can use Open. GL, But… ã Open. GL 을 이용하지 않아도 이것들을 충분히 할 수 있다. Ý Direct. X Graphics component Ý Win 32 GDI

Then, why Open. GL ? ã Open. GL 을 이용하면 이것보다 훨씬 더 많은

Then, why Open. GL ? ã Open. GL 을 이용하면 이것보다 훨씬 더 많은 것 들을 할 수 있다. Ý Open. GL = 3 D graphics rendering API Ý But, Direct. X Graphics ≈ Open. GL ? Ý Open. GL = window system independent API Ý Open. GL = operating system independent API

Open. GL solutions ( on win 32 ) ã 일반적으로 다음의 세 가지 선택

Open. GL solutions ( on win 32 ) ã 일반적으로 다음의 세 가지 선택 가능성이 있다. Ý Win 32 API + Open. GL Ý MFC + Open. GL Ý GLUT + Open. GL → We’ll choose this !

What is GLUT ? ã Mark J. Kilgard 가 개발한 portable windowing API Ý

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

Open. GL Part 2. First experience with Open. GL

Open. GL Part 2. First experience with Open. GL

Simplest Open. GL sample

Simplest Open. GL sample

Simplest Open. GL sample #include <GL/glut. h> void display() { gl. Clear( GL_COLOR_BUFFER_BIT );

Simplest Open. GL sample #include <GL/glut. h> void display() { gl. Clear( GL_COLOR_BUFFER_BIT ); gl. Begin( GL_TRIANGLES ); gl. Color 3 f( 1. 0 f, 1. 0 f ); gl. Vertex 2 f( -0. 8 f, -0. 5 f ); gl. Vertex 2 f( 0. 0 f, 0. 8 f ); gl. Vertex 2 f( 0. 8 f, -0. 5 f ); gl. End(); gl. Flush(); } int main(int argc, char** argv) { glut. Init( &argc, argv ); glut. Create. Window( "Simplest Open. GL sample" ); glut. Display. Func( display ); glut. Main. Loop(); return 0; }

You’ll learn these, step by step 1) 2) 3) 4) 5) Open. GL+GLUT downloading

You’ll learn these, step by step 1) 2) 3) 4) 5) Open. GL+GLUT downloading & installation Project generation & setup on Visual Studio Basic template of the general GLUT program Callback functions of the general GLUT program Open. GL Basic

Step 1 : Open. GL + GLUT q Open. GL 1) You already have

Step 1 : Open. GL + GLUT q Open. GL 1) You already have Open. GL dll on your Win 32 system X: WINDOWSSYSTEM 32Open. GL 32. dll 2) You also have Open. GL lib & header in your Visual Studio directory X: …Visual. StudioVC 98LibOpen. GL 32. lib X: …Visual. StudioVC 98IncludeGLgl. h

Step 1 : Open. GL + GLUT q GLUT 1) Perhaps, you don’t have

Step 1 : Open. GL + GLUT q GLUT 1) Perhaps, you don’t have GLUT library 2) You can download GLUT library at, http: //reality. sgi. com/mjk/glut 3. h tml 3) You should place the unzipped files at, X: WINDOWSSYSTEM 32glut 32. dll X: …Visual. StudioVC 98Libglut 32. lib X: …Visual. StudioVC 98IncludeGLglut. h

Step 2 : Project on VS 1) File / New / Projects / Win

Step 2 : Project on VS 1) File / New / Projects / Win 32 Console Application 2) Project / Settings / Link / Object&library modules append : opengl 32. lib glut 32. lib ã Make new files & add those to the project & compile, link & so on …

Step 3 : GLUT template q Win 32 API template Win. Main() 2) Message

Step 3 : GLUT template q Win 32 API template Win. Main() 2) Message pump 3) Registers a Window class, including Wnd. Proc() 4) Wnd. Proc() receives & processes messages 1) q GLUT template main() normal C program 2) glut. Main. Loop() 3) Registers some callback functions 4) Callback functions are called with some values 1)

Step 3 : GLUT template q main() int main( int argc, char** argv )

Step 3 : GLUT template q main() int main( int argc, char** argv ) { glut. Init( &argc, argv ); init. Window(); init. Callback. Functions(); init. Menu(); init. Open. GL(); glut. Main. Loop(); }

Step 3 : GLUT template q init. Window() void init. Window() { glut. Init.

Step 3 : GLUT template q init. Window() void init. Window() { glut. Init. Window. Size( 512, 512 ); glut. Init. Window. Position( 0, 0 ); glut. Init. Display. Mode( GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glut. Create. Window( “Simple Open. GL sample" ); }

Step 3 : GLUT template q init. Callback. Functions() void init. Callback. Functions() {

Step 3 : GLUT template q init. Callback. Functions() void init. Callback. Functions() { glut. Reshape. Func( reshape ); glut. Display. Func( display ); glut. Keyboard. Func( keyboard ); glut. Mouse. Func( mouse ); // & many other callback functions can be added }

Step 3 : GLUT template q init. Menu() void init. Menu() { int h_submenu

Step 3 : GLUT template q init. Menu() void init. Menu() { int h_submenu = glut. Create. Menu( submenu ); int h_menu = glut. Create. Menu( menu ); glut. Add. Sub. Menu( “submenu”, h_submenu ); glut. Add. Menu. Entry( “exit”, 0 ); glut. Attach. Menu( GLUT_RIGHT_BUTTON ); }

Step 3 : GLUT template q init. Open. GL() ü Initialize lights, materials, etc.

Step 3 : GLUT template q init. Open. GL() ü Initialize lights, materials, etc. …

Step 4 : Callback functions q Frequently used callback functions void reshape( int w,

Step 4 : Callback functions q 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 q Reshape ( WM_SIZE ) void { // //

Step 4 : Callback functions q 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 q Display ( WM_PAINT ) void display() { //

Step 4 : Callback functions q Display ( WM_PAINT ) void display() { // GLUT calls this function when, // window contents need to be re-drawn. . } q Related APIs glut. Post. Redisplay() ü glut. Swap. Buffers() ü

Step 4 : Callback functions q Keyboard ( WM_KEYDOWN ) void keyboard( unsigned char

Step 4 : Callback functions q 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 q Mouse ( WM_[ L/R ]BUTTON[ DOWN/UP ] )

Step 4 : Callback functions q 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 q Timer ( WM_TIMER ) void timer( int value

Step 4 : Callback functions q Timer ( WM_TIMER ) void timer( int value ) { // GLUT calls this function when, // the registered timer has expired. . // - value : registered timer ID } ü glut. Timer. Func( unsigned int msecs, void (*func)(int value), int value )

Step 4 : Callback functions q Menu ( WM_COMMAND ) void menu( int value

Step 4 : Callback functions q 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 q Idle ( Peek. Message(. . . )==0 )

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

Step 5 : Open. GL Basic q Drawing Primitives ü You can draw “ready-made”

Step 5 : Open. GL Basic q Drawing Primitives ü You can draw “ready-made” primitives which Open. GL provides ü In fact, you only gives “vertices” to Open. GL ü & inform that “what the vertices compose” ü Then, what kind of “Primitive” s ?

Step 5 : Open. GL Basic GL_LINES GL_LINE_STRIP GL_POINTS GL_LINE_LOOP GL_POLYGON GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_FAN

Step 5 : Open. GL Basic GL_LINES GL_LINE_STRIP GL_POINTS GL_LINE_LOOP GL_POLYGON GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_FAN

Step 5 : Open. GL Basic q How to code ? void display() {

Step 5 : Open. GL Basic q How to code ? void display() { gl. Begin( GL_xxxx ); gl. Vertex 3 d( 0. 0, 1. 0, 0. 0 ); //. . . gl. Vertex 3 d( -1. 0, 0. 0 ); gl. End(); }

Step 5 : Open. GL Basic gl. Vertex 3 fv( v ) Number of

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

Step 5 : Open. GL Basic q But, I want to ü control the

Step 5 : Open. GL Basic q 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. . . ü ü ü q You should know this ! ü Open. GL is a State Machine. . .

Step 5 : Open. GL Basic q Then, how to code ? void display()

Step 5 : Open. GL Basic q Then, how to code ? void display() { gl. Begin( GL_XXXX ); gl. Color 3 d( 1. 0, 0. 0 ); gl. Vertex 3 d( 0. 0, 0. 0 ); //. . . gl. Color 3 d( 0. 0, 1. 0 ); gl. Vertex 3 d( -1. 0, 0. 0 ); gl. End(); }

Step 5 : Open. GL Basic

Step 5 : Open. GL Basic

Step 5 : Open. GL Basic

Step 5 : Open. GL Basic

Step 5 : Open. GL Basic

Step 5 : Open. GL Basic

Step 5 : Open. GL Basic q Is that all ? ü ü q

Step 5 : Open. GL Basic q Is that all ? ü ü q Complie, and link, and execute. . . Of course, nothing appears. . . Why ? ü ü ü Where do I draw them ? You should know some Open. GL buffers ! Color buffer, Depth buffer, and Double buffering. . .

Step 5 : Open. GL Basic q Open. GL Buffers ü Color buffer ;

Step 5 : Open. GL Basic q Open. GL 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 : Open. GL Basic q So, how to code ? ü Remember

Step 5 : Open. GL Basic q So, how to code ? ü Remember the function init. Window() glut. Init. Display. Mode( GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE ); ü Then, insert & append next 2 lines in display() gl. Clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUF FER_BIT); //. . . from gl. Begin() to gl. End(). . . glut. Swap. Buffers();

Step 5 : Open. GL Basic q We did it !!! but. . .

Step 5 : Open. GL Basic q We did it !!! but. . . ü q Resizing, minimizing or maximizing window makes the image ugly. . . Why ? ü ü ü You should remember that Open. GL is a 3 D API, not 2 D It means, you don’t draw directly on the window Rather, a camera takes a picture of the 3 D space

Step 5 : Open. GL Basic q Camera analogy viewing volume camera tripod model

Step 5 : Open. GL Basic q Camera analogy viewing volume camera tripod model

Step 5 : Open. GL Basic q 3 D rendering pipeline ü We’ll play

Step 5 : Open. GL Basic q 3 D rendering pipeline ü We’ll play with 2 D objects for a while ü But, Open. GL is a 3 D rendering API in itself, never a 2 D drawing API ü You’ll learn the complete 3 D rendering pipeline in on-going class ü At now, I’ll introduce it very simply, so you can understand some Open. GL commands

Step 5 : Open. GL Basic q 3 D rendering pipeline

Step 5 : Open. GL Basic q 3 D rendering pipeline

Step 5 : Open. GL Basic q 3 D rendering pipeline ü Seeing is

Step 5 : Open. GL Basic q 3 D rendering pipeline ü Seeing is believing : Projection http: //www. xmission. com/~nate/tutors. htm l

Step 5 : Open. GL Basic q Camera analogy & Transformations q Projection transformations

Step 5 : Open. GL Basic q Camera analogy & Transformations q Projection transformations adjust the lens of the camera q Viewing transformations tripod–define position and orientation of the viewing volume in the world q Modeling transformations moving the model q Viewport transformations enlarge or reduce the physical film

Step 5 : Open. GL Basic q Transformation q Common senses about Open. GL

Step 5 : Open. GL Basic q Transformation q Common senses about Open. GL transformation Transformation = Matrix ü Matrix multiplication = Matrix stack ü Two matrix mode : Projection / Model-View ü q Common Open. GL commands about Matrices gl. Matrix. Mode( GL_PROJECTION|GL_MODELVIEW ); gl. Load. Identity(); gl. Push. Matrix(); gl. Pop. Matrix();

Step 5 : Open. GL Basic q Projection Transformation q Orthographic projection Adequate for

Step 5 : Open. GL Basic q Projection Transformation q Orthographic projection Adequate for our 2 D examples gl. Ortho( left, right, bottom, top, near, far ); glu. Ortho 2 D( left, right, bottom, top ); ü q Perspective projection Adequate for realistic 3 D examples gl. Frustum( left, right, bottom, top, near, far ); glu. Perspective( fovy, aspect, near, far ); ü

Step 5 : Open. GL Basic q Question ? q We’ve got a little

Step 5 : Open. GL Basic q Question ? q We’ve got a little understanding of “ 3 D rendering pipeline ” q Then, how can we solve the previous image “disappearance” & “distortion” problem ? q Hint : reshape() callback function

Step 5 : Open. GL Basic q Answer ! void reshape( int w, int

Step 5 : Open. GL Basic q Answer ! void reshape( int w, int h ) { // next line solves “disappearance” problem gl. Viewport( 0, 0, w, h ); gl. Matrix. Mode( GL_PROJECTION ); gl. Load. Identity(); gl. Ortho( -w/2, -h/2, -1, 1); }

Reference

Reference