osg shader雷达

#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();
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值