25
Programming with Visual Studio MFC and OpenGL

Programming with Visual Studio MFC and OpenGL

  • Upload
    king

  • View
    109

  • Download
    5

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Programming with Visual Studio MFC and OpenGL

Programming with Visual Studio MFC and OpenGL

Page 2: 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

Page 3: Programming with Visual Studio MFC and OpenGL

Creating a Project in Visual Studio

Page 4: Programming with Visual Studio MFC and OpenGL

Creating a Project in Visual Studio

Page 5: Programming with Visual Studio MFC and OpenGL

Creating a Project in Visual Studio

Page 6: Programming with Visual Studio MFC and OpenGL

Creating a Project in Visual Studio• Header File – Code that need to be added for

OpenGLHDC m_hDC;HGLRC m_hGLContext;BOOL SetWindowPixelFormat(HDC hDC);BOOL CreateViewGLContext(HDC hDC);int m_GLPixelIndex;afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);afx_msg void OnDestroy();afx_msg void OnSize(UINT nType, int cx, int cy);

DECLARE_MESSAGE_MAP() //already in .h file

Page 7: Programming with Visual Studio MFC and OpenGL

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 commandsON_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()

Page 8: Programming with Visual Studio MFC and OpenGL

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

Page 9: Programming with Visual Studio MFC and OpenGL

Creating a Project in Visual Studioint 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;}

Page 10: Programming with Visual Studio MFC and OpenGL

Creating a Project in Visual Studiovoid 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();

}

Page 11: Programming with Visual Studio MFC and OpenGL

Creating a Project in Visual Studiovoid CCssample1View::OnSize(UINT nType, int cx, 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 bufferingglDrawBuffer(GL_BACK);

}

Page 12: Programming with Visual Studio MFC and OpenGL

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

Page 13: Programming with Visual Studio MFC and OpenGL

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 codeCRect rcClient;GetClientRect(rcClient);

Page 14: Programming with Visual Studio MFC and OpenGL

Adding OpenGL initialization code

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

Page 15: Programming with Visual Studio MFC and OpenGL

Adding OpenGL Libraries

Page 16: Programming with Visual Studio MFC and OpenGL

Adding OpenGL Libraries

Page 17: Programming with Visual Studio MFC and OpenGL

Adding OpenGL Libraries

Page 18: Programming with Visual Studio MFC and OpenGL

Adding OpenGL Libraries (Paths)

Page 19: Programming with Visual Studio MFC and OpenGL

Adding OpenGL Libraries (Paths)

Page 20: Programming with Visual Studio MFC and OpenGL

Creating a mouse event

• afx_msg void 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

}

Page 21: Programming with Visual Studio MFC and OpenGL

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

Page 22: Programming with Visual Studio MFC and OpenGL

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.

Page 23: Programming with Visual Studio MFC and OpenGL

Saving info to use later (Point data)

• Suppose we have a right button press eventvoid CCssample1View::OnRButtonDown(UINT nFlags, CPoint point) {

line savedLine;savedLine.start = start; //start variable was found with a left button press, save nowsavedLine.end = point; //save the point where the right click occurredsavedLinesVector.push_back(savedLine); //save line into vector

}//in the header file, in the class itselfstruct savedLine{

CPoint start, end; //Cpoint is mfc defined type};vector <savedLine> savedLinesVector; //vector of type savedLine

Page 24: Programming with Visual Studio MFC and OpenGL

Saving info to use later (Point data)

for(unsigned i=0; i<savedLinesVector.size(); i++){//iterate through vector printing linesglBegin(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

Page 25: Programming with Visual Studio MFC and OpenGL

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 CPointp1.x; //x coordinate at pointp1.y; //y coordinate at point