十一,从摄像机打印HDR环境贴图

news2024/11/17 5:24:50

越来越接近真相了。我们很自然地想到,如果把漫游器放在中心打印,是不是就可以打印整个等距柱状投影图了呢?是的,但是,只是要注意的是,立方体贴图的内部和外部尽管一样,但是还是稍微有点模糊,也可以在外部设置漫游器位置六次,打印六次,就像上节那样。但是,这里不考虑这些细节。
也就是把漫游器位置设置为
osg::Vec3d newEye(0, 0, 0);

运行结果不出所料。
在这里插入图片描述

当然,也可以把osg::Image和osg::TextureCubeMap关联起来。使用osg::TextureCubeMap打印。即

int textureWidth = 512;
int textureHeight = 512;

osg::ref_ptr<osg::TextureCubeMap> texture = new osg::TextureCubeMap;

texture->setTextureSize(textureWidth, textureHeight);
texture->setInternalFormat(GL_RGB);
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);

各个面关联,比如
camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Y);

	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	texture->setImage(0, printImage);
	camera->attach(osg::Camera::COLOR_BUFFER, printImage);

打印时

		int imageNumber = textureCubeMap->getNumImages(); 
		for (int i = 0; i < imageNumber; i++)
		{

			osg::ref_ptr<osg::Image> theImage = textureCubeMap->getImage(i); 
			std::string strPrintName = "e:/" + theImage->getFileName() + ".bmp";
			osgDB::writeImageFile(* theImage, strPrintName);
		}

完整代码如下:
#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);
}

};

osg::ref_ptrosg::TextureCubeMap getTextureCubeMap(osgViewer::Viewer& viewer)
{
unsigned int screenWidth, screenHeight;
osg::GraphicsContext::WindowingSystemInterface * wsInterface = osg::GraphicsContext::getWindowingSystemInterface();
wsInterface->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), screenWidth, screenHeight);

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 NULL;
}

int textureWidth = 512;
int textureHeight = 512;

osg::ref_ptr<osg::TextureCubeMap> texture = new osg::TextureCubeMap;

texture->setTextureSize(textureWidth, textureHeight);
texture->setInternalFormat(GL_RGB);
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);

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);
	//关联采样贴图
	camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Y);

	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	texture->setImage(0, printImage);
	camera->attach(osg::Camera::COLOR_BUFFER, printImage);
	viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());
}


// 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);
	//关联采样贴图
	camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Z);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	texture->setImage(1, printImage);
	camera->attach(osg::Camera::COLOR_BUFFER, printImage);
	viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0, 0.0, 0.0));
}

// 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); 
	//关联采样贴图
	camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	texture->setImage(2, printImage);
	camera->attach(osg::Camera::COLOR_BUFFER, 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));
}

// 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);
	//关联采样贴图
	camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_X);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	texture->setImage(3, printImage);
	camera->attach(osg::Camera::COLOR_BUFFER, 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));
	
}

// 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);
	//关联采样贴图
	camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Z);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	texture->setImage(4, printImage);
	camera->attach(osg::Camera::COLOR_BUFFER, 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));

}

// 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);
	//关联采样贴图
	camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Y);
	osg::ref_ptr<osg::Image> printImage = new osg::Image;
	printImage->setFileName(camera->getName());
	printImage->allocateImage(textureWidth, textureHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE);
	texture->setImage(5, printImage);
	camera->attach(osg::Camera::COLOR_BUFFER, printImage);
	viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(180.0f), 1.0, 0.0, 0.0));

}

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



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

}

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(0, 0, 0);
osg::Vec3 newCenter(0, 0, 0);
osg::Vec3 newUp(0, 1, 0);
manipulator->setHomePosition(newEye, newCenter, newUp);
osg::ref_ptr<osg::TextureCubeMap> textureCubeMap = getTextureCubeMap(viewer);
viewer.setSceneData(geode.get());

