~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/core/graphicsEngine/WFiberDrawable.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
10
// OpenWalnut is free software: you can redistribute it and/or modify
 
11
// it under the terms of the GNU Lesser General Public License as published by
 
12
// the Free Software Foundation, either version 3 of the License, or
 
13
// (at your option) any later version.
 
14
//
 
15
// OpenWalnut is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU Lesser General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU Lesser General Public License
 
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#include <vector>
 
26
 
 
27
#include "../kernel/WKernel.h"
 
28
 
 
29
#include "WFiberDrawable.h"
 
30
 
 
31
// The constructor here does nothing. One thing that may be necessary is
 
32
// disabling display lists. This can be done by calling
 
33
//    setSupportsDisplayList (false);
 
34
// Display lists should be disabled for 'Drawable's that can change over
 
35
// time (that is, the vertices drawn change from time to time).
 
36
WFiberDrawable::WFiberDrawable():
 
37
    osg::Drawable(),
 
38
    m_useTubes( false )
 
39
{
 
40
    setSupportsDisplayList( false );
 
41
    // This contructor intentionally left blank. Duh.
 
42
}
 
43
 
 
44
// I can't say much about the methods below, but OSG seems to expect
 
45
// that we implement them.
 
46
WFiberDrawable::WFiberDrawable( const WFiberDrawable& /*pg*/, const osg::CopyOp& /*copyop*/ ):
 
47
    osg::Drawable()
 
48
{
 
49
}
 
50
 
 
51
osg::Object* WFiberDrawable::cloneType() const
 
52
{
 
53
    return new WFiberDrawable();
 
54
}
 
55
 
 
56
osg::Object* WFiberDrawable::clone( const osg::CopyOp& copyop ) const
 
57
{
 
58
    return new WFiberDrawable( *this, copyop );
 
59
}
 
60
 
 
61
// Real work is done here. THERE IS A VERY IMPORTANT THING TO NOTE HERE:
 
62
// the 'drawImplementation()' method receives an 'osg::State' as
 
63
// parameter. This can be used to change the OpenGL state, but changing
 
64
// the OpenGL state here is something to be avoided as much as possible.
 
65
// Do this *only* if it is *absolutely* necessary to make your rendering
 
66
// algorithm work. The "right" (most efficient and flexible) way to change
 
67
// the OpenGL state in OSG is by attaching 'StateSet's to 'Node's and
 
68
// 'Drawable's.
 
69
// That said, the example below shows how to change the OpenGL state in
 
70
// these rare cases in which it is necessary. But always keep in mind:
 
71
// *Change the OpenGL state only if strictly necessary*.
 
72
void WFiberDrawable::drawImplementation( osg::RenderInfo& renderInfo ) const //NOLINT
 
73
{
 
74
    if( m_useTubes )
 
75
    {
 
76
        drawTubes();
 
77
    }
 
78
    else
 
79
    {
 
80
        drawFibers( renderInfo );
 
81
    }
 
82
}
 
83
 
 
84
void WFiberDrawable::drawFibers( osg::RenderInfo& renderInfo ) const //NOLINT
 
85
{
 
86
    osg::State& state = *renderInfo.getState();
 
87
 
 
88
    state.disableAllVertexArrays();
 
89
    state.setVertexPointer( 3, GL_FLOAT , 0, &( *m_verts )[0] );
 
90
    state.setColorPointer( 3 , GL_FLOAT , 0, &( *m_colors )[0] );
 
91
    //state.setNormalPointer( GL_FLOAT , 0, &( *m_tangents )[0] );
 
92
    for( size_t i = 0; i < m_active->size(); ++i )
 
93
    {
 
94
        if( (*m_active)[i] )
 
95
        {
 
96
            state.glDrawArraysInstanced( GL_LINE_STRIP, (*m_startIndexes)[i], (*m_pointsPerLine)[i], 1);
 
97
        }
 
98
    }
 
99
 
 
100
    state.disableVertexPointer();
 
101
    state.disableColorPointer();
 
102
}
 
103
 
 
104
void WFiberDrawable::drawTubes() const
 
105
{
 
106
    for( size_t i = 0; i < m_active->size(); ++i )
 
107
    {
 
108
        if( (*m_active)[i] )
 
109
        {
 
110
            glBegin( GL_QUAD_STRIP );
 
111
            int idx = m_startIndexes->at( i ) * 3;
 
112
            for( size_t k = 0; k < m_pointsPerLine->at( i ); ++k )
 
113
            {
 
114
                glNormal3f( m_tangents->at( idx ), m_tangents->at( idx + 1 ), m_tangents->at( idx + 2 ) );
 
115
                glColor3f( m_colors->at( idx ), m_colors->at( idx + 1 ), m_colors->at( idx + 2 ) );
 
116
                glTexCoord1f( -1.0f );
 
117
                glVertex3f( m_verts->at( idx ), m_verts->at( idx + 1 ), m_verts->at( idx + 2 ) );
 
118
                glTexCoord1f( 1.0f );
 
119
                glVertex3f( m_verts->at( idx ), m_verts->at( idx + 1 ), m_verts->at( idx + 2 ) );
 
120
                idx += 3;
 
121
                //
 
122
            }
 
123
            glEnd();
 
124
        }
 
125
    }
 
126
}
 
127