WNavigationSliceModule.cpp 6.92 KB
Newer Older
1 2
//---------------------------------------------------------------------------
//
3
// Project: OpenWalnut ( http://www.openwalnut.org )
4
//
5 6
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
//
// 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 <iostream>
#include <string>
27
#include <vector>
28 29 30 31

#include <osg/Group>
#include <osg/Geode>
#include <osg/Geometry>
32 33 34
#include <osg/Texture3D>

#include "boost/smart_ptr.hpp"
35 36

#include "WNavigationSliceModule.h"
37
#include "../../kernel/WKernel.h"
38

39 40 41 42
#include "../../dataHandler/WDataSetSingle.h"
#include "../../dataHandler/WSubject.h"
#include "../../dataHandler/WValueSet.hpp"

43 44
#include "../../graphicsEngine/WShader.h"

45 46 47 48
WNavigationSliceModule::WNavigationSliceModule():
    WModule()
{
    // initialize members
49
    m_shader = boost::shared_ptr< WShader > ( new WShader( "slice" ) );
50
    m_textureAssigned = false;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

WNavigationSliceModule::~WNavigationSliceModule()
{
    // cleanup
}

WNavigationSliceModule::WNavigationSliceModule( const WNavigationSliceModule& other )
    : WModule()
{
    *this = other;
}

const std::string WNavigationSliceModule::getName() const
{
66
    return "Navigation Slice Module";
67 68 69 70
}

const std::string WNavigationSliceModule::getDescription() const
{
71
    return "This module shows 3 orthogonal navigation slices.";
72 73 74 75 76 77 78 79 80
}

void WNavigationSliceModule::threadMain()
{
    createSlices();

    // Since the modules run in a separate thread: such loops are possible
    while ( !m_FinishRequested )
    {
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
        if ( WKernel::getRunningKernel()->getDataHandler()->getNumberOfSubjects() > 0 )
        {
            if ( WKernel::getRunningKernel()->getDataHandler()->getSubject(0)->getNumberOfDataSets() > 0 )
            {
                if ( !m_textureAssigned )
                {
                    boost::shared_ptr< WDataSetSingle > ds = boost::shared_dynamic_cast< WDataSetSingle >(
                            WKernel::getRunningKernel()->getDataHandler()->getSubject( 0 )->getDataSet( 0 ) );
                    boost::shared_ptr< WValueSet< int8_t > > vs = boost::shared_dynamic_cast< WValueSet<
                            int8_t > >( ds->getValueSet() );
                    int8_t* source = const_cast< int8_t* > ( vs->rawData() );

                    std::cout << "hier gehts los" << std::endl;

                    osg::ref_ptr< osg::Image > ima = new osg::Image;
                    ima->allocateImage( 160, 200, 160, GL_LUMINANCE, GL_UNSIGNED_BYTE );

                    unsigned char* data = ima->data();

                    for ( unsigned int i = 0; i < 160* 200* 160 ; ++i )
                    {
                        data[i] = source[i];
                    }
                    osg::Texture3D* texture3D = new osg::Texture3D;
                    texture3D->setFilter( osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR );
                    texture3D->setFilter( osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR );
                    texture3D->setWrap( osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT );
                    texture3D->setImage( ima );
                    texture3D->setResizeNonPowerOfTwoHint( false );

                    osg::StateSet* sliceState = m_sliceNode->getOrCreateStateSet();

                    sliceState->setTextureAttributeAndModes( 0, texture3D, osg::StateAttribute::ON );

                    m_textureAssigned = true;
                }
            }
        }
119 120 121 122 123 124 125 126 127
        // do fancy stuff
        sleep( 1 );
    }

    // clean up stuff
}

void WNavigationSliceModule::createSlices()
{
128
    m_sliceNode = new osg::Geode();
129 130

    osg::Geometry* sliceGeometry = new osg::Geometry();
131
    m_sliceNode->addDrawable( sliceGeometry );
132 133

    osg::Vec3Array* sliceVertices = new osg::Vec3Array;
134 135 136 137 138 139 140 141 142 143 144 145 146 147
    sliceVertices->push_back( osg::Vec3( 0, 100, 0 ) );
    sliceVertices->push_back( osg::Vec3( 0, 100, 160 ) );
    sliceVertices->push_back( osg::Vec3( 160, 100, 160 ) );
    sliceVertices->push_back( osg::Vec3( 160, 100, 0 ) );

    sliceVertices->push_back( osg::Vec3( 80, 0, 0 ) );
    sliceVertices->push_back( osg::Vec3( 80, 0, 160 ) );
    sliceVertices->push_back( osg::Vec3( 80, 200, 160 ) );
    sliceVertices->push_back( osg::Vec3( 80, 200, 0 ) );

    sliceVertices->push_back( osg::Vec3( 0, 0, 80 ) );
    sliceVertices->push_back( osg::Vec3( 0, 200, 80 ) );
    sliceVertices->push_back( osg::Vec3( 160, 200, 80 ) );
    sliceVertices->push_back( osg::Vec3( 160, 0, 80 ) );
148 149 150 151

    sliceGeometry->setVertexArray( sliceVertices );

    osg::Vec3Array* texCoords = new osg::Vec3Array;
schurade's avatar
schurade committed
152 153 154 155
    texCoords->push_back( osg::Vec3( 0.0, 0.5, 0.0 ) );
    texCoords->push_back( osg::Vec3( 0.0, 0.5, 1.0 ) );
    texCoords->push_back( osg::Vec3( 1.0, 0.5, 1.0 ) );
    texCoords->push_back( osg::Vec3( 1.0, 0.5, 0.0 ) );
156

schurade's avatar
schurade committed
157 158 159 160
    texCoords->push_back( osg::Vec3( 0.5, 0.0, 0.0 ) );
    texCoords->push_back( osg::Vec3( 0.5, 0.0, 1.0 ) );
    texCoords->push_back( osg::Vec3( 0.5, 1.0, 1.0 ) );
    texCoords->push_back( osg::Vec3( 0.5, 1.0, 0.0 ) );
161

schurade's avatar
schurade committed
162 163 164 165
    texCoords->push_back( osg::Vec3( 0.0, 0.0, 0.5 ) );
    texCoords->push_back( osg::Vec3( 0.0, 1.0, 0.5 ) );
    texCoords->push_back( osg::Vec3( 1.0, 1.0, 0.5 ) );
    texCoords->push_back( osg::Vec3( 1.0, 0.0, 0.5 ) );
166 167 168

    sliceGeometry->setTexCoordArray( 0, texCoords );

schurade's avatar
schurade committed
169 170 171 172 173
    osg::DrawElementsUInt* slice0 = new osg::DrawElementsUInt( osg::PrimitiveSet::QUADS, 0 );
    slice0->push_back( 3 );
    slice0->push_back( 2 );
    slice0->push_back( 1 );
    slice0->push_back( 0 );
174

schurade's avatar
schurade committed
175 176 177 178 179
    osg::DrawElementsUInt* slice1 = new osg::DrawElementsUInt( osg::PrimitiveSet::QUADS, 0 );
    slice1->push_back( 7 );
    slice1->push_back( 6 );
    slice1->push_back( 5 );
    slice1->push_back( 4 );
180

schurade's avatar
schurade committed
181 182 183 184 185
    osg::DrawElementsUInt* slice2 = new osg::DrawElementsUInt( osg::PrimitiveSet::QUADS, 0 );
    slice2->push_back( 11 );
    slice2->push_back( 10 );
    slice2->push_back( 9 );
    slice2->push_back( 8 );
186 187 188 189 190

    sliceGeometry->addPrimitiveSet( slice0 );
    sliceGeometry->addPrimitiveSet( slice1 );
    sliceGeometry->addPrimitiveSet( slice2 );

191 192 193 194
    WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->addChild( m_sliceNode );

    osg::StateSet* sliceState = m_sliceNode->getOrCreateStateSet();

195
    sliceState->setAttributeAndModes( m_shader->getProgramObject(), osg::StateAttribute::ON );
196
}