| 1 | #include <osg/BoundingSphere> |
|---|
| 2 | #include <osg/Geometry> |
|---|
| 3 | #include <osg/Group> |
|---|
| 4 | #include <osg/Object> |
|---|
| 5 | #include <osg/Material> |
|---|
| 6 | #include <osg/MatrixTransform> |
|---|
| 7 | #include <osg/Node> |
|---|
| 8 | #include <osg/Notify> |
|---|
| 9 | #include <osg/StateSet> |
|---|
| 10 | #include <osg/Texture1D> |
|---|
| 11 | #include <osg/Texture2D> |
|---|
| 12 | #include <osg/Texture3D> |
|---|
| 13 | #include <osgDB/Registry> |
|---|
| 14 | #include <osgDB/FileUtils> |
|---|
| 15 | #include <osgDB/ReadFile> |
|---|
| 16 | #include <osg/io_utils> |
|---|
| 17 | #include <iostream> |
|---|
| 18 | #include <string.h> |
|---|
| 19 | |
|---|
| 20 | #include "VVDReader.h" |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | using namespace mdl; |
|---|
| 24 | using namespace osg; |
|---|
| 25 | using namespace osgDB; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | VVDReader::VVDReader() |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | memset(vertex_buffer, 0, sizeof(vertex_buffer)); |
|---|
| 32 | memset(vertex_buffer_size, 0, sizeof(vertex_buffer_size)); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | VVDReader::~VVDReader() |
|---|
| 37 | { |
|---|
| 38 | int i; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | for (i = 0; i < MAX_LODS; i++) |
|---|
| 42 | delete [] vertex_buffer[i]; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | bool VVDReader::readFile(const std::string & file) |
|---|
| 47 | { |
|---|
| 48 | osgDB::ifstream * vvdFile; |
|---|
| 49 | VVDHeader header; |
|---|
| 50 | int vertIndex; |
|---|
| 51 | int i, j; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | vvd_name = getStrippedName(file); |
|---|
| 55 | |
|---|
| 56 | vvdFile = new osgDB::ifstream(file.c_str(), std::ios::binary); |
|---|
| 57 | if (!vvdFile) |
|---|
| 58 | { |
|---|
| 59 | OSG_NOTICE << "Vertex data file not found" << std::endl; |
|---|
| 60 | return false; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | memset(&header, 0xcd, sizeof(VVDHeader)); |
|---|
| 65 | vvdFile->read((char *) &header, sizeof(VVDHeader)); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | if (header.magic_number != VVD_MAGIC_NUMBER) |
|---|
| 69 | { |
|---|
| 70 | OSG_NOTICE << "Vertex data file not valid" << std::endl; |
|---|
| 71 | return false; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | fixup_table = new VVDFixupEntry[header.num_fixups]; |
|---|
| 79 | vvdFile->seekg(header.fixup_table_offset); |
|---|
| 80 | for (i = 0; i < header.num_fixups; i++) |
|---|
| 81 | vvdFile->read((char *) &fixup_table[i], sizeof(VVDFixupEntry)); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | for (i = 0; i < header.num_lods; i++) |
|---|
| 85 | { |
|---|
| 86 | |
|---|
| 87 | vertex_buffer[i] = new VVDVertex[header.num_lod_verts[i]]; |
|---|
| 88 | vertex_buffer_size[i] = header.num_lod_verts[i]; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | if (header.num_fixups > 0) |
|---|
| 92 | { |
|---|
| 93 | |
|---|
| 94 | vertIndex = 0; |
|---|
| 95 | for (j = 0; j < header.num_fixups; j++) |
|---|
| 96 | { |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | if (fixup_table[j].lod_number >= i) |
|---|
| 100 | { |
|---|
| 101 | |
|---|
| 102 | vvdFile->seekg(header.vertex_data_offset + |
|---|
| 103 | fixup_table[j].source_vertex_id * |
|---|
| 104 | sizeof(VVDVertex)); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | vvdFile->read((char *) &vertex_buffer[i][vertIndex], |
|---|
| 108 | fixup_table[j].num_vertices * |
|---|
| 109 | sizeof(VVDVertex)); |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | vertIndex += fixup_table[j].num_vertices; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | else |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | vvdFile->seekg(header.vertex_data_offset); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | vvdFile->read((char *) &vertex_buffer[i][0], |
|---|
| 123 | header.num_lod_verts[i] * sizeof(VVDVertex)); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | for (j = 0; j < vertex_buffer_size[i]; j++) |
|---|
| 128 | vertex_buffer[i][j].vertex_position *= 0.0254; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | vvdFile->close(); |
|---|
| 133 | delete vvdFile; |
|---|
| 134 | |
|---|
| 135 | return true; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | int VVDReader::getNumLODVertices(int lod) |
|---|
| 140 | { |
|---|
| 141 | return vertex_buffer_size[lod]; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | Vec3 VVDReader::getVertex(int lod, int index) |
|---|
| 146 | { |
|---|
| 147 | return vertex_buffer[lod][index].vertex_position; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | Vec3 VVDReader::getNormal(int lod, int index) |
|---|
| 152 | { |
|---|
| 153 | return vertex_buffer[lod][index].vertex_normal; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | Vec2 VVDReader::getTexCoords(int lod, int index) |
|---|
| 158 | { |
|---|
| 159 | return vertex_buffer[lod][index].vertex_texcoord; |
|---|
| 160 | } |
|---|