Fixed GLTF bugs, and added a few test models

pull/885/head
Otger 2016-05-10 16:25:15 +02:00
parent a55e31bc72
commit 159af06524
18 changed files with 1343 additions and 15 deletions

View File

@ -355,6 +355,10 @@ namespace glTF
{ return false; }
virtual ~Object() {}
//! Maps special IDs to another ID, where needed. Subclasses may override it (statically)
static const char* TranslateId(Asset& r, const char* id)
{ return id; }
};
@ -484,6 +488,8 @@ namespace glTF
bool IsSpecial() const
{ return mIsSpecial; }
static const char* TranslateId(Asset& r, const char* id);
};

View File

@ -184,6 +184,8 @@ Ref<T> LazyDict<T>::Get(unsigned int i)
template<class T>
Ref<T> LazyDict<T>::Get(const char* id)
{
id = T::TranslateId(mAsset, id);
typename Dict::iterator it = mObjsById.find(id);
if (it != mObjsById.end()) { // already created?
return Ref<T>(mObjs, it->second);
@ -191,7 +193,7 @@ Ref<T> LazyDict<T>::Get(const char* id)
// read it from the JSON object
if (!mDict) {
return Ref<T>(); // section is missing
throw DeadlyImportError("GLTF: Missing section \"" + std::string(mDictId) + "\"");
}
Value::MemberIterator obj = mDict->FindMember(id);
@ -242,6 +244,15 @@ inline Buffer::Buffer()
: byteLength(0), type(Type_arraybuffer), mIsSpecial(false)
{ }
inline const char* Buffer::TranslateId(Asset& r, const char* id)
{
// Compatibility with old spec
if (r.extensionsUsed.KHR_binary_glTF && strcmp(id, "KHR_binary_glTF") == 0) {
return "binary_glTF";
}
return id;
}
inline void Buffer::Read(Value& obj, Asset& r)
{
@ -266,10 +277,16 @@ inline void Buffer::Read(Value& obj, Asset& r)
this->mData.reset(data);
if (statedLength > 0 && this->byteLength != statedLength) {
throw DeadlyImportError("GLTF: buffer length mismatch");
throw DeadlyImportError("GLTF: buffer \"" + id + "\", expected " + std::to_string(statedLength) +
" bytes, but found " + std::to_string(dataURI.dataLength));
}
}
else { // assume raw data
if (statedLength != dataURI.dataLength) {
throw DeadlyImportError("GLTF: buffer \"" + id + "\", expected " + std::to_string(statedLength) +
" bytes, but found " + std::to_string(dataURI.dataLength));
}
this->mData.reset(new uint8_t[dataURI.dataLength]);
memcmp(dataURI.data, this->mData.get(), dataURI.dataLength);
}
@ -589,14 +606,16 @@ inline void Material::Read(Value& material, Asset& r)
else if (strcmp(t, "CONSTANT") == 0) technique = Technique_CONSTANT;
}
ReadMaterialProperty(r, *ext, "ambient", this->ambient);
ReadMaterialProperty(r, *ext, "diffuse", this->diffuse);
ReadMaterialProperty(r, *ext, "specular", this->specular);
if (Value* values = FindObject(*ext, "values")) {
ReadMaterialProperty(r, *values, "ambient", this->ambient);
ReadMaterialProperty(r, *values, "diffuse", this->diffuse);
ReadMaterialProperty(r, *values, "specular", this->specular);
ReadMember(*ext, "doubleSided", doubleSided);
ReadMember(*ext, "transparent", transparent);
ReadMember(*ext, "transparency", transparency);
ReadMember(*ext, "shininess", shininess);
ReadMember(*values, "doubleSided", doubleSided);
ReadMember(*values, "transparent", transparent);
ReadMember(*values, "transparency", transparency);
ReadMember(*values, "shininess", shininess);
}
}
}
}

View File

