十,从摄像机打印立方体的一个外表面

news2024/10/6 6:49:35

从摄像机是与主摄像机保持同样的投影矩阵,所以,不用单独设置。如果把漫游器还是在(1,0,0)这个位置,各个从摄像机看向上下左右前后六个面,那么会出现什么现象呢?应该是x正轴打印出来,其他均为空。

为了避免上一节出现的宽度方向有背景,所以应该把从摄像机视口设置为正方形,比如512x512。

运行结果如下
在这里插入图片描述
代码如下:
#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);
}

};

std::vector<osg::ref_ptrosg::Image> getImageVectorBysetDomeCorrection(osgViewer::Viewer& viewer)
{
unsigned int screenWidth, screenHeight;
osg::GraphicsContext::WindowingSystemInterface * wsInterface = osg::GraphicsContext::getWindowingSystemInterface();
wsInterface->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), screenWidth, screenHeight);

std::vector < osg::ref_ptr<osg::Image>> imageVector;
imageVector.clear();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = 0;
traits->y = 0;
traits->width = screenWidth;
traits->height = screenHeight;
traits->windowDecoration = false;
traits->doubleBuffer = true;
traits->sharedContext = 0;
traits->readDISPLAY();
traits->setUndefinedScreenDetailsToDefaultScreen();

osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
if (!gc)
{
	osg::notify(osg::NOTICE) << "GraphicsWindow has not been created successfully." << std::endl;
	return imageVector;
}

int textureWidth = 512;
int textureHeight = 512;

osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT;
// front face
{

	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setName("Front face camera");
	camera->setGraphicsContext(gc.get());
	camera->setViewport(new osg::Viewport(0, 0, textureWidth, textureHeight));
	camera->setAllowEventFocus(false);
	camera->setRenderTargetImplementation(renderTargetImplementation);
	camera->setRenderOrder(osg::Camera::PRE_RENDER);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	camera->attach(osg::Camera::COLOR_BUFFER0, printImage); //关联采样贴图
	viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());
	imageVector.push_back(printImage);
}


// top face
{
	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setName("Top face camera");
	camera->setGraphicsContext(gc.get());
	camera->setViewport(new osg::Viewport(0, 0, textureWidth, textureHeight));
	camera->setAllowEventFocus(false);
	camera->setRenderTargetImplementation(renderTargetImplementation);
	camera->setRenderOrder(osg::Camera::PRE_RENDER);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	camera->attach(osg::Camera::COLOR_BUFFER0, printImage); //关联采样贴图
	viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0, 0.0, 0.0));
	imageVector.push_back(printImage);
}

// left face
{
	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setName("Left face camera");
	camera->setGraphicsContext(gc.get());
	camera->setViewport(new osg::Viewport(0, 0, textureWidth, textureHeight));
	camera->setAllowEventFocus(false);
	camera->setRenderTargetImplementation(renderTargetImplementation);
	camera->setRenderOrder(osg::Camera::PRE_RENDER);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	camera->attach(osg::Camera::COLOR_BUFFER0, printImage); //关联采样贴图
	viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0, 1.0, 0.0) * osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0, 0.0, 1.0));
	imageVector.push_back(printImage);
}

// right face
{
	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setName("Right face camera");
	camera->setGraphicsContext(gc.get());
	camera->setViewport(new osg::Viewport(0, 0, textureWidth, textureHeight));
	camera->setAllowEventFocus(false);
	camera->setRenderTargetImplementation(renderTargetImplementation);
	camera->setRenderOrder(osg::Camera::PRE_RENDER);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	camera->attach(osg::Camera::COLOR_BUFFER0, printImage); //关联采样贴图
	viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0, 1.0, 0.0) * osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0, 0.0, 1.0));
	imageVector.push_back(printImage);
}

// bottom face
{
	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setGraphicsContext(gc.get());
	camera->setName("Bottom face camera");
	camera->setViewport(new osg::Viewport(0, 0, textureWidth, textureHeight));
	camera->setAllowEventFocus(false);
	camera->setRenderTargetImplementation(renderTargetImplementation);
	camera->setRenderOrder(osg::Camera::PRE_RENDER);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	camera->attach(osg::Camera::COLOR_BUFFER0, printImage); //关联采样贴图
	viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0, 0.0, 0.0) * osg::Matrixd::rotate(osg::inDegrees(180.0f), 0.0, 0.0, 1.0));
	imageVector.push_back(printImage);
}

// back face
{
	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setName("Back face camera");
	camera->setGraphicsContext(gc.get());
	camera->setViewport(new osg::Viewport(0, 0, textureWidth, textureHeight));
	camera->setAllowEventFocus(false);
	camera->setRenderTargetImplementation(renderTargetImplementation);
	camera->setRenderOrder(osg::Camera::PRE_RENDER);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	camera->attach(osg::Camera::COLOR_BUFFER0, printImage); //关联采样贴图
	viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(180.0f), 1.0, 0.0, 0.0));

	imageVector.push_back(printImage);
}

viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 0.1, 10);



//viewer.getCamera()->setNearFarRatio(0.0001f);
return imageVector;

}

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;
osg::ref_ptr<osgGA::TrackballManipulator> manipulator = new osgGA::TrackballManipulator();
viewer.setCameraManipulator(manipulator);
osg::Vec3d newEye(1, 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();
std::vector<osg::ref_ptr<osg::Image>> imageVector = getImageVectorBysetDomeCorrection(viewer);
viewer.setSceneData(geode.get());

double fovy, aspectRatio, zNear, zFar;
bool bPrinted = false;
while (!viewer.done())
{
	viewer.frame();
	camera->getProjectionMatrixAsPerspective(fovy, aspectRatio, zNear, zFar);
	if (!bPrinted)
	{
		bPrinted = true;
		for (int i = 0; i < imageVector.size(); i++)
		{
			std::string strPrintName = "e:/" + imageVector[i]->getFileName() + ".bmp";
			osgDB::writeImageFile(*(imageVector[i]), strPrintName);
		}
	}
}
return viewer.run();

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1045093.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

DataOps课程:DataOps实施,花更少的时间发现和纠正错误 | 内附视频

《DataOps实施》课程内容包括《数据之旅第一数据运营》《精益数据运营的四个阶段》《DataOps的流程及结论》。本文汲取课程精华要点&#xff0c;如需完整版可观看视频讲解&#xff0c;关注公众号回复关键字【第五课】&#xff0c;获取课程完整版文字内容。 课程完整版&#xff…

IT项目管理十大模版(三)

一、项目组成员表 要把项目组成员的名单都罗列出来&#xff0c;形成一个有效的团队&#xff1b;成员角色和职责要写清楚&#xff0c;职责分明、各司其职&#xff1b;领导审核并签字确认。 二、项目范围说明书 此表&#xff0c;包含了6个部分&#xff0c;基本情况、项目描述…

训练聊天机器人,改善客户体验

谈及对待客户&#xff0c;最重要的一点便是尊重他们&#xff0c;并尊重他们的时间。这意味着在与客户互动过程中&#xff0c;回应需及时有用&#xff0c;而且要赢得回头客尤是如此。社会约定俗成的期望是&#xff1a;客户能够全天候随时提出问题&#xff0c;并获得近乎即时的回…

终于有人能说清火爆全网的AIGC了 | 附赠试用

AIGC全称为AI Generated Content&#xff0c;直译为人工智能生产的内容&#xff0c;认为是继PGC、UGC之后的新型内容创作方式。也是现在市场最火的”AI“概念的延伸应用。 AIGC之以这么热门&#xff0c;主要因为其上手非常简单大大降低了创作门槛&#xff0c;只需用文字描述您想…

FPGA行业应用一:LED控制器

什么是LED控制器 LED控制器已经有很多年头了&#xff0c;应该是上世纪90年代就开始有了。它的主要构成是&#xff1a; 1&#xff1a;视频信号源——如 电脑&#xff0c;机机&#xff0c;DVD&#xff0c;U盘等 2&#xff1a;视频处理器——通过 HDMI/DVI/网口接收来自视频源的…

Tensorboard中常用的函数和类

常用函数 ①tf.summary.scalar 用于汇总标量数据,共有四个参数,格式如下: tf.summary.scalar(tags,values,collections None,name None) 例如:tf.summary.scalar(test,test) 以标量的形式显示变量test的变化。该函数一般用于表示损失值、准确率的变化情况。 ②tf.summary.h…

开利网络受邀参与生态合作伙伴和合控股“数利丰”品牌营销会议

近日&#xff0c;开利网络受邀出席生态合作伙伴“数利丰”品牌营销会议&#xff0c;就“数利丰”产品的技术能力和案例沉淀进行分享。 作为“数利丰”项目的技术支持方&#xff0c;开利网络创始人付立军在分享会上表示&#xff0c;现如今&#xff0c;每个企业都至少做过一套系统…

双翼邮件群发软件怎么用?怎么做邮件营销?

如何使用双翼邮件群发软件&#xff1f;营销邮件群发系统哪个好&#xff1f; 近年来&#xff0c;随着电子邮件在商业和个人通信中的普及&#xff0c;双翼邮件群发软件已经成为了一个不可或缺的工具。蜂邮EDM将深入探讨这一强大工具的使用方法&#xff0c;以及如何充分利用其崭新…

✔ ★ 算法基础笔记(Acwing)(六)—— 贪心【java版本】

贪心 一、 区间问题1. 区间选点2. 最大不相交区间数量3. 区间分组(用 堆top 代表区间 头头)POJ3614Sunscreen(优先队列贪心) 4. 区间覆盖 二、哈夫曼树1. 合并果子 三、排序不等式1. 排队打水 四、绝对值不等式货仓选址 五、推公式耍杂技的牛 一、 区间问题 1. 区间选点 原题…

爬虫代理请求转换selenium添加带有账密的socks5代理

爬虫代理请求转换selenium添加带有账密的socks5代理。 一、安装三方库 二、使用方法 1、在cmd命令行输入&#xff1a; 2、给selenium添加代理 最近因为工作需要&#xff0c;需要selenium添加带有账密的socks5代理&#xff0c;贴出一个可用的方法。 把带有账密的socks5代理&am…

Xshell安装使用教程~

简介 Xshell 是一个强大的安全终端模拟软件&#xff0c;它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议。Xshell 通过互联网到远程主机的安全连接以及它创新性的设计和特色帮助用户在复杂的网络环境中享受他们的工作。 Xshell可以在Windows界面下用来访问远端不…

iOS 视频压缩 mov转mp4 码率

最近还是因为IM模块的功能&#xff0c;IOS录制MOV视频发送后&#xff0c;安卓端无法播放&#xff0c;迫不得已兼容将MOV视频转为MP4发送。 其中mov视频包括4K/24FPS、4K/30FPS、4K/60FPS、720p HD/30FPS、1080p HD/30FPS、1080p HD/60FPS&#xff01; 使用AVAssetExportSessi…

14. Redisson 分布式锁

Spring Cloud 微服务系列文章&#xff0c;点击上方合集↑ 1. 开头 在单体应用中&#xff0c;我们可以用Java的synchronized或lock来使用锁&#xff0c;但在微服务的场景下&#xff0c;一个应用会部署多个实例&#xff0c;就需要保证多个实例的多个线程同时只能有一个线程来操…

破信息壁垒,亿发一站式ERP系统建设,打造五金制造信息管理平台

五金制造拥有明显的行业特征&#xff0c;如体量小、品种繁多、颜色多样、加工工艺不断演进等&#xff0c;呈现出一种独特的管理挑战。大多数五金企业仍然依赖人工管理和经验决策&#xff0c;如今需要寻求更合理和科学的决策方法&#xff0c;以实现生产、销售、仓储、采购和财务…

无人机如何做到自动巡检?关键技术步骤分析

无人机应用在电网、水利、交通、城管等巡逻巡检领域带来了巡视效率的提升。同时飞手操作的难度和门槛、野外环境的影响、巡检结果处理难度大等带来一系列的巡检问题&#xff0c;自动化的无人机巡检则能很好的解决这些问题&#xff0c;比如我们比较熟知的自动机场&#xff0c;它…

【DETR】

https://tianfeng.space/ 前言 论文 代码 DETR&#xff08;Data-efficient Image Transformer&#xff09;是一种用于目标检测任务的深度学习模型。它与传统的目标检测方法不同&#xff0c;采用了Transformer架构&#xff0c;将目标检测问题转化为一个序列到序列的问题。以下…

【广州华锐互动】VR消防队灭火实训:让消防安全教育变得更生动有趣!

VR消防队灭火实训是一种基于虚拟现实技术的消防培训及模拟&#xff0c;学习如何在火灾中保护自己的自救和逃生方法、技能。这种平台可以让市民在虚拟环境中进行火灾逃生训练&#xff0c;提高人的消防意识和自救能力。 传统的消防培训方式通常是通过理论讲解和现场演示来进行&am…

《论文阅读27》SuperGlue: Learning Feature Matching with Graph Neural Networks

一、论文 研究领域&#xff1a; 图像特征点匹配论文&#xff1a;SuperGlue: Learning Feature Matching with Graph Neural NetworksCVPR 2020veido论文code 二、论文简述 [参考] [参考] [参考] 三、论文详述 SuperGlue&#xff1a;使用图神经网络学习特征匹配 本文介绍了…

【AI视野·今日Sound 声学论文速览 第十一期】Mon, 25 Sep 2023

AI视野今日CS.Sound 声学论文速览 Mon, 25 Sep 2023 Totally 1 papers &#x1f449;上期速览✈更多精彩请移步主页 Daily Sound Papers Deepfake audio as a data augmentation technique for training automatic speech to text transcription models Authors Alexandre R. …

Hashable/哈希协议, Arrays/数组 的使用

1. Hashable 模型实现哈希协议 1.1 实现 /// Identifiable struct MyCustomModel: Hashable{//let id UUID().uuidStringlet title: Stringfunc hash(into hasher: inout Hasher) {hasher.combine(title)} }/// 哈希协议: 唯一标识值 struct HashableBootcamp: View {// 每个…