|
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- #pragma once
- namespace OSGViewer {
- osg::ref_ptr<osg::Group> root ;
- osg::ref_ptr<osgViewer::Viewer> viewer;
- bool renderok;
- /// <summary>
- /// frmMain 摘要
- /// </summary>
- public ref class frmMain : public System::Windows::Forms::Form
- {
- public:
- frmMain(void)
- {
- InitializeComponent();
- //
- //TODO: 在此处添加构造函数代码
- //
- }
- protected:
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- ~frmMain()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::MenuStrip^ menuStrip;
- protected:
- private: System::Windows::Forms::ToolStripMenuItem^ 文件FToolStripMenuItem;
- private: System::Windows::Forms::ToolStripMenuItem^ 打开ToolStripMenuItem;
- private: System::Windows::Forms::ToolStripSeparator^ toolStripMenuItem1;
- private: System::Windows::Forms::ToolStripMenuItem^ 退出XToolStripMenuItem;
- private: System::Windows::Forms::StatusStrip^ statusStrip1;
- private: System::Windows::Forms::ToolStripStatusLabel^ toolStripStatusLabel1;
- private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
- private:
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- System::ComponentModel::Container ^components;
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// 设计器支持所需的方法 - 不要
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- void InitializeComponent(void)
- {
- System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(frmMain::typeid));
- this->menuStrip = (gcnew System::Windows::Forms::MenuStrip());
- this->文件FToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
- this->打开ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
- this->toolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripSeparator());
- this->退出XToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
- this->statusStrip1 = (gcnew System::Windows::Forms::StatusStrip());
- this->toolStripStatusLabel1 = (gcnew System::Windows::Forms::ToolStripStatusLabel());
- this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
- this->menuStrip->SuspendLayout();
- this->statusStrip1->SuspendLayout();
- this->SuspendLayout();
- //
- // menuStrip
- //
- this->menuStrip->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->文件FToolStripMenuItem});
- this->menuStrip->Location = System::Drawing::Point(0, 0);
- this->menuStrip->Name = L"menuStrip";
- this->menuStrip->Size = System::Drawing::Size(632, 24);
- this->menuStrip->TabIndex = 0;
- this->menuStrip->Text = L"menuStrip";
- //
- // 文件FToolStripMenuItem
- //
- this->文件FToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(3) {this->打开ToolStripMenuItem,
- this->toolStripMenuItem1, this->退出XToolStripMenuItem});
- this->文件FToolStripMenuItem->Name = L"文件FToolStripMenuItem";
- this->文件FToolStripMenuItem->Size = System::Drawing::Size(57, 20);
- this->文件FToolStripMenuItem->Text = L"文件(&F)";
- //
- // 打开ToolStripMenuItem
- //
- this->打开ToolStripMenuItem->Name = L"打开ToolStripMenuItem";
- this->打开ToolStripMenuItem->Size = System::Drawing::Size(152, 22);
- this->打开ToolStripMenuItem->Text = L"打开(&O)";
- this->打开ToolStripMenuItem->Click += gcnew System::EventHandler(this, &frmMain::打开ToolStripMenuItem_Click);
- //
- // toolStripMenuItem1
- //
- this->toolStripMenuItem1->Name = L"toolStripMenuItem1";
- this->toolStripMenuItem1->Size = System::Drawing::Size(149, 6);
- //
- // 退出XToolStripMenuItem
- //
- this->退出XToolStripMenuItem->Name = L"退出XToolStripMenuItem";
- this->退出XToolStripMenuItem->Size = System::Drawing::Size(152, 22);
- this->退出XToolStripMenuItem->Text = L"退出(&X)";
- this->退出XToolStripMenuItem->Click += gcnew System::EventHandler(this, &frmMain::退出XToolStripMenuItem_Click);
- //
- // statusStrip1
- //
- this->statusStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->toolStripStatusLabel1});
- this->statusStrip1->Location = System::Drawing::Point(0, 428);
- this->statusStrip1->Name = L"statusStrip1";
- this->statusStrip1->Size = System::Drawing::Size(632, 22);
- this->statusStrip1->TabIndex = 1;
- this->statusStrip1->Text = L"statusStrip1";
- //
- // toolStripStatusLabel1
- //
- this->toolStripStatusLabel1->Name = L"toolStripStatusLabel1";
- this->toolStripStatusLabel1->Size = System::Drawing::Size(227, 17);
- this->toolStripStatusLabel1->Text = L"Windows 窗体应用程序+ OSG 制作:王毫安";
- //
- // openFileDialog1
- //
- this->openFileDialog1->Filter = L"ive文件|*.ive|osg文件|*.osg|所有文件|*.*";
- //
- // frmMain
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(632, 450);
- this->Controls->Add(this->statusStrip1);
- this->Controls->Add(this->menuStrip);
- this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
- this->MainMenuStrip = this->menuStrip;
- this->Name = L"frmMain";
- this->Text = L"OSG模型浏览器";
- this->Load += gcnew System::EventHandler(this, &frmMain::frmMain_Load);
- this->menuStrip->ResumeLayout(false);
- this->menuStrip->PerformLayout();
- this->statusStrip1->ResumeLayout(false);
- this->statusStrip1->PerformLayout();
- this->ResumeLayout(false);
- this->PerformLayout();
- }
- #pragma endregion
- #pragma region 自定义函数
- private:
- // System::String转换为std::string
- std::string ConvertToString(System::String^ str)
- {
- int q=(int)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
- char* p=(char*)q;
- return std::string(p);
- }
- // 渲染
- static void render(void* ptr)
- {
- osgViewer::Viewer* viewer = (osgViewer::Viewer*)ptr;
- while (!viewer->done())
- {
- viewer->frame();
- }
- renderok = true;
- _endthread();
- }
- // 初始化
- void InitViewer()
- {
- // Set window traits and gc
- osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
- osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData( (HWND)this->Handle.ToPointer() );
- traits->x = 0;
- traits->y = 0;
- traits->width = this->ClientSize.Width;
- traits->height = this->ClientSize.Height;
- traits->windowDecoration = false;
- traits->doubleBuffer = true;
- traits->sharedContext = 0;
- traits->inheritedWindowData = windata;
- traits->setInheritedWindowPixelFormat = true;
- osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits.get());
- // Create camera
- osg::ref_ptr<osg::Camera> camera;
- camera = new osg::Camera;
- camera->setGraphicsContext(gc);
- camera->setViewport(new osg::Viewport(traits->x, traits->y, traits->width, traits->height));
- // Create viewer
- viewer = new osgViewer::Viewer;
- viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
- viewer->setCameraManipulator(new osgGA::TrackballManipulator);
- viewer->addSlave(camera.get());
- // 快捷键 1;2;3;4 四种动画模式
- osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
- keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
- keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
- keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
- keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
- viewer->setCameraManipulator( keyswitchManipulator.get() );
- viewer->addEventHandler(new osgViewer::StatsHandler);
- // Set root node of the scene
- root = new osg::Group;
- viewer->setSceneData(root.get());
- viewer->realize();
-
- // Create a thread for the simulation loop
- renderok = false;
- _beginthread(&render, 0, viewer);
- }
-
- // 加载模型文件
- void LoadModel(String^ fileName)
- {
- // 如果Viewer窗口关闭,则重新初始化Viewer窗口
- if(viewer->done())
- {
- InitViewer();
- }
- //载入模型
- osg::ref_ptr<osg::Node> model;
- model= osgDB::readNodeFile( ConvertToString(fileName) );
- if( !model ) return;
- viewer->setSceneData( model.get() );
- viewer->realize();
- }
- #pragma endregion
- private: System::Void 打开ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
- if(openFileDialog1->ShowDialog() == Windows::Forms::DialogResult::OK)
- {
- try
- {
- LoadModel(openFileDialog1->FileName);
- }
- catch(...)
- {
- MessageBox::Show("模型文件打开失败!","错误",MessageBoxButtons::OK,MessageBoxIcon::Error);
- }
- }
- }
- private: System::Void frmMain_Load(System::Object^ sender, System::EventArgs^ e) {
- // 初始化Viewer窗口
- InitViewer();
- }
- private: System::Void 退出XToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
- // 退出
- this->Close();
- }
- };
- }
复制代码 我用windows 窗体应用程序 做的osg模型浏览器。连续打开6、7个模型后就报error.我估计是void LoadModel(String^ fileName)函数有问题,应该怎么优化?????????
[ 本帖最后由 playwww 于 2009-4-15 00:56 编辑 ] |
-
|