@ -214,14 +214,14 @@ void glTFImporter::ImportMaterials(glTF::Asset& r)
}
inline void SetFace(aiFace& face, int a)
static inline void SetFace(aiFace& face, int a)
{
face.mNumIndices = 1;
face.mIndices = new unsigned int[1];
face.mIndices[0] = a;
}
inline void SetFace(aiFace& face, int a, int b)
static inline void SetFace(aiFace& face, int a, int b)
{
face.mNumIndices = 2;
face.mIndices = new unsigned int[2];
@ -229,7 +229,7 @@ inline void SetFace(aiFace& face, int a, int b)
face.mIndices[1] = b;
}
inline void SetFace(aiFace& face, int a, int b, int c)
static inline void SetFace(aiFace& face, int a, int b, int c)
{
face.mNumIndices = 3;
face.mIndices = new unsigned int[3];
@ -238,6 +238,18 @@ inline void SetFace(aiFace& face, int a, int b, int c)
face.mIndices[2] = c;
}
static inline bool CheckValidFacesIndices(aiFace* faces, unsigned nFaces, unsigned nVerts)
{
for (unsigned i = 0; i < nFaces; ++i) {
for (unsigned j = 0; j < faces[i].mNumIndices; ++j) {
unsigned idx = faces[i].mIndices[j];
if (idx >= nVerts)
return false;
}
}
return true;
}
void glTFImporter::ImportMeshes(glTF::Asset& r)
{
std::vector<aiMesh*> meshes;
@ -294,6 +306,11 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)
for (size_t tc = 0; tc < attr.texcoord.size() && tc <= AI_MAX_NUMBER_OF_TEXTURECOORDS; ++tc) {
attr.texcoord[tc]->ExtractData(aim->mTextureCoords[tc]);
aim->mNumUVComponents[tc] = attr.texcoord[tc]->GetNumComponents();
aiVector3D* values = aim->mTextureCoords[tc];
for (unsigned int i = 0; i < aim->mNumVertices; ++i) {
values[i].y = 1 - values[i].y; // Flip Y coords
}
}
@ -304,7 +321,7 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)
unsigned int count = prim.indices->count;
Accessor::Indexer data = prim.indices->GetIndexer();
assert(data.IsValid());
ai_assert(data.IsValid());
switch (prim.mode) {
case PrimitiveMode_POINTS: {
@ -369,6 +386,7 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)
if (faces) {
aim->mFaces = faces;
aim->mNumFaces = nFaces;
ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices));
}
}
@ -466,7 +484,7 @@ aiNode* ImportNode(aiScene* pScene, glTF::Asset& r, std::vector<unsigned int>& m
}
}
aiMatrix4x4 matrix = ainode->mTransformation;
aiMatrix4x4& matrix = ainode->mTransformation;
if (node.matrix.isPresent) {
CopyValue(node.matrix.value, matrix);
}

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 840 B

View File

