tags: vs2008 mfc opengl
BOOL CTextView::PreCreateWindow
(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
//An OpenGL window must be created with the following flag
// and must not include CS_PARENTIDC for the class style.
cs.style|=WS_CLIPSIBLINGS|WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
BOOL CTextView::SetupPixelFormat()
{
//Create a rendering context
CDC* m_pDC=GetDC();
if(m_pDC==NULL) //failure to get DC
{
MessageBox(“Could't get a valid DC.");
return FALSE;
}
//Default pixel format is a single-buffered,
//OpenGL support hardware-accelerated,RGBA mode format
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),//Structure size.
1,
// Structure version number.Property flags:
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA, // RGBA type
24, // 32-bit color.
0, 0, 0, 0, 0, 0, // Not concerned with these: attributes not involved
0, // No alpha : no alpha buffer
0, // Shift bit ignored: ignore conversion bits
0, 0, 0, 0, 0, / / No accum buffer: no accumulation cache
32, // 32-bit depth buffer.
0, // No stencil: no template cache
0, // No auxliliary buffers: no secondary cache
PFD_MAIN_PLANE, // Main layer type.: Main layer type
0, // Reserved.: Number of reserved structures
0, 0, 0 // Unsupported.: Number of structures not supported
};
int nPixelFormat=
ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd);
if( nPixelFormat ==0)
{
MessageBox(“ChoosePixelFormat failed.");
return FALSE;
}
if(SetPixelFormat(m_pDC->GetSafeHdc(),
nPixelFormat,&pfd)==0)
{
MessageBox(“SetPixelFormat failed.");
return FALSE;
}
if( (m_hRC=wglCreateContext(m_pDC->
GetSafeHdc())) ==0)
{
MessageBox(“wglCreateContext failed.");
return FALSE;
}
if( (wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC)) ==0)
{
MessageBox(“wglMakeCurrent failed.");
return FALSE;
}
if(m_pDC) ReleaseDC(m_pDC);
return TRUE;
}
void CTextView::InitializeOpenGL()
{
glClearColor(0.2f,0.2f,0.2f,0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
}
void CTextView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
//Save the wide and height of the current window Client
GLsizei nWidth=(GLsizei)cx;
GLsizei nHeight=(GLsizei)cy;
ratio=(double)cx/(double)cy;
//Coupute the aspect ratio
GLdouble dAspect=(GLdouble)nWidth/(GLdouble)nHeight;
glViewport(0,0,nWidth,nHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective
(FOV,dAspect,NEARPLANE,FARPLANE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void CTextView::OnDraw(CDC* pDC)
{
CTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
DrawScene();
//Invalidate();
}
void CTextView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
//This call makes the current RC not current
if(wglMakeCurrent(0,0)==FALSE)
MessageBox(“wglMakeCurrent failed.");
//delete the RC
if(m_hRC && (wglDeleteContext(m_hRC)==FALSE))
MessageBox(“wglDeleteContext fail.");
KillTimer(1);
}
void CTextView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
Invalidate();
CView::OnTimer(nIDEvent);
}
void CTextView::OnGdiText()
{
// TODO: Add your command handler code here
m_iWhichText=0;
Invalidate();
}
void CTextView::OnUpdateGdiText(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(m_iWhichText==0) pCmdUI->SetCheck();
else pCmdUI->SetCheck(0);
}
void CTextView::OnDraw(CDC* pDC)
{
CTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
DrawScene();
//Invalidate();
}
void CTextView::DrawScene()
{
glClear
(GL_COLOR_BUFFERBIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0f,0.0f,-FARPLANE);
//TextureMap();
glPopMatrix();
glPushMatrix();
glTranslatef
(0.0f,0.0f,-(FARPLANE+NEARPLANE)/2);
if(m_iWhichText==1) DrawListText();
if(m_iWhichText==2) Draw3DText();
glPopMatrix();
glFinish();
SwapBuffers(wglGetCurrentDC());
if(m_iWhichText==0) DrawGdiText();
}
void CTextView::DrawGdiText()
{
HDC hdc=wglGetCurrentDC();
::SetBkMode( hdc, TRANSPARENT );
::SetTextColor( hdc, RGB(250,0,0) );
CString sState("Show GDI text.");
::TextOut(hdc,5,5,sState,sState.GetLength());
}
void CTextView::DrawListText()
{
wglUseFontBitmaps(wglGetCurrentDC(),0,256,1000);
glListBase(1000);
glRasterPos3f(-5.0f,0.0f,0.0f);
glCallLists(20,GL_UNSIGNED
_BYTE,“Draw with List Text.");
}
void CTextView::Draw3DText()
{
GLYPHMETRICSFLOAT agmf[256];
// create display lists for glyphs 0 through 255
// with 0.1 extrusion and default deviation.
//The display list numbering starts at 1000
(it could be any number)
wglUseFontOutlines(wglGetCurrentDC(),
0,255,1000,0.3f,0.8f, WGL_FONT_LINES ,agmf);
// Set up transformation to draw the string
glTranslatef(-15.0f,0.0f,0.0f);
glScalef(4.0f, 4.0f, 4.0f);
// Display a string
glListBase(1000);
// Indicates the start of display lists for the glyphs
// Draw the characters in a string
glCallLists(26, GL_UNSIGNED_BYTE,
“Draw outline list 3D text.");
}
Continuing the notes from the previous section: VS2008, MFC file operation 4-CFile class, CFileDialog class Method Open in text mode 1. In the engineering APP category, first demonstrate in InitInstan...
1. The VS2008 tool is downloaded and installed on the Internet by itself. Now only the detailed steps for configuring the OPENGL environment in the VS2008 development tool are provided. The developmen...
Handling from the CSDN blog:Embed OpenGL to MFC My big job in computer graphics is to implement some functions based on the MFC framework using OpenGL. But I didn't know how to add OpenGL modules to M...
Reprinted address: https://blog.csdn.net/u013232740/article/details/47904115 ------------------------------------------------------------------------------------------------------------ This example u...
MFC OpenGl cube...
cpp Added package #include <gl/gl.h> #include <gl/glu.h> //#include <gl/glaux.h> #include <gl/glut.h>...
When we write software, we always have our own interface. Of course, we have to deal with MFC when using C++. The visual interface usually uses MFC's Dialog; OpenGL usually generates a window when dra...
aims (1) Draw three cubes, the positions are as shown in the figure. Request to draw the coordinate system 3 axes. (2) The three cubes rotate together along the coordinate axis (x or y or z). Say Ming...
MFC correctly configures OpenGL Library download link After the library downloads, decompress the project file: Subsequently modified attributes in the project: Finally, the header file is included in...
MFC OpenGL Record https://www.songho.ca/opengl/gl_mvc.html https://www.opengldn.com/codes glWin.exe This earth can rotate The code is written by VS2015 Win32, not MFC project, and there are main.cpp f...