Changeset 4273 for trunk


Ignore:
Timestamp:
Mar 26, 2014 3:25:30 AM (10 years ago)
Author:
ldelgass
Message:

Add terrain options, get/set viewpoint

Location:
trunk/packages/vizservers/geovis
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/geovis/Renderer.cpp

    r4247 r4273  
    2323#include <osgEarth/Bounds>
    2424#include <osgEarth/Profile>
     25#include <osgEarth/Viewpoint>
    2526#include <osgEarth/TerrainLayer>
    2627#include <osgEarth/ImageLayer>
     
    8283    _manipulator = new osgEarth::Util::EarthManipulator;
    8384    _viewer->setCameraManipulator(_manipulator.get());
    84     _viewer->addEventHandler(new osgGA::StateSetManipulator(_viewer->getCamera()->getOrCreateStateSet()));
     85    _stateManip = new osgGA::StateSetManipulator(_viewer->getCamera()->getOrCreateStateSet());
     86    _viewer->addEventHandler(_stateManip);
    8587    _coordsCallback = new MouseCoordsCallback();
    8688    _mouseCoordsTool = new osgEarth::Util::MouseCoordsTool(mapNode);
     
    293295    mpOpt.color() = osg::Vec4(1, 1, 1, 1);
    294296    //mpOpt.minLOD() = 1;
     297    // Sets shader uniform for terrain renderer (config var defaults to false)
     298    mpOpt.enableLighting() = false;
    295299    osgEarth::MapNodeOptions mapNodeOpts(mpOpt);
    296     mapNodeOpts.enableLighting() = false;
     300    // Sets GL_LIGHTING state in MapNode's StateSet (config var defaults to true)
     301    mapNodeOpts.enableLighting() = true;
    297302    osgEarth::MapNode *mapNode = new osgEarth::MapNode(map, mapNodeOpts);
    298303    _mapNode = mapNode;
     
    328333    }
    329334    _needsRedraw = true;
     335}
     336
     337void Renderer::setLighting(bool state)
     338{
     339    if (_mapNode.valid()) {
     340        TRACE("Setting lighting: %d", state ? 1 : 0);
     341        _mapNode->getOrCreateStateSet()
     342            ->setMode(GL_LIGHTING, state ? 1 : 0);
     343    }
     344    _needsRedraw = true;
     345}
     346
     347void Renderer::setTerrainVerticalScale(double scale)
     348{
     349    if (_mapNode.valid()) {
     350        _mapNode->getTerrainEngine()->setVerticalScale(scale);
     351    }
     352    _needsRedraw = true;
     353}
     354
     355void Renderer::setTerrainLighting(bool state)
     356{
     357#if 1
     358    if (_mapNode.valid()) {
     359        // XXX: HACK alert
     360        // Find the terrain engine container (might be above one or more decorators)
     361        osg::Group *group = _mapNode->getTerrainEngine();
     362        while (group->getParent(0) != NULL && group->getParent(0) != _mapNode.get()) {
     363            group = group->getParent(0);
     364        }
     365        if (group != NULL && group->getParent(0) == _mapNode.get()) {
     366            TRACE("Setting terrain lighting: %d", state ? 1 : 0);
     367            if (group->getOrCreateStateSet()->getUniform("oe_mode_GL_LIGHTING") != NULL) {
     368                group->getStateSet()->getUniform("oe_mode_GL_LIGHTING")->set(state);
     369            } else {
     370                ERROR("Can't get terrain lighting uniform");
     371            }
     372        } else {
     373            ERROR("Can't get terrain lighting uniform");
     374        }
     375    }
     376#else
     377    if (_stateManip.valid()) {
     378        _stateManip->setLightingEnabled(state);
     379    }
     380#endif
     381    _needsRedraw = true;
     382}
     383
     384void Renderer::setTerrainWireframe(bool state)
     385{
     386#if 0
     387    if (_mapNode.valid()) {
     388        TRACE("Setting terrain wireframe: %d", state ? 1 : 0);
     389        osg::StateSet *state = _mapNode->getOrCreateStateSet();
     390        osg::PolygonMode *pmode = dynamic_cast< osg::PolygonMode* >(state->getAttribute(osg::StateAttribute::POLYGONMODE));
     391        if (pmode == NULL) {
     392            pmode = new osg::PolygonMode;
     393            state->setAttribute(pmode);
     394        }
     395        if (state) {
     396            pmode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
     397        } else {
     398            pmode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL);
     399        }
     400    }
     401#else
     402    if (_stateManip.valid()) {
     403        _stateManip->setPolygonMode(state ? osg::PolygonMode::LINE : osg::PolygonMode::FILL);
     404    }
     405#endif
     406    _needsRedraw = true;
     407}
     408
     409void Renderer::setViewpoint(const osgEarth::Viewpoint& v, double durationSecs)
     410{
     411    if (_manipulator.valid()) {
     412        _manipulator->setViewpoint(v, durationSecs);
     413    }
     414    _needsRedraw = true;
     415}
     416
     417osgEarth::Viewpoint Renderer::getViewpoint()
     418{
     419    if (_manipulator.valid()) {
     420        return _manipulator->getViewpoint();
     421    } else {
     422        // Uninitialized, invalid viewpoint
     423        return osgEarth::Viewpoint();
     424    }
    330425}
    331426
     
    602697          x, y, (absolute ? 1 : 0));
    603698
     699    // Wants mouse delta x,y in normalized screen coords
    604700    _manipulator->pan(x, y);
    605701    _needsRedraw = true;
     
    628724    // FIXME: zoom here wants y mouse coords in normalized viewport coords
    629725
    630     _manipulator->zoom(0, z);
     726    //_manipulator->zoom(0, z);
     727
     728    double dist = _manipulator->getDistance();
     729    dist *= (1.0 + z);
     730    _manipulator->setDistance(dist);
     731
    631732    _needsRedraw = true;
    632733}
  • trunk/packages/vizservers/geovis/Renderer.h

    r4246 r4273  
    1919#include <osg/TransferFunction>
    2020#include <osgViewer/Viewer>
     21#include <osgGA/StateSetManipulator>
    2122
    2223#include <osgEarth/Map>
     24#include <osgEarth/Viewpoint>
    2325#include <osgEarth/ImageLayer>
    2426#include <osgEarth/ElevationLayer>
     
    154156    void setLighting(bool state);
    155157
     158    void setTerrainLighting(bool state);
     159
     160    void setTerrainVerticalScale(double scale);
     161
     162    void setTerrainWireframe(bool state);
     163
    156164    // Image raster layers
    157165
     
    221229        return _windowHeight;
    222230    }
     231
     232    // Camera
     233
     234    osgEarth::Viewpoint getViewpoint();
     235
     236    void setViewpoint(const osgEarth::Viewpoint& v, double durationSecs = 0.0);
    223237
    224238    void resetCamera(bool resetOrientation = true);
     
    304318    osg::ref_ptr<MouseCoordsCallback> _coordsCallback;
    305319    osg::ref_ptr<osgEarth::Util::EarthManipulator> _manipulator;
     320    osg::ref_ptr<osgGA::StateSetManipulator> _stateManip;
    306321};
    307322
  • trunk/packages/vizservers/geovis/RendererCmd.cpp

    r4246 r4273  
    887887}
    888888
     889static int
     890MapTerrainEdgesOp(ClientData clientData, Tcl_Interp *interp, int objc,
     891                  Tcl_Obj *const *objv)
     892{
     893    bool state;
     894    if (GetBooleanFromObj(interp, objv[3], &state) != TCL_OK) {
     895        return TCL_ERROR;
     896    }
     897    TRACE("Not implemented");
     898    //g_renderer->setTerrainEdges(state);
     899    return TCL_OK;
     900}
     901
     902static int
     903MapTerrainLightingOp(ClientData clientData, Tcl_Interp *interp, int objc,
     904                     Tcl_Obj *const *objv)
     905{
     906    bool state;
     907    if (GetBooleanFromObj(interp, objv[3], &state) != TCL_OK) {
     908        return TCL_ERROR;
     909    }
     910
     911    g_renderer->setTerrainLighting(state);
     912    return TCL_OK;
     913}
     914
     915static int
     916MapTerrainLineColorOp(ClientData clientData, Tcl_Interp *interp, int objc,
     917                      Tcl_Obj *const *objv)
     918{
     919    float color[3];
     920    if (GetFloatFromObj(interp, objv[2], &color[0]) != TCL_OK ||
     921        GetFloatFromObj(interp, objv[3], &color[1]) != TCL_OK ||
     922        GetFloatFromObj(interp, objv[4], &color[2]) != TCL_OK) {
     923        return TCL_ERROR;
     924    }
     925    TRACE("Not implemented");
     926    //g_renderer->setTerrainLineColor(color);
     927    return TCL_OK;
     928}
     929
     930static int
     931MapTerrainVertScaleOp(ClientData clientData, Tcl_Interp *interp, int objc,
     932                      Tcl_Obj *const *objv)
     933{
     934    double scale;
     935    if (Tcl_GetDoubleFromObj(interp, objv[3], &scale) != TCL_OK) {
     936        return TCL_ERROR;
     937    }
     938
     939    g_renderer->setTerrainVerticalScale(scale);
     940    return TCL_OK;
     941}
     942
     943static int
     944MapTerrainWireframeOp(ClientData clientData, Tcl_Interp *interp, int objc,
     945                      Tcl_Obj *const *objv)
     946{
     947    bool state;
     948    if (GetBooleanFromObj(interp, objv[3], &state) != TCL_OK) {
     949        return TCL_ERROR;
     950    }
     951
     952    g_renderer->setTerrainWireframe(state);
     953    return TCL_OK;
     954}
     955
     956static CmdSpec mapTerrainOps[] = {
     957    {"edges",     1, MapTerrainEdgesOp,     4, 4, "bool"},
     958    {"lighting",  2, MapTerrainLightingOp,  4, 4, "bool"},
     959    {"linecolor", 2, MapTerrainLineColorOp, 6, 6, "r g b"},
     960    {"vertscale", 1, MapTerrainVertScaleOp, 4, 4, "val"},
     961    {"wireframe", 1, MapTerrainWireframeOp, 4, 4, "bool"},
     962};
     963static int nMapTerrainOps = NumCmdSpecs(mapTerrainOps);
     964
     965static int
     966MapTerrainOp(ClientData clientData, Tcl_Interp *interp, int objc,
     967           Tcl_Obj *const *objv)
     968{
     969    Tcl_ObjCmdProc *proc;
     970
     971    proc = GetOpFromObj(interp, nMapTerrainOps, mapTerrainOps,
     972                        CMDSPEC_ARG2, objc, objv, 0);
     973    if (proc == NULL) {
     974        return TCL_ERROR;
     975    }
     976    return (*proc) (clientData, interp, objc, objv);
     977}
     978
    889979static CmdSpec mapOps[] = {
    890     {"layer",    2, MapLayerOp,       3, 9, "op ?params...?"},
     980    {"layer",    2, MapLayerOp,       3, 0, "op ?params...?"},
    891981    {"load",     2, MapLoadOp,        4, 5, "options"},
    892982    {"reset",    1, MapResetOp,       3, 8, "type ?profile xmin ymin xmax ymax?"},
     983    {"terrain",  1, MapTerrainOp,     3, 0, "op ?params...?"},
    893984};
    894985static int nMapOps = NumCmdSpecs(mapOps);
Note: See TracChangeset for help on using the changeset viewer.