OSG的配置以及两个例子

OSG的配置以及例子

2015/1/3

 OSG配置

1, 文件(这个配置中是把拷贝的OSG文件夹放在D盘)


2, 环境变量

在用户变量中添加,不是系统变量中添加


变量:PATH

值:

D:\OSG\OSG-3.2.0-bin\bin;D:\OSG\3rdParty\bin;D:\OSG\osgEarth-2.5-bin\bin;D:\OSG\gdal-1.11.0-bin\bin;D:\OSG\geos-3.4.2-bin\bin;D:\OSG\proj-4.8.0-bin\bin;

 

示例1-1

OSGHelloWorld

(1)建立一个WIN32控制台输出程序。下一步,附加项:空项目。

添加main.cpp

 

#ifdef _DEBUG

#pragma comment(lib,"osgViewerd.lib")

#pragma comment(lib,"osgDBd.lib")

#pragma comment(lib,"OpenThreadsd.lib")

#pragma comment(lib,"osgd.lib")

#else

#pragma comment(lib,"osgViewer.lib")

#pragma comment(lib,"osgDB.lib")

#pragma comment(lib,"OpenThreads.lib")

#pragma comment(lib,"osg.lib")

 

#endif

 

#include <osgViewer/Viewer>

#include <osgDB/ReadFile>

 

int main(  )

{

 

    osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;

    viewer->setSceneData(osgDB::readNodeFile("glider.osg"));

   

    returnviewer->run();

 }

(2)配置

Project->helloworld property pages->configurationproperties ->VC++ 目录->将主程序中include bin lib的路径分别添加到相应的包含目录,可执行目录,目录

include   D:\OSG\OSG-3.2.0-bin\include

bin      D:\OSG\OSG-3.2.0-bin\bin

lib      D:\OSG\OSG-3.2.0-bin\lib

(3)执行,成功时,展示出glider.osg模型

 

 

1

问题:OSG3.2下,vs2008配置不成功,文件读取时内存溢出。

解决方案:不使用vs2008,使用vs2010进行配置成功。Project->helloworld propertypages->configuration properties ->VC++ 目录->将主程序中include bin lib的路径分别添加到相应的包含目录,可执行目录,目录

include   D:\OSG\OSG-3.2.0-bin\include

bin      D:\OSG\OSG-3.2.0-bin\bin

lib      D:\OSG\OSG-3.2.0-bin\lib

 

 

示例2-1

OSGRateDisplay 显示中文帧速

 

Main.cpp

 

#ifdef _DEBUG

#pragma comment(lib,"osgViewerd.lib")

#pragma comment(lib,"osgDBd.lib")

#pragma comment(lib,"OpenThreadsd.lib")

#pragma comment(lib,"osgd.lib")

#pragma comment(lib,"osgGAd.lib")//Debug

 

 

 

 

#else

#pragma comment(lib,"osgViewer.lib")

#pragma comment(lib,"osgDB.lib")

#pragma comment(lib,"OpenThreads.lib")

#pragma comment(lib,"osg.lib")

#pragma comment(lib,"osgGA.lib")//release

#endif

 

#include <osgViewer/Viewer>

#include <osgDB/ReadFile>

#include <osgViewer/ViewerEventHandlers>

 

 

int main( )

{

    osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;

    viewer->addEventHandler(new osgViewer::StatsHandler);

    //viewer->setSceneData(osgDB::readNodeFile("glider.osg"));

   viewer->setSceneData(osgDB::readNodeFile("cow.osg"));

    returnviewer->run();

 }

(2)配置

Project->helloworld property pages->configurationproperties ->VC++ 目录->将主程序中include bin lib的路径分别添加到相应的包含目录,可执行目录,目录

include   D:\OSG\OSG-3.2.0-bin\include

bin      D:\OSG\OSG-3.2.0-bin\bin

lib      D:\OSG\OSG-3.2.0-bin\lib

(3)执行,成功时,展示出cow.osg模型

 

 

1

问题:无法解析的外部符号

 

