render cubemaps in OGL coordinates

0.6.3
Miloslav Číž 7 years ago
parent 1c3d45f641
commit 226fb9c26b

@ -829,13 +829,20 @@ namespace MWRender
SphericalScreenshot s(cubeSize); SphericalScreenshot s(cubeSize);
osg::Vec3 directions[6] = { osg::Vec3 directions[6] = {
osg::Vec3(0,0,-1), osg::Vec3(0,0,1),
osg::Vec3(-1,0,0), osg::Vec3(0,0,-1),
osg::Vec3(0,0,1), osg::Vec3(-1,0,0),
osg::Vec3(1,0,0), osg::Vec3(1,0,0),
osg::Vec3(0,1,0), osg::Vec3(0,1,0),
osg::Vec3(0,-1,0), osg::Vec3(0,-1,0)};
};
double rotations[] = {
-osg::PI / 2.0,
osg::PI / 2.0,
osg::PI,
0,
osg::PI / 2.0,
osg::PI / 2.0};
double fovBackup = mFieldOfView; double fovBackup = mFieldOfView;
mFieldOfView = 90.0; // each cubemap side sees 90 degrees mFieldOfView = 90.0; // each cubemap side sees 90 degrees
@ -847,51 +854,55 @@ namespace MWRender
for (int i = 0; i < 6; i++) // for each cubemap side for (int i = 0; i < 6; i++) // for each cubemap side
{ {
osg::Matrixd transform = osg::Matrixd::rotate(osg::Vec3(0,0,-1),directions[i]) * osg::Matrixd::rotate(rotations[i],osg::Vec3(0,0,-1));
osg::Image *sideImage = s.getImage(i); osg::Image *sideImage = s.getImage(i);
screenshot(sideImage,cubeSize,cubeSize,directions[i]); screenshot(sideImage,cubeSize,cubeSize,transform);
sideImage->flipHorizontal();
} }
s.create(image,screenshotW,screenshotMapping != SphericalScreenshot::MAPPING_SMALL_PLANET ? screenshotH : screenshotW,screenshotMapping); // s.create(image,screenshotW,screenshotMapping != SphericalScreenshot::MAPPING_SMALL_PLANET ? screenshotH : screenshotW,screenshotMapping);
mPlayerAnimation->getObjectRoot()->setNodeMask(maskBackup); mPlayerAnimation->getObjectRoot()->setNodeMask(maskBackup);
mFieldOfView = fovBackup; mFieldOfView = fovBackup;
osg::ref_ptr<osg::TextureCubeMap> cubeTexture (new osg::TextureCubeMap);
for (int i = 0; i < 6; ++i)
cubeTexture->setImage(i,s.getImage(i));
osg::ref_ptr<osg::Camera> screenshotCamera (new osg::Camera); osg::ref_ptr<osg::TextureCubeMap> cubeTexture (new osg::TextureCubeMap);
osg::ref_ptr<osg::ShapeDrawable> quad (new osg::ShapeDrawable(new osg::Box(osg::Vec3(0,0,0),2.0)));
std::map<std::string, std::string> defineMap; for (int i = 0; i < 6; ++i)
cubeTexture->setImage(i,s.getImage(i));
Shader::ShaderManager& shaderMgr = mResourceSystem->getSceneManager()->getShaderManager(); osg::ref_ptr<osg::Camera> screenshotCamera (new osg::Camera);
osg::ref_ptr<osg::Shader> fragmentShader (shaderMgr.getShader("s360_fragment.glsl",defineMap,osg::Shader::FRAGMENT)); osg::ref_ptr<osg::ShapeDrawable> quad (new osg::ShapeDrawable(new osg::Box(osg::Vec3(0,0,0),2.0)));
osg::ref_ptr<osg::Shader> vertexShader (shaderMgr.getShader("s360_vertex.glsl", defineMap, osg::Shader::VERTEX));
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
osg::ref_ptr<osg::Program> program (new osg::Program); std::map<std::string, std::string> defineMap;
program->addShader(fragmentShader);
program->addShader(vertexShader);
stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
stateset->addUniform(new osg::Uniform("cubeMap",0)); Shader::ShaderManager& shaderMgr = mResourceSystem->getSceneManager()->getShaderManager();
stateset->setTextureAttributeAndModes(0,cubeTexture,osg::StateAttribute::ON); osg::ref_ptr<osg::Shader> fragmentShader (shaderMgr.getShader("s360_fragment.glsl",defineMap,osg::Shader::FRAGMENT));
osg::ref_ptr<osg::Shader> vertexShader (shaderMgr.getShader("s360_vertex.glsl", defineMap, osg::Shader::VERTEX));
quad->setStateSet(stateset); osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
quad->setUpdateCallback(NULL);
osg::ref_ptr<osg::Program> program (new osg::Program);
program->addShader(fragmentShader);
program->addShader(vertexShader);
stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
stateset->addUniform(new osg::Uniform("cubeMap",0));
stateset->setTextureAttributeAndModes(0,cubeTexture,osg::StateAttribute::ON);
quad->setStateSet(stateset);
quad->setUpdateCallback(NULL);
screenshotCamera->addChild(quad); screenshotCamera->addChild(quad);
mRootNode->addChild(screenshotCamera); mRootNode->addChild(screenshotCamera);
renderCameraToImage(screenshotCamera,image,1000,640); renderCameraToImage(screenshotCamera,image,1000,640);
screenshotCamera->removeChildren(0,screenshotCamera->getNumChildren()); screenshotCamera->removeChildren(0,screenshotCamera->getNumChildren());
mRootNode->removeChild(screenshotCamera); mRootNode->removeChild(screenshotCamera);
return true; return true;
@ -936,20 +947,16 @@ mRootNode->removeChild(screenshotCamera);
mViewer->advance(mViewer->getFrameStamp()->getSimulationTime()); mViewer->advance(mViewer->getFrameStamp()->getSimulationTime());
} }
void RenderingManager::screenshot(osg::Image *image, int w, int h, osg::Vec3 direction) void RenderingManager::screenshot(osg::Image *image, int w, int h, osg::Matrixd cameraTransform)
{ {
osg::ref_ptr<osg::Camera> rttCamera (new osg::Camera); osg::ref_ptr<osg::Camera> rttCamera (new osg::Camera);
rttCamera->setProjectionMatrixAsPerspective(mFieldOfView, w/float(h), mNearClip, mViewDistance);
rttCamera->setViewMatrix(mViewer->getCamera()->getViewMatrix() * osg::Matrixd::rotate(osg::Vec3(0,0,-1),direction));
rttCamera->setNodeMask(Mask_RenderToTexture); rttCamera->setNodeMask(Mask_RenderToTexture);
rttCamera->attach(osg::Camera::COLOR_BUFFER, image); rttCamera->attach(osg::Camera::COLOR_BUFFER, image);
rttCamera->setRenderOrder(osg::Camera::PRE_RENDER); rttCamera->setRenderOrder(osg::Camera::PRE_RENDER);
rttCamera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); rttCamera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
rttCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT, osg::Camera::PIXEL_BUFFER_RTT); rttCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT, osg::Camera::PIXEL_BUFFER_RTT);
rttCamera->setProjectionMatrixAsPerspective(mFieldOfView, w/float(h), mNearClip, mViewDistance); rttCamera->setProjectionMatrixAsPerspective(mFieldOfView, w/float(h), mNearClip, mViewDistance);
rttCamera->setViewMatrix(mViewer->getCamera()->getViewMatrix() * osg::Matrixd::rotate(osg::Vec3(0,0,-1),direction)); rttCamera->setViewMatrix(mViewer->getCamera()->getViewMatrix() * cameraTransform);
rttCamera->setViewport(0, 0, w, h); rttCamera->setViewport(0, 0, w, h);

