WGECamera.cpp 4.2 KB
Newer Older
ebaum's avatar
ebaum committed
1 2
//---------------------------------------------------------------------------
//
3
// Project: OpenWalnut ( http://www.openwalnut.org )
ebaum's avatar
ebaum committed
4
//
5 6
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
ebaum's avatar
ebaum committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25
#include <iostream>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
26 27
#include <string>

28
#include "exceptions/WGEInitFailed.h"
ebaum's avatar
ebaum committed
29 30
#include "WGECamera.h"

31 32 33
WGECamera::WGECamera( int width, int height, ProjectionMode projectionMode )
    : osg::Camera(),
      m_DefProjMode( projectionMode )
ebaum's avatar
ebaum committed
34
{
35 36 37
    // needed since OSG 3.2 to ensure a properly initialized stateset. Also works with OSG 3.0
    getOrCreateStateSet()->setGlobalDefaults();

38 39
    setViewport( 0, 0, width, height );
    setClearColor( osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) );
40 41 42 43 44 45 46

    // disable all culling
    setCullingActive( false );
    setCullingMode( osg::CullSettings::NO_CULLING );

    // near-far computation is done using the bounding volumes
    setComputeNearFarMode(
47 48
        // osg::CullSettings::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES
        osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR
49 50
        // osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES
    );
51
    reset();
ebaum's avatar
ebaum committed
52 53
}

54 55 56 57 58 59 60 61 62 63 64 65 66
WGECamera::WGECamera()
    : osg::Camera(),
      m_DefProjMode( ORTHOGRAPHIC )
{
    // needed since OSG 3.2 to ensure a properly initialized stateset. Also works with OSG 3.0
    getOrCreateStateSet()->setGlobalDefaults();

    // disable all culling
    setCullingActive( false );
    setCullingMode( osg::CullSettings::NO_CULLING );

    // near-far computation is done using the bounding volumes
    setComputeNearFarMode(
67 68
        // osg::CullSettings::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES
        osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR
69 70 71 72
        // osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES
    );
}

ebaum's avatar
ebaum committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
WGECamera::~WGECamera()
{
    // cleanup
}

void WGECamera::setDefaultProjectionMode( WGECamera::ProjectionMode mode )
{
    m_DefProjMode = mode;
}

WGECamera::ProjectionMode WGECamera::getDefaultProjectionMode()
{
    return m_DefProjMode;
}

void WGECamera::reset()
{
90 91
    switch( m_DefProjMode )
    {
92
        case ORTHOGRAPHIC:
93
            setProjectionMatrixAsOrtho( -120.0 * getViewport()->aspectRatio(), 120.0 * getViewport()->aspectRatio(),
94
                -120.0, 120.0, -1000.0, +20000.0 );
95 96
            setProjectionResizePolicy( HORIZONTAL );
            break;
97
        case PERSPECTIVE:
98
            setProjectionMatrixAsPerspective( 30.0, getViewport()->aspectRatio(), 1.0, 1000.0 );
99
            setProjectionResizePolicy( WGECamera::HORIZONTAL );
100
            break;
101 102
        case TWO_D:
            resize();
103
            setProjectionResizePolicy( WGECamera::FIXED );
104 105 106
            break;
        case TWO_D_UNIT:
            resize();
107
            setProjectionResizePolicy( WGECamera::FIXED );
108 109
            break;
        default:
110
            throw WGEInitFailed( std::string( "Unknown projection mode." ) );
111
    }
ebaum's avatar
ebaum committed
112
}
113 114 115 116 117 118 119

void WGECamera::resize()
{
    if( m_DefProjMode == TWO_D )
    {
        setProjectionMatrixAsOrtho2D( 0.0, getViewport()->width(), 0.0, getViewport()->height() );
    }
120 121 122 123 124 125 126 127 128 129 130 131
    else if( m_DefProjMode == TWO_D_UNIT )
    {
        double aspectWH = static_cast< double >( getViewport()->width() ) / static_cast< double >( getViewport()->height() );
        double aspectHW = 1.0 / aspectWH;

        double w = aspectWH > aspectHW ? aspectWH : 1.0;
        double h = aspectWH > aspectHW ? 1.0 : aspectHW;

        w *= 0.5;
        h *= 0.5;
        setProjectionMatrixAsOrtho( -w, w, -h, h, 0.0, 1.0 );
    }
132
}