Shader实现雷达扫描效果

1 篇文章 0 订阅

使用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 float num; \n"
	"uniform float height; \n"
	"varying in vec3 pos;\n"
	"float Alpha = 1.0; \n"
	"float f = pos.z;\n"
	"uniform float osg_FrameTime;\n"
	"void main()\n"
	"{\n"
	"if (sin(f/height*3.14*2*num+ osg_FrameTime*10) > 0)\n"
	"{\n"
	"	Alpha = 0.5;\n"
	"}\n"
	"else\n"
	"{\n"
	"	Alpha = 0;\n"
	"}\n"
	"	gl_FragColor = vec4(1,0,1,Alpha);\n"
	"}\n "
};

#include <math.h>
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);
	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;

	vt->push_back(osg::Vec3(0, 0, 100));
	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));
	}
	geom->setVertexArray(vt);
	geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN, 0, vt->size()));

	//直接用Shader上色 此处可以注释
	//osg::Vec4Array* colors = new osg::Vec4Array;
	//colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)); //index 0 red
	//geom->setColorArray(colors);
	//geom->setColorBinding(osg::Geometry::BIND_OVERALL);

	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);

	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));

	osg::ref_ptr<osg::Uniform> uniform_num = new osg::Uniform("num", float(5.0));  //设置为5层
	osg::ref_ptr<osg::Uniform> uniform_height = new osg::Uniform("height", float(100.0));  //设置最大高度


	
	stateset->addUniform(uniform_num.get()); 
	stateset->addUniform(uniform_height.get());

	stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
	stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

	osgViewer::Viewer viewer;
	viewer.setSceneData(root.get());
	return viewer.run();
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个使用 GLSL 着色器实现扇形雷达扫描效果的示例代码。 首先是顶点着色器代码: ``` #version 330 uniform float angle; in vec3 position; out float scanPos; void main() { gl_Position = vec4(position, 1.0); scanPos = degrees(atan(position.y, position.x)); if(scanPos < 0.0) { scanPos += 360.0; } scanPos = mod(scanPos - angle, 360.0); } ``` 这个顶点着色器使用一个 uniform 变量 angle 表示当前扫描的角度,使用 in 变量 position 表示顶点的位置。在主函数中,先计算出当前顶点对应的角度 scanPos,然后根据 angle 和 scanPos 计算出该点在扇形雷达扫描中的位置。 接下来是片段着色器代码: ``` #version 330 uniform vec4 scanColor; uniform vec4 bgColor; in float scanPos; out vec4 fragColor; void main() { if(scanPos < 90.0 || scanPos > 270.0) { fragColor = scanColor; } else { fragColor = bgColor; } } ``` 这个片段着色器使用两个 uniform 变量 scanColor 和 bgColor 分别表示雷达波形颜色和背景颜色。使用 in 变量 scanPos 表示当前点在扇形雷达扫描中的位置。在主函数中,根据 scanPos 判断当前点是否在雷达扫描范围内,如果在范围内,则使用雷达波形颜色,否则使用背景颜色。 最后,在主程序中使用以下代码来设置 uniform 变量和绘制场景: ``` // 设置扫描角度 glUniform1f(glGetUniformLocation(shaderProgram, "angle"), angle); // 设置雷达波形颜色 glUniform4f(glGetUniformLocation(shaderProgram, "scanColor"), scanColor.r, scanColor.g, scanColor.b, scanColor.a); // 设置背景颜色 glUniform4f(glGetUniformLocation(shaderProgram, "bgColor"), bgColor.r, bgColor.g, bgColor.b, bgColor.a); // 绘制场景 glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount); ``` 其中,shaderProgram 是编译好的着色器程序,angle 是扫描角度,scanColor 是雷达波形颜色,bgColor 是背景颜色,vertexCount 是场景顶点数量。 以上就是一个简单的使用 GLSL 着色器实现扇形雷达扫描效果的示例代码,希望对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值