Skip to content

Instantly share code, notes, and snippets.

@n00ner
Created April 24, 2022 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n00ner/978e9e6c6f94ec3544a78c36f40bcaf0 to your computer and use it in GitHub Desktop.
Save n00ner/978e9e6c6f94ec3544a78c36f40bcaf0 to your computer and use it in GitHub Desktop.
#include <jni.h>
#include <string>
#include <pthread.h>
#include <queue>
#include <android/log.h>
#include <filament/FilamentAPI.h>
#include <filament/Engine.h>
#include <utils/EntityManager.h>
#include <filament/Skybox.h>
#include <filament/View.h>
#include <filament/Material.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <math/vec3.h>
#include <math/vec4.h>
#include <math/mat3.h>
#include <math/norm.h>
#include <filament/VertexBuffer.h>
#include <filament/IndexBuffer.h>
#include <filament/RenderableManager.h>
#include <filament/Scene.h>
#include <filament/LightManager.h>
#include <filament/Camera.h>
#include <filament/Renderer.h>
#include <filament/View.h>
#include <filament/SwapChain.h>
#include <android/native_window_jni.h>
using namespace filament;
using namespace filament::math;
using namespace utils;
AAssetManager *globalManager = nullptr;
struct Managers {
EntityManager *entityManager;
};
Skybox *skybox = nullptr;
Material* material = nullptr;
MaterialInstance* materialInstance = nullptr;
SwapChain *swapChain = nullptr;
struct App {
Engine *engine;
Renderer *render;
Scene *scene;
View *view;
Camera * camera;
Managers *managers;
};
App *app = nullptr;
void initFilament(){
Engine *engine = Engine::create();
auto *managers = new Managers{
&engine->getEntityManager()
};
app = new App {
engine,
engine->createRenderer(),
engine->createScene(),
engine->createView(),
engine->createCamera(managers->entityManager->create()),
managers
};
}
void initView(){
filament::Skybox::Builder *builder;
builder = new Skybox::Builder{};
builder->color({0.035f, 0.035f, 0.07f, 1.0f});
skybox = builder->build(*app->engine);
app->scene->setSkybox(skybox);
app->view->setCamera(app->camera);
app->view->setScene(app->scene);
app->camera->setExposure(16.0f, 1.0f / 125.0f, 100.0f);
app->camera->lookAt({0.0, 3.0, 4.0}, {0.0, 0.0, 0.0}, {0.0, 1.0, 0.0});
}
void createSwapChain(JNIEnv *env, jobject surface){
void* win = ANativeWindow_fromSurface(env, surface);
if(swapChain != nullptr){
app->engine->destroy(swapChain);
}
swapChain = app->engine->createSwapChain(win, (uint64_t) 0);
}
extern "C"
JNIEXPORT void JNICALL
Java_net_libcore_projectar_Engine_createSwapChain(JNIEnv *env, jobject thiz, jobject surface) {
createSwapChain(env, surface);
}
void initEngine(){
initFilament();
initView();
}
void renderFrame(){
if (app->render->beginFrame(swapChain)) {
// for each View
app->render->render(app->view);
app->render->endFrame();
}
}
extern "C"
JNIEXPORT void JNICALL
Java_net_libcore_projectar_Engine_renderFrame(JNIEnv *env, jobject thiz) {
renderFrame();
}
extern "C"
JNIEXPORT void JNICALL
Java_net_libcore_projectar_Engine_initEngine(JNIEnv *env, jobject thiz) {
initEngine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment