Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
opt_loader.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef OPT_LOADER_H
20 #define OPT_LOADER_H
21 
22 #include <map>
23 #include <vector>
24 #include "../types.h"
25 #include "../loader.h"
26 
27 namespace smlt {
28 
29 class SubMesh;
30 
31 namespace loaders {
32 
33 typedef int32_t Offset;
34 
35 class OPTLoader : public Loader {
36 public:
37  OPTLoader(const Path& filename, std::shared_ptr<std::istream> data):
38  Loader(filename, data) {}
39 
40  void into(Loadable& resource, const LoaderOptions& options=LoaderOptions());
41 
42 private:
43  void read_block(std::istream& file, Offset offset);
44  void process_vertex_block(std::istream& file);
45  void process_texcoord_block(std::istream& file);
46  void process_normal_block(std::istream& file);
47  void process_face_block(std::istream& file);
48  void process_reused_texture_block(std::istream& file);
49  void process_lod_block(std::istream& file);
50  void process_embedded_texture_block(std::istream& file);
51 
52  Offset global_offset;
53 
54  /* Temp Data storage */
55  std::vector<Vec3> vertices;
56  std::vector<Vec2> texture_vertices;
57  std::vector<Vec3> vertex_normals;
58 
59  struct Texture {
60  std::string name;
61  int32_t width;
62  int32_t height;
63  int32_t bytes_per_pixel;
64  std::vector<uint8_t> data;
65  };
66  std::vector<Texture> textures;
67  std::string current_texture;
68 
69  std::map<std::string, SubMesh*> texture_submesh;
70  std::map<std::string, TextureID> texture_name_to_id;
71 
72  struct Triangle {
73  Vec3 positions[3];
74  Vec2 tex_coords[3];
75  Vec3 normals[3];
76 
77  Vec3 face_normal;
78  std::string texture_name;
79  };
80  std::vector<std::vector<Triangle> > triangles; //Triangles for each LOD
81  uint8_t current_lod;
82 };
83 
84 class OPTLoaderType : public LoaderType {
85 public:
86  const char* name() { return "opt"; }
87  bool supports(const Path& filename) const {
88  //FIXME: check magic
89  return filename.ext() == ".opt";
90  }
91 
92  Loader::ptr loader_for(const Path& filename, std::shared_ptr<std::istream> data) const {
93  return Loader::ptr(new OPTLoader(filename, data));
94  }
95 };
96 
97 
98 }
99 }
100 
101 #endif // OPT_LOADER_H
smlt::Vec3
Definition: vec3.h:23
smlt
Definition: animation.cpp:25
smlt::LoaderType
Definition: loader.h:111
smlt::Loader
Definition: loader.h:66
smlt::loaders::OPTLoader
Definition: opt_loader.h:35
smlt::Path
Definition: path.h:7
smlt::loaders::OPTLoaderType
Definition: opt_loader.h:84
smlt::Loadable
Definition: loadable.h:26
smlt::Vec2
Definition: vec2.h:14