八,打印功能

文章描述了如何使用Osg库对图像进行离线处理,并通过osg::Image和OpenGL进行帧内打印操作,同时展示了相关的Shader编程和相机设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于要把辐照度文件进行离线处理,这一步有现成的工具可以使用,但是我想用Osg推导一下,所以,先看看如何打印。
打印的是osg::Image,而osg::Image是与摄像机关联的。
根据osg最长的一帧,要经过事件回调,更新回调,绘制等几个过程,所以,在每帧结束之后进行
也就是
osg::ref_ptrosg::Image printImage = new osg::Image;
printImage->allocateImage(screenWidth, screenHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
camera->attach(osg::Camera::COLOR_BUFFER0, printImage); //关联采样贴图
int printCount = 0;
while (!viewer.done())
{
viewer.frame();

	if (printCount == 0)
	{
		printCount = 1;
		osgDB::writeImageFile(*printImage, "d:/posX.bmp");
	}
}
这里把漫游器设置如下:
osg::Vec3d newEye(3, 0, 0);
osg::Vec3 newCenter(0, 0, 0);
osg::Vec3 newUp(0, 1, 0);
manipulator->setHomePosition(newEye, newCenter, newUp);
果然打印出来了,

在这里插入图片描述
代码如下:
#include <osg/TextureCubeMap>
#include <osg/TexGen>
#include <osg/TexEnvCombine>
#include <osgUtil/ReflectionMapGenerator>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/NodeVisitor>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>
#include <osgGA/TrackballManipulator>
#include <osgDB/WriteFile>
static const char * vertexShader =
{
“in vec3 aPos;\n”
“varying vec3 outPos;”
“void main(void)\n”
“{\n”
“outPos = aPos;\n”
" gl_Position = ftransform();\n"
“}\n”
};

static const char *psShader =
{
“varying vec3 outPos;”
“uniform sampler2D tex0;”
"const vec2 invAtan = vec2(0.1591, 0.3183); "
"vec2 SampleSphericalMap(vec3 v) "
"{ "
" vec2 uv = vec2(atan(v.z, v.x), asin(v.y)); "
" uv *= invAtan; "
" uv += 0.5; "
" return uv; "
"} "

"void main()"
"{"
"vec2 uv = SampleSphericalMap(normalize(outPos)); "
"vec3 color = texture(tex0, uv).rgb;"
"gl_FragColor = vec4(color,1.0);\n"
"}\n"

};
class MyNodeVisitor : public osg::NodeVisitor
{
public:
MyNodeVisitor() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{

}
void apply(osg::Geode& geode)
{
	int count = geode.getNumDrawables();
	for (int i = 0; i < count; i++)
	{
		osg::ref_ptr<osg::Geometry> geometry = geode.getDrawable(i)->asGeometry();
		if (!geometry.valid())
		{
			continue;
		}
		osg::Array* vertexArray = geometry->getVertexArray();
		geometry->setVertexAttribArray(1, vertexArray);

	}
	traverse(geode);
}

};

int main()
{
std::string strHDRImageName = “D:/tutorial/LearnOpenGL-master/LearnOpenGL-master/resources/textures/hdr/newport_loft.hdr”;
osg::ref_ptrosg::Image image = osgDB::readImageFile(strHDRImageName);

int imageWidth = image->s();
int imageHeight = image->t();
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setImage(image.get());
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, data);
//
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//texture->

osg::ref_ptr<osg::Box> box = new osg::Box(osg::Vec3(0, 0, 0), 1);
osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(box);
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(drawable);
MyNodeVisitor nv;
geode->accept(nv);
osg::ref_ptr<osg::StateSet> stateset = geode->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);

//shader

osg::ref_ptr<osg::Shader> vs1 = new osg::Shader(osg::Shader::VERTEX, vertexShader);
osg::ref_ptr<osg::Shader> ps1 = new osg::Shader(osg::Shader::FRAGMENT, psShader);
osg::ref_ptr<osg::Program> program1 = new osg::Program;
program1->addShader(vs1);
program1->addShader(ps1);
program1->addBindAttribLocation("aPos", 1);

osg::ref_ptr<osg::Uniform> tex0Uniform = new osg::Uniform("tex0", 0);
stateset->addUniform(tex0Uniform);
stateset->setAttribute(program1, osg::StateAttribute::ON);

osgViewer::Viewer viewer;
viewer.setSceneData(geode.get());
osg::ref_ptr<osgGA::TrackballManipulator> manipulator = new osgGA::TrackballManipulator();
viewer.setCameraManipulator(manipulator);
//osg::Vec3d eye, center, up;
//manipulator->getHomePosition(eye, center, up);
osg::Vec3d newEye(3, 0, 0);
osg::Vec3 newCenter(0, 0, 0);
osg::Vec3 newUp(0, 1, 0);
manipulator->setHomePosition(newEye, newCenter, newUp);

osg::ref_ptr<osg::Camera> camera = viewer.getCamera();
//camera->setProjectionMatrixAsPerspective(osg::PI_2, 1.0, 0.1, 10.0);
//camera->setViewport(0, 0, 512, 512);
unsigned int screenWidth, screenHeight;
osg::GraphicsContext::WindowingSystemInterface * wsInterface = osg::GraphicsContext::getWindowingSystemInterface();
wsInterface->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), screenWidth, screenHeight);
osg::ref_ptr<osg::Image> printImage = new osg::Image;
printImage->allocateImage(screenWidth, screenHeight,  1, GL_RGBA, GL_UNSIGNED_BYTE);
camera->attach(osg::Camera::COLOR_BUFFER0, printImage); //关联采样贴图
////osgDB::writeImageFile(*printImage, "d:/1.bmp");
////theCamera->setViewMatrixAsLookAt(newEye, newCenter, newUp);
int printCount = 0;
while (!viewer.done())
{
	viewer.frame();
	
	if (printCount == 0)
	{
		printCount = 1;
		osgDB::writeImageFile(*printImage, "d:/posX.bmp");
	}
}
return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值