File: corona_loader.cpp

package info (click to toggle)
embree 3.13.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,924 kB
  • sloc: cpp: 180,815; xml: 3,877; ansic: 2,957; python: 1,466; sh: 502; makefile: 229; csh: 42
file content (299 lines) | stat: -rw-r--r-- 10,912 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "corona_loader.h"
#include "xml_parser.h"
#include "obj_loader.h"

namespace embree
{
  class CoronaLoader
  {
  public:

    static Ref<SceneGraph::Node> load(const FileName& fileName, const AffineSpace3fa& space);
    CoronaLoader(const FileName& fileName, const AffineSpace3fa& space);

  private:
    template<typename T> T load(const Ref<XML>& xml) { assert(false); return T(zero); }
    Ref<SceneGraph::MaterialNode> loadMaterial(const Ref<XML>& xml);
    void  loadMaterialDefinition(const Ref<XML>& xml);
    std::shared_ptr<Texture> loadMap(const Ref<XML>& xml);
    void  loadMapDefinition(const Ref<XML>& xml);
    Ref<SceneGraph::Node> loadMaterialLibrary(const FileName& fileName);
    Ref<SceneGraph::Node> loadObject(const Ref<XML>& xml);
    std::pair<Ref<SceneGraph::MaterialNode>, avector<AffineSpace3fa> > loadInstances(const Ref<XML>& xml);
    Ref<SceneGraph::Node> loadGroupNode(const Ref<XML>& xml);
    Ref<SceneGraph::Node> loadNode(const Ref<XML>& xml);

  private:
    FileName path; 
    std::map<std::string,Ref<SceneGraph::MaterialNode> > materialMap; 
    std::map<std::string,std::shared_ptr<Texture>> textureMap; 
    std::map<std::string,std::shared_ptr<Texture>> textureFileMap; 
  public:
    Ref<SceneGraph::Node> root;
  };

  template<> FileName CoronaLoader::load<FileName>(const Ref<XML>& xml) 
  {
    if (xml->body.size() != 1) THROW_RUNTIME_ERROR(xml->loc.str()+": wrong FileName body");
    return xml->body[0].Identifier();
  }

  template<> std::string CoronaLoader::load<std::string>(const Ref<XML>& xml) 
  {
    if (xml->body.size() != 1) THROW_RUNTIME_ERROR(xml->loc.str()+": wrong string body");
    return xml->body[0].Identifier();
  }

  template<> int CoronaLoader::load<int>(const Ref<XML>& xml) {
    if (xml->body.size() < 1) THROW_RUNTIME_ERROR(xml->loc.str()+": wrong int body");
    return xml->body[0].Int();
  }

  template<> float CoronaLoader::load<float>(const Ref<XML>& xml) {
    if (xml->body.size() < 1) THROW_RUNTIME_ERROR(xml->loc.str()+": wrong float body");
    return xml->body[0].Float();
  }

  template<> Vec3f CoronaLoader::load<Vec3f>(const Ref<XML>& xml) {
    if (xml->body.size() < 3) THROW_RUNTIME_ERROR(xml->loc.str()+": wrong float3 body");
    return Vec3f(xml->body[0].Float(),xml->body[1].Float(),xml->body[2].Float());
  }

  template<> Vec3fa CoronaLoader::load<Vec3fa>(const Ref<XML>& xml) {
    if (xml->body.size() < 3) THROW_RUNTIME_ERROR(xml->loc.str()+": wrong float3 body");
    return Vec3fa(xml->body[0].Float(),xml->body[1].Float(),xml->body[2].Float());
  }

  template<> AffineSpace3fa CoronaLoader::load<AffineSpace3fa>(const Ref<XML>& xml) 
  {
    if (xml->body.size() != 12) THROW_RUNTIME_ERROR(xml->loc.str()+": wrong AffineSpace body");
    return AffineSpace3fa(LinearSpace3fa(xml->body[0].Float(),xml->body[1].Float(),xml->body[ 2].Float(),
                                         xml->body[4].Float(),xml->body[5].Float(),xml->body[ 6].Float(),
                                         xml->body[8].Float(),xml->body[9].Float(),xml->body[10].Float()),
                          Vec3fa(xml->body[3].Float(),xml->body[7].Float(),xml->body[11].Float()));
  }