@ -0,0 +1,269 @@
{
"accessors": {
"accessor_21": {
"bufferView": "bufferView_29",
"byteOffset": 0,
"byteStride": 0,
"componentType": 5123,
"count": 36,
"type": "SCALAR"
},
"accessor_23": {
"bufferView": "bufferView_30",
"byteOffset": 0,
"byteStride": 12,
"componentType": 5126,
"count": 24,
"max": [
0.5,
0.5,
0.5
],
"min": [
-0.5,
-0.5,
-0.5
],
"type": "VEC3"
},
"accessor_25": {
"bufferView": "bufferView_30",
"byteOffset": 288,
"byteStride": 12,
"componentType": 5126,
"count": 24,
"max": [
1,
1,
1
],
"min": [
-1,
-1,
-1
],
"type": "VEC3"
},
"accessor_27": {
"bufferView": "bufferView_30",
"byteOffset": 576,
"byteStride": 8,
"componentType": 5126,
"count": 24,
"max": [
6,
1
],
"min": [
0,
0
],
"type": "VEC2"
}
},
"animations": {},
"asset": {
"generator": "collada2gltf@027f74366341d569dea42e9a68b7104cc3892054",
"premultipliedAlpha": true,
"profile": {
"api": "WebGL",
"version": "1.0.2"
},
"version": "1.0"
},
"bufferViews": {
"bufferView_29": {
"buffer": "BoxTextured",
"byteLength": 72,
"byteOffset": 0,
"target": 34963
},
"bufferView_30": {
"buffer": "BoxTextured",
"byteLength": 768,
"byteOffset": 72,
"target": 34962
}
},
"buffers": {
"BoxTextured": {
"byteLength": 840,
"type": "arraybuffer",
"uri": "BoxTextured.bin"
}
},
"extensionsUsed": [
"KHR_materials_common"
],
"images": {
"Image0001": {
"name": "Image0001",
"uri": "CesiumLogoFlat.png"
}
},
"materials": {
"Effect-Texture": {
"extensions": {
"KHR_materials_common": {
"doubleSided": false,
"jointCount": 0,
"technique": "PHONG",
"transparent": false,
"values": {
"diffuse": "texture_Image0001",
"shininess": 256,
"specular": [
0.2,
0.2,
0.2,
1
]
}
}
},
"name": "Texture"
}
},
"meshes": {
"Geometry-mesh002": {
"name": "Mesh",
"primitives": [
{
"attributes": {
"NORMAL": "accessor_25",
"POSITION": "accessor_23",
"TEXCOORD_0": "accessor_27"
},
"indices": "accessor_21",
"material": "Effect-Texture",
"mode": 4
}
]
}
},
"nodes": {
"Geometry-mesh002Node": {
"children": [],
"matrix": [
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1
],
"meshes": [
"Geometry-mesh002"
],
"name": "Mesh"
},
"groupLocator030Node": {
"children": [
"txtrLocator026Node"
],
"matrix": [
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1
],
"name": "Texture_Group"
},
"node_3": {
"children": [
"Geometry-mesh002Node",
"groupLocator030Node"
],
"matrix": [
1,
0,
0,
0,
0,
0,
-1,
0,
0,
1,
0,
0,
0,
0,
0,
1
],
"name": "Y_UP_Transform"
},
"txtrLocator026Node": {
"children": [],
"matrix": [
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1
],
"name": "Cesium_Logo_Flat__Image___Texture_"
}
},
"samplers": {
"sampler_0": {
"magFilter": 9729,
"minFilter": 9987,
"wrapS": 10497,
"wrapT": 10497
}
},
"scene": "defaultScene",
"scenes": {
"defaultScene": {
"nodes": [
"node_3"
]
}
},
"skins": {},
"textures": {
"texture_Image0001": {
"format": 6408,
"internalFormat": 6408,
"sampler": "sampler_0",
"source": "Image0001",
"target": 3553,
"type": 5121
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 840 B

View File

@ -0,0 +1,339 @@
{
"accessors": {
"accessor_21": {
"bufferView": "bufferView_29",
"byteOffset": 0,
"byteStride": 0,
"componentType": 5123,
"count": 36,
"type": "SCALAR"
},
"accessor_23": {
"bufferView": "bufferView_30",
"byteOffset": 0,
"byteStride": 12,
"componentType": 5126,
"count": 24,
"max": [
0.5,
0.5,
0.5
],
"min": [
-0.5,
-0.5,
-0.5
],
"type": "VEC3"
},
"accessor_25": {
"bufferView": "bufferView_30",
"byteOffset": 288,
"byteStride": 12,
"componentType": 5126,
"count": 24,
"max": [
1,
1,
1
],
"min": [
-1,
-1,
-1
],
"type": "VEC3"
},
"accessor_27": {
"bufferView": "bufferView_30",
"byteOffset": 576,
"byteStride": 8,
"componentType": 5126,
"count": 24,
"max": [
6,
1
],
"min": [
0,
0
],
"type": "VEC2"
}
},
"animations": {},
"asset": {
"generator": "collada2gltf@027f74366341d569dea42e9a68b7104cc3892054",
"premultipliedAlpha": true,
"profile": {
"api": "WebGL",
"version": "1.0.2"
},
"version": "1.0"
},
"bufferViews": {
"bufferView_29": {
"buffer": "BoxTextured",
"byteLength": 72,
"byteOffset": 0,
"target": 34963
},
"bufferView_30": {
"buffer": "BoxTextured",
"byteLength": 768,
"byteOffset": 72,
"target": 34962
}
},
"buffers": {
"BoxTextured": {
"byteLength": 840,
"type": "arraybuffer",
"uri": "BoxTextured.bin"
}
},
"images": {
"Image0001": {
"name": "Image0001",
"uri": "CesiumLogoFlat.png"
}
},
"materials": {
"Effect-Texture": {
"name": "Texture",
"technique": "technique0",
"values": {
"diffuse": "texture_Image0001",
"shininess": 256,
"specular": [
0.2,
0.2,
0.2,
1
]
}
}
},
"meshes": {
"Geometry-mesh002": {
"name": "Mesh",
"primitives": [
{
"attributes": {
"NORMAL": "accessor_25",
"POSITION": "accessor_23",
"TEXCOORD_0": "accessor_27"
},
"indices": "accessor_21",
"material": "Effect-Texture",
"mode": 4
}
]
}
},
"nodes": {
"Geometry-mesh002Node": {
"children": [],
"matrix": [
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1
],
"meshes": [
"Geometry-mesh002"
],
"name": "Mesh"
},
"groupLocator030Node": {
"children": [
"txtrLocator026Node"
],
"matrix": [
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1
],
"name": "Texture_Group"
},
"node_3": {
"children": [
"Geometry-mesh002Node",
"groupLocator030Node"
],
"matrix": [
1,
0,
0,
0,
0,
0,
-1,
0,
0,
1,
0,
0,
0,
0,
0,
1
],
"name": "Y_UP_Transform"
},
"txtrLocator026Node": {
"children": [],
"matrix": [
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1
],
"name": "Cesium_Logo_Flat__Image___Texture_"
}
},
"programs": {
"program_0": {
"attributes": [
"a_normal",
"a_position",
"a_texcoord0"
],
"fragmentShader": "BoxTextured0FS",
"vertexShader": "BoxTextured0VS"
}
},
"samplers": {
"sampler_0": {
"magFilter": 9729,
"minFilter": 9987,
"wrapS": 10497,
"wrapT": 10497
}
},
"scene": "defaultScene",
"scenes": {
"defaultScene": {
"nodes": [
"node_3"
]
}
},
"shaders": {
"BoxTextured0FS": {
"type": 35632,
"uri": "BoxTextured0FS.glsl"
},
"BoxTextured0VS": {
"type": 35633,
"uri": "BoxTextured0VS.glsl"
}
},
"skins": {},
"techniques": {
"technique0": {
"attributes": {
"a_normal": "normal",
"a_position": "position",
"a_texcoord0": "texcoord0"
},
"parameters": {
"diffuse": {
"type": 35678
},
"modelViewMatrix": {
"semantic": "MODELVIEW",
"type": 35676
},
"normal": {
"semantic": "NORMAL",
"type": 35665
},
"normalMatrix": {
"semantic": "MODELVIEWINVERSETRANSPOSE",
"type": 35675
},
"position": {
"semantic": "POSITION",
"type": 35665
},
"projectionMatrix": {
"semantic": "PROJECTION",
"type": 35676
},
"shininess": {
"type": 5126
},
"specular": {
"type": 35666
},
"texcoord0": {
"semantic": "TEXCOORD_0",
"type": 35664
}
},
"program": "program_0",
"states": {
"enable": [
2929,
2884
]
},
"uniforms": {
"u_diffuse": "diffuse",
"u_modelViewMatrix": "modelViewMatrix",
"u_normalMatrix": "normalMatrix",
"u_projectionMatrix": "projectionMatrix",
"u_shininess": "shininess",
"u_specular": "specular"
}
}
},
"textures": {
"texture_Image0001": {
"format": 6408,
"internalFormat": 6408,
"sampler": "sampler_0",
"source": "Image0001",
"target": 3553,
"type": 5121
}
}
}

View File

@ -0,0 +1,18 @@
precision highp float;
varying vec3 v_normal;
varying vec2 v_texcoord0;
uniform sampler2D u_diffuse;
uniform vec4 u_specular;
uniform float u_shininess;
void main(void) {
vec3 normal = normalize(v_normal);
vec4 color = vec4(0., 0., 0., 0.);
vec4 diffuse = vec4(0., 0., 0., 1.);
vec4 specular;
diffuse = texture2D(u_diffuse, v_texcoord0);
specular = u_specular;
diffuse.xyz *= max(dot(normal,vec3(0.,0.,1.)), 0.);
color.xyz += diffuse.xyz;
color = vec4(color.rgb * diffuse.a, diffuse.a);
gl_FragColor = color;
}

View File

@ -0,0 +1,15 @@
precision highp float;
attribute vec3 a_position;
attribute vec3 a_normal;
varying vec3 v_normal;
uniform mat3 u_normalMatrix;
uniform mat4 u_modelViewMatrix;
uniform mat4 u_projectionMatrix;
attribute vec2 a_texcoord0;
varying vec2 v_texcoord0;
void main(void) {
vec4 pos = u_modelViewMatrix * vec4(a_position,1.0);
v_normal = u_normalMatrix * a_normal;
v_texcoord0 = a_texcoord0;
gl_Position = u_projectionMatrix * pos;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 648 B

View File

@ -0,0 +1,17 @@
precision highp float;
varying vec3 v_normal;
uniform vec4 u_diffuse;
uniform vec4 u_specular;
uniform float u_shininess;
void main(void) {
vec3 normal = normalize(v_normal);
vec4 color = vec4(0., 0., 0., 0.);
vec4 diffuse = vec4(0., 0., 0., 1.);
vec4 specular;
diffuse = u_diffuse;
specular = u_specular;
diffuse.xyz *= max(dot(normal,vec3(0.,0.,1.)), 0.);
color.xyz += diffuse.xyz;
color = vec4(color.rgb * diffuse.a, diffuse.a);
gl_FragColor = color;
}

View File

@ -0,0 +1,12 @@
precision highp float;
attribute vec3 a_position;
attribute vec3 a_normal;
varying vec3 v_normal;
uniform mat3 u_normalMatrix;
uniform mat4 u_modelViewMatrix;
uniform mat4 u_projectionMatrix;
void main(void) {
vec4 pos = u_modelViewMatrix * vec4(a_position,1.0);
v_normal = u_normalMatrix * a_normal;
gl_Position = u_projectionMatrix * pos;
}

View File

@ -0,0 +1,276 @@
{
"accessors": {
"accessor_21": {
"bufferView": "bufferView_29",
"byteOffset": 0,
"byteStride": 0,
"componentType": 5123,
"count": 36,
"type": "SCALAR"
},
"accessor_23": {
"bufferView": "bufferView_30",
"byteOffset": 0,
"byteStride": 12,
"componentType": 5126,
"count": 24,
"max": [
0.5,
0.5,
0.5
],
"min": [
-0.5,
-0.5,
-0.5
],
"type": "VEC3"
},
"accessor_25": {
"bufferView": "bufferView_30",
"byteOffset": 288,
"byteStride": 12,
"componentType": 5126,
"count": 24,
"max": [
1,
1,
1
],
"min": [
-1,
-1,
-1
],
"type": "VEC3"
}
},
"animations": {},
"asset": {
"generator": "collada2gltf@027f74366341d569dea42e9a68b7104cc3892054",
"premultipliedAlpha": true,
"profile": {
"api": "WebGL",
"version": "1.0.2"
},
"version": "1.0"
},
"bufferViews": {
"bufferView_29": {
"buffer": "Box",
"byteLength": 72,
"byteOffset": 0,
"target": 34963
},
"bufferView_30": {
"buffer": "Box",
"byteLength": 576,
"byteOffset": 72,
"target": 34962
}
},
"buffers": {
"Box": {
"byteLength": 648,
"type": "arraybuffer",
"uri": "Box.bin"
}
},
"materials": {
"Effect-Red": {
"name": "Red",
"technique": "technique0",
"values": {
"diffuse": [
0.8,
0,
0,
1
],
"shininess": 256,
"specular": [
0.2,
0.2,
0.2,
1
]
}
}
},
"meshes": {
"Geometry-mesh002": {
"name": "Mesh",
"primitives": [
{
"attributes": {
"NORMAL": "accessor_25",
"POSITION": "accessor_23"
},
"indices": "accessor_21",
"material": "Effect-Red",
"mode": 4
}
]
}
},
"nodes": {
"BottomBox": {
"children": [],
"matrix": [
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1
],
"meshes": [
"Geometry-mesh002"
],
"name": "BottomBox"
},
"TopBox": {
"children": [],
"matrix": [
0.5,
0,
0,
0,
0,
0.5,
0,
0,
0,
0,
0.5,
0,
0,
0,
1,
1
],
"meshes": [
"Geometry-mesh002"
],
"name": "TopBox"
},
"node_1": {
"children": [
"BottomBox",
"TopBox"
],
"matrix": [
1,
0,
0,
0,
0,
0,
-1,
0,
0,
1,
0,
0,
0,
0,
0,
1
],
"name": "Y_UP_Transform"
}
},
"programs": {
"program_0": {
"attributes": [
"a_normal",
"a_position"
],
"fragmentShader": "Box0FS",
"vertexShader": "Box0VS"
}
},
"scene": "defaultScene",
"scenes": {
"defaultScene": {
"nodes": [
"node_1"
]
}
},
"shaders": {
"Box0FS": {
"type": 35632,
"uri": "Box0FS.glsl"
},
"Box0VS": {
"type": 35633,
"uri": "Box0VS.glsl"
}
},
"skins": {},
"techniques": {
"technique0": {
"attributes": {
"a_normal": "normal",
"a_position": "position"
},
"parameters": {
"diffuse": {
"type": 35666
},
"modelViewMatrix": {
"semantic": "MODELVIEW",
"type": 35676
},
"normal": {
"semantic": "NORMAL",
"type": 35665
},
"normalMatrix": {
"semantic": "MODELVIEWINVERSETRANSPOSE",
"type": 35675
},
"position": {
"semantic": "POSITION",
"type": 35665
},
"projectionMatrix": {
"semantic": "PROJECTION",
"type": 35676
},
"shininess": {
"type": 5126
},
"specular": {
"type": 35666
}
},
"program": "program_0",
"states": {
"enable": [
2929,
2884
]
},
"uniforms": {
"u_diffuse": "diffuse",
"u_modelViewMatrix": "modelViewMatrix",
"u_normalMatrix": "normalMatrix",
"u_projectionMatrix": "projectionMatrix",
"u_shininess": "shininess",
"u_specular": "specular"
}
}
}
}

Binary file not shown.