从摄像机是与主摄像机保持同样的投影矩阵,所以,不用单独设置。如果把漫游器还是在(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();
}