Скачать презентацию Computer Graphics Programming with Open GL and MFC Скачать презентацию Computer Graphics Programming with Open GL and MFC

e144e7481333bf37125621840dd936c5.ppt

  • Количество слайдов: 46

Computer Graphics Programming with Open. GL and MFC Computer Graphics Programming with Open. GL and MFC

Computer Graphics Progamming Windows programming Graphics APIs Open. GL Programming in Visual C++ GLUT Computer Graphics Progamming Windows programming Graphics APIs Open. GL Programming in Visual C++ GLUT Open. GL Overview Appendix

Windows Programming Prepare Your Development Environment To write a Windows program in C or Windows Programming Prepare Your Development Environment To write a Windows program in C or C++, you must install the Windows SDK or a development environment that includes the Windows SDK, such as Microsoft Visual C++.

What is MFC? • • Microsoft Foundation Classes C++ wrappers to the Windows SDK What is MFC? • • Microsoft Foundation Classes C++ wrappers to the Windows SDK An application framework A useful set of extensions

MFC v. Microsoft Foundation Class Library v. A set of wrapper classes of win MFC v. Microsoft Foundation Class Library v. A set of wrapper classes of win 32 API to better support windows programming v. What’s the benefit and how it looks? v. A windows program with win 32 API (win 32) v. A windows program with MFC

Win 32 program v. Win. Main v. Declare a window and set properties v. Win 32 program v. Win. Main v. Declare a window and set properties v. Register the window v. Create the window and show v. Listen to user and dispatch messages to Win. Proc v. Message loop routine v. Wait for user messages and act as set by programmer

Win. Main: The Application Entry Point Every Windows program includes an entry-point function that Win. Main: The Application Entry Point Every Windows program includes an entry-point function that is named either Win. Main or w. Win. Main. Here is an empty Win. Main function. INT Win. Main(HINSTANCE h. Instance, HINSTANCE h. Prev. Instance, PSTR lp. Cmd. Line, INT n. Cmd. Show) { return 0; }

Windows Message WM_CREATE, WM_DESTROY, WM_CLOSE, WM_QUIT v WM_MOVE, WM_SIZE v WM_SETTEXT, WM_GETTEXT v WM_PAINT Windows Message WM_CREATE, WM_DESTROY, WM_CLOSE, WM_QUIT v WM_MOVE, WM_SIZE v WM_SETTEXT, WM_GETTEXT v WM_PAINT v WM_KEYDOWN, WM_KEYUP v WM_MOUSEMOVE, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_RBUTTONDOWN, WM_RBUTTONUP, WM_MOUSEWHEEL v v. A full list

Creating a mouse event } afx_msg void On. LButton. Down(UINT n. Flags, CPoint point); Creating a mouse event } afx_msg void On. LButton. Down(UINT n. Flags, CPoint point); //added to header file ON_WM_LBUTTONDOWN() //added to message map at top of cpp file void CCssample 1 View: : On. LButton. Down(UINT n. Flags, CPoint point) { //code to be exectud when Lbutton is pressed down

Building blocks of a MFC program vsimple. MFC v. CWin. App: CWin. Thread: CCmd. Building blocks of a MFC program vsimple. MFC v. CWin. App: CWin. Thread: CCmd. Target: CObject v. Define the application: Csimple. MFCApp v. Declare the message map: DECLARE_MESSAGE_MAP v. Main. Frm v. CMain. Frame: CFrame. Wnd: CCmd. Target: CObject v. Define the main window

Building blocks of a MFC program. 2 vsimple. MFCDoc v. CDocument: CCmd. Target: Cobject Building blocks of a MFC program. 2 vsimple. MFCDoc v. CDocument: CCmd. Target: Cobject v. Encapsulate the application data vsimple. MFCView v. Cview: CWnd: CCmd. Target: Cobject v. Encapsulate the data views and interactions with user

A sample program: Sketch v. Create a new SDI project v. Add member variables A sample program: Sketch v. Create a new SDI project v. Add member variables to Csketch. GDIview v. CDC* p. DC v. CPoint m_pt v. Cpoint m_ptold vbool m_drawenabled

A sample program: Sketch v. Add event handlers v. Open property page of CSketch. A sample program: Sketch v. Add event handlers v. Open property page of CSketch. GDIView class and add handlers for v. WM_LBUTTONDOWN v. WM_LBUTTONUP v. WM_MOUSEMOVE v. In On. LButton. Down vm_drawenabled = true; //enable drawing mode vm_ptold = point; //save the current point

