osg2cpp例子

本文介绍了如何学习并解析osg3.0.1中的osg2cpp例子,该例子展示了如何将GLSL的顶点着色器和片段着色器文件转换为char数组。文章讲解了ArgumentParser和ApplicationUsage类用于处理命令行参数和显示帮助信息,同时提到了osgUtil库中字符串处理的使用,并详细解释了std::string的find_first_of、size_type和npos等相关函数的用法。

今天我开始一边学习一边解析osg3.0.1中的每一个例子。
首先,开始是osg2cpp,这个例子主要说明了把GLSL中的定点着色器和片源着色器文件读取成一个char数组,可以在应用程序中直接拷贝定义。
首先是命令行处理类ArgumentParser和ApplicationUsage,通过这两个类可以处理控制台程序的命令行命令,程序的操作说明,命令行参数方法等。
arguments.getApplicationUsage()->setApplicationName 设置应用程序名称;
arguments.getApplicationUsage()->setDescription设置应用程序描述
arguments.getApplicationUsage()->setCommandLineUsage设置应用程序使用描述
arguments.getApplicationUsage()->addCommandLineOption设置应用程序具体的命令描述

if (arguments.read("-h") || arguments.read("--help"))
    {
        arguments.getApplicationUsage()->write(std::cout);
        return 1;
    }
很常用的方法,显示帮助信息。

osgUtil库中存放各种工具类,方便操作,本例子中用到了字符串的处理(当然,qt等中也有自己的一套处理字符串函数),建议使用osg的。

然后就是string中的find_first_of 、std::string::size_type、std::string::npos。
std::string::size_type返回一种unsigned类型,不同的机器解析不同,对于string的size一定要返回size_type类型而不是int,

以下是一个简单的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)绑定到状态集中。最后将状态集设置到模型上,用于渲染整个场景。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值