287 lines
5.9 KiB
C++
287 lines
5.9 KiB
C++
/*
|
|
以下命名空间定义了 TreeJs所支持的Json文件格式所对应的C++结构
|
|
|
|
https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3
|
|
|
|
请注意上面的网址,里面说明了如何解析faces数组
|
|
|
|
RvtVa3c 插件中导出的结构如下:
|
|
|
|
{
|
|
Type bitmask = 0 (TRIANGLE)
|
|
例如:
|
|
0,[vertex-0][vertex-1][vertex-2]
|
|
}
|
|
|
|
*/
|
|
#pragma once
|
|
|
|
#include <set>
|
|
#include <vector>
|
|
#include <map>
|
|
|
|
// 初始化一个可索引类的构造函数,std::wstring uuid 为此类的索引成员
|
|
#define CONSTRUCT_3JS_CLASS(classname) \
|
|
classname(std::wstring &id): uuid(id) {}\
|
|
classname(){}
|
|
|
|
namespace ThreeJs
|
|
{
|
|
/* Type Bitmask
|
|
|
|
0 bit: 0 = triangle (3 indices), 1 = quad (4 indices)
|
|
1 bit: 0 = no face material, 1 = face material (1 index)
|
|
2 bit: 0 = no face uvs, 1 = face uvs (1 index)
|
|
3 bit: 0 = no face vertex uvs, 1 = face vertex uvs (3 indices or 4 indices)
|
|
4 bit: 0 = no face normal, 1 = face normal (1 index)
|
|
5 bit: 0 = no face vertex normals, 1 = face vertex normals (3 indices or 4 indices)
|
|
6 bit: 0 = no face color, 1 = face color (1 index)
|
|
7 bit: 0 = no face vertex colors, 1 = face vertex colors (3 indices or 4 indices)
|
|
*/
|
|
|
|
const unsigned QUAD = 0x01;
|
|
const unsigned FACE_MATERIAL = 0x02;
|
|
const unsigned FACE_UV = 0x04;
|
|
const unsigned FACE_VERTEX_UV = 0x08;
|
|
const unsigned FACE_NORMAL = 0x10;
|
|
const unsigned FACE_VERTEX_NORMAL = 0x20;
|
|
const unsigned FACE_COLOR = 0x40;
|
|
const unsigned FACE_VERTEX_COLOR = 0x80;
|
|
|
|
/* DEMO: bitmask 为 11 11 11 11 时 一个 face 在 faces 内的表述
|
|
// 255,
|
|
// [vertex_index, vertex_index, vertex_index, vertex_index],
|
|
// [material_index],
|
|
// [face_uv],
|
|
// [face_vertex_uv, face_vertex_uv, face_vertex_uv, face_vertex_uv],
|
|
// [face_normal],
|
|
// [face_vertex_normal, face_vertex_normal, face_vertex_normal, face_vertex_normal],
|
|
// [face_color]
|
|
// [face_vertex_color, face_vertex_color, face_vertex_color, face_vertex_color],
|
|
*/
|
|
|
|
const std::wstring THREEJS_GEOMETRIES(_T("geometries"));
|
|
const std::wstring THREEJS_OBJECT(_T("object"));
|
|
const std::wstring THREEJS_METADATA(_T("metadata"));
|
|
const std::wstring THREEJS_MATERIALS(_T("materials"));
|
|
|
|
typedef std::vector<double> MATRIX;
|
|
typedef std::vector<int> FACES;
|
|
|
|
template <typename T>
|
|
struct compare_uuid
|
|
{
|
|
bool operator () (const T &a, const T &b) const
|
|
{
|
|
return a.uuid < b.uuid;
|
|
}
|
|
};
|
|
|
|
// 一个geometyrs数组由多个Geometry结构组成
|
|
/*
|
|
"geometrys": [
|
|
{
|
|
"uuid": "",
|
|
"type": "",
|
|
"data": {
|
|
"vertices": [],
|
|
"normals": [],
|
|
"edges": [],
|
|
"uvs": [],
|
|
"faces": [],
|
|
"scale": 1.0,
|
|
"visible": true,
|
|
"castShadow": true,
|
|
"receiveShadow": false,
|
|
"doubleSided": true
|
|
}
|
|
},
|
|
]
|
|
*/
|
|
struct Geometry
|
|
{
|
|
struct Data
|
|
{
|
|
std::vector<double> vertices, normals, uvs, edges;
|
|
FACES faces;
|
|
double scale;
|
|
bool visible, castShadow, receiveShadow, doubleSided;
|
|
void clear()
|
|
{
|
|
vertices.clear();
|
|
normals.clear();
|
|
edges.clear();
|
|
uvs.clear();
|
|
faces.clear();
|
|
}
|
|
};
|
|
CONSTRUCT_3JS_CLASS(Geometry)
|
|
std::wstring uuid, type;
|
|
Data data;
|
|
};
|
|
typedef std::set<Geometry, compare_uuid<Geometry> > GeometrySet;
|
|
|
|
// 一个rvtJson文件内,只有一个Object对象,内部有多个child,child内部有多个subChild
|
|
/*
|
|
"object":{
|
|
"uuid": "",
|
|
"name": "",
|
|
"type": "",
|
|
"matrix":[],
|
|
"children":[
|
|
{
|
|
"uuid": "",
|
|
"name": "",
|
|
"type": "",
|
|
"matrix": [],
|
|
"children": [
|
|
{
|
|
"uuid": "",
|
|
"name": "",
|
|
"type": "",
|
|
"matrix": [ ],
|
|
"geometry": "",
|
|
"material": ""
|
|
},
|
|
],
|
|
"material": "",
|
|
"userdata": {
|
|
}
|
|
},
|
|
]
|
|
}
|
|
*/
|
|
struct Object
|
|
{
|
|
struct Child
|
|
{
|
|
struct SubChild
|
|
{
|
|
CONSTRUCT_3JS_CLASS(SubChild)
|
|
std::wstring uuid, name, type, geometry, material;
|
|
MATRIX matrix;
|
|
void clear() { matrix.clear(); }
|
|
};
|
|
typedef std::set<SubChild, compare_uuid<SubChild> > SubChildSet;
|
|
CONSTRUCT_3JS_CLASS(Child)
|
|
std::wstring uuid, name, type, material;
|
|
MATRIX matrix;
|
|
SubChildSet children;
|
|
std::map<std::wstring, std::wstring> userData;
|
|
void clear()
|
|
{
|
|
userData.clear();
|
|
matrix.clear();
|
|
children.clear();
|
|
}
|
|
};
|
|
typedef std::set<Child, compare_uuid<Child> > ChildSet;
|
|
|
|
std::wstring uuid, name, type;
|
|
MATRIX matrix;
|
|
ChildSet children;
|
|
};
|
|
|
|
// 一个materials数组由多个material组成
|
|
/*
|
|
"materials": [
|
|
{
|
|
"uuid": "",
|
|
"name": "",
|
|
"type": "",
|
|
"color": 8421504,
|
|
"ambient": 8421504,
|
|
"emissive": 0,
|
|
"specular": 8421504,
|
|
"shininess": 1,
|
|
"opacity": 1.0,
|
|
"transparent": false,
|
|
"wireframe": false
|
|
},
|
|
]
|
|
*/
|
|
struct Material
|
|
{
|
|
std::wstring uuid, name, type;
|
|
int color, ambient, specular, emissive, shininess;
|
|
double opacity;
|
|
bool transparent, wireframe;
|
|
};
|
|
typedef std::set<Material, compare_uuid<Material> > MaterialSet;
|
|
|
|
|
|
// 唯一一个
|
|
/*
|
|
"metadata": {
|
|
"type": "",
|
|
"version": 4.3,
|
|
"generator": ""
|
|
}
|
|
*/
|
|
struct Metadata
|
|
{
|
|
std::wstring type, generator;
|
|
double version;
|
|
};
|
|
|
|
}
|
|
|
|
namespace ShJs
|
|
{
|
|
/* 上华简化Js文件格式如下
|
|
{
|
|
"objs":[
|
|
{
|
|
"obj":{
|
|
"id":"",
|
|
"name":"",
|
|
"type":"",
|
|
"userData": {
|
|
},
|
|
"faces":[
|
|
],
|
|
"vertices": [
|
|
],
|
|
"edges": [
|
|
],
|
|
}
|
|
},
|
|
{},
|
|
]
|
|
}
|
|
*/
|
|
|
|
const std::wstring SHJS_OBJECTS(_T("objects"));
|
|
const std::wstring SHJS_PTLD(_T("ptLeftDown"));
|
|
|
|
const std::wstring SHJS_LENGTH(_T("length"));
|
|
const std::wstring SHJS_HEIGHT(_T("height"));
|
|
const std::wstring SHJS_WIDTH(_T("width"));
|
|
|
|
const std::wstring SHJS_OBJECT_ID(_T("id"));
|
|
const std::wstring SHJS_OBJECT_NAME(_T("name"));
|
|
const std::wstring SHJS_OBJECT_TYPE(_T("type"));
|
|
const std::wstring SHJS_OBJECT_USERDATA(_T("userData"));
|
|
const std::wstring SHJS_OBJECT_FACES(_T("faces"));
|
|
const std::wstring SHJS_OBJECT_EDGES(_T("edges"));
|
|
const std::wstring SHJS_OBJECT_VERTICES(_T("vertices"));
|
|
|
|
struct Object
|
|
{
|
|
std::wstring id, name, type;
|
|
std::vector<double> vertices, edges;
|
|
std::vector<int> faces;
|
|
std::map<std::wstring, std::wstring> userData;
|
|
void clear()
|
|
{
|
|
userData.clear();
|
|
faces.clear();
|
|
edges.clear();
|
|
vertices.clear();
|
|
}
|
|
};
|
|
|
|
typedef std::vector<Object> Objects;
|
|
|
|
}
|