bool bPrinted = false;
while (!viewer.done())
{
	viewer.frame();
	if (!bPrinted)
	{
		bPrinted = true;
		int imageNumber = textureCubeMap->getNumImages(); 
		for (int i = 0; i < imageNumber; i++)
		{

			osg::ref_ptr<osg::Image> theImage = textureCubeMap->getImage(i); 
			std::string strPrintName = "e:/" + theImage->getFileName() + ".bmp";
			osgDB::writeImageFile(* theImage, strPrintName);
		}
	}
}
return 0;

}

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

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

相关文章

Git 学习(2)

Git 学习&#xff08;2&#xff09; 版本号 Git 中文件的版本号是 40 位十六进制的数字字符串&#xff0c;采用 SHA-1 加密算法计算获得 这样一方面可避免在合并时的冲突问题 另一方面可以用于文件定位&#xff0c;其中前两位表示文件夹&#xff0c;后 38 位表示文件 指令介…

DDS信号发生器Verilog波形发生器FPGA

名称&#xff1a;DDS信号发生器Verilog波形发生器 软件&#xff1a;Quartus 语言&#xff1a;Verilog 要求&#xff1a; 1.可产生正弦波&#xff0c;锯齿波&#xff0c;三角波&#xff0c;方波4种波形&#xff0c;频率可调 2.具有波形选择、起动、停止功能。 代码下载&…

Flink on yarn 实战和源码分析

版本&#xff1a;1.13.6 目录 Flink on yarn 的3种模式的使用 yarn session 模式源码分析 yarn per-job模式源码分析 application模式源码分析 Flink on yarn 的3种模式的使用 Application Mode # ./bin/flink run-application -t yarn-application ./examples/streaming…

Apollo简易地图制作

在Apollo中模拟障碍物 一、准备工作 在模拟障碍物之前&#xff0c;需要下载并编译Apollo源码&#xff0c;过程可以依据Apollo开放平台文档&#xff0c;其中可能遇到的问题在这里或许可以寻找到答案 二、运行Dreamview 进入容器 cd ~/apollobash docker/scripts/dev_start.s…

515万新作者投身电商事业,抖音电商将投入更多资源扶持作者长期发展

9月27日&#xff0c;2023抖音电商作者峰会在上海举办。上千位抖音电商作者、MCN机构、精选联盟服务商、商家等重要生态伙伴参会&#xff0c;围绕大会主题“向新成长”进行了深入探讨。会上&#xff0c;抖音电商总裁魏雯雯提到&#xff0c;电商作者的事业有更多发展方向。为助力…

输送机使用的常见误区

输送机也称流水线&#xff0c;是指在自动化生产过程中起到运输货物&#xff0c;联通各个生产设备的主要机械设备。但在使用的过程中&#xff0c;很多用户对于输送机的使用存在一定的误区&#xff0c;导致设备故障频出&#xff0c;下面就针对用户已在使用输送机过程中的常见误区…

以太网中的介质共享访问控制机制

什么是CSMA/CD CSMA/CD&#xff08;Carrier Sense Multiple Access with Collision Detection&#xff09;是一种用于以太网等共享介质的访问控制机制。它用于协调多个设备共享同一物理介质&#xff08;例如同一局域网&#xff09;上的传输权利&#xff0c;以避免碰撞并提供公…

mac安装python2

Python 2 于 2020 年 1 月 1 日宣布结束支持&#xff0c;包括 Homebrew 在内的许多项目和包管理器已经停止支持 Python 2。 如果现在你还要安装 Python 2&#xff0c;需要从 Python 官网下载安装包&#xff1a; 访问 Python 的发布页面。从页面底部找到 Python 2 的最后一个版…

tp8 Editor.md

Editor.md - 开源在线 Markdown 编辑器 放于public文件夹下 html代码&#xff1a; <div class"layui-col-md12" id"content"><textarea name"content" placeholder"详情" class"layui-textarea">{notempty nam…

【Unity的HDRP渲染管线搭建配置VR交互场景_SteamVR 插件和Pico串流助手_经验分享】

HDRP渲染管线配置VR交互场景 Unity创建场景和相关配置下载导入项目打开PICO串流助手在Pico中的配置:用Steam串流VR_这篇的前置补充 Unity创建场景和相关配置 带HDRP Sample Scene 示例的 下载 SteamVR Unity插件地址02 导入项目

GEO生信数据挖掘(二)下载基因芯片平台文件及注释

检索到目标数据集后&#xff0c;开始数据挖掘&#xff0c;本文以阿尔兹海默症数据集GSE1297为例 目录 下载平台文件 1.AnnotGPL参数改为TRUE,联网下载芯片平台的soft文件。&#xff08;国内网速奇慢经常中断&#xff09; 2.手工去GEO官网下载 转换芯片探针ID为gene name 拓…

ADS-B及雷达显示终端8.3

新版本功能升级主要有如下: 1、地图更新 在上一版本8.2中使用的高程地图为由SRTM经过地形晕渲后&#xff0c;生成地形图片&#xff0c;然后对图片进行贴图&#xff0c;一一按规定位置、大小将地形图贴至底图上&#xff0c;而后在底图上进行二维矢量地图的绘制&#xff0c;包括…

uniapp app 导出excel 表格

直接复制运行 <template><view><button click"tableToExcel">导出一个表来看</button><view>{{ successTip }}</view></view> </template><script>export default {data() {return {successTip: }},metho…

【面试高高手】—— SpringBoot(11题)

文章目录 1.什么是SpringBoot?2.为什么需要Spring Boot&#xff1f;3.SpringBoot的特征&#xff1f;4.SpringBoot的两个策略是什么&#xff1f;5.说一下SpringBoot的自动装配流程&#xff1f;6.说下什么是 Bean?7.什么是 CSRF 攻击&#xff1f;如何避免&#xff1f;8. Spring…

python ToastNotifier TypeError got Nonetype

这个错误没什么影响&#xff0c;只是在通知结束后会抛出 如果你实在不爽&#xff0c;办法如下&#xff1a; 找到"<你的python安装路径>\Lib\site-packages\win10toast"&#xff0c;里面应该有__main__.py和__init__.py两个文件&#xff0c;打开__init__.py 找到…

人工智能(AI)在产生新创意方面有多出色?

传统智慧一直不太擅长此道。发现新的创业机会、为未满足的需求提供解决方案&#xff0c;以及为新公司命名都是非结构化的任务&#xff0c;似乎不适合由算法来完成。然而&#xff0c;人工智能的最新进展——特别是像ChatGPT这样的大语言模型的出现——正在挑战这种假定。 我们教…

【ES6知识】Promise 对象

文章目录 1.1 概述1.2 静态方法1.3 实例方法1.4 Promise 拒绝事件 1.1 概述 Promise 对象用于表示一个异步操作的最终完成&#xff08;或失败&#xff09;及其结果值。是异步编程的一种解决方案&#xff08;可以解决回调地狱问题&#xff09;。 一个 Promise 对象代表一个在这…

Python实用技术——爬虫(二):爬虫需要使用的库

一&#xff0c;Requests库 1&#xff0c;主要使用方法&#xff1a; 1&#xff09;get&#xff08;&#xff09;方法&#xff1a; 这个Response对象中包含爬虫返回的内容。 除了request方法是基础方法外&#xff0c;其他都是通过调用request方法来实现的。 所以&#xff0c;我…

[React] React高阶组件(HOC)

文章目录 1.Hoc介绍2.几种包装强化组件的方式2.1 mixin模式2.2 extends继承模式2.3 HOC模式2.4 自定义hooks模式 3.高阶组件产生初衷4.高阶组件使用和编写结构4.1 装饰器模式和函数包裹模式4.2 嵌套HOC 5.两种不同的高阶组件5.1 正向的属性代理5.2 反向的继承 6.如何编写高阶组…

使用 Express 设置 GraphQL

使用 Express 设置 GraphQL 在本文中&#xff0c;我们将探讨如何在 Node.js 中设置 Express.js 和 GraphQL。另外&#xff0c;本文还将分享一些基本技巧&#xff0c;以确保我们的服务器已准备好投入实际使用&#xff01; 什么是 GraphQL GraphQL 是 API 的查询语言&#xff…