COpenglstep1View::COpenglstep1View()
{
// TODO: add construction code here
this->m_GLPixelIndex = 0;
this->m_hGLContext = NULL;
}
COpenglstep1View::~COpenglstep1View()
{
}
BOOL COpenglstep1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
return CView::PreCreateWindow(cs);
//return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// COpenglstep1View drawing
void COpenglstep1View::OnDraw(CDC* pDC)
{
COpenglstep1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// COpenglstep1View printing
BOOL COpenglstep1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void COpenglstep1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void COpenglstep1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// COpenglstep1View diagnostics
#ifdef _DEBUG
void COpenglstep1View::AssertValid() const
{
CView::AssertValid();
}
void COpenglstep1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
COpenglstep1Doc* COpenglstep1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COpenglstep1Doc)));
return (COpenglstep1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COpenglstep1View message handlers
BOOL COpenglstep1View::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
BOOL COpenglstep1View::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
this->m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
if(this->m_GLPixelIndex==0)
{
this->m_GLPixelIndex = 1;
if(DescribePixelFormat(hDC,this->m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
{
return FALSE;
}
}
if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
int COpenglstep1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
HWND hWnd = this->GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
if(this->SetWindowPixelFormat(hDC)==FALSE)
{
return 0;
}
if(this->CreateViewGLContext(hDC)==FALSE)
{
return 0;
}
return 0;
}
BOOL COpenglstep1View::CreateViewGLContext(HDC hDC)
{
this->m_hGLContext = wglCreateContext(hDC);
if(this->m_hGLContext==NULL)
{//創建失敗
return FALSE;
}
if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
{//選為當前RC失敗
return FALSE;
}
return TRUE;
}
void COpenglstep1View::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
if(wglGetCurrentContext()!=NULL)
{
wglMakeCurrent(NULL,NULL);
}
if(this->m_hGLContext!=NULL)
{
wglDeleteContext(this->m_hGLContext);
this->m_hGLContext = NULL;
}
}
void COpenglstep1View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GLsizei width,height;
GLdouble aspect;
width = cx;
height = cy;
if(cy==0)
{
aspect = (GLdouble)width;
}
else
{
aspect = (GLdouble)width/(GLdouble)height;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0*aspect,0.0,500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void COpenglstep1View::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CView::OnPaint() for painting messages
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor4f(1.0f,0.0f,0.0f,1.0f);
glVertex2f(100.0f,50.0f);
glColor4f(0.0f,1.0f,0.0f,1.0f);
glVertex2f(450.0f,400.0f);
glColor4f(0.0f,0.0f,1.0f,1.0f);
glVertex2f(450.0f,50.0f);
glEnd();
glFlush();
// glutSwapBuffers();
}
uj5u.com熱心網友回復:
百度搜“MFC OpenGL”uj5u.com熱心網友回復:
OpenGL的繪制代碼需要通過while(true)回圈進行繪制,不能放在OnPaint()里面uj5u.com熱心網友回復:
資料不對吧?uj5u.com熱心網友回復:
1void COpenglstep1View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if((cx==0) || (cy==0)) return;
GLdouble aspect;
aspect = (GLdouble)cx/(GLdouble)cy;
glViewport(0,0,cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0 * aspect,0.0,500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
2
void COpenglstep1View::OnDraw(CDC* pDC)
{
COpenglstep1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_POLYGON);
glColor4f(1.0f,0.0f,0.0f,1.0f);
glVertex2f(100.0f,50.0f);
glColor4f(0.0f,1.0f,0.0f,1.0f);
glVertex2f(450.0f,400.0f);
glColor4f(0.0f,0.0f,1.0f,1.0f);
glVertex2f(450.0f,50.0f);
glEnd();
glFlush();
SwapBuffers(pDC->m_hDC);
}
通常不放 OnPaint 而放 OnDraw
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
沒在MFC里面做過OpenGL繪制,不過依據win32的做法來看,我竊以為應該把繪制代碼放到CWinApp::OnIdle()多載函式里面去做,如果防在OnPaint()或OnDraw()應該只能刷出一幀來
uj5u.com熱心網友回復:
樓主 這個 就是 一個圖, 沒有 影片,(不 旋轉),旋轉 時 要 用 OnTimeOnIdle() 是不 勻勻的
uj5u.com熱心網友回復:
下面是 旋轉 代碼
COpenglstep1View::COpenglstep1View()
{
// TODO: add construction code here
m_GLPixelIndex = 0;
m_rotAngle=0;
m_hGLContext = NULL;
}
void COpenglstep1View::OnDraw(CDC* pDC)
{
COpenglstep1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //設定矩陣模式為模型變換模式,表示在世界坐標系下
glLoadIdentity(); //將當前矩陣設定為單位矩陣
float centerX,centerY;
centerX=(450-100)/2+100;
centerY=(400-50)/2+50;
glTranslatef(centerX,centerY,0); // ;
glRotatef((float)m_rotAngle,0,0,1);
glTranslatef(-centerX,-centerY,0);
glBegin(GL_POLYGON);
glColor4f(0.0f,1.0f,0.0f,1.0f);// green
glVertex2f(450.0f,400.0f); // .
glColor4f(1.0f,0.0f,0.0f,1.0f);// red
glVertex2f(100.0f,50.0f); // .
glColor4f(0.0f,0.0f,1.0f,1.0f);// blue
glVertex2f(450.0f,50.0f); // .
glEnd();
SwapBuffers(pDC->m_hDC);
}
void COpenglstep1View::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
SetTimer(1,100,0);
}
void COpenglstep1View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
m_rotAngle+=1;
m_rotAngle%=360;
Invalidate();
// CView::OnTimer(nIDEvent);
}
BOOL COpenglstep1View::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
// return CView::OnEraseBkgnd(pDC);
}
注意 m_rotAngle=0;
uj5u.com熱心網友回復:
MFC中呼叫OpenGL的例子你可以參考一下
http://blog.csdn.net/xianglitian/article/details/6164103
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/115469.html
標籤:基礎類
上一篇:MFC寫讀.hea檔案的代碼報錯不會改了。求助大神!
下一篇:關于窗內文字輸出的問題
