Files
filament/samples/vk_hellopbr.cpp
Philip Rideout 4278e1bd59 Remove assimp usage from some sample apps.
This shows how resgen can be used for mesh data. It also removes the
MeshAssimp dependency from samples that were meant to be small and
self-contained.
2018-11-28 19:46:37 -08:00

101 lines
3.5 KiB
C++

/*
* 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 <filament/Engine.h>
#include <filament/LightManager.h>
#include <filament/RenderableManager.h>
#include <filament/Scene.h>
#include <filament/TransformManager.h>
#include <filament/View.h>
#include <filameshio/MeshIO.h>
#include "app/Config.h"
#include "app/FilamentApp.h"
#include "generated/resources/resources.h"
using namespace filament;
using namespace math;
using Backend = Engine::Backend;
struct App {
utils::Entity light;
Material* material;
MaterialInstance* materialInstance;
MeshIO::Mesh mesh;
mat4f transform;
};
static const char* IBL_FOLDER = "envs/office";
int main(int argc, char** argv) {
Config config;
config.title = "hellopbr";
config.backend = Backend::VULKAN;
config.iblDirectory = FilamentApp::getRootPath() + IBL_FOLDER;
App app;
auto setup = [config, &app](Engine* engine, View* view, Scene* scene) {
auto& tcm = engine->getTransformManager();
auto& rcm = engine->getRenderableManager();
auto& em = utils::EntityManager::get();
// Instantiate material.
app.material = Material::Builder()
.package(RESOURCES_AIDEFAULTMAT_DATA, RESOURCES_AIDEFAULTMAT_SIZE).build(*engine);
auto mi = app.materialInstance = app.material->createInstance();
mi->setParameter("baseColor", RgbType::LINEAR, float3{0.8});
mi->setParameter("metallic", 1.0f);
mi->setParameter("roughness", 0.4f);
mi->setParameter("reflectance", 0.5f);
// Add geometry into the scene.
app.mesh = MeshIO::loadMeshFromBuffer(engine, RESOURCES_SUZANNE_DATA, nullptr, nullptr, mi);
auto ti = tcm.getInstance(app.mesh.renderable);
app.transform = mat4f{ mat3f(1), float3(0, 0, -4) } * tcm.getWorldTransform(ti);
rcm.setCastShadows(rcm.getInstance(app.mesh.renderable), false);
scene->addEntity(app.mesh.renderable);
// Add light sources into the scene.
app.light = em.create();
LightManager::Builder(LightManager::Type::SUN)
.color(Color::toLinear<ACCURATE>(sRGBColor(0.98f, 0.92f, 0.89f)))
.intensity(110000)
.direction({ 0.7, -1, -0.8 })
.sunAngularRadius(1.9f)
.castShadows(false)
.build(*engine, app.light);
scene->addEntity(app.light);
};
auto cleanup = [&app](Engine* engine, View*, Scene*) {
Fence::waitAndDestroy(engine->createFence());
engine->destroy(app.light);
engine->destroy(app.materialInstance);
engine->destroy(app.mesh.renderable);
engine->destroy(app.material);
};
FilamentApp::get().animate([&app](Engine* engine, View* view, double now) {
auto& tcm = engine->getTransformManager();
auto ti = tcm.getInstance(app.mesh.renderable);
tcm.setTransform(ti, app.transform * mat4f::rotate(now, float3{0, 1, 0}));
});
FilamentApp::get().run(config, setup, cleanup);
}