OpenGL Programming Getting Started 1 st Week, 2008 Sun-Jeong Kim Visual Studio 2005 Windows Programming 2
Visual C++ Win32 Application New Project 3 Empty project Application Settings 4
Solution Prac01 5 Downloading main0.cpp 6
Adding a Source File (1) Project Add Existing Item 7 Adding a Source File (2) 8
Building the Solution 9 Compiling Errors 10
Unicode vs. Multi-Byte SBCS (Single Byte Character Set) ASCII code: 1 byte character set DBCS (Double Byte Character Set) Korean MBCS (Multi Byte Character Set) SBCS + DBCS: ~ Windows 9x WBCS (Wide Byte Character Set) Unicode: Windows 2000 ~ 11 Project Properties 12
Result main0.cpp 13 main0.cpp #include <windows.h> HWND MyMWindowHandle = 0; LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE, LPSTR, int nshowcmd ) { // Create the main window } // Main message loop LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) { } 14
Includes, Global Variables, and Prototypes Includes #include <windows.h> Obtaining the structures, types, and function declarations needed for using the basic elements of the Win32 API Global Variables HWND MainWindowHandle = 0; Handle to a window to refer our main application window Prototypes LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); Our main window s window procedure 15 WinMain ( ) The Windows equivalent to the main() in normal C++ programming int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, PSTR pcmdline, int nshowcmd ); hinstance: Handle to the current application instance hprevinstance: Not used in Win32 programming lpcmdline: The command line argument string used to run the program ncmdshow: Specifying how the application should be displayed 16
WNDCLASS (1) Describing our window with WNDCLSS typedef struct _WNDCLASS { UINT style; WNDPROC lpfnwndproc; int cbclsextra; int cbwndextra; HANDLE hinstance; HICON hicon; HCURSOR hcursor; HBRUSH hbrbackground; LPCTSTR lpszmenuname; LPCTSTR lpszclassname; } WNDCLASS; style: Specifying the class style lpfnwndproc: Pointer to the window procedure function cbclsextra and cbwndextra: Extra memory slots 17 WNDCLASS (2) hinstance: A handle to our application instance hicon: Specifying a handle to an icon hcursor: Specifying a handle to a cursor hbrbackground: Specifying the background of the client area of the window lpszmenuname: Specifying the window s menu lpszclassname: Specifying the name of the window class structure 18
Creating the Window (1) Creating a window based on WNDCLASS description HWND CreateWindow( LPCTSTR lpclassname, LPCTSTR lpwindowname, DWORD dwstyle, int x, int y, int nwidth, int nheight, HWND hwndparent, HMENU hmenu, HANDLE hinstance, PVOID lpparam ); lpclassname: : The name of the registered WNDCLASS structure lpwindowname: The name we want to give our window 19 Creating the Window (2) dwstyle: Defining the style of window x: The position at the top-left corner of the window y: The position at the top-left corner of the window nwidth: The width of the window in pixels nheight: The height of the window in pixels hwndparent: Handle to a window that is to be the parent of this window hmenu: A handle to menu hinstance: Handle to the application lpparam: A pointer to user-defined data 20
The Message Loop (1) The message structure that represents a Windows message typedef struct tagmsg { HWND hwnd; UINT message; WPARAM wparam; LPARAM lparam; DWORD time; POINT pt; } MSG; hwnd: The handle to the window message: A predefined constant value identifying the message (e.g. WM_QUIT) wparam and lparam: : Extra information time: The time the message was posted pt: The (x, y) coordinates of the mouse cursor 21 The Message Loop (2) Entering the message loop while( GetMessage( &msg, 0, 0, 0 ) ) { TranslateMessage( &msg g ); DispatchMessage( &msg ); } GetMessage() Returning true unless a WM_QUIT Retrieving a message from the message queue Filling in members of MSG structure TranslateMessage() Performing some keyboard translation and virtual key messages to character messages specifically DispatchMessage() Dispatching the message off to the appropriate window proc. 22
The Window Procedure (1) Executing in response to a message our window LRESULT CALLBACK WndProc( receives ); HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam Callback function: Windows will be calling this function outside of the code space of the program hwnd: The handle to the window receiving the message umsg: A predefined value that identifies the particular message (e.g. WM_QUIT) wparam and lparam: Extra information about the message 23 The Window Procedure (2) Handling three messages: switch( msg ) { case WM_KEYDOWN: if( wparam == VK_ESCAPE ) if( IDYES == MessageBox( NULL, "Are you sure to exit?", "Hello", MB_YESNO ) ) DestroyWindow( MyMWindowHandle ); break; case WM_DESTROY: PostQuitMessage( 0 ); break; } default: return DefWindowProc( hwnd, msg, wparam, lparam ); WM_KEYDOWN: pressing a key (c.f. virtual key code) WM_DESTROY: destroying the window 24
The Window Procedure (3) Default window procedure return DefWindowProc( hwnd, msg, wparam, lparam ); Win32 API function Using default behavior for all other messages (e.g. minimizing, maximizing, resizing, and closing a window) 25 The Message Box Function Win32 API function int MessageBox( HWND hwnd, LPCTSTR lptext, LPCTSTR lpcaption, UINT utype ); hwnd: Handle of owner window (may specify null) lptext: Text to put in the message box lpcaption: : Text for the title of the message box utype: Style of the message box MessageBox( NULL, Are you sure to exit?", "Hello", MB_YESNO ); 26
Exercises (1) Window 의크기를 800x600 으로고정하시오. Window 의배경을검정색으로칠하시오. Window 의제목을자신의학번과이름으로고치시오. 화살표키를눌렀을때, 다음과같은 Message Box 를생성하시오. 좌측마우스버튼을클릭했을때다음과같은 Message Box를생성하시오. 27 Changing a Source File (1) 28
Changing a Source File (2) 29 Building the Solution 30
Compiling Errors 31 Linking the Library Files Project Properties Configuration Properties Linker Input Additional Dependencies: opengl32.lib lib glu32.lib lib 32
OpenGL API (1) API (application programming interface) Interface between an application program and a graphics system Application Program OpenGL interface Graphics Library (API) GLU Hardware Input Device Output Device OpenGL Application Program GL GLUT Xlib, Xtk Frame Buffer GLX 33 OpenGL interface OpenGL API (2) OpenGL core library (names : gl~) : GL OpenGL utility library (names : glu~) : GLU Using only GL functions Creating common objects Other tasks that users prefer not to write repeatedly OpenGL utility toolkit (names : glut~) : GLUT Interface with the window system 34
OpenGL API (3) OpenGL architecture Immediate Mode Geometry Pipeline Polynomial Evaluator Per Vertex Operations & Primitive Assembly CPU Display List Rasterization Per Fragment Operations Frame Buffer Pixel Operations Texture Memory 35 OpenGL API (4) OpenGL function format Function name Dimensions glvertex3f( x, y, z ); Belongs to GL library x, y, z are floats p is a pointer to an array glvertex3fv( p ); 36
Result main1.cpp 37 main1.cpp #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> HWND MyMWindowHandle dl = 0; HDC MyDC; HGLRC MyRC; LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); bool bsetuppixelformat( void ); void Resize( const int cx, const int cy ); void DrawScene( void ); int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE, LPSTR, int nshowcmd ) { // Create the main window // Main message loop } 38
OpenGL Camera OpenGL places a camera at the origin in world space pointing in the negative z direction Default viewing volume a box centered at the origin with a side of length 2 The default camera and an orthographic view volume 39 Orthographic Viewing Default orthographic view Projecting points forward along the z axis onto the plane z=0 z=0 z=0 View Volume Orthographic Projection 40
Viewing (1) In OpenGL, viewing is carried out by projection matrix First, set the matrix mode first Two most important matrices: model-view and projection glmatrixmode(gl_projection GL_PROJECTION); Start with an identity matrix Multiplying or concatenating a number of transformation matrices glloadidentity(); Alter it with a projection matrix Two- and three-dimensional viewing glortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 41 Viewing (2) Three-dimensional viewing glortho(left, right, bottom, top, near, far); near, far: distances measured from the camera Two-dimensional viewing Application i is in two dimension i Placing all vertices in the plane z=0 gluortho2d(left, right, bottom, top); View or clipping volume == clipping window Objects before clipping 42 Image after clipping
Viewport Viewports A rectangular area of the display window Values in pixels Aspect ratio of a rectangle glviewport(x, y, w, h); The ratio of the rectangle s width to its height 43 Points Lines Line segments Polylines Polygons Polygons Primitives in OpenGL Triangles and quadrilaterals Strips and fans Text Curved objects 44
Points in OpenGL glbegin(gl_points); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); p6 p2 glvertex2fv(p4); glvertex2fv(p5); p5 p3 glvertex2fv(p6); glvertex2fv(p7); p4 glend(); 45 Line Segments Lines in OpenGL (1) glbegin(gl_lines); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); p5 p3 glvertex2fv(p7); glend(); p4 46
Polylines Line Strip Lines in OpenGL (2) glbegin(gl_line_strip); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 47 Polylines Line Loop Lines in OpenGL (3) glbegin(gl_line_loop); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 48
Polygon Polygons in OpenGL (1) glbegin(gl_polygon); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 49 Quadrilaterals Polygons in OpenGL (2) glbegin(gl_quads); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 50
Quadstrip Polygons in OpenGL (3) glbegin(gl_quad_strip); glvertex2fv(p0); glvertex2fv(p1); p0 p1 p3 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p2 p5 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p4 p6 p7 51 Triangles Polygons in OpenGL (4) glbegin(gl_triangles); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 52
Triangle Strip Polygons in OpenGL (5) glbegin(gl_triangle_strip); glvertex2fv(p0); glvertex2fv(p1); p1 p0 p2 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p3 p4 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p7 p6 53 Triangle Fan Polygons in OpenGL (6) glbegin(gl_triangle_fan); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 54
Setting of Color Attributes (1) Setting of the clear color glclearcolor(1.0, 1.0, 1.0, 1.0); Opaque: opacity is 1.0 Window is cleared by white color Setting of the color state variable glcolor3f(1.0, 0.0, 0.0); RGB color red color Setting of the size of points 2 pixels wide: glpointsize(2.0); Setting of the width of lines 2 pixels width: gllinewidth(2.0); 55 Setting of Color Attributes (2) Creating conceptual vertex colors glcolor3f(1.0, 0.0, 0.0); glvertex2f(0.5, 0.5); glcolor3f(0.0, 0.0, 1.0); glvertex2f(-0.5, -0.5); Default smooth shading Alternative flat shading Color of first vertex determines fill color glshademodel(gl_smooth); glshademodel(gl_flat); 56
The Coordinate Systems The origin + the bases (axes) Right-handed handed coordinate system: OpenGL Left-handed coordinate system: DirectX 57 The Coordinate System The Origin 58
Result The Origin 59 The Coordinate System The Axes 60
Result The Axes 61 Exercises (2) glveiwport( ) 함수의입력인자를변경해보시오. 예 ) (cx, cy) (200, 200), (0, 0) (100, 100) glorhto( ) 함수의입력인자를변경해보시오. 예 )10 1.0 20 2.0, ratio 제거 glclearcolor( ) 함수의입력인자를변경해보시오. 예 ) (1.0f, 1.0f, 1.0f, 1.0f) (0.0f, 0.0f, 0.0f, 1.0f) 62
Locating Camera (1) Setting position and orient of the camera within the world coordinate system void glulookat(gldouble eyex, GLdouble eyey, GLdouble eyez, GLdouble atx, GLdouble aty, GLdouble atz, GLdouble upx, GLdouble upy, GLdouble upz) 63 Locating Camera (2) 64
Result Locating Camera 65 Exercises (3) 화살표에따라카메라의위치를변경해보시오. Left 화살표 : 왼쪽으로 0.1 씩이동 Right 화살표 : 오른쪽으로 01 0.1 씩이동 Up 화살표 : 위쪽으로 0.1 씩이동 Down 화살표 : 아래로 01 0.1 씩이동 66