root/OpenSceneGraph/trunk/src/osgPlugins/bsp/ReaderWriterBSP.cpp @ 13041

Revision 13041, 3.2 kB (checked in by robert, 3 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#include <osg/Node>
2#include <osg/Notify>
3#include <osgDB/Registry>
4#include <osgDB/FileUtils>
5#include <osgDB/ReadFile>
6
7#include "ReaderWriterBSP.h"
8#include "VBSPReader.h"
9#include "Q3BSPReader.h"
10
11using namespace bsp;
12using namespace osg;
13using namespace osgDB;
14
15
16// "VBSP" for Valve BSP files
17const int VBSP_MAGIC_NUMBER  = (('P'<<24)+('S'<<16)+('B'<<8)+'V');
18
19// "IBSP" for id (Quake 3) BSP files
20const int IBSP_MAGIC_NUMBER = (('P'<<24)+('S'<<16)+('B'<<8)+'I');
21
22
23
24const char* ReaderWriterBSP::className() const
25{
26    // Return a description of this class
27    return "BSP File Reader";
28}
29
30
31bool ReaderWriterBSP::acceptsExtension(const std::string& extension) const
32{
33    // If the extension is empty or "bsp", we accept it
34    return osgDB::equalCaseInsensitive(extension, "bsp") || extension.empty();
35}
36
37
38ReaderWriter::ReadResult ReaderWriterBSP::readNode(
39                                  const std::string& file,
40                                  const ReaderWriter::Options* options) const
41{
42    VBSPReader *               vbspReader;
43    Q3BSPReader *              q3bspReader;
44    ref_ptr<Node>              result;
45    osgDB::ifstream            stream;
46    int                        magicNumber;
47    int                        version;
48
49    // See if we handle this kind of file
50    if (!acceptsExtension(osgDB::getFileExtension(file)))
51        return ReadResult::FILE_NOT_HANDLED;
52
53    // See if we can find the requested file
54    std::string fileName = osgDB::findDataFile(file, options);
55    if (fileName.empty())
56        return ReadResult::FILE_NOT_FOUND;
57
58    // Open the file and read the magic number and version
59    stream.open(fileName.c_str(), std::ios::binary);
60    stream.read((char *) &magicNumber, sizeof(int));
61    stream.read((char *) &version, sizeof(int));
62    stream.close();
63
64    // See which kind of BSP file this is
65    if ((magicNumber == VBSP_MAGIC_NUMBER) &&
66        (version >= 19) && (version <= 20))
67    {
68        // Read the Valve file
69        vbspReader = new VBSPReader();
70        if (vbspReader->readFile(fileName))
71        {
72            // Get the results of our read
73            result = vbspReader->getRootNode();
74
75            // Clean up the reader
76            delete vbspReader;
77
78            // Return the results
79            return ReadResult(result.get());
80        }
81        else
82        {
83            // Clean up the reader
84            delete vbspReader;
85
86            // Return the error
87            return ReadResult::ERROR_IN_READING_FILE;
88        }
89    }
90    else if ((magicNumber == IBSP_MAGIC_NUMBER) && (version == 0x2E))
91    {
92        // Read the Quake 3 file
93        q3bspReader = new Q3BSPReader();
94        if (q3bspReader->readFile(file, options))
95        {
96            // Get the results of our read
97            result = q3bspReader->getRootNode();
98
99            // Clean up the reader
100            delete q3bspReader;
101
102            // Return the results
103            return ReadResult(result.get());
104        }
105        else
106        {
107            // Clean up the reader
108            delete q3bspReader;
109
110            // Return the error
111            return ReadResult::ERROR_IN_READING_FILE;
112        }
113    }
114
115    return ReadResult::FILE_NOT_HANDLED;
116}
117
118
119REGISTER_OSGPLUGIN(bsp, ReaderWriterBSP)
Note: See TracBrowser for help on using the browser.