WTubeDrawable.cpp 6.33 KB
Newer Older
schurade's avatar
schurade committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// 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/>.
//
//---------------------------------------------------------------------------

#include <vector>

#include "../../dataHandler/WDataSetFibers.h"
#include "../../kernel/WKernel.h"
#include "WTubeDrawable.h"

// The constructor here does nothing. One thing that may be necessary is
// disabling display lists. This can be done by calling
//    setSupportsDisplayList (false);
// Display lists should be disabled for 'Drawable's that can change over
// time (that is, the vertices drawn change from time to time).
WTubeDrawable::WTubeDrawable():
    osg::Drawable(),
38 39
    m_useTubes( false ),
    m_globalColoring( true )
schurade's avatar
schurade committed
40
{
41
    setSupportsDisplayList( false );
schurade's avatar
schurade committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    // This contructor intentionally left blank. Duh.
}

// I can't say much about the methods below, but OSG seems to expect
// that we implement them.
WTubeDrawable::WTubeDrawable( const WTubeDrawable& /*pg*/, const osg::CopyOp& /*copyop*/ ):
    osg::Drawable()
{
}

osg::Object* WTubeDrawable::cloneType() const
{
    return new WTubeDrawable();
}

osg::Object* WTubeDrawable::clone( const osg::CopyOp& copyop ) const
{
    return new WTubeDrawable( *this, copyop );
}

// Real work is done here. THERE IS A VERY IMPORTANT THING TO NOTE HERE:
// the 'drawImplementation()' method receives an 'osg::State' as
// parameter. This can be used to change the OpenGL state, but changing
// the OpenGL state here is something to be avoided as much as possible.
// Do this *only* if it is *absolutely* necessary to make your rendering
// algorithm work. The "right" (most efficient and flexible) way to change
// the OpenGL state in OSG is by attaching 'StateSet's to 'Node's and
// 'Drawable's.
// That said, the example below shows how to change the OpenGL state in
// these rare cases in which it is necessary. But always keep in mind:
// *Change the OpenGL state only if strictly necessary*.
73
void WTubeDrawable::drawImplementation( osg::RenderInfo& renderInfo ) const //NOLINT
schurade's avatar
schurade committed
74 75 76 77 78 79 80
{
    if ( m_useTubes )
    {
        drawTubes();
    }
    else
    {
81
        drawFibers( renderInfo );
schurade's avatar
schurade committed
82 83 84 85 86 87 88 89 90 91 92 93 94
    }
}

void WTubeDrawable::setDataset( boost::shared_ptr< const WDataSetFibers > dataset )
{
    m_dataset = dataset;
}

void WTubeDrawable::setUseTubes( bool flag )
{
    m_useTubes = flag;
}

95 96 97 98 99 100 101 102 103 104
void WTubeDrawable::setColoringMode( bool globalColoring )
{
    m_globalColoring = globalColoring;
}

bool WTubeDrawable::getColoringMode() const
{
    return m_globalColoring;
}

Alexander Wiebel's avatar
Alexander Wiebel committed
105
void WTubeDrawable::drawFibers( osg::RenderInfo& /* renderInfo */ ) const //NOLINT
schurade's avatar
schurade committed
106 107 108 109
{
    boost::shared_ptr< std::vector< size_t > > startIndexes = m_dataset->getLineStartIndexes();
    boost::shared_ptr< std::vector< size_t > > pointsPerLine = m_dataset->getLineLengths();
    boost::shared_ptr< std::vector< float > > verts = m_dataset->getVertices();
110
    boost::shared_ptr< std::vector< float > > colors = ( m_globalColoring ? m_dataset->getGlobalColors() : m_dataset->getLocalColors() );
schurade's avatar
schurade committed
111
    boost::shared_ptr< std::vector< bool > > active = WKernel::getRunningKernel()->getRoiManager()->getBitField();
112 113 114
#if 0
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );
schurade's avatar
schurade committed
115

116 117
    glVertexPointer( 3, GL_FLOAT, 0, &(*verts)[0] ); //NOLINT
    glColorPointer( 3, GL_FLOAT, 0, &(*colors)[0] ); //NOLINT
118 119

    for ( size_t i = 0; i < active->size(); ++i )
schurade's avatar
schurade committed
120
    {
121
        if ( (*active)[i] )
schurade's avatar
schurade committed
122
        {
123 124 125
            glDrawArrays( GL_LINE_STRIP, (*startIndexes)[i] * 3, (*pointsPerLine)[i] );
        }
    }
126

127 128 129 130 131 132 133
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );
#else
    for ( size_t i = 0; i < active->size(); ++i )
    {
        if ( (*active)[i] )
        {
schurade's avatar
schurade committed
134
            glBegin( GL_LINE_STRIP );
135
            int idx = (*startIndexes)[i] * 3;
schurade's avatar
schurade committed
136 137
            for ( size_t k = 0; k < pointsPerLine->at( i ); ++k )
            {
138 139
                glColor3f( (*colors)[idx], (*colors)[idx + 1], (*colors)[idx + 2] );
                glVertex3f( (*verts)[idx], (*verts)[idx + 1], (*verts)[idx + 2] );
schurade's avatar
schurade committed
140 141 142 143 144
                idx += 3;
            }
            glEnd();
        }
    }
145
#endif
schurade's avatar
schurade committed
146 147 148 149 150 151 152 153
}

void WTubeDrawable::drawTubes() const
{
    boost::shared_ptr< std::vector< size_t > > startIndexes = m_dataset->getLineStartIndexes();
    boost::shared_ptr< std::vector< size_t > > pointsPerLine = m_dataset->getLineLengths();
    boost::shared_ptr< std::vector< float > > verts = m_dataset->getVertices();
    boost::shared_ptr< std::vector< float > > tangents = m_dataset->getTangents();
154
    boost::shared_ptr< std::vector< float > > colors = ( m_globalColoring ? m_dataset->getGlobalColors() : m_dataset->getLocalColors() );
schurade's avatar
schurade committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    boost::shared_ptr< std::vector< bool > > active = WKernel::getRunningKernel()->getRoiManager()->getBitField();

    for( size_t i = 0; i < active->size(); ++i )
    {
        if ( active->at( i ) )
        {
            glBegin( GL_QUAD_STRIP );
            int idx = startIndexes->at( i ) * 3;
            for ( size_t k = 0; k < pointsPerLine->at( i ); ++k )
            {
                glNormal3f( tangents->at( idx ), tangents->at( idx + 1 ), tangents->at( idx + 2 ) );
                glColor3f( colors->at( idx ), colors->at( idx + 1 ), colors->at( idx + 2 ) );
                glTexCoord1f( -1.0f );
                glVertex3f( verts->at( idx ), verts->at( idx + 1 ), verts->at( idx + 2 ) );
                glTexCoord1f( 1.0f );
                glVertex3f( verts->at( idx ), verts->at( idx + 1 ), verts->at( idx + 2 ) );
                idx += 3;
                //
            }
            glEnd();
        }
    }
}