  Ref<SceneGraph::MaterialNode> CoronaLoader::loadMaterial(const Ref<XML>& xml) 
  {
    if (xml->name != "material") 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid material: "+xml->name);

    /* native material */
    if (xml->parm("class") == "Native") 
    {
      /* we convert into an OBJ material */
      Ref<OBJMaterial> objmaterial = new OBJMaterial;
      for (auto child : xml->children)
      {
        if (child->name == "diffuse") {
          objmaterial->Kd = load<Vec3fa>(child);
          if (child->children.size() && child->children[0]->name == "map")
            objmaterial->_map_Kd = loadMap(child->children[0]);
        }
        else if (child->name == "reflect") {
          objmaterial->Ks = load<Vec3fa>(child->child("color"));
          objmaterial->Ni = load<float >(child->child("ior"));
          objmaterial->Ns = load<float >(child->child("glossiness"));
        }
        else if (child->name == "translucency") {
          objmaterial->Kt = load<Vec3fa>(child->child("color"));
        }
        else if (child->name == "opacity") {
          objmaterial->d = load<Vec3fa>(child).x;
          if (child->children.size() && child->children[0]->name == "map")
            objmaterial->_map_d = loadMap(child->children[0]);
        }
      }
      return objmaterial.dynamicCast<SceneGraph::MaterialNode>();
    }

    /* reference by name */
    else if (xml->parm("class") == "Reference") 
    {
      const std::string name = load<std::string>(xml);
      return materialMap[name];
    }

    /* else return default material */
    else 
      return new OBJMaterial;
  }

  void CoronaLoader::loadMaterialDefinition(const Ref<XML>& xml) 
  {
    if (xml->name != "materialDefinition") 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid material definition: "+xml->name);
    if (xml->children.size() != 1) 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid material definition");

    const std::string name = xml->parm("name");
    materialMap[name] = loadMaterial(xml->children[0]);
  }

  std::shared_ptr<Texture> CoronaLoader::loadMap(const Ref<XML>& xml) 
  {
    /* process map node */
    if (xml->name == "map")
    {
      std::string mapClass = xml->parm("class");

      /* load textures */
      if (mapClass == "Texture")
      {
        const FileName src = load<FileName>(xml->child("image"));
        
        /* load images only once */
        if (textureFileMap.find(src) != textureFileMap.end())
          return textureFileMap[src];
        
        try {
          return Texture::load(path+src);
        } catch (const std::runtime_error& e) {
          std::cerr << "failed to load " << path+src << ": " << e.what() << std::endl;
        }
      }
      else if (mapClass == "Reference") {
        const std::string name = load<std::string>(xml);
        return textureMap[name];
      }
    }

    /* recurse into every unknown node to find some texture */
    for (auto child : xml->children) {
      std::shared_ptr<Texture> texture = loadMap(child);
      if (texture) return texture;
    }
    return std::shared_ptr<Texture>();
  }

  void CoronaLoader::loadMapDefinition(const Ref<XML>& xml) 
  {
    if (xml->name != "mapDefinition") 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid map definition: "+xml->name);
    if (xml->children.size() != 1) 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid map definition");

    const std::string name = xml->parm("name");
    std::shared_ptr<Texture> texture = loadMap(xml->children[0]);
    if (texture) textureMap[name] = texture;
  }

  Ref<SceneGraph::Node> CoronaLoader::loadMaterialLibrary(const FileName& fileName) 
  {
    Ref<XML> xml = parseXML(path+fileName,"/.-",false);
    if (xml->name != "mtlLib") 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid material library");
    
    for (auto child : xml->children)
    {
      if (child->name == "materialDefinition") {
        loadMaterialDefinition(child);
      }
      else if (child->name == "mapDefinition")
        loadMapDefinition(child);
    }

    return nullptr;
  }

