diff --git a/src/osgSim/OverlayNode.cpp b/src/osgSim/OverlayNode.cpp index f51712ffe..1b9070cee 100644 --- a/src/osgSim/OverlayNode.cpp +++ b/src/osgSim/OverlayNode.cpp @@ -18,6 +18,8 @@ #include #include +#include + #include #include @@ -972,16 +974,76 @@ OverlayNode::OverlayData& OverlayNode::getOverlayData(osgUtil::CullVisitor* cv) overlayData._texgenNode->setTextureUnit(_textureUnit); } + if (!overlayData._y0) + { + overlayData._y0 = new osg::Uniform("y0",-2.0f); + } + + if (!overlayData._inverse_one_minus_y0) + { + overlayData._inverse_one_minus_y0 = new osg::Uniform("inverse_one_minus_y0",-1.0f/3.0f); + } + if (!overlayData._overlayStateSet) { overlayData._overlayStateSet = new osg::StateSet; - // overlayData._overlayStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF); + overlayData._overlayStateSet->addUniform(overlayData._y0.get()); + overlayData._overlayStateSet->addUniform(overlayData._inverse_one_minus_y0.get()); + + osg::Program* program = new osg::Program; + overlayData._overlayStateSet->setAttribute(program); + + // get shaders from source + std::string vertexShaderFile = osgDB::findDataFile("overlay.vert"); + if (!vertexShaderFile.empty()) + { + program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, vertexShaderFile)); + } + else + { + char vertexShaderSource[] = + "varying vec3 texcoord;\n" + "\n" + "void main(void)\n" + "{\n" + " texcoord = gl_MultiTexCoord0.xyz;\n" + " gl_Position = ftransform(); \n" + "}\n"; + + osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource); + program->addShader(vertex_shader); + } + + + } + + if (!overlayData._mainSubgraphProgram) + { + overlayData._mainSubgraphProgram = new osg::Program; + + // get shaders from source + std::string vertexShaderFile = osgDB::findDataFile("mainsubgraph.vert"); + if (!vertexShaderFile.empty()) + { + overlayData._mainSubgraphProgram->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, vertexShaderFile)); + } + + std::string fragmentShaderFile = osgDB::findDataFile("mainsubgraph.frag"); + if (!fragmentShaderFile.empty()) + { + overlayData._mainSubgraphProgram->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, fragmentShaderFile)); + } } if (!overlayData._mainSubgraphStateSet) { overlayData._mainSubgraphStateSet = new osg::StateSet; + overlayData._mainSubgraphStateSet->addUniform(overlayData._y0.get()); + overlayData._mainSubgraphStateSet->addUniform(overlayData._inverse_one_minus_y0.get()); + overlayData._mainSubgraphStateSet->addUniform(new osg::Uniform("texture_0",0)); + overlayData._mainSubgraphStateSet->addUniform(new osg::Uniform("texture_1",1)); + overlayData._mainSubgraphStateSet->setTextureAttributeAndModes(_textureUnit, overlayData._texture.get(), osg::StateAttribute::ON); overlayData._mainSubgraphStateSet->setTextureMode(_textureUnit, GL_TEXTURE_GEN_S, osg::StateAttribute::ON); overlayData._mainSubgraphStateSet->setTextureMode(_textureUnit, GL_TEXTURE_GEN_T, osg::StateAttribute::ON); @@ -1289,7 +1351,6 @@ void OverlayNode::traverse_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVis } else { - double minHeight = -1000.0; overlayPolytope.projectDowntoBase(osg::Vec3d(0.0,0.0,_overlayBaseHeight), osg::Vec3d(0.0,0.0,1.0)); } @@ -1354,132 +1415,98 @@ void OverlayNode::traverse_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVis double max_side = -DBL_MAX; double min_up = DBL_MAX; double max_up = -DBL_MAX; - - unsigned int leftPointIndex = 0; - unsigned int rightPointIndex = 0; + double min_distanceEye = DBL_MAX; + double max_distanceEye = -DBL_MAX; typedef std::vector ProjectedVertices; ProjectedVertices projectedVertices; + + osg::Vec3 eyeLocal = cv->getEyeLocal(); + + // computed the expected near/far ratio unsigned int i; for(i=0; i< corners.size(); ++i) { + double distanceEye = (corners[i] - eyeLocal).length(); + if (distanceEye < min_distanceEye) min_distanceEye = distanceEye; + if (distanceEye > max_distanceEye) max_distanceEye = distanceEye; + osg::Vec3d delta = corners[i] - center; double distance_side = delta * sideVector; double distance_up = delta * upVector; projectedVertices.push_back(osg::Vec2d(distance_side, distance_up)); - + if (distance_sidemax_side) { max_side = distance_side; - rightPointIndex = i; } if (distance_upmax_up) max_up = distance_up; } - - double mid_side = (min_side + max_side)*0.5; - - osg::Vec2d topLeft(min_side, max_up); - osg::Vec2d topRight(max_side, max_up); - osg::Vec2d lowerRight(max_side, min_up); - osg::Vec2d lowerLeft(min_side, min_up); + + double mid_side = (min_side + max_side) * 0.5; + double ratio = min_distanceEye / max_distanceEye; + bool usePerspectiveShaders = ratio<0.95; -#if 0 - osg::notify(osg::NOTICE)<<"b topLeft = "<= mid_side && vb.x() >= mid_side) - { - // osg::notify(osg::NOTICE)<<"Both on right va="<