一开始,没有设置对,所以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;
}