感谢
感谢网友提出这个问题。大家有问题也可以在评论区提出,有问必有答。
功能描述
网友问怎么做出来一个假红外,其实只需要一个RTT就可以了,但是网友表示用了也不行,估计是有BUG。为了模拟网友的环境,重现问题,这个是用osg341+osgEarth28,以及CompositeViewer 来做的,其它版本的osg+oe或者只使用osg和使用osgViewer应该也是可以的。就不再多进行测试了。
本节代码在网盘中:
注意:务必使用浏览器打开:
链接:https://pan.baidu.com/s/13gwJLwo_LbRnN3Bl2NXXXw
提取码:xrf5
以下是原色和转换的结果
以上是原色
以下是转换结果
关键代码
上网一查,彩色转黑白算法竟然有一大摞。灰色的图片其rgb值是一样的,比如(0.5, 0.5, 0.5)就是一张灰度图。因此由彩色转黑白关键就是由彩色的rgb算出灰度gray,然后最终的颜色就是(gray, gray , gray),RGB转gray的算法如下如下:
/*
浮点算法:Gray=R0.3+G0.59+B0.11
整数方法:Gray=(R30+G59+B11)/100
移位方法:Gray=(R76+G151+B*28)>>8;
平均值法:Gray=(R+G+B)/3;
仅取绿色:Gray=G;
*/
上面是网上查的,下面是代码里实际用的
"vec3 W = vec3(0.2125, 0.7154, 0.0721);\n"
"vec4 mask = texture2D(rtt, vTexCoord);\n"
"float luminance = dot(mask.rgb, W);\n"
其实也就相当于Gray=R0.2125+G0.7154+B*0.0721,如果效果不满意可以自己再查查相关算法。
框架
1.我们先使用了一个RTT相机,把渲染的纹理取出来,这就是正常的纹理。
view->getCamera()->attach(osg::Camera::COLOR_BUFFER, t2d);
上面就给一个相机关联了一个纹理,它的渲染结果会存到这张纹理上。
2.接着我们又创建了一个相机,这个相机会最后渲染,覆盖整个屏幕,在这个相机上画一个和屏幕一样大的方块,贴上这个纹理就可以了,如下代码geom就是这个方块。
geom->getOrCreateStateSet()->setTextureAttributeAndModes(0, t2d, osg::StateAttribute::ON);
3.最后对这个相机应用shader,在片元shader中做如上算法的处理。
"#version 120\n"
"uniform sampler2D rtt;\n"
"varying vec2 vTexCoord;\n"
"void main(void)\n"
"{\n"
"vec3 W = vec3(0.2125, 0.7154, 0.0721);\n"
"vec4 mask = texture2D(rtt, vTexCoord);\n"
"float luminance = dot(mask.rgb, W);\n"
"gl_FragColor = vec4(vec3(luminance), 1.0);\n"
"}\n"
以下是全部代码
可以不用osgEarth和CompositeViewer ,用oe只是为了重现客户问题,思想都是一样的,有兴趣的快试试吧。如果修改不定,又需要纯osg的,可以在评论区留言。我再给大家改一个。
#include
#include
#include
#include
#include
#include
using namespace osgEarth;
using namespace osgEarth::Util;
osg::ref_ptr t2d = new osg::Texture2D;
int screenWidth = 1280;
int screenHeight = 1024;
static const char* vs = {
"#version 120\n"
"varying vec2 vTexCoord;\n"
"void main(void)\n"
"{\n"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
" vTexCoord = gl_MultiTexCoord0.xy;\n"
"}\n"
};
/*
1.浮点算法:Gray=R*0.3+G*0.59+B*0.11
2.整数方法:Gray=(R*30+G*59+B*11)/100
3.移位方法:Gray=(R*76+G*151+B*28)>>8;
4.平均值法:Gray=(R+G+B)/3;
5.仅取绿色:Gray=G;
*/
static const char* fs = {
"#version 120\n"
"uniform sampler2D rtt;\n"
"varying vec2 vTexCoord;\n"
"void main(void)\n"
"{\n"
"vec3 W = vec3(0.2125, 0.7154, 0.0721);\n"
"vec4 mask = texture2D(rtt, vTexCoord);\n"
"float luminance = dot(mask.rgb, W);\n"
"gl_FragColor = vec4(vec3(luminance), 1.0);\n"
"}\n"
};
osg::Camera* createHUD()
{
osg::Camera* camera = new osg::Camera;
camera->setProjectionMatrix(osg::Matrix::ortho2D(0, screenWidth, 0, screenHeight));
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrix::identity());
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->setRenderOrder(osg::Camera::POST_RENDER);
camera->setAllowEventFocus(false);
osg::Geode* geode = new osg::Geode();
camera->addChild(geode);
osg::Geometry* geom = new osg::Geometry;
geode->addDrawable(geom);
osg::Vec3Array* vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(0, 0, -0.1));
vertices->push_back(osg::Vec3(screenWidth, 0, -0.1));
vertices->push_back(osg::Vec3(screenWidth, screenHeight, -0.1));
vertices->push_back(osg::Vec3(0, screenHeight, -0.1));
geom->setVertexArray(vertices);
osg::Vec2Array* coords = new osg::Vec2Array;
coords->push_back(osg::Vec2(0.0, 0.0));
coords->push_back(osg::Vec2(1.0, 0.0));
coords->push_back(osg::Vec2(1.0, 1.0));
coords->push_back(osg::Vec2(0.0, 1.0));
geom->setTexCoordArray(0, coords);
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f, 1.0, 0.8f, 0.2f));
geom->setColorArray(colors, osg::Array::BIND_OVERALL);
geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
geom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF || osg::StateAttribute::PROTECTED);
geom->getOrCreateStateSet()->setTextureAttributeAndModes(0, t2d, osg::StateAttribute::ON);
//写个简单shader来对图像变一下色
osg::Program* program = new osg::Program;
program->addShader(new osg::Shader(osg::Shader::VERTEX, vs));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fs));
geom->getOrCreateStateSet()->setAttributeAndModes(program, osg::StateAttribute::ON);
geom->getOrCreateStateSet()->addUniform(new osg::Uniform("rtt", 0));
return camera;
}
int main()
{
osgViewer::CompositeViewer viewer;
osgViewer::View* view = new osgViewer::View;
view->setName("Single view");
view->setUpViewOnSingleScreen(0);
view->setSceneData(osgDB::readNodeFile("D:\\code\\imp\\osgoe\\osg341oe28\\oe28\\tests\\gdal_tiff.earth"));
view->setCameraManipulator(new EarthManipulator());
viewer.addView(view);
t2d->setInternalFormat(GL_RGBA);
view->getCamera()->attach(osg::Camera::COLOR_BUFFER, t2d);
//此时view里已经有个主相机了,加个HUD给它
osgViewer::Viewer::Windows windows;
viewer.getWindows(windows);
if (windows.empty()) return 1;
osg::Camera* hudCamera = createHUD();
hudCamera->setGraphicsContext(windows[0]);
hudCamera->setViewport(0, 0, windows[0]->getTraits()->width, windows[0]->getTraits()->height);
view->addSlave(hudCamera, false);
return viewer.run();
}