首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

MFC停的opengl框架的程序可以运行但什么都没画出来

2013-04-21 
MFC下的opengl框架的程序可以运行但什么都没画出来绘制部分代码在WIN32程序中可以正常运行,转到MFC下,怎么

MFC下的opengl框架的程序可以运行但什么都没画出来
绘制部分代码在WIN32程序中可以正常运行,转到MFC下,怎么什么都画不出来呢
上代码  前面略去些自动生成的代码。。
CBevelView::CBevelView()
{
// TODO: add construction code here
m_nWidth  = 520;
m_nHeight = 600;
m_bLfDown = false;
m_bRtDown = false;
m_bMdDown = false;
m_nColorCount = 100 * 3;
m_pbColor = NULL;

HWND hWnd = this->GetSafeHwnd();
m_hDC = ::GetDC(hWnd);
}

CBevelView::~CBevelView()
{
}

BOOL CBevelView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs
cs.style|=WS_CLIPSIBLINGS|WS_CLIPCHILDREN;

return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBevelView drawing

void CBevelView::OnDraw(CDC* pDC)
{
CBevelDoc* pDoc = (CBevelDoc*)CView::GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
        //绘图函数。在win32程序中可以正常运行
RenderScene();
}

/////////////////////////////////////////////////////////////////////////////
// CBevelView printing

BOOL CBevelView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CBevelView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CBevelView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CBevelView diagnostics

#ifdef _DEBUG
void CBevelView::AssertValid() const
{
CView::AssertValid();
}

void CBevelView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}


#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBevelView message handlers

int CBevelView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here

InitGL();
InitData();

SetTimer(1, 30, NULL);

return 0;
}

void CBevelView::OnDestroy() 
{
CView::OnDestroy();

// TODO: Add your message handler code here
FinalData();
FinalGL();

}

void CBevelView::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default
Invalidate(0);
CView::OnTimer(nIDEvent);
}

void CBevelView::OnSize(UINT nType, int cx, int cy) 
{
CView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
m_nWidth = cx;
m_nHeight = cy;

glViewport(0, 0, cx, cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();



//平行投影
glOrtho(-cx / 2 , cx / 2, -cy / 2, cy / 2, -500.0f, 5000.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


bool CBevelView::InitGL()
{
//
// 装载OpenGL环境
//

if (NULL == m_hDC )
{
return false;
}

staticPIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
//查找是否有与前面设置的pfd相应的像素格式
int nFormat = ::ChoosePixelFormat(m_hDC, &pfd);
if (0 == nFormat ||
//设置像素格式
FALSE == ::SetPixelFormat(m_hDC, nFormat, &pfd))
{
return false;
}
//创建与当前DC相关的RC
if (NULL == (m_hRC = ::wglCreateContext(m_hDC)) ||
//为线程设置一个当前RC
FALSE == ::wglMakeCurrent(m_hDC, m_hRC))
{
return false;
}

//
// 预设OpenGL环境变量
//
::glShadeModel(GL_SMOOTH);  //设置着色模式
::glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
::glClearDepth(1.0f);
::glEnable(GL_DEPTH_TEST);
::glDepthFunc(GL_LEQUAL);//深度小于或等于时渲染


//
// 光照
//

    GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.4f, 1.0f };
GLfloat position[] = { 1.0, 1.0, 1.0, 0.0 };
//指定第0号光源的位置 
::glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
::glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
::glLightfv(GL_LIGHT0, GL_POSITION, position);

::glEnable(GL_LIGHTING);
::glEnable(GL_LIGHT0);   //使用0号光照
::glEnable(GL_NORMALIZE);

return true;
}

void CBevelView::FinalGL()
{
::wglMakeCurrent(NULL, NULL);
::wglDeleteContext(m_hRC);
}




void CBevelView::BevelArith_1(void)
{
CBevelDoc* pDoc = (CBevelDoc*)CBevelView::GetDocument();
// 描述
KBevelTool::BEVELDESC oDesc;
::memset(&oDesc, 0, sizeof(oDesc));
oDesc.eTopTyp = pDoc->m_eBevelType;
oDesc.rTopWid = pDoc->m_weight;
oDesc.rTopHgt = pDoc->m_height;
 oDesc.rDepth = pDoc->m_depth;
 oDesc.eBtmTyp = pDoc->m_eBevelType;
 oDesc.rBtmWid = pDoc->m_weight;
 oDesc.rBtmHgt = pDoc->m_height;
oDesc.nCurveHits = 4;
oDesc.nGroupSize = 4096;

// 建模
KBevelTool oBTool;
KBevelTool::BEVELMODEL oModel;
oBTool.BuildModel(m_oPolygons, oDesc, &oModel);

// 渲染模型
{
KBevelTool::VERT3DARRAY& oVertexs = oModel.oBevelVertices;

for (int n = 0; n < oVertexs.size(); n += 4)
{
::glBegin(GL_POLYGON);
::glNormal3f(oVertexs[n].n.x, oVertexs[n].n.y, oVertexs[n].n.z);
::glVertex3f(oVertexs[n].pt.x, oVertexs[n].pt.y, oVertexs[n].pt.z);
::glNormal3f(oVertexs[n+1].n.x, oVertexs[n+1].n.y, oVertexs[n+1].n.z);


::glVertex3f(oVertexs[n+1].pt.x, oVertexs[n+1].pt.y, oVertexs[n+1].pt.z);
::glNormal3f(oVertexs[n+2].n.x, oVertexs[n+2].n.y, oVertexs[n+2].n.z);
::glVertex3f(oVertexs[n+2].pt.x, oVertexs[n+2].pt.y, oVertexs[n+2].pt.z);
::glNormal3f(oVertexs[n+3].n.x, oVertexs[n+3].n.y, oVertexs[n+3].n.z);
::glVertex3f(oVertexs[n+3].pt.x, oVertexs[n+3].pt.y, oVertexs[n+3].pt.z);
::glEnd();
}
}

{
KBevelTool::VERT3DARRAY& oVertexs = oModel.oDepthVertices;
//KBevelTool::INDEXARRAY& oIndexs = oModel.oBevelIndices;

for (int n = 0; n < oVertexs.size(); n += 4)
{
::glBegin(GL_POLYGON);
::glNormal3f(oVertexs[n].n.x, oVertexs[n].n.y, oVertexs[n].n.z);
::glVertex3f(oVertexs[n].pt.x, oVertexs[n].pt.y, oVertexs[n].pt.z);
::glNormal3f(oVertexs[n+1].n.x, oVertexs[n+1].n.y, oVertexs[n+1].n.z);
::glVertex3f(oVertexs[n+1].pt.x, oVertexs[n+1].pt.y, oVertexs[n+1].pt.z);
::glNormal3f(oVertexs[n+2].n.x, oVertexs[n+2].n.y, oVertexs[n+2].n.z);
::glVertex3f(oVertexs[n+2].pt.x, oVertexs[n+2].pt.y, oVertexs[n+2].pt.z);
::glNormal3f(oVertexs[n+3].n.x, oVertexs[n+3].n.y, oVertexs[n+3].n.z);
::glVertex3f(oVertexs[n+3].pt.x, oVertexs[n+3].pt.y, oVertexs[n+3].pt.z);
::glEnd();
}
}

{

KBevelTool::VERT3DARRAY& oVertexs2 = oModel.oCapVertices;

::glBegin(GL_POLYGON);
for (int n = 0; n < oVertexs2.size()/2; n ++)
{
::glNormal3f(oVertexs2[n].n.x, oVertexs2[n].n.y, oVertexs2[n].n.z);
::glVertex3f(oVertexs2[n].pt.x, oVertexs2[n].pt.y, oVertexs2[n].pt.z);
}
::glEnd();

::glBegin(GL_POLYGON);
for ( n = oVertexs2.size()/2; n < oVertexs2.size(); n ++)
{
::glNormal3f(oVertexs2[n].n.x, oVertexs2[n].n.y, oVertexs2[n].n.z);
::glVertex3f(oVertexs2[n].pt.x, oVertexs2[n].pt.y, oVertexs2[n].pt.z);
}
::glEnd();
}

}

void CBevelView::Render3DScene()
{
// 两种效果:网格线框 和 材质光照
::glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

if (m_oPolygons.size() > 0)
{
BevelArith_1();
}
}

void CBevelView::RenderScene()
{
glClearColor(0.0, 0.0, 1.0, 0.0);

// 清除帧缓冲
::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// 载入矩阵并变换
::glLoadIdentity();

// 绘画
::glColor3ub(0, 0, 255);
//线段相连 不封闭
::glBegin(GL_LINE_STRIP);
for (int i = 0; i < m_oPoints.size(); i++)
{
::glVertex3f(m_oPoints[i].x, m_oPoints[i].y, -1.0);
}
::glEnd();

Render3DScene();
}

bool CBevelView::InitData()
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_nGdiplusToken, &gdiplusStartupInput, NULL);

CreatePoint(0, 0);

m_pbColor = new GLubyte[m_nColorCount];

for (int i = 0; i < m_nColorCount; i++)
{
m_pbColor[i] = rand() % 255;
}

return true;
}

void CBevelView::FinalData()
{
Gdiplus::GdiplusShutdown(m_nGdiplusToken);
delete[] m_pbColor;
DeletePolygons();
}

void CBevelView::OnLButtonDown(UINT nFlags, CPoint point) 


{
// TODO: Add your message handler code here and/or call default
UpdatePoint(point.x, point.y);
CreatePoint(point.x, point.y);

CView::OnLButtonDown(nFlags, point);
}

void CBevelView::OnRButtonDown(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
m_oPoints.pop_back();
FinishPolygon();
CreatePoint(0, 0);
InvalidateRect(NULL,FALSE);

CView::OnRButtonDown(nFlags, point);
}

void CBevelView::OnMouseMove(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
UpdatePoint(point.x, point.y);
InvalidateRect(NULL,FALSE);

CView::OnMouseMove(nFlags, point);
}

void CBevelView::FinishPolygon()
{
if (!m_oPoints.empty())
{
KPolygon* pPoly = new KPolygon();
pPoly->m_nVertCount = m_oPoints.size();
pPoly->m_pVerts = new KPoint2D[pPoly->m_nVertCount];
::memcpy(pPoly->m_pVerts, &m_oPoints[0],
sizeof(KPoint2D) * pPoly->m_nVertCount);
m_oPolygons.push_back(pPoly);
m_oPoints.clear();
}
}

void CBevelView::DeletePolygons()
{
for (int i = 0; i < m_oPolygons.size(); i++)
{
KPolygon::DeletePolygon(m_oPolygons[i]);
}
m_oPolygons.clear();
}

void CBevelView::CreatePoint(int x, int y)
{
KPoint2D pt;
pt.x = (float)-(m_nWidth / 2 - x);
    pt.y = (float)(m_nHeight / 2 - y);
m_oPoints.push_back(pt);
}

void CBevelView::UpdatePoint(int x, int y)
{
if (m_oPoints.empty()) return;

KPoint2D& pt = m_oPoints.back();
pt.x = (float)-(m_nWidth / 2 - x);
    pt.y = (float)(m_nHeight / 2 - y);
}
OpenGL MFC
[解决办法]
new一个ClientDC试试

热点排行