@ -126,7 +126,7 @@ namespace MWRender
void setWaterHeight(float level); void setWaterHeight(float level);
/// Take a screenshot of w*h onto the given image, not including the GUI. /// Take a screenshot of w*h onto the given image, not including the GUI.
void screenshot(osg::Image* image, int w, int h, osg::Vec3 direction=osg::Vec3(0,0,-1)); void screenshot(osg::Image* image, int w, int h, osg::Matrixd cameraTransform=osg::Matrixd());
bool screenshot360(osg::Image* image, std::string settingStr); bool screenshot360(osg::Image* image, std::string settingStr);
struct RayResult struct RayResult

@ -3,7 +3,19 @@
varying vec2 uv; varying vec2 uv;
uniform samplerCube cubeMap; uniform samplerCube cubeMap;
#define PI 3.1415926535
vec3 cylindricalCoords(vec2 coords)
{
return normalize(vec3(cos(-1 * coords.x * 2 * PI),sin(-1 * coords.x * 2 * PI),coords.y * 2.0 - 1.0));
}
void main(void) void main(void)
{ {
gl_FragData[0] = textureCube(cubeMap,vec3(uv.x * 2.0 - 1.0,uv.y * 2.0 - 1.0,1)); vec3 c;
c.x = uv.x * 2.0 - 1.0;
c.y = uv.y * 2.0 - 1.0;
c.z = 1.0;
gl_FragData[0] = textureCube(cubeMap,vec3(cylindricalCoords(uv)));
} }

@ -5,5 +5,5 @@ varying vec2 uv;
void main(void) void main(void)
{ {
gl_Position = gl_Vertex; gl_Position = gl_Vertex;
uv = (gl_Vertex.xy + vec2(1.0)) / 2.0; uv = (gl_Vertex.xy * vec2(1.0,-1.0) + vec2(1.0)) / 2.0;
} }

Loading…
Cancel
Save