root/OpenSceneGraph/trunk/src/osgPlugins/OpenFlight/Record.cpp @ 13041

Revision 13041, 4.5 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14//
15// OpenFlight® loader for OpenSceneGraph
16//
17//  Copyright (C) 2005-2007  Brede Johansen
18//
19
20#include <stdexcept>
21#include <osg/Texture2D>
22#include <osg/MatrixTransform>
23#include "Record.h"
24#include "Document.h"
25
26using namespace flt;
27
28
29Record::Record()
30{
31}
32
33Record::~Record()
34{
35}
36
37void Record::read(RecordInputStream& in, Document& document)
38{
39    _parent = document.getCurrentPrimaryRecord();
40
41    // Read record body.
42    readRecord(in,document);
43}
44
45void Record::readRecord(RecordInputStream& /*in*/, Document& /*document*/)
46{
47}
48
49PrimaryRecord::PrimaryRecord() :
50    _numberOfReplications(0)
51{
52}
53
54void PrimaryRecord::read(RecordInputStream& in, Document& document)
55{
56    PrimaryRecord* parentPrimary = document.getTopOfLevelStack();
57    PrimaryRecord* currentPrimary = document.getCurrentPrimaryRecord();
58
59    // Finally call dispose() for primary without push, pop level pair.
60    if (currentPrimary && currentPrimary!=parentPrimary)
61    {
62        currentPrimary->dispose(document);
63    }
64
65   // Update current primary record.
66    document.setCurrentPrimaryRecord(this);
67
68     _parent = parentPrimary;
69
70    // Read record body.
71    readRecord(in,document);
72}
73
74///////////////////////////////////////////////////////////////////////////////////
75// Helper methods
76
77// Insert matrix-tranform(s)
78//
79// node: node to apply transform
80// matrix: transformation matrix
81// numberOfReplications: zero for regular transform, number of copies if replication is used.
82void flt::insertMatrixTransform(osg::Node& node, const osg::Matrix& matrix, int numberOfReplications)
83{
84    osg::ref_ptr<osg::Node> ref = &node;
85    osg::Node::ParentList parents = node.getParents();
86
87    // Disconnect node from parents.
88    for (osg::Node::ParentList::iterator itr=parents.begin();
89        itr!=parents.end();
90        ++itr)
91    {
92        (*itr)->removeChild(&node);
93    }
94
95    // Start without transformation if replication.
96    osg::Matrix accumulatedMatrix = (numberOfReplications > 0)? osg::Matrix::identity() : matrix;
97
98    for (int n=0; n<=numberOfReplications; n++)
99    {
100        // Accumulate transformation for each replication.
101        osg::ref_ptr<osg::MatrixTransform> transform = new osg::MatrixTransform(accumulatedMatrix);
102        transform->setDataVariance(osg::Object::STATIC);
103
104        // Add transform to parents
105        for (osg::Node::ParentList::iterator itr=parents.begin();
106            itr!=parents.end();
107            ++itr)
108        {
109            (*itr)->addChild(transform.get());
110        }
111
112        // Make primary a child of matrix transform.
113        transform->addChild(&node);
114
115        // Accumulate transform if multiple replications.
116        accumulatedMatrix *= matrix;
117    }
118}
119
120
121///////////////////////////////////////////////////////////////////////////////////
122
123
124osg::Vec3Array* flt::getOrCreateVertexArray(osg::Geometry& geometry)
125{
126    osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry.getVertexArray());
127    if (!vertices)
128    {
129        vertices = new osg::Vec3Array;
130        geometry.setVertexArray(vertices);
131    }
132    return vertices;
133}
134
135osg::Vec3Array* flt::getOrCreateNormalArray(osg::Geometry& geometry)
136{
137    osg::Vec3Array* normals = dynamic_cast<osg::Vec3Array*>(geometry.getNormalArray());
138    if (!normals)
139    {
140        normals = new osg::Vec3Array;
141        geometry.setNormalArray(normals);
142    }
143    return normals;
144}
145
146osg::Vec4Array* flt::getOrCreateColorArray(osg::Geometry& geometry)
147{
148    osg::Vec4Array* colors = dynamic_cast<osg::Vec4Array*>(geometry.getColorArray());
149    if (!colors)
150    {
151        colors = new osg::Vec4Array;
152        geometry.setColorArray(colors);
153    }
154    return colors;
155}
156
157
158osg::Vec2Array* flt::getOrCreateTextureArray(osg::Geometry& geometry, int unit)
159{
160    osg::Vec2Array* UVs = dynamic_cast<osg::Vec2Array*>(geometry.getTexCoordArray(unit));
161    if (!UVs)
162    {
163        UVs = new osg::Vec2Array;
164        geometry.setTexCoordArray(unit,UVs);
165    }
166    return UVs;
167}
Note: See TracBrowser for help on using the browser.