A sample program: Sketch v. In On. LButton. Up m_drawenabled = false; //quit drawing A sample program: Sketch v. In On. LButton. Up m_drawenabled = false; //quit drawing mode v. In On. Mouse. Move If in drawing mode, start drawing, else quit m_pt = point; //save the current point p. DC = Get. DC(); //get the device context m_color = RGB(rand(), rand()); //generate a color CPen new. Pen(PS_SOLID, 1, m_color); //create a pen p. DC->Move. To(m_ptold); p. DC->Line. To(m_pt); // draw a line from m_ptold to m_ptold = m_pt; //update the old point for next drawing

MFC class hierachy vhttp: //msdn. microsoft. com/en- us/library/ws 8 s 10 w 4. aspx MFC class hierachy vhttp: //msdn. microsoft. com/en- us/library/ws 8 s 10 w 4. aspx

Graphics APIs Provide a software interface to graphics hardware (an intermediary between an application Graphics APIs Provide a software interface to graphics hardware (an intermediary between an application program and the graphics hardware) Increase software portability Reduce the development time Example: Open. GL, Direct. X

Open. GL 2 D/3 D graphics API Most widely used/supported by industry Runs on Open. GL 2 D/3 D graphics API Most widely used/supported by industry Runs on Unix, Linux, Window, etc. C, C++, Java, etc. Hardware independent Easy to render objects

Open. GL install Download the GLUT library. Copy the glut 32. dll and paste Open. GL install Download the GLUT library. Copy the glut 32. dll and paste it in C: WindowsSystem 32 folder. copy glut. h file and paste it in C: Program FilesMicrosoft Visual StudioVCinclude folder copy glut 32. lib and paste it in c: Program FilesMicrosoft Visual StudioVClib folder.

Open. GL Now you can create visual c++ console application project and include glut. Open. GL Now you can create visual c++ console application project and include glut. h header file then you can write code for GLUT project. If you are using 64 bit windows machine then path and glut library may be different but process is similar.

Open. GL in VC++ Include files: Library files need linked to: Device context (Drawing Open. GL in VC++ Include files: Library files need linked to: Device context (Drawing to window) ◦ #include ◦ #include ◦ opengl 32. lib, glu 32. lib ◦ HDC h. DC = Get. DC(h. Wnd) ◦ Release. DC(h. Wnd, h. DC)

Program Structure Pseudocode - Initialize variables, memory space - Create & show window, update Program Structure Pseudocode - Initialize variables, memory space - Create & show window, update window for client area updating Loop Fetch any message sent from Windows to this program If message is QUIT Terminate program, return control to Windows If any other messages Do corresponding actions depending on the message ID and parameters Return control to Windows End Loop

Open. GL in VC++ Need to set pixel format (tells system how we are Open. GL in VC++ Need to set pixel format (tells system how we are going to use the DC) ◦ PIXELFORMATDESCRIPTOR pfd; ◦ Set. Pixel. Format(…); Render context HGLRC h. RC; //handle h. RC = wgl. Create. Context(h. DC); //get context wgl. Delete. Context(h. RC); //free up context

GLUT – Open. GL Utility Toolkit GLUT is a window system independent toolkit for GLUT – Open. GL Utility Toolkit GLUT is a window system independent toolkit for writing Open. GL programs. It implements a simple windowing application programming interface (API) for Open. GLUT makes it considerably easier to learn about and explore Open. GL programming. GLUT provides a portable API so you can write a single Open. GL program that works across all PC and workstation OS platforms.

GLUT – Open. GL Utility Toolkit GLUT is designed for constructing small to medium GLUT – Open. GL Utility Toolkit GLUT is designed for constructing small to medium sized Open. GL programs. While GLUT is well-suited to learning Open. GL and developing simple Open. GL applications, GLUT is not a full-featured toolkit so large applications requiring sophisticated user interfaces are better off using native window system toolkits. GLUT is simple, easy, and small.

GLUT – Open. GL Utility Toolkit Advantage: ◦ Simplify opening window, I/O, etc. glut. GLUT – Open. GL Utility Toolkit Advantage: ◦ Simplify opening window, I/O, etc. glut. Init(…); glut. Create. Window(…); glut. Display. Fun(display); glut. Main. Loop(); glut. Swap. Buffers();

Open. GL Low-level graphics library specification Programmer has access to primitives ◦ Lines ◦ Open. GL Low-level graphics library specification Programmer has access to primitives ◦ Lines ◦ Points ◦ Polygons Can specify objects in 2 D and 3 D Using the primitives with certain commands, we control how object is rendered

Open. GL Sample syntax gl. Begin(GL_POINTS); for(i=1 to size of data) gl. Vertex 3 Open. GL Sample syntax gl. Begin(GL_POINTS); for(i=1 to size of data) gl. Vertex 3 d(x, y, z); gl. End(); Primitives: ◦ GL_LINE ◦ GL_POINTS ◦ GL_LINE_LOOP

Open. GL vs. Windows Programming Set. Pixel(h. DC, x, y, col); Move. To. Ex(h. Open. GL vs. Windows Programming Set. Pixel(h. DC, x, y, col); Move. To. Ex(h. DC, x, y, NULL); Line. To(h. DC, x, y); Polygon(h. DC, lppts, num_points) Polyline(h. DC, lppts, num_points) Windows gl. Begin(GL_POINTS); gl. Begin(GL_LINES); gl. Begin(GL_POLYGON) gl. Begin(GL_LINE_LOOP) Open. GL

Open. GL Attributes (State variables) ◦ Property of primitives (color, line style, etc. ) Open. GL Attributes (State variables) ◦ Property of primitives (color, line style, etc. ) gl. Color 3 f(R, G, B); Transformations (Matrix math) ◦ Setting View ports, object rotation, translation, etc. gl. Load. Matrix(. . ); gl. Rotatef(. . ); gl. Translatef(. . ); Execution ◦ gl. Flush(); //needed to make sure commands //are executed

Open. GL Change state ◦ gl. Enable(); ◦ gl. Disable(); Drawing in 3 D Open. GL Change state ◦ gl. Enable(); ◦ gl. Disable(); Drawing in 3 D ◦ Hidden surface removal Depth buffer and depth testing gl. Enable(GL_DEPTH_TEST); Culling the object (remove polygons) gl. Enable(GL_CULL_FACE); gl. Cull. Face(GL_BACK);

Open. GL Viewing ◦ View transform analogous to camera positioning ◦ Model transform analogous Open. GL Viewing ◦ View transform analogous to camera positioning ◦ Model transform analogous to positioning model Viewing transforms must precede model ◦ ◦ ◦ ◦ gl. Viewport(. . ); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); gl. Ortho(. . ); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); //translate, scale, rotate object…

Open. GL I/O – GLUT handles this (mouse, key, etc. . ) Initialize graphics Open. GL I/O – GLUT handles this (mouse, key, etc. . ) Initialize graphics system ◦ aux. Init. Window(); ◦ glut. Create. Window(); Store/read pixels ◦ gl. Read. Pixels(); ◦ gl. Copy. Pixels(); Photorealism ◦ gl. Lightf(); ◦ gl. Shade. Model();

Open. GL Texture mapping //Initialization gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_WRAP_S, GL_REPEAT); gl. Tex. Parameteri(GL_TEXTURE_2 Open. GL Texture mapping //Initialization gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_WRAP_S, GL_REPEAT); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_WRAP_T, GL_REPEAT); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); gl. Bind. Texture(GL_TEXTURE_2 D, tex. Name[1]); glu. Build 2 DMipmaps( GL_TEXTURE_2 D, 3, BMBACK. bm. Width, BMBACK. bm. Height, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMBACK. bm. Bits ); gl. Tex. Image 2 D(…. . , bm. Bits); //Texture mapping gl. Bind. Texture(GL_TEXTURE_2 D, tex. Name[1]); gl. Tex. Coord 2 f(0, 0); gl. Vertex 3 f(-BM. bm. Width, 0, 0); gl. Tex. Coord 2 f(0, 1); gl. Vertex 3 f(-BM. bm. Width, BM. bm. Height, 0); gl. Tex. Coord 2 f(1, 1); gl. Vertex 3 f(0, BM. bm. Height, 0); gl. Tex. Coord 2 f(1, 0); gl. Vertex 3 f(0, 0, 0); gl. End();

Open. GL Order of operations ◦ ◦ Construct shapes from primitives Arrange objects in Open. GL Order of operations ◦ ◦ Construct shapes from primitives Arrange objects in 3 d space, select view Calculate color of object(s) Convert mathematical description of object(s) to screen (rasterization)

v. This picture borrowed from prof. Eckert’s course slides v. This picture borrowed from prof. Eckert’s course slides

 You may find a lot of useful materials and tutorial on youtube and You may find a lot of useful materials and tutorial on youtube and opengl “redbook” You could also come to my office hour

Appendix Visual Studio MFC Programming ◦ File->New-> MFC App. Wizard (exe) -> SDI (MDI) Appendix Visual Studio MFC Programming ◦ File->New-> MFC App. Wizard (exe) -> SDI (MDI) ◦ MFC Document/View structure CDocument CView Screen Printer Serialize & deserialize Data Events Driven and Messages Delivery (WM_Create, WM_Size, WM_LMouse. Down etc) Device Context and Device Independence Same program use different hardware by letting windows take care of hardware interface Program draws on an abstract surface called a “device context” (DC) DC is accessed with a “handle to a DC”, which can be get from windows by using Get. DC( ) DC need to be released after have finished using it by Release. DC( )

Appendix Use Open. GL in MFC Programming Structure of a typical Open. GL program Appendix Use Open. GL in MFC Programming Structure of a typical Open. GL program Create application window (Get. Safe. Hwnd()) Get proper Device Context (m_h. DC= Get. DC()) Set pixel format for windows (Set. Window. Pixel. Format(m_h. DC)) Create the Open. GL view context (Create. View. GLContext(m_h. DC)) Response to Messages, complete required tasks and clear resources § We need to create a new Open. GL rendering context that is suitable for drawing on the device referenced by device context. The rendering context has the same pixel format as the device context Implementation, a small code example: HDC hdc; HGLRC hglrc; hglrc=wgl. Create. Context(hdc); //create rendering context wgl. Make. Current(hdc, hglrc); //make it the current rendering context //call Open. GL APIs to fulfill your task …… //when the rendering context is no longer needed wgl. Make. Current(NULL, NULL); //make the rendering context not current wgl. Delete. Context(hglrc); //delete the rendering context

Appendix Application Window Initialization Get handle of window On. Create( ) On. Size( ) Appendix Application Window Initialization Get handle of window On. Create( ) On. Size( ) Waiting for Messages and response to them On. Draw( ) Set Pixel Format Create and make current rendering context On. Mouse. Move() On. LMouse. Down() Get handle of Device Context Make rendering context not current and delete it ……… On. Destroy( ) Release resources and close app window

Appendix 1. 2. 3. 4. Some Concepts of Open. GL Double Buffer, use for Appendix 1. 2. 3. 4. Some Concepts of Open. GL Double Buffer, use for no flickering animation Color Buffer, use gl. Clear(GL_COLOR_BUFFER_BIT) to enable PIXELFORMATDESCRIPTOR structure describes the pixel format of a drawing surface Set. Pixel. Format(…) is use to sets the pixel format of the specified device context to the format specified by the i. Pixel. Format index BOOL Set. Pixel. Format( HDC hdc, //device context whose pixel format the function attempts to set int i. Pixel. Format, //pixel format index (one-based) CONST PIXELFORMATDESCRIPTOR * ppfd //pointer to logical pixel format specification );

Appendix Code Example (from MSDN): PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // size of this Appendix Code Example (from MSDN): PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd 1, // version number PFD_DRAW_TO_WINDOW | // support window PFD_SUPPORT_OPENGL | // support Open. GL PFD_DOUBLEBUFFER, // double buffered PFD_TYPE_RGBA, // RGBA type 24, // 24 -bit color depth 0, 0, 0, // color bits ignored 0, // no alpha buffer 0, // shift bit ignored 0, // no accumulation buffer 0, 0, // accumulate bits ignored 32, // 32 -bit z-buffer 0, // no stencil buffer 0, // no auxiliary buffer PFD_MAIN_PLANE, // main layer 0, // reserved 0, 0, 0 // layer masks ignored }; HDC hdc; int i. Pixel. Format; // get the best available match of pixel format for the device context i. Pixel. Format = Choose. Pixel. Format(hdc, &pfd); // make that the pixel format of the device context Set. Pixel. Format(hdc, i. Pixel. Format, &pfd);

Appendix Open. GL Functions Use to Create a view window with a 2 D Appendix Open. GL Functions Use to Create a view window with a 2 D orthogonal view: gl. Viewport(0, 0, width, height); gl. Matrix. Mode(GL_PROJECTION); //current matrix specifies projection transformation, subsequent calls affect the projection matrix gl. Load. Identity(); //clear current matrix by loading with identity matrix glu. Ortho 2 D(0. 0, width, 0. 0, height); gl. Matrix. Mode(GL_MODELVIEW); //succeeding transformations affect the modelview matrix now gl. Load. Identity();

Appendix Open. GL drawing primitives, Code Example: gl. Color 4 f(1. 0, 0. 0); Appendix Open. GL drawing primitives, Code Example: gl. Color 4 f(1. 0, 0. 0); gl. Push. Matrix(); gl. Begin(GL_LINE_LOOP); gl. Vertex 3 d(x 1, y 1, 0); gl. Vertex 3 d(x 2, y 2, 0); gl. Vertex 3 d(x 3, y 3, 0); gl. End(); gl. Flush(); gl. Pop. Matrix(); This code draws three lines to form a triangle. If we use gl. Begin(GL_TRIANGLES) we will get a filled triangle.