Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created July 7, 2020 21:21
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 roxlu/3465800d06a75ab9931f76a20c44a515 to your computer and use it in GitHub Desktop.
Save roxlu/3465800d06a75ab9931f76a20c44a515 to your computer and use it in GitHub Desktop.
/*
---------------------------------------------------------------
https://gist.github.com/roxlu/2ac1aa06222ef788f9df235a5b2fbf7c
---------------------------------------------------------------
oooo
'888
oooo d8b .ooooo. oooo ooo 888 oooo oooo
'888""8P d88' '88b '88b..8P' 888 '888 '888
888 888 888 Y888' 888 888 888
888 888 888 .o8"'88b 888 888 888
d888b 'Y8bod8P' o88' 888o o888o 'V88V"V8P'
www.roxlu.com
www.twitter.com/roxlu
---------------------------------------------------------------
FILAMENT MODEL VIEWER
=====================
See `FilaModelViewer.h` for a more elaborate description of the
FilaModelViewer. In short you can use it to render a 3D
SimpleScene using Filament. It adds a couple of often
used features like viewpoints and a turntable.
*/
/* -------------------------------------------------------- */
#if defined(__linux)
# define USE_GL_LINUX 1
# define GLFW_EXPOSE_NATIVE_X11
# define GLFW_EXPOSE_NATIVE_GLX
#endif
#if defined(_WIN32)
# define USE_GL_WIN 1
# define GLFW_EXPOSE_NATIVE_WGL
# define GLFW_EXPOSE_NATIVE_WIN32
#endif
/* -------------------------------------------------------- */
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <poly/Glad.h>
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <poly/Log.h>
#include <poly/FilaContext.h>
#include <poly/FilaModelViewer.h>
#include <poly/ImageTexture.h>
/* -------------------------------------------------------- */
using namespace poly;
void button_callback(GLFWwindow* win, int bt, int action, int mods);
void cursor_callback(GLFWwindow* win, double x, double y);
void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods);
void char_callback(GLFWwindow* win, unsigned int key);
void error_callback(int err, const char* desc);
void resize_callback(GLFWwindow* window, int width, int height);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
/* -------------------------------------------------------- */
int win_w = 1280;
int win_h = 720;
FilaModelViewer viewer;
/* -------------------------------------------------------- */
int main(int argc, char* argv[]) {
glfwSetErrorCallback(error_callback);
poly_log_init(256, argc, argv);
poly_log_add_sink_stdout();
if(!glfwInit()) {
printf("Error: cannot setup glfw.\n");
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GL_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* win = NULL;
int w = win_w;
int h = win_h;
win = glfwCreateWindow(w, h, "FILAMENT MODEL VIEWER TEST", NULL, NULL);
if(!win) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetFramebufferSizeCallback(win, resize_callback);
glfwSetKeyCallback(win, key_callback);
glfwSetCharCallback(win, char_callback);
glfwSetCursorPosCallback(win, cursor_callback);
glfwSetMouseButtonCallback(win, button_callback);
glfwSetScrollCallback(win, scroll_callback);
glfwMakeContextCurrent(win);
glfwSwapInterval(1);
if (!gladLoadGL()) {
printf("Cannot load GL.\n");
exit(1);
}
// ----------------------------------------------------------------
// THIS IS WHERE YOU START CALLING OPENGL FUNCTIONS, NOT EARLIER!!
// ----------------------------------------------------------------
/* -------------------------------------------------------- */
#if USE_GL_LINUX
void* main_opengl_context = (void*)glfwGetGLXContext(win);
void* native_window = (void*)glfwGetX11Window(win);
#endif
#if USE_GL_WIN
void* main_opengl_context = (void*)glfwGetWGLContext(win);
void* native_window = (void*)glfwGetWin32Window(win);
#endif
FilaContextSettings fila_ctx_cfg;
fila_ctx_cfg.native_window = native_window;
fila_ctx_cfg.shared_gl_context = main_opengl_context;
FilaContext fila_ctx;
if (0 != fila_ctx.init(fila_ctx_cfg)) {
exit(EXIT_FAILURE);
}
/* -------------------------------------------------------- */
std::string xml_file = "";
xml_file = "/home/roxlu/Downloads/wtc-guy-v3.2.0/generated/xml/simplescene-generated-guy2.xml";
xml_file = "/home/roxlu/2020/2020-experiments/2020-020-polytrope-filament/install/bin/data/blender/001-cube-scene/simplescene/xml/simplescene-generated-block.xml";
FilaModelViewerSettings viewer_cfg;
viewer_cfg.fila_ctx = &fila_ctx;
viewer_cfg.xml_filepath = xml_file;
viewer_cfg.view_width = w;
viewer_cfg.view_height = h;
if (0 != viewer.init(viewer_cfg)) {
exit(EXIT_FAILURE);
}
ImageTexture img_tex;
if (0 != img_tex.initWithTexture(viewer.getTextureColorId(), w, h)) {
exit(EXIT_FAILURE);
}
img_tex.setFlipVertical(true);
/* -------------------------------------------------------- */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while(!glfwWindowShouldClose(win)) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, w, h);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Update and render into the FBO */
viewer.update();
viewer.draw();
/* Draw the result. */
#if 0
glfwMakeContextCurrent(win);
glDisable(GL_BLEND); /* IMPORTANT: w/o the colors are incorrect */
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, w, h);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
#endif
img_tex.draw();
glfwSwapBuffers(win);
glfwPollEvents();
}
viewer.shutdown();
img_tex.shutdown();
fila_ctx.shutdown();
glfwTerminate();
return EXIT_SUCCESS;
}
/* -------------------------------------------------------- */
void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods) {
if (GLFW_RELEASE == action) {
return;
}
switch(key) {
case GLFW_KEY_LEFT: {
viewer.setInteractionModeViewPoints();
viewer.goToPreviousViewPoint();
break;
}
case GLFW_KEY_RIGHT: {
viewer.setInteractionModeViewPoints();
viewer.goToNextViewPoint();
break;
}
case GLFW_KEY_T: {
viewer.setInteractionModeTable();
break;
}
case GLFW_KEY_ESCAPE: {
glfwSetWindowShouldClose(win, GL_TRUE);
break;
}
};
}
void cursor_callback(GLFWwindow* win, double x, double y) {
viewer.onMouseMove(x, win_h - y);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
viewer.onMouseScroll(yoffset);
}
void button_callback(GLFWwindow* win, int bt, int action, int mods) {
double mx = 0.0;
double my = 0.0;
glfwGetCursorPos(win, &mx, &my);
if (GLFW_PRESS == action) {
viewer.onMouseDown(mx, win_h - my, bt);
}
else if (GLFW_RELEASE == action) {
viewer.onMouseUp(bt);
}
}
/* -------------------------------------------------------- */
void error_callback(int err, const char* desc) {
printf("GLFW error: %s (%d)\n", desc, err);
}
/* -------------------------------------------------------- */
void char_callback(GLFWwindow* win, unsigned int key) { }
void resize_callback(GLFWwindow* window, int width, int height) { }
/* -------------------------------------------------------- */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment