mirror of https://github.com/bjornbytes/lovr.git
Add LOVR_USE_ASSIMP;
lovr.graphics.newModel and lovr.data.newModelData will return nil if assimp is disabled.
This commit is contained in:
parent
7932f3340a
commit
2a17f6e4bb
|
@ -17,6 +17,7 @@ option(LOVR_ENABLE_ENET "Bundle with lua-enet" ON)
|
||||||
option(LOVR_ENABLE_JSON "Bundle with lua-cjson" ON)
|
option(LOVR_ENABLE_JSON "Bundle with lua-cjson" ON)
|
||||||
|
|
||||||
option(LOVR_USE_LUAJIT "Use LuaJIT instead of Lua" ON)
|
option(LOVR_USE_LUAJIT "Use LuaJIT instead of Lua" ON)
|
||||||
|
option(LOVR_USE_ASSIMP "Enable model loading with Assimp" ON)
|
||||||
option(LOVR_USE_OPENVR "Enable the OpenVR backend for the headset module" ON)
|
option(LOVR_USE_OPENVR "Enable the OpenVR backend for the headset module" ON)
|
||||||
option(LOVR_USE_WEBVR "Enable the WebVR backend for the headset module" OFF)
|
option(LOVR_USE_WEBVR "Enable the WebVR backend for the headset module" OFF)
|
||||||
option(LOVR_USE_OCULUS "Enable the LibOVR backend for the headset module (be sure to also set LOVR_OCULUS_PATH to point to the Oculus SDK)" OFF)
|
option(LOVR_USE_OCULUS "Enable the LibOVR backend for the headset module (be sure to also set LOVR_OCULUS_PATH to point to the Oculus SDK)" OFF)
|
||||||
|
@ -90,7 +91,7 @@ if(LOVR_ENABLE_FILESYSTEM)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Assimp
|
# Assimp
|
||||||
if(LOVR_ENABLE_DATA)
|
if(LOVR_ENABLE_DATA AND LOVR_USE_ASSIMP)
|
||||||
if(LOVR_SYSTEM_ASSIMP)
|
if(LOVR_SYSTEM_ASSIMP)
|
||||||
pkg_search_module(ASSIMP REQUIRED assimp)
|
pkg_search_module(ASSIMP REQUIRED assimp)
|
||||||
include_directories(${ASSIMP_INCLUDE_DIRS})
|
include_directories(${ASSIMP_INCLUDE_DIRS})
|
||||||
|
@ -365,6 +366,10 @@ if(LOVR_ENABLE_DATA)
|
||||||
src/lib/stb/stb_truetype.c
|
src/lib/stb/stb_truetype.c
|
||||||
src/lib/stb/stb_vorbis.c
|
src/lib/stb/stb_vorbis.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (LOVR_USE_ASSIMP)
|
||||||
|
add_definitions(-DLOVR_USE_ASSIMP)
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(LOVR_ENABLE_EVENT)
|
if(LOVR_ENABLE_EVENT)
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#ifdef LOVR_USE_ASSIMP
|
||||||
#include <assimp/cfileio.h>
|
#include <assimp/cfileio.h>
|
||||||
#include <assimp/cimport.h>
|
#include <assimp/cimport.h>
|
||||||
#include <assimp/config.h>
|
#include <assimp/config.h>
|
||||||
|
@ -93,6 +95,35 @@ static void assimpNodeTraversal(ModelData* modelData, struct aiNode* assimpNode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void aabbIterator(ModelData* modelData, ModelNode* node, float aabb[6]) {
|
||||||
|
for (int i = 0; i < node->primitives.length; i++) {
|
||||||
|
ModelPrimitive* primitive = &modelData->primitives[node->primitives.data[i]];
|
||||||
|
for (int j = 0; j < primitive->drawCount; j++) {
|
||||||
|
uint32_t index;
|
||||||
|
if (modelData->indexSize == sizeof(uint16_t)) {
|
||||||
|
index = modelData->indices.shorts[primitive->drawStart + j];
|
||||||
|
} else {
|
||||||
|
index = modelData->indices.ints[primitive->drawStart + j];
|
||||||
|
}
|
||||||
|
float vertex[3];
|
||||||
|
VertexPointer vertices = { .raw = modelData->vertexData->blob.data };
|
||||||
|
vec3_init(vertex, (float*) (vertices.bytes + index * modelData->vertexData->format.stride));
|
||||||
|
mat4_transform(node->globalTransform, &vertex[0], &vertex[1], &vertex[2]);
|
||||||
|
aabb[0] = MIN(aabb[0], vertex[0]);
|
||||||
|
aabb[1] = MAX(aabb[1], vertex[0]);
|
||||||
|
aabb[2] = MIN(aabb[2], vertex[1]);
|
||||||
|
aabb[3] = MAX(aabb[3], vertex[1]);
|
||||||
|
aabb[4] = MIN(aabb[4], vertex[2]);
|
||||||
|
aabb[5] = MAX(aabb[5], vertex[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < node->children.length; i++) {
|
||||||
|
ModelNode* child = &modelData->nodes[node->children.data[i]];
|
||||||
|
aabbIterator(modelData, child, aabb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static float readMaterialScalar(struct aiMaterial* assimpMaterial, const char* key, unsigned int type, unsigned int index) {
|
static float readMaterialScalar(struct aiMaterial* assimpMaterial, const char* key, unsigned int type, unsigned int index) {
|
||||||
float scalar;
|
float scalar;
|
||||||
if (aiGetMaterialFloatArray(assimpMaterial, key, type, index, &scalar, NULL) == aiReturn_SUCCESS) {
|
if (aiGetMaterialFloatArray(assimpMaterial, key, type, index, &scalar, NULL) == aiReturn_SUCCESS) {
|
||||||
|
@ -517,6 +548,12 @@ ModelData* lovrModelDataCreate(Blob* blob) {
|
||||||
aiReleaseImport(scene);
|
aiReleaseImport(scene);
|
||||||
return modelData;
|
return modelData;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
static void aabbIterator(ModelData* modelData, ModelNode* node, float aabb[6]) {}
|
||||||
|
ModelData* lovrModelDataCreate(Blob* blob) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
ModelData* lovrModelDataCreateEmpty() {
|
ModelData* lovrModelDataCreateEmpty() {
|
||||||
return lovrAlloc(ModelData, lovrModelDataDestroy);
|
return lovrAlloc(ModelData, lovrModelDataDestroy);
|
||||||
|
@ -570,35 +607,6 @@ void lovrModelDataDestroy(void* ref) {
|
||||||
free(modelData);
|
free(modelData);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void aabbIterator(ModelData* modelData, ModelNode* node, float aabb[6]) {
|
|
||||||
for (int i = 0; i < node->primitives.length; i++) {
|
|
||||||
ModelPrimitive* primitive = &modelData->primitives[node->primitives.data[i]];
|
|
||||||
for (int j = 0; j < primitive->drawCount; j++) {
|
|
||||||
uint32_t index;
|
|
||||||
if (modelData->indexSize == sizeof(uint16_t)) {
|
|
||||||
index = modelData->indices.shorts[primitive->drawStart + j];
|
|
||||||
} else {
|
|
||||||
index = modelData->indices.ints[primitive->drawStart + j];
|
|
||||||
}
|
|
||||||
float vertex[3];
|
|
||||||
VertexPointer vertices = { .raw = modelData->vertexData->blob.data };
|
|
||||||
vec3_init(vertex, (float*) (vertices.bytes + index * modelData->vertexData->format.stride));
|
|
||||||
mat4_transform(node->globalTransform, &vertex[0], &vertex[1], &vertex[2]);
|
|
||||||
aabb[0] = MIN(aabb[0], vertex[0]);
|
|
||||||
aabb[1] = MAX(aabb[1], vertex[0]);
|
|
||||||
aabb[2] = MIN(aabb[2], vertex[1]);
|
|
||||||
aabb[3] = MAX(aabb[3], vertex[1]);
|
|
||||||
aabb[4] = MIN(aabb[4], vertex[2]);
|
|
||||||
aabb[5] = MAX(aabb[5], vertex[2]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < node->children.length; i++) {
|
|
||||||
ModelNode* child = &modelData->nodes[node->children.data[i]];
|
|
||||||
aabbIterator(modelData, child, aabb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void lovrModelDataGetAABB(ModelData* modelData, float aabb[6]) {
|
void lovrModelDataGetAABB(ModelData* modelData, float aabb[6]) {
|
||||||
aabb[0] = FLT_MAX;
|
aabb[0] = FLT_MAX;
|
||||||
aabb[1] = -FLT_MAX;
|
aabb[1] = -FLT_MAX;
|
||||||
|
|
Loading…
Reference in New Issue