1 / 25

Programming with Visual Studio MFC and OpenGL

Programming with Visual Studio MFC and OpenGL. Outline. Creating a project Adding OpenGL initialization code and libraries Creating a mouse event Drawing with OpenGL Saving information to use later (Point data) Relevant Questions about OpenGL/MFC. Creating a Project in Visual Studio.

king
Download Presentation

Programming with Visual Studio MFC and 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. Programming with Visual Studio MFC and OpenGL

  2. Outline • Creating a project • Adding OpenGL initialization code and libraries • Creating a mouse event • Drawing with OpenGL • Saving information to use later (Point data) • Relevant Questions about OpenGL/MFC

  3. Creating a Project in Visual Studio

  4. Creating a Project in Visual Studio

  5. Creating a Project in Visual Studio

  6. Creating a Project in Visual Studio • Header File – Code that need to be added for OpenGL HDC m_hDC; HGLRC m_hGLContext; BOOL SetWindowPixelFormat(HDC hDC); BOOL CreateViewGLContext(HDC hDC); intm_GLPixelIndex; afx_msgintOnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnDestroy(); afx_msgvoid OnSize(UINT nType, intcx, intcy); DECLARE_MESSAGE_MAP() //already in .h file

  7. Creating a Project in Visual Studio • Need to make sure the messages are getting sent/received - .cpp file BEGIN_MESSAGE_MAP(CCssample1View, CView) ON_WM_CREATE() ON_WM_DESTROY() ON_WM_SIZE() // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP()

  8. Creating a Project in Visual Studio BOOL CCssample1View::CreateViewGLContext(HDC hDC){ m_hGLContext = wglCreateContext(m_hDC); if (m_hGLContext == NULL) return FALSE; if (wglMakeCurrent(m_hDC, m_hGLContext)==FALSE) return FALSE; return TRUE; }

  9. Creating a Project in Visual Studio int CCssample1View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; HWND hWnd = GetSafeHwnd(); m_hDC= ::GetDC(hWnd); if (SetWindowPixelFormat(m_hDC)==FALSE) return 0; if (CreateViewGLContext(m_hDC)==FALSE) return 0; return 0; }

  10. Creating a Project in Visual Studio void CCssample1View::OnDestroy() { CView::OnDestroy(); if(wglGetCurrentContext()!=NULL) { // make the rendering context not current wglMakeCurrent(NULL, NULL) ; } if (m_hGLContext!=NULL){ wglDeleteContext(m_hGLContext); m_hGLContext = NULL; } // Now the associated DC can be released. CView::OnDestroy(); }

  11. Creating a Project in Visual Studio void CCssample1View::OnSize(UINT nType, intcx, int cy) { CView::OnSize(nType, cx, cy); glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, width, 0.0, height); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // for double buffering glDrawBuffer(GL_BACK); }

  12. Creating a Project in Visual Studio • BOOL CCssample1View::SetWindowPixelFormat(HDC hdc){…} • This function is pretty big, in the interest of space, here is a link to a site that shows how it is done. This site also goes over the stuff mentioned here in more detail about creating the project. • http://web.agelid.com/protect/utile/documentation/OpenGL/Example%201%20-%20Writing%20an%20OpenGL%20Program.htm

  13. Creating a Project in Visual Studio • The previously mentioned functions and variables will allow you to use OpenGL with your project once they are added. • This code below must also be added to your OnDraw function to get the render context. //openGL code CRect rcClient; GetClientRect(rcClient);

  14. Adding OpenGL initialization code • The following code can be added to your OnDraw function. //openGL code glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, width, 0.0, height); glMatrixMode(GL_MODELVIEW); glLoadIdentity();

  15. Adding OpenGL Libraries

  16. Adding OpenGL Libraries

  17. Adding OpenGL Libraries

  18. Adding OpenGL Libraries (Paths)

  19. Adding OpenGL Libraries (Paths)

  20. Creating a mouse event • afx_msgvoid OnLButtonDown(UINT nFlags, CPoint point); //added to header file • ON_WM_LBUTTONDOWN() //added to message map at top of cpp file • void CCssample1View::OnLButtonDown(UINT nFlags, CPoint point) { //code to be exectud when Lbutton is pressed down }

  21. Drawing with OpenGL glBegin(GL_LINES); glVertex3d(100, height-100, 0); //height-100 is to make sure point is actually at 100, 100. It actually starts at bottom left of screen glVertex3d(200, height-200, 0); glEnd(); The above code draws a line from coordinates (100, 100) to (200, 200).

  22. Drawing with OpenGL • Primitives you can draw with OpenGL • glBegin(GL_LINES); • glBegin(GL_POINTS); • glBegin(GL_TRIANGLES); • glBegin(GL_LINE_LOOP); • There are others, these are just a few that you will most likely use.

  23. Saving info to use later (Point data) • Suppose we have a right button press event void CCssample1View::OnRButtonDown(UINT nFlags, CPoint point) { line savedLine; savedLine.start = start; //start variable was found with a left button press, save now savedLine.end = point; //save the point where the right click occurred savedLinesVector.push_back(savedLine); //save line into vector } //in the header file, in the class itself struct savedLine{ CPoint start, end; //Cpoint is mfc defined type }; vector <savedLine> savedLinesVector; //vector of type savedLine

  24. Saving info to use later (Point data) for(unsigned i=0; i<savedLinesVector.size(); i++){ //iterate through vector printing lines glBegin(GL_LINES); //this can be outside for loop also, depending on what you are doing glVertex2d(savedLinesVector[i].start.x, savedLinesVector[i].start.y); glVertex2d(savedLinesVector[i].end.x, savedLinesVector[i].end.y); glEnd(); } //this will draw all lines to the screen, this is done in the OnDraw function

  25. Relevant Questions about OpenGL/MFC • How do I read pixels from the screen in OpenGL? • glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *data); • If you wanted to read one pixel at (100, 100): • glReadPixels(100, 100, 1, 1, GL_RGB, GL_BYTE, data); • The variable data is where the pixel information is stored • How do I change the color of pixel(s)? • glColor3f(1.0, 0.0, 0.0); //red color • How do I clear the buffer? • glClear(GL_COLOR_BUFFER_BIT); • What is the CPoint type? • It allows you to gain access to the x, y coordinates at a particular point. Ex: CPoint p1; // variable p1 of type CPoint p1.x; //x coordinate at point p1.y; //y coordinate at point

More Related