1. 程式人生 > >OSG場景漫遊(二)

OSG場景漫遊(二)

二、測試TravelManipulator.DLL

下面我們來建立一個只需幾行程式碼就可以完成漫遊功能的程式,具體步驟如下:

第一步:選單檔案->新建專案->WIN32/WIN32控制檯應用程式,專案名取test,在應用程式設定中點選空專案後完成。

第二步:選單專案->test屬性->配置屬性->連結器->命令列中新增:OpenThreadsWin32d.lib Producerd.lib osgd.lib 

osgDBd.lib osgFXd.lib osgGAd.lib osgParticled.lib osgProducerd.lib osgSimd.lib osgTerraind.lib osgTextd.lib 

osgUtild.lib TravelManipulator.lib注意,要新增剛才我們設計的lib。

第三步:把剛才TravelManipulator專案以下三個檔案拷貝到該專案目錄中:TravelManipulator.h, TravelManipulator.lib,

 TravelManipulator.DLL,(該專案目錄是指放有該專案原始檔的目錄)

第四步:在解決方案檢視中,對原始檔資料夾點右鍵->新增->新建項->程式碼/CPP檔案,檔名輸入main,然後在該檔案中新增下面內容:

#include <iostream>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osg/MatrixTransform>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include "TravelManipulator.h"
using namespace std;

int main()
{

//建立Viewer物件
osg::ref_ptr<osgViewer::Viewer>viewer = new osgViewer::Viewer();
//新增狀態事件
//viewer.get()->addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
//視窗大小變化事件
viewer.get()->addEventHandler(new osgViewer::WindowSizeHandler);
//新增一些常用狀態設定
viewer.get()->addEventHandler(new osgViewer::StatsHandler);
// 把漫遊器加入到場景中
TravelManipulator::TravelToScence(viewer.get());
osg::ref_ptr<osg::Group>root = new osg::Group();
// 讀取地形
osg::ref_ptr<osg::Node>node = osgDB::readNodeFile("D:\\OSG\\data\\lz.osg");
// 讀取飛機
//osg::ref_ptr<osg::Node>tank = osgDB::readNodeFile("F:\\Models\\Amx30\\Amx30.IVE");
osg::ref_ptr<osg::Node>B25 = osgDB::readNodeFile("Models\\B25\\B25.IVE");
osg::ref_ptr < osg::MatrixTransform> scale = new osg::MatrixTransform ;
scale ->setMatrix(osg::Matrix::scale(0.1, 0.1, 0.1)*osg::Matrix::translate(-10.0f, -250.0f, 99.0f)) ;
scale ->addChild(B25.get()) ;
// 新增到場景
root->addChild(node.get());
root->addChild(scale.get());
//root->addChild(B25.get());
// 優化場景資料
osgUtil::Optimizer optimizer;
optimizer.optimize(root.get());
viewer->setSceneData(root.get());
viewer->realize();
// 視窗模式
((osgViewer::GraphicsWindow*)viewer->getCamera()->getGraphicsContext())->setWindowRectangle(100,100,800,600);
((osgViewer::GraphicsWindow*)viewer->getCamera()->getGraphicsContext())->setWindowDecoration(true);
viewer->run();
return 0;
}
三、出現的錯誤和解決

1.編譯dll卻沒有lib檔案產生

解決方法:(1).確定建立的專案是DLL專案

(2).dll沒有定義任何輸出函式,所以沒有生成lib檔案.

(3).在TravelManipulator.h標頭檔案前面新增如下程式碼

#ifdef TRVLM_EXPORTS #define TRVLM_API __declspec(dllexport) #else #define TRVLM_API __declspec(dllimport) #pragma comment(lib, "TransfPkg.lib") #endif ... class   TRVLM_API TravelManipulator

2.在編譯test時,出現瞭如下錯誤

MSVCRT.lib(crtexew.obj) : error LNK2001: 無法解析的外部符號 [email protected]

解決方法:在控制檯程式中

(1)選單中選擇 Project->Properties, 彈出Property Pages視窗

(2).在左邊欄中依次選擇:Configuration Properties->C/C++->Preprocessor,然後在右邊欄的Preprocessor Definitions對應的項中刪除_WINDOWS, 新增_CONSOLE.

(3)在左邊欄中依次選擇:Configuration Properties->Linker->System,然後在右邊欄的SubSystem對應的項改為CONSOLE(/SUBSYSTEM:CONSOLE)

四、執行結果圖