#include <windows.h>
#include <osg/Geometry>
#include <osg/Geode>
#include <osgUtil/SmoothingVisitor>
#include <osgViewer/Viewer>
#include <osg/LineWidth>
#include <osg/PolygonMode>
//顶点着色器
static const char* vertSource = {
"varying out vec3 pos;\n"
"void main()\n"
"{\n"
"pos.x=gl_Vertex.x;\n"
"pos.y=gl_Vertex.y;\n"
"pos.z=gl_Vertex.z;\n"
"gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;\n"
"}\n"
};
//片元着色器
static const char* fragSource = {
"uniform vec4 color ; \n"
"varying vec3 pos;\n"
"uniform float osg_FrameTime;\n"
"float endlen = length(pos.xy); \n"
"void main()\n"
"{ \n"
"float sinvalue = sin(osg_FrameTime*-2); \n"
"float cosvalue = cos(osg_FrameTime*-2); \n"
"vec3 begin = vec3(endlen*cosvalue,endlen*sinvalue,0); \n"
"float beginlen = length(begin.xy); \n"
"float dotvalue = dot(begin,pos)/(beginlen*endlen); \n"
"vec3 cc = cross(begin,pos); \n"
"if(dotvalue>0.8 && dotvalue<1.0 && cc.z<0) \n"
"{ \n"
" float angle = acos(dotvalue); \n"
" float alpha = sin(angle); \n"
" color = vec4(0,1,0,alpha); \n"
"} \n"
"else \n"
"{ \n"
" color = vec4(1,1,1,0); \n"
"} \n"
"gl_FragColor = color; \n"
"} \n"
};
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
osg::ref_ptr<osg::Geometry> geom2 = new osg::Geometry();
geode->addDrawable(geom);
geode->addDrawable(geom2);
osg::ref_ptr<osg::PolygonMode> polygonMode = new osg::PolygonMode();
polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
//geode->getOrCreateStateSet()->setAttribute(polygonMode);
root->addChild(geode);
osg::Vec3Array* vt = new osg::Vec3Array;
osg::Vec3Array* vt2 = new osg::Vec3Array;
vt->push_back(osg::Vec3(0, 0, 0));
for (float i = 0; i < osg::PI * 2; i += osg::PI / 180.0)
{
float x = 30 * cos(i);
float y = 30 * sin(i);
vt->push_back(osg::Vec3(x, y, 0));
vt2->push_back(osg::Vec3(x, y, 0));
}
geom->setVertexArray(vt);
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN, 0, vt->size()));
geom->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
geom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
//取消深度测试很关键,防止转动时颜色变化
geom->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
geom2->setVertexArray(vt2);
geom2->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, vt2->size()));
geom2->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
geom2->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
//取消深度测试很关键,防止转动时颜色变化
geom2->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
osg::StateSet* stateset = geom->getOrCreateStateSet();
osg::Program * program = new osg::Program;
program->addShader(new osg::Shader(osg::Shader::VERTEX, vertSource));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragSource));
stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
return viewer.run();
}
osg shader雷达
最新推荐文章于 2024-03-01 09:53:07 发布