osg glsl添加半透明

一开始,没有设置对,所以GLSL半透明没出来,看了下例子,实际上很简单,除了在片元着色器alpha设置外,还要启动混合

osg::ref_ptr<osg::StateSet> stateset = geode->getOrCreateStateSet();
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

废话不多说,上代码

//点集合启动半透明

#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>

#include <osg/Switch>
#include <osg/Types>
#include <osgText/Text>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>

#include <osgGA/Device>

#include
#include <osg/Shader>

osg::ref_ptrosg::Geode createPointsGeode()
{
osg::ref_ptrosg::Geode geode = new osg::Geode;
osg::ref_ptrosg::Geometry geom = new osg::Geometry;

osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
for (int i = 0; i < 100; i++)
{
	float x = i * 10;
	for (int j = 0; j < 100; j++)
	{
		float y = j * 10;
		for (int k = 0; k < 100; k++)
		{
			float z = k * 10;
			vertices->push_back(osg::Vec3(x, y, z));
		}
	}
}
geom->setVertexArray(vertices);
//	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
//	colors->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 0.5f));
//	geom->setColorArray(colors, osg::Array::BIND_OVERALL);

osg::Vec3Array* normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0, -1.0f, 0.0f));
geom->setNormalArray(normals, osg::Array::BIND_OVERALL);

geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, vertices->size()));
geode->addDrawable(geom);
return geode;

}

static const char * vertexShader =
{
“void main(void)\n”
“{\n”
" gl_Position = ftransform();\n"
“}\n”
};
static const char *psShader =
{
“void main(void)\n”
“{\n”
“gl_FragColor =vec4(0.0f,0.0f,1.0f,1.0f);\n”
“}\n”
};

int main()
{
osg::ref_ptrosg::Group grp = new osg::Group;
osg::ref_ptrosg::Geode geode = createPointsGeode();
grp->addChild(geode);
osg::ref_ptrosg::StateSet stateset = geode->getOrCreateStateSet();
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
osg::ref_ptrosg::Shader vs = new osg::Shader(osg::Shader::VERTEX, vertexShader);
osg::ref_ptrosg::Shader ps = new osg::Shader(osg::Shader::FRAGMENT, psShader);
osg::ref_ptrosg::Program program = new osg::Program;
program->addShader(vs);
program->addShader(ps);
stateset->setAttribute(program, osg::StateAttribute::ON);
osg::ref_ptrosgViewer::Viewer viewer = new osgViewer::Viewer;
viewer->setSceneData(grp);
viewer->run();

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值