From 09631385c37f76821461d37e54623434fb9830f6 Mon Sep 17 00:00:00 2001 From: scrawl Date: Wed, 28 Oct 2015 19:25:46 +0100 Subject: [PATCH] Use boost ifstream for water resources --- apps/openmw/mwrender/water.cpp | 45 ++++++++++++++++++++++++++++--- files/shaders/water_fragment.glsl | 1 - 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/apps/openmw/mwrender/water.cpp b/apps/openmw/mwrender/water.cpp index b56f21754..af9fae04a 100644 --- a/apps/openmw/mwrender/water.cpp +++ b/apps/openmw/mwrender/water.cpp @@ -16,6 +16,9 @@ #include // XXX remove +#include +#include + #include #include @@ -264,6 +267,41 @@ void addDebugOverlay(osg::Texture2D* texture, int pos, osg::Group* parent) parent->addChild(debugCamera); } +osg::ref_ptr readShader (osg::Shader::Type type, const std::string& file) +{ + osg::ref_ptr shader (new osg::Shader(type)); + + // use boost in favor of osg::Shader::readShaderFile, to handle utf-8 path issues on Windows + boost::filesystem::ifstream inStream; + inStream.open(boost::filesystem::path(file)); + std::stringstream strstream; + strstream << inStream.rdbuf(); + shader->setShaderSource(strstream.str()); + return shader; +} + +osg::ref_ptr readPngImage (const std::string& file) +{ + // use boost in favor of osgDB::readImage, to handle utf-8 path issues on Windows + boost::filesystem::ifstream inStream; + inStream.open(file, std::ios_base::in | std::ios_base::binary); + if (inStream.fail()) + std::cerr << "Failed to open " << file << std::endl; + osgDB::ReaderWriter* reader = osgDB::Registry::instance()->getReaderWriterForExtension("png"); + if (!reader) + { + std::cerr << "Failed to read " << file << ", no png readerwriter found" << std::endl; + return osg::ref_ptr(); + } + osgDB::ReaderWriter::ReadResult result = reader->readImage(inStream); + if (!result.success()) + std::cerr << "Failed to read " << file << ": " << result.message() << " code " << result.status() << std::endl; + + return result.getImage(); +} + + + Water::Water(osg::Group *parent, osg::Group* sceneRoot, Resource::ResourceSystem *resourceSystem, osgUtil::IncrementalCompileOperation *ico, const MWWorld::Fallback* fallback, const std::string& resourcePath) : mParent(parent) @@ -399,17 +437,16 @@ Water::Water(osg::Group *parent, osg::Group* sceneRoot, Resource::ResourceSystem addDebugOverlay(reflectionTexture, 2, mParent); // shader - // FIXME: windows utf8 path handling? - osg::ref_ptr vertexShader (osg::Shader::readShaderFile(osg::Shader::VERTEX, resourcePath + "/shaders/water_vertex.glsl")); + osg::ref_ptr vertexShader (readShader(osg::Shader::VERTEX, resourcePath + "/shaders/water_vertex.glsl")); - osg::ref_ptr fragmentShader (osg::Shader::readShaderFile(osg::Shader::FRAGMENT, resourcePath + "/shaders/water_fragment.glsl")); + osg::ref_ptr fragmentShader (readShader(osg::Shader::FRAGMENT, resourcePath + "/shaders/water_fragment.glsl")); osg::ref_ptr program (new osg::Program); program->addShader(vertexShader); program->addShader(fragmentShader); - osg::ref_ptr normalMap (new osg::Texture2D(osgDB::readImageFile(resourcePath + "/shaders/water_nm.png"))); + osg::ref_ptr normalMap (new osg::Texture2D(readPngImage(resourcePath + "/shaders/water_nm.png"))); normalMap->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); normalMap->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); normalMap->setMaxAnisotropy(16); diff --git a/files/shaders/water_fragment.glsl b/files/shaders/water_fragment.glsl index 8dcd73fab..c11c18a2d 100644 --- a/files/shaders/water_fragment.glsl +++ b/files/shaders/water_fragment.glsl @@ -166,7 +166,6 @@ void main(void) waterColor = waterColor * length(gl_LightModel.ambient.xyz); #if REFRACTION float refractionDepth = texture2D(refractionDepthMap, screenCoords-(normal.xy*REFR_BUMP)).x; - // make linear float z_n = 2.0 * refractionDepth - 1.0; refractionDepth = 2.0 * near * far / (far + near - z_n * (far - near));