Edited MeshAssimp and added gltf_viewer sample code

This commit is contained in:
magicwhale
2018-11-19 09:27:08 -08:00
committed by magicwhale
parent 84d2fb222b
commit bc38c8a773
7 changed files with 587 additions and 9 deletions
+229
View File
@@ -0,0 +1,229 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <memory>
#include <map>
#include <string>
#include <vector>
#include <getopt/getopt.h>
#include <utils/Path.h>
#include <filament/Engine.h>
#include <filament/LightManager.h>
#include <filament/Material.h>
#include <filament/MaterialInstance.h>
#include <filament/RenderableManager.h>
#include <filament/TransformManager.h>
#include <filament/Scene.h>
#include <filament/Texture.h>
#include <filament/IndirectLight.h>
#include <math/mat3.h>
#include <math/mat4.h>
#include <math/vec3.h>
#include "app/Config.h"
#include "app/FilamentApp.h"
#include "app/MeshAssimp.h"
#include <stb_image.h>
#include <utils/EntityManager.h>
#include <filamat/MaterialBuilder.h>
using namespace math;
using namespace filament;
using namespace filamat;
using namespace utils;
static std::vector<Path> g_filenames;
static std::map<std::string, MaterialInstance*> g_materialInstances;
static std::unique_ptr<MeshAssimp> g_meshSet;
static const Material* g_material;
static Entity g_light;
static Texture* g_metallicMap = nullptr;
static Texture* g_roughnessMap = nullptr;
static Texture* g_aoMap = nullptr;
static Texture* g_normalMap = nullptr;
static Texture* g_baseColorMap = nullptr;
static Config g_config;
static struct PbrConfig {
std::string materialDir;
bool clearCoat = false;
bool anisotropy = false;
} g_pbrConfig;
static void printUsage(char* name) {
std::string exec_name(Path(name).getName());
std::string usage(
"SAMPLE_PBR is an example of loading PBR assets with base color + packed metallic/roughness\n"
"Usage:\n"
" SAMPLE_PBR [options] <OBJ/FBX/COLLADA>\n"
"Options:\n"
" --help, -h\n"
" Prints this message\n\n"
" --ibl=<path to cmgen IBL>, -i <path>\n"
" Applies an IBL generated by cmgen's deploy option\n\n"
" --split-view, -v\n"
" Splits the window into 4 views\n\n"
" --scale=[number], -s [number]\n"
" Applies uniform scale\n\n"
" --material=<path>, -m <path>\n"
" Directory containing the textures named <DIR_NAME>_*.png where * is:\n"
" - AO\n"
" - Color\n"
" - Metallic\n"
" - Normal\n"
" - Roughness\n"
"\n"
" --clear-coat, -c\n"
" Add a clear coat layer to the material\n\n"
" --anisotropy, -a\n"
" Enable anisotropy on the material\n\n"
);
const std::string from("SAMPLE_PBR");
for (size_t pos = usage.find(from); pos != std::string::npos; pos = usage.find(from, pos)) {
usage.replace(pos, from.length(), exec_name);
}
std::cout << usage;
}
static int handleCommandLineArgments(int argc, char* argv[], Config* config) {
static constexpr const char* OPTSTR = "hi:vs:m:ca";
static const struct option OPTIONS[] = {
{ "help", no_argument, nullptr, 'h' },
{ "ibl", required_argument, nullptr, 'i' },
{ "split-view", no_argument, nullptr, 'v' },
{ "scale", required_argument, nullptr, 's' },
{ "material", required_argument, nullptr, 'm' },
{ "clear-coat", no_argument, nullptr, 'c' },
{ "anisotropy", no_argument, nullptr, 'a' },
{ 0, 0, 0, 0 } // termination of the option list
};
int opt;
int option_index = 0;
while ((opt = getopt_long(argc, argv, OPTSTR, OPTIONS, &option_index)) >= 0) {
std::string arg(optarg ? optarg : "");
switch (opt) {
default:
case 'h':
printUsage(argv[0]);
exit(0);
case 'i':
config->iblDirectory = arg;
break;
case 's':
try {
config->scale = std::stof(arg);
} catch (std::invalid_argument& e) {
// keep scale of 1.0
} catch (std::out_of_range& e) {
// keep scale of 1.0
}
break;
case 'v':
config->splitView = true;
break;
case 'm':
g_pbrConfig.materialDir = arg;
break;
case 'c':
g_pbrConfig.clearCoat = true;
break;
case 'a':
g_pbrConfig.anisotropy = true;
break;
}
}
return optind;
}
static void cleanup(Engine* engine, View* view, Scene* scene) {
for (auto& item : g_materialInstances) {
auto materialInstance = item.second;
engine->destroy(materialInstance);
}
g_meshSet.reset(nullptr);
engine->destroy(g_material);
engine->destroy(g_baseColorMap);
engine->destroy(g_metallicMap);
engine->destroy(g_roughnessMap);
engine->destroy(g_aoMap);
engine->destroy(g_normalMap);
EntityManager& em = EntityManager::get();
engine->destroy(g_light);
em.destroy(g_light);
}
static void setup(Engine* engine, View* view, Scene* scene) {
Path path(g_pbrConfig.materialDir);
std::string name(path.getName());
g_meshSet = std::make_unique<MeshAssimp>(*engine);
for (auto& filename : g_filenames) {
g_meshSet->addFromFile(filename, g_materialInstances, false);
}
auto& rcm = engine->getRenderableManager();
auto& tcm = engine->getTransformManager();
for (auto renderable : g_meshSet->getRenderables()) {
if (rcm.hasComponent(renderable)) {
auto ti = tcm.getInstance(renderable);
tcm.setTransform(ti, mat4f{ mat3f(g_config.scale), float3(0.0f, 0.0f, -4.0f) } *
tcm.getWorldTransform(ti));
scene->addEntity(renderable);
}
}
g_light = EntityManager::get().create();
LightManager::Builder(LightManager::Type::DIRECTIONAL)
.color(Color::toLinear<ACCURATE>({0.98f, 0.92f, 0.89f}))
.intensity(110000)
.direction({0.6, -1, -0.8})
.build(*engine, g_light);
scene->addEntity(g_light);
}
int main(int argc, char* argv[]) {
int option_index = handleCommandLineArgments(argc, argv, &g_config);
int num_args = argc - option_index;
if (num_args < 1) {
printUsage(argv[0]);
return 1;
}
for (int i = option_index; i < argc; i++) {
utils::Path filename = argv[i];
if (!filename.exists()) {
std::cerr << "file " << argv[option_index] << " not found!" << std::endl;
return 1;
}
g_filenames.push_back(filename);
}
g_config.title = "gltf_viewer";
FilamentApp& filamentApp = FilamentApp::get();
filamentApp.run(g_config, setup, cleanup);
return 0;
}