错误       5     errorLNK2019: 无法解析的外部符号"__declspec(dllimport) public: virtual class osg::Object * __thiscallosgGA::GUIEventHandler::cloneType(void)const "(__imp_?cloneType@GUIEventHandler@osgGA@@UBEPAVObject@osg@@XZ),该符号在函数 "[thunk]:public:virtual class osg::Object * __thiscallosgGA::GUIEventHandler::cloneType`vtordisp{4294967292,152}' (void)const "(?cloneType@GUIEventHandler@osgGA@@$4PPPPPPPM@JI@BEPAVObject@osg@@XZ) 中被引用       D:\OSGProject\RateDisplay1\RateDisplay1\main.obj   RateDisplay1

 

解决方案:教材中的源代码有问题,应该添加句

#pragma comment(lib,"osgGAd.lib")//Debug

 

.........

#pragma comment(lib,"osgGA.lib")//release

 

笔记:一般出现无法解析的外部符号为库没有添加进去,根据错误提示的osgGA锁定问题是osgGA库未添加。

 

 

 

示例2-2

OSG  DefaultEvent添加默认事件

1

问题:按键盘中的“S/s”无法显示状态(state),这并非键盘不响应所致,而是未添加显示状态(state)的事件响应,可能是我的版本3.2和教材版本3.0不同所致。那3.2版本要显示state该添加哪个事件呢?

 

示例2-3

OSGBasicGraphic 绘制正方形

1

pragma comment:

该指令将一个注释记录放入一个对象文件或可执行文件中。常用的lib关键字,可以帮我们连入一个库文件。

 

 

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的osg shader的例子,它使用了相对复杂的光照和纹理映射,用于渲染一个纹理贴图的模型: ```cpp osg::ref_ptr<osg::Shader> vertexShader = new osg::Shader(osg::Shader::VERTEX, R"( #version 330 in vec3 vertex; in vec3 normal; in vec2 texCoord; uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; uniform mat4 normalMatrix; out vec3 normalInterp; out vec2 texCoordInterp; out vec3 fragPos; void main() { normalInterp = vec3(normalMatrix * vec4(normal, 0.0)); texCoordInterp = texCoord; fragPos = vec3(modelViewMatrix * vec4(vertex, 1.0)); gl_Position = projectionMatrix * modelViewMatrix * vec4(vertex, 1.0); } )"); osg::ref_ptr<osg::Shader> fragmentShader = new osg::Shader(osg::Shader::FRAGMENT, R"( #version 330 in vec3 normalInterp; in vec2 texCoordInterp; in vec3 fragPos; uniform sampler2D tex; uniform vec3 lightPos; uniform vec3 lightColor; uniform vec3 objectColor; uniform float shininess; out vec4 fragColor; void main() { vec3 ambient = 0.1 * objectColor; vec3 norm = normalize(normalInterp); vec3 lightDir = normalize(lightPos - fragPos); float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * lightColor; vec3 viewDir = normalize(-fragPos); vec3 reflectDir = reflect(-lightDir, norm); float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); vec3 specular = spec * lightColor; vec4 texColor = texture(tex, texCoordInterp); fragColor = vec4((ambient + diffuse + specular) * texColor.rgb, texColor.a); } )"); osg::ref_ptr<osg::Program> program = new osg::Program; program->addShader(vertexShader.get()); program->addShader(fragmentShader.get()); osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet; stateSet->setAttributeAndModes(program.get(), osg::StateAttribute::ON); osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D; texture->setImage(osgDB::readImageFile("texture.png")); stateSet->setTextureAttributeAndModes(0, texture.get(), osg::StateAttribute::ON); osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("model.obj"); model->setStateSet(stateSet.get()); osgViewer::Viewer viewer; viewer.setSceneData(model.get()); viewer.run(); ``` 这段代码使用了OpenGL的着色器语言(GLSL)来定义顶点着色器和片段着色器,并创建了一个OpenGL程序(Program)对象,将两个着色器链接在一起。然后创建了一个状态集(StateSet)对象,并将程序和纹理对象(texture)绑定到状态集中。最后将状态集设置到模型上,用于渲染整个场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值