  Ref<SceneGraph::Node> CoronaLoader::loadObject(const Ref<XML>& xml) 
  {
    if (xml->name != "object") 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid object node");
    if (xml->parm("class") != "file")
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid object class");
    const FileName fileName = load<FileName>(xml);
    return SceneGraph::load(path+fileName);
  }

  std::pair<Ref<SceneGraph::MaterialNode>, avector<AffineSpace3fa> > CoronaLoader::loadInstances(const Ref<XML>& xml) 
  {
    if (xml->name != "instance") 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid instance node");

    /* create default material */
    Ref<SceneGraph::MaterialNode> material = new OBJMaterial;

    avector<AffineSpace3fa> xfms;
    for (size_t i=0; i<xml->children.size(); i++)
    {
      Ref<XML> child = xml->children[i];
      if      (child->name == "material" ) material = loadMaterial(child);
      else if (child->name == "transform") xfms.push_back(load<AffineSpace3fa>(child));
      else THROW_RUNTIME_ERROR(child->loc.str()+": unknown node: "+child->name);
    }

    return std::make_pair(material,xfms);
  }

  Ref<SceneGraph::Node> CoronaLoader::loadGroupNode(const Ref<XML>& xml) 
  {
    if (xml->children.size() < 1) 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid group node");

    /* load instances */
    auto p = loadInstances(xml->children[0]);
    Ref<SceneGraph::MaterialNode> material = p.first;
    avector<AffineSpace3fa>& xfms = p.second;
    
    /* load meshes */
    Ref<SceneGraph::GroupNode> objects = new SceneGraph::GroupNode;
    for (size_t i=1; i<xml->children.size(); i++)
      objects->add(loadObject(xml->children[i]));
    
    /* force material */
    objects->setMaterial(material);

    /* create instances */
    Ref<SceneGraph::GroupNode> instances = new SceneGraph::GroupNode;
    for (size_t i=0; i<xfms.size(); i++) 
      instances->add(new SceneGraph::TransformNode(xfms[i],objects.cast<SceneGraph::Node>()));

    return instances.cast<SceneGraph::Node>();
  }
  
  Ref<SceneGraph::Node> CoronaLoader::loadNode(const Ref<XML>& xml)
  {
    if      (xml->name == "conffile"     ) return nullptr;
    else if (xml->name == "mtllib"       ) return loadMaterialLibrary(load<FileName>(xml));
    else if (xml->name == "camera"       ) return nullptr;
    else if (xml->name == "environment"  ) return nullptr;
    else if (xml->name == "geometryGroup") return loadGroupNode(xml);
    else if (xml->name == "renderElement") return nullptr;
    else THROW_RUNTIME_ERROR(xml->loc.str()+": unknown tag: "+xml->name);
    return nullptr;
  }

  Ref<SceneGraph::Node> CoronaLoader::load(const FileName& fileName, const AffineSpace3fa& space) {
    CoronaLoader loader(fileName,space); return loader.root;
  }

  CoronaLoader::CoronaLoader(const FileName& fileName, const AffineSpace3fa& space)
  {
    path = fileName.path();
    Ref<XML> xml = parseXML(fileName,"/.-",false);
    if (xml->name == "scene") 
    {
      Ref<SceneGraph::GroupNode> group = new SceneGraph::GroupNode;
      for (size_t i=0; i<xml->children.size(); i++) { 
        group->add(loadNode(xml->children[i]));
      }
      root = group.cast<SceneGraph::Node>();
    }
    else 
      THROW_RUNTIME_ERROR(xml->loc.str()+": invalid scene tag");

    if (space == AffineSpace3fa(one)) 
      return;
    
    root = new SceneGraph::TransformNode(space,root);
  }

  /*! read from disk */
  Ref<SceneGraph::Node> SceneGraph::loadCorona(const FileName& fileName, const AffineSpace3fa& space) {
    return CoronaLoader::load(fileName,space);
  }
}