WTubeGeometry.cpp 6.45 KB
Newer Older
Dirk Albrecht's avatar
Dirk Albrecht 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 38 39 40 41 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
//---------------------------------------------------------------------------
//
// 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 "WTubeGeometry.h"

WTubeGeometry::WTubeGeometry() : osg::Geometry()
{
}

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

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

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

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

void WTubeGeometry::setStartIndices( boost::shared_ptr< std::vector< size_t > > startIndices )
{
    m_startIndices = startIndices;
}

void WTubeGeometry::setViewPortUniform( osg::ref_ptr<osg::Uniform> uniformViewportHeight, osg::ref_ptr<osg::Uniform> uniformViewportWidth )
{
    m_uniformViewportHeight = uniformViewportHeight;
    m_uniformViewportWidth = uniformViewportWidth;
}

void WTubeGeometry::drawImplementation( osg::RenderInfo& renderInfo ) const
{
    if ( _internalOptimizedGeometry.valid() )
    {
        _internalOptimizedGeometry->drawImplementation( renderInfo );
        return;
    }

    osg::State& state = *renderInfo.getState();

    bool checkForGLErrors = state.getCheckForGLErrors() == osg::State::ONCE_PER_ATTRIBUTE;
    if (checkForGLErrors) state.checkGLErrors( "start of Geometry::drawImplementation()" );

    bool useFastPath = areFastPathsUsed();

    state.disableAllVertexArrays();

    state.lazyDisablingOfVertexAttributes();

    // set up arrays
    state.setVertexPointer( _vertexData.array.get() );

    state.setNormalPointer( _normalData.array.get() );

    state.setColorPointer( _colorData.array.get() );

    for( unsigned int unit = 0; unit < _texCoordList.size(); ++unit )
    {
        const osg::Array* array = _texCoordList[unit].array.get();
        if ( array ) state.setTexCoordPointer( unit, array );
    }

    for( unsigned int index = 0; index < _vertexAttribList.size(); ++index )
    {
        const osg::Array* array = _vertexAttribList[index].array.get();
        const AttributeBinding ab = _vertexAttribList[index].binding;
        if( ab == BIND_PER_VERTEX && array )
        {
            state.setVertexAttribPointer( index, array, _vertexAttribList[index].normalize );
        }
    }


    state.applyDisablingOfVertexAttributes();

    unsigned int primitiveNum = 0;

    if ( checkForGLErrors ) state.checkGLErrors("Geometry::drawImplementation() after vertex arrays setup.");


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    // draw the primitives themselves.
    //

    GLenum mode = _primitives[0].get()->getMode();
    switch( mode )
    {
        case( GL_POINTS ):
            {
                glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
                glEnable( GL_VERTEX_PROGRAM_POINT_SIZE );
                glEnable( GL_ALPHA_TEST );
                glEnable( GL_POINT_SPRITE );
                glAlphaFunc( GL_NOTEQUAL, 0.0 );
                glTexEnvi( GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE );
                m_uniformViewportHeight->set( static_cast<int>( ( *renderInfo.getCurrentCamera()->getViewport() ).height() ) );
                m_uniformViewportWidth->set( static_cast<int>( ( *renderInfo.getCurrentCamera()->getViewport() ).width() ) );
                break;
            }
        case( GL_QUAD_STRIP ):
            {
                glDisable( GL_VERTEX_PROGRAM_POINT_SIZE );
                glDisable( GL_POINT_SPRITE );
143
                glDisable( GL_ALPHA_TEST );
Dirk Albrecht's avatar
Dirk Albrecht committed
144 145 146 147 148 149 150 151 152 153
                glAlphaFunc( GL_NOTEQUAL, 0.0 );
                break;
            }
    }
    glEnable( GL_DEPTH_TEST );
    glDisable( GL_LIGHTING );
    glDisable( GL_BLEND );

    boost::shared_ptr< std::vector< bool > > active = WKernel::getRunningKernel()->getRoiManager()->getBitField();
    boost::shared_ptr< std::vector< size_t > > pointsPerLine = m_dataset->getLineLengths();
154
  //useFastPath = false;
Dirk Albrecht's avatar
Dirk Albrecht committed
155 156 157 158 159
    if ( useFastPath )
    {
        for( unsigned int primitiveSetNum = 0; primitiveSetNum != _primitives.size(); ++primitiveSetNum )
        {
            const osg::PrimitiveSet* primitiveset = _primitives[primitiveSetNum].get();
160 161 162 163
            if( (*active)[primitiveSetNum] )
            {
                primitiveset->draw( state, true );
            }
Dirk Albrecht's avatar
Dirk Albrecht committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        }
    }
    else
    {
        for ( size_t i = 0; i < active->size(); ++i )
        {
            if( (*active)[i] )
            {
                switch( mode )
                {
                    case( GL_POINTS ):
                        {
                            state.glDrawArraysInstanced( mode, m_startIndices->at( i ), ( *pointsPerLine )[i], 1);
                            break;
                        }
                    case( GL_QUAD_STRIP ):
                        {
                            state.glDrawArraysInstanced( mode, m_startIndices->at( i ), 2 * ( *pointsPerLine )[i], 1);
                            break;
                        }
                }
            }
        }
    }

    // unbind the VBO's if any are used.
    state.unbindVertexBufferObject();
    state.unbindElementBufferObject();
    state.disableVertexPointer();
    state.disableColorPointer();
    state.disableNormalPointer();
    state.disableVertexAttribPointer( 0 );
}