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