|
本帖最后由 chyzh 于 2011-9-2 19:46 编辑
我最近在做一个MFC ActiveX程序,通过IE浏览器来运行,需要循环加载同一个模型多次,但浏览器经常出现崩溃报内存“0X000000”不能read,困扰多日未果,望高手指点迷津。
下面是主要的代码:
osg::ref_ptr<osg::MatrixTransform> car[14][2];
void RenderThread(void* ptr)
{
osgViewer::Viewer* viewer = (osgViewer::Viewer*)ptr;
while( !viewer->done() )
{
viewer->frame();
}
::MessageBox(0, L"渲染进程已经终止", L"警告", MB_OK);
_endthread();
}
int CosgMFCAxCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
//初始化局部变量,开辟内存空间
m_Filename = new char[255];
m_Viewpoint= new char[255];
m_Url=new char[255];
m_Area= new char[10];
m_Option=new char[10];
filename=new char[255];
loadfile=new char[255];
// chyzh获取当前响应窗口的大小
RECT rect;
GetWindowRect(&rect);
/*WCHAR str[255];
swprintf(str, L"(%d,%d) - (%d,%d)", rect.left,rect.top,rect.right,rect.bottom);
::MessageBox(0, (LPCWSTR)str, L"Hint", MB_OK);*/
// Set window traits and gc
m_Traits = new osg::GraphicsContext::Traits;
osg::ref_ptr<osg::Referenced> windata = new
osgViewer::GraphicsWindowWin32::WindowData( GetSafeHwnd() );
m_Traits->x = 0;
m_Traits->y = 0;
m_Traits->width = rect.right - rect.left;
m_Traits->height = rect.bottom - rect.top;
m_Traits->windowDecoration = false;
m_Traits->doubleBuffer = true;
m_Traits->sharedContext = 0;
m_Traits->setInheritedWindowPixelFormat = true;
m_Traits->inheritedWindowData = windata;
osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(
m_Traits.get() );
// chyzh将窗口图形上下文传递给新建的摄像机,并设置摄像机的视口,透视矩阵等参数
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(gc);
camera->setViewport( new osg::Viewport(m_Traits->x, m_Traits->y, m_Traits->width,
m_Traits->height) );
camera->setProjectionMatrixAsPerspective( 10.0f, (double)m_Traits->width/(double)
m_Traits->height, 1.0f, 10000.0f );
m_Viewer = new osgViewer::Viewer;
m_Viewer->setThreadingModel( osgViewer::Viewer::SingleThreaded );
m_Viewer->setCamera( camera.get() );
//增加控制器,以调整视点
mycamera = new MyCameraManipulator;
mycamera->setPosRot(0,-30,0,0,0,0);
m_Viewer->setCameraManipulator( mycamera );
m_Root = new osg::Group;
m_Viewer->setSceneData( m_Root.get() );
m_Viewer->realize();
// 开启新的渲染线程
_beginthread(&RenderThread, 0, m_Viewer);
return 0;
}
osg::MatrixTransform* createobject1(const char*strFileName, double x, double y, double z)
{
osg::ref_ptr<osg::Node> model=new osg::Node;
model=osgDB::readNodeFile(strFileName);
osg::ref_ptr<osg::MatrixTransform> mt=new osg::MatrixTransform;
mt->setMatrix(osg::Matrix::translate(x,y,z));
mt->addChild(model);
return mt.release();
}
//-------------------------------------------------------------------------------------
//加载模型部分代码
void CosgMFCAxCtrl:oadFile(){
filename="car.osg";
for (int i=0;i<14;i++)
{
car[i ][0]=createobject1(loadfile, 0, -40, 0);
if ( !car[i ][0].valid() )
{::MessageBox(0, A2BSTR("读取节点car0失败"), L"警告", MB_OK);}
else{
car[i ][0]->setDataVariance( osg::Object:YNAMIC );
m_Root->addChild(car[i ][0]);
}
car[i ][1]=createobject1(loadfile, 0, -40, 0);
if ( !car[i ][1].valid() )
{::MessageBox(0, A2BSTR("读取节点car1失败"), L"警告", MB_OK);}
else{
car[i ][1]->setDataVariance( osg::Object::DYNAMIC );
m_Root->addChild(car[i ][1]);
}
} |
|