osgclip例子

该博客详细解析了OSG中的ClipNode和ClipPlane的使用,通过创建裁剪盒子来展示如何定义和应用裁剪面。内容涵盖了ClipNode的状态设置、裁剪面的构造、PolygonMode的修改以及MatrixTransform的旋转效果,最终实现了一个线状模型和面状模型被裁剪的示例。
摘要由CSDN通过智能技术生成
本来自演示了ClipNode。
主要的内容都在decorate_with_clip_node中。
Clip在OpenGL中是定义裁剪面,然后glEnable,设置使用裁剪,于是在OpenGL的固定管线中就根据指定的


裁剪面裁剪。
才以上分析我们可以指定,在OSG中Clip属于一种状态,因此,定义裁剪就是设置一个StateSet。OSG中会


根据设置了节点StateSet构建一棵状态树,这棵状态树会根据State改变当时OpenGL的状态,比如,进入A


节点,它设置了StateSet,此时,根据StateSet设置状态,离开的时候再设置回去。这就是State类中非


常有用的pushStateSet和popStateSet。
因此,ClipNode的实现就不能理解,ClipNode就是定义了裁剪面的组节点,加入到CLipNode中的子节点都


会根据指定的裁剪面裁掉。
在OSG中ClipPlane是一个裁剪面,继承StateAttribute,可以通过Plane指定空间中任意的一个裁剪面。
看看ClipNode中的createClipBox方法,根据BoundingBox定义了一个裁剪盒子,实际上是定义了六个裁剪


面。
_planes.clear();
    if (!_stateset.valid()) _stateset = new osg::StateSet;


    


_pla
以下是一个简单的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、付费专栏及课程。

余额充值