osg传递数组到shader,(以qedl为例)

一,对float类型的数组
osg::ref_ptrosg::FloatArray m_dampingPixelDist;
赋值

void ccBilateralFilter::updateDampingTable()
{
m_dampingPixelDist->clear();
m_dampingPixelDist->resize(64, 1.0f);(设定数组有64个浮点值,默认为1.0)
//constant quotient
float q = m_halfSpatialSize * m_spatialSigma;
q = 2 * (q*q);

for (int c = 0; c <= m_halfSpatialSize; c++)
{
	for (int d = 0; d <= m_halfSpatialSize; d++)
	{
		int index = c*(m_halfSpatialSize + 1) + d;
		//pixel distance based damping
		m_dampingPixelDist->at(index) = std::exp((c*c + d*d) / (-q));
	}
}

}
传递到shader

		osg::ref_ptr<osg::Uniform> distCoefsUniform = new osg::Uniform(osg::Uniform::FLOAT, "DistCoefs", m_dampingPixelDist->size());
		for (int i = 0; i < m_dampingPixelDist->size(); i++)
		{
			distCoefsUniform->setElement(i, m_dampingPixelDist->at(i));
		}

glsl中设置
uniform float DistCoefs[64];

二,同样,
对于osg::vec2的数组,传递到shader

osg::Vec2Array* m_neighbours = new osg::Vec2Array;
m_neighbours->clear();
for (int i = 0; i < 8; i++)                //8邻域
{
	float x = static_cast<float>(std::cos(i * osg::PI / 4.0));
	float y = static_cast<float>(std::sin(i * osg::PI / 4.0));
	m_neighbours->push_back(osg::Vec2(x, y));

}

		osg::ref_ptr<osg::Uniform> neighbourUniform = new osg::Uniform(osg::Uniform::FLOAT_VEC2, "Neigh_pos_2D", m_neighbours->size());
		for (int i = 0; i < m_neighbours->size(); i++)
		{
			neighbourUniform->setElement(i, m_neighbours->at(i));
		}

在GLSL中
“uniform vec2 Neigh_pos_2D[8];”

以下是使用 osgEarth 3.2 绘制云层的示例代码: ```cpp #include <osgEarth/Registry> #include <osgEarth/ImageUtils> #include <osgEarth/ShaderUtils> #include <osgEarth/Sky> #include <osgEarthUtil/Sky> #include <osgEarthUtil/Clouds> osg::Node* createCloudLayer() { // 创建一个 CloudLayer 对象 osgEarth::Util::CloudLayerOptions options; options.altitude() = 20000.0f; options.thickness() = 2000.0f; options.color() = osg::Vec3(1.0f, 1.0f, 1.0f); options.brightness() = 1.0f; options.speed() = 0.0001f; osg::ref_ptr<osgEarth::Util::CloudLayer> cloudLayer = new osgEarth::Util::CloudLayer(options); // 加载云层纹理 osg::ref_ptr<osg::Image> cloudImage = osgDB::readImageFile("path_to_cloud_texture"); osg::ref_ptr<osg::Texture2D> cloudTexture = new osg::Texture2D(cloudImage); cloudTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); cloudTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); cloudTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); cloudTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); cloudLayer->setTexture(cloudTexture); // 设置云层渲染 shader osg::ref_ptr<osg::Program> cloudShader = new osg::Program; cloudShader->addShader(osgEarth::ShaderUtils::loadShaderFile("path_to_cloud_shader.vert", osg::Shader::VERTEX)); cloudShader->addShader(osgEarth::ShaderUtils::loadShaderFile("path_to_cloud_shader.frag", osg::Shader::FRAGMENT)); osg::ref_ptr<osgEarth::StateSetCache> stateSetCache = osgEarth::Registry::instance()->getStateSetCache(); osg::StateSet* stateSet = stateSetCache->getOrCreateStateSet(cloudShader.get()); stateSet->addUniform(new osg::Uniform("oe_Sky_CloudsAltitude", options.altitude().get())); stateSet->addUniform(new osg::Uniform("oe_Sky_CloudsThickness", options.thickness().get())); stateSet->addUniform(new osg::Uniform("oe_Sky_CloudsColor", options.color().get())); stateSet->addUniform(new osg::Uniform("oe_Sky_CloudsBrightness", options.brightness().get())); stateSet->addUniform(new osg::Uniform("oe_Sky_CloudsSpeed", options.speed().get())); stateSet->addUniform(new osg::Uniform("oe_Sky_Time", 0.0f)); // 创建云层节点 osg::ref_ptr<osg::Group> cloudNode = new osg::Group; cloudNode->addChild(cloudLayer->getOrCreateStateSetGroup()); cloudNode->addChild(cloudLayer->getOrCreateGeometry()); return cloudNode.release(); } ``` 这段代码使用 `osgEarth::Util::CloudLayer` 类来创建一个云层节点,并设置云层纹理和渲染 shader。其中,`path_to_cloud_texture` 和 `path_to_cloud_shader.vert`/`path_to_cloud_shader.frag` 分别表示云层纹理和 shader 的路径。你可以根据实际情况进行修改。 最后,将云层节点添加到场景图中即可。例如: ```cpp osgViewer::Viewer viewer; viewer.setSceneData(createCloudLayer()); viewer.run(); ``` 这将创建一个带有云层的场景,并且使用时钟来不断更新 shader 中的 `oe_Sky_Time` 变量,以实现云层的运动效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值