Document

advertisement
Programming with Visual Studio
MFC and OpenGL
Outline
•
•
•
•
•
Creating a project
Creating a mouse event
Drawing with OpenGL
Saving information to use later (Point data)
Relevant Questions about OpenGL/MFC
Application Window
Initialization
Get handle of window
OnCreate( )
OnSize( )
Set Pixel Format
Waiting for Messages
and response to them
OnDraw( )
Create and make current rendering context
OnMouseMove()
OnLMouseDown()
Get handle of Device Context
Make rendering context
not current and delete it
………
OnDestroy( )
Release resources and close app window
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;
}
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();
}
Adding OpenGL initialization code
• The following code can be added to your OnDraw
function.
glViewport(0, 0, width, height);//glViewport specifies the affine transformation
of x and y from normalized device coordinates to window coordinates
glMatrixMode(GL_PROJECTION);
//current matrix specifies projection transformation, subsequent calls affect the projection matrix
glLoadIdentity();
//clear current matrix by loading with identity matrix
gluOrtho2D(0.0, width, 0.0, height);//gluOrtho2D sets up a two-dimensional
orthographic viewing region. This is equivalent to calling glOrtho with near = -1 and far = 1
glMatrixMode(GL_MODELVIEW);
//succeeding transformations affect the modelview matrix now
glLoadIdentity();
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).
//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
Saving info to use later (Point data)
• Suppose we have a right button press event
void CCssample1View::OnLButtonDown(UINT nFlags, CPoint point) {
start = point;
current = point;
}
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
}
void CCssample1View::OnMouseMove(UINT nFlags, CPoint
point)
{
current = point;
}
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
void CCssample1View::Draw_lines()
{
for(int m=0;m<abs((int)line.size());m++ )
{…draw previous lines…}
glBegin(GL_LINES);
glVertex3d(line.start.x, height-line.start.y, 0);
glVertex3d(line.current.x, height-line.current.y, 0);
glEnd();
}
• http://nehe.gamedev.net/
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
Download