为什么通过菜单点击画OpenGL图形提示内存不能读?恳求帮小弟看看!

sujinzhao407 2004-05-03 10:14:34
我想通过点击菜单“立方体”画出一个旋转的立方体,我已经实现了不加菜单时的程序,现在只是想通过一个菜单点击之后再显示立方体,但是提示内存不能读,我怀疑是设备描述表和渲染描述表的问题,因为我在OnDraw()里还要画直线,平面,圆等其它图形,但是我又不知道该如何改!恳求各位大哥帮小弟看看,小弟正在做毕业设计,马上就要交了,请大家耐心看完View.cpp文件,里面用到的函数和变量已经在View.h里定义过了,谢谢各位大哥了!至于里面画直线圆等的函数请大家不要管,只需要看看立方体的就行了。其中,m_bTimerEnabled控制立方体的旋转与停止,step控制每次旋转的角度,s控制旋转时立方体大小的变化。openGl环境我已经配好了。

CGradDesignView::CGradDesignView()
{
// TODO: add construction code here
m_bTimerEnabled =TRUE;
step=0.0;
s=0.0;
}

void CGradDesignView::OnDraw(CDC* pDC)
{
CGradDesignDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here

//直线
CPen linePen(0,0,RGB(0,100,200));
CPen* oldPen_line=pDC->SelectObject(&linePen);
pDC->MoveTo(x1_line,y1_line);
pDC->LineTo(x2_line,y2_line);
pDC->SelectObject(oldPen_line);
linePen.Detach();
//立方体
/*wglMakeCurrent(pDC->m_hDC,m_hRC);
RenderScene();
wglMakeCurrent(NULL,NULL);*/


}

void CGradDesignView::OnCube()
{
// TODO: Add your command handler code here
CDC* pDC=GetDC();
wglMakeCurrent(pDC->m_hDC,m_hRC);
RenderScene();
wglMakeCurrent(NULL,NULL);
}

场景绘制与渲染
//////////////////////////////////////////////////////////
BOOL CGradDesignView::RenderScene()
{
glClearColor(0.1,0.5,0.7,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glScalef(s,s,s);
glRotatef(step,0.0,1.0,0.0);
glRotatef(step,0.0,0.0,1.0);
glRotatef(step,1.0,0.0,0.0);
DrawColorBox(); // 绘制立方体
glPopMatrix();

glFlush();

::SwapBuffers(m_pDC->GetSafeHdc()); //交互缓冲区
return TRUE;
}

void CGradDesignView::DrawColorBox(void)
{
GLfloat p1[]={0.5,-0.5,-0.5}, p2[]={0.5,0.5,-0.5},
p3[]={0.5,0.5,0.5}, p4[]={0.5,-0.5,0.5},
p5[]={-0.5,-0.5,0.5}, p6[]={-0.5,0.5,0.5},
p7[]={-0.5,0.5,-0.5}, p8[]={-0.5,-0.5,-0.5};

GLfloat m1[]={0.0,0.0,0.0}, m2[]={-1.0,0.0,0.0},
m3[]={0.0,1.0,0.0}, m4[]={0.0,-1.0,0.0},
m5[]={0.0,0.0,1.0}, m6[]={0.0,0.0,-1.0};


GLfloat c1[]={1.0,0.0,1.0}, c2[]={0.0,1.0,1.0},
c3[]={1.0,1.0,1.0}, c4[]={1.0,0.0,1.0},
c5[]={1.0,0.0,0.0}, c6[]={1.0,1.0,0.0},
c7[]={0.0,1.0,0.0}, c8[]={1.0,1.0,1.0};
glBegin (GL_QUADS);

glColor3fv(c1);
glNormal3fv(m1);
glVertex3fv(p1);
glColor3fv(c2);
glVertex3fv(p2);
glColor3fv(c3);
glVertex3fv(p3);
glColor3fv(c4);
glVertex3fv(p4);

glColor3fv(c5);
glNormal3fv(m5);
glVertex3fv(p5);
glColor3fv(c6);
glVertex3fv(p6);
glColor3fv(c7);
glVertex3fv(p7);
glColor3fv(c8);
glVertex3fv(p8);

glColor3fv(c5);
glNormal3fv(m3);
glVertex3fv(p5);
glColor3fv(c6);
glVertex3fv(p6);
glColor3fv(c3);
glVertex3fv(p3);
glColor3fv(c4);
glVertex3fv(p4);

glColor3fv(c1);
glNormal3fv(m4);
glVertex3fv(p1);
glColor3fv(c2);
glVertex3fv(p2);
glColor3fv(c7);
glVertex3fv(p7);
glColor3fv(c8);
glVertex3fv(p8);

glColor3fv(c2);
glNormal3fv(m5);
glVertex3fv(p2);
glColor3fv(c3);
glVertex3fv(p3);
glColor3fv(c6);
glVertex3fv(p6);
glColor3fv(c7);
glVertex3fv(p7);

glColor3fv(c1);
glNormal3fv(m6);
glVertex3fv(p1);
glColor3fv(c4);
glVertex3fv(p4);
glColor3fv(c5);
glVertex3fv(p5);
glColor3fv(c8);
glVertex3fv(p8);

glEnd();
}

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

// TODO: Add your message handler code here
//添加窗口缩放时的图形变换函数
glViewport(0,0,cx,cy);
/////////////////////////////////////////////////////////////////
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (cx <= cy)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)cy/(GLfloat)cx,1.50*(GLfloat)cy/(GLfloat)cx, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)cx/(GLfloat)cy,1.5*(GLfloat)cx/(GLfloat)cy, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();

}

void CGradDesignView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
//添加定时器响应函数和场景更新函数
Invalidate(FALSE);
/////////////////////////////////////////////////////////////////
if(m_bTimerEnabled)
{
step = step + 1.0;
if (step > 360.0)
step = step - 360.0;
s+=0.005;
if(s>1.0)
s=0.1;
}
CView::OnTimer(nIDEvent);
}

void CGradDesignView::OnLine()
{
// TODO: Add your command handler code here
CLineInputDlg m_lineinputdlg;
if(m_lineinputdlg.DoModal()==IDOK)
{
x1_line=m_lineinputdlg.m_sx;
y1_line=m_lineinputdlg.m_sy;
x2_line=m_lineinputdlg.m_ex;
y2_line=m_lineinputdlg.m_ey;
}
Invalidate();
}
...全文
12 回复 点赞 打赏 收藏 举报
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
VC/MFC
加入

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2004-05-03 10:14
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……