Skip to content

Commit

Permalink
Image-based-lighting for real-time viewers (#1034)
Browse files Browse the repository at this point in the history
* update yocto_gui

* compiles

* init ibl data

* baking does not crash

* environment works

* renaming

* towards new shapes

* no more set attributes during draw

* removed VAO from ogl_program

* ibl works

* updated

* shading types

* embedding shaders

* update

* fix image viewer

* fixes

* fix for no environment

* get environment texture in a safer way
  • Loading branch information
giacomonazzaro authored Aug 20, 2020
1 parent 5cbc3aa commit 75332ed
Show file tree
Hide file tree
Showing 8 changed files with 1,947 additions and 986 deletions.
32 changes: 19 additions & 13 deletions apps/ysceneview/ysceneview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#include <yocto/yocto_parallel.h>
#include <yocto/yocto_sceneio.h>
#include <yocto/yocto_shape.h>
#include <yocto_gui/yocto_draw.h>
#include <yocto_gui/yocto_imgui.h>
#include <yocto_gui/yocto_opengl.h>
using namespace yocto;

#include <deque>
Expand All @@ -56,15 +56,15 @@ struct app_state {
string name = "";

// options
ogl_scene_params drawgl_prms = {};
gui_scene_params drawgl_prms = {};

// scene
sceneio_scene* ioscene = new sceneio_scene{};
sceneio_camera* iocamera = nullptr;

// rendering state
ogl_scene* glscene = new ogl_scene{};
ogl_camera* glcamera = nullptr;
gui_scene* glscene = new gui_scene{};
gui_camera* glcamera = nullptr;

// editing
sceneio_camera* selected_camera = nullptr;
Expand Down Expand Up @@ -97,7 +97,7 @@ struct app_states {
std::deque<app_state*> loading = {};

// default options
ogl_scene_params drawgl_prms = {};
gui_scene_params drawgl_prms = {};

// cleanup
~app_states() {
Expand Down Expand Up @@ -129,7 +129,7 @@ void load_scene_async(
if (!apps->selected) apps->selected = app;
}

void update_lights(ogl_scene* glscene, sceneio_scene* ioscene) {
void update_lights(gui_scene* glscene, sceneio_scene* ioscene) {
clear_lights(glscene);
for (auto ioobject : ioscene->instances) {
if (has_max_lights(glscene)) break;
Expand Down Expand Up @@ -159,8 +159,8 @@ void update_lights(ogl_scene* glscene, sceneio_scene* ioscene) {
}
}

void init_glscene(ogl_scene* glscene, sceneio_scene* ioscene,
ogl_camera*& glcamera, sceneio_camera* iocamera,
void init_glscene(gui_scene* glscene, sceneio_scene* ioscene,
gui_camera*& glcamera, sceneio_camera* iocamera,
progress_callback progress_cb) {
// handle progress
auto progress = vec2i{
Expand All @@ -172,7 +172,7 @@ void init_glscene(ogl_scene* glscene, sceneio_scene* ioscene,
init_scene(glscene);

// camera
auto camera_map = unordered_map<sceneio_camera*, ogl_camera*>{};
auto camera_map = unordered_map<sceneio_camera*, gui_camera*>{};
camera_map[nullptr] = nullptr;
for (auto iocamera : ioscene->cameras) {
if (progress_cb) progress_cb("convert camera", progress.x++, progress.y);
Expand All @@ -198,7 +198,7 @@ void init_glscene(ogl_scene* glscene, sceneio_scene* ioscene,
}

// material
auto material_map = unordered_map<sceneio_material*, ogl_material*>{};
auto material_map = unordered_map<sceneio_material*, gui_material*>{};
material_map[nullptr] = nullptr;
for (auto iomaterial : ioscene->materials) {
if (progress_cb) progress_cb("convert material", progress.x++, progress.y);
Expand Down Expand Up @@ -248,6 +248,12 @@ void init_glscene(ogl_scene* glscene, sceneio_scene* ioscene,
set_material(globject, material_map.at(ioinstance->material));
}

// bake prefiltered environments
if (ioscene->environments.size()) {
ibl::init_ibl_data(
glscene, texture_map[ioscene->environments[0]->emission_tex]);
}

// done
if (progress_cb) progress_cb("convert done", progress.x++, progress.y);

Expand Down Expand Up @@ -450,7 +456,7 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
}
auto& params = app->drawgl_prms;
draw_slider(win, "resolution", params.resolution, 0, 4096);
draw_combobox(win, "shading", (int&)params.shading, ogl_shading_names);
draw_combobox(win, "shading", (int&)params.shading, gui_shading_names);
draw_checkbox(win, "wireframe", params.wireframe);
continue_line(win);
draw_checkbox(win, "edges", params.edges);
Expand Down Expand Up @@ -583,7 +589,7 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
void draw(gui_window* win, app_states* apps, const gui_input& input) {
if (!apps->selected || !apps->selected->ok) return;
auto app = apps->selected;
if (app->drawgl_prms.shading == ogl_shading_type::lights)
if (app->drawgl_prms.shading == gui_shading_type::eyelight)
update_lights(app->glscene, app->ioscene);
draw_scene(app->glscene, app->glcamera, input.framebuffer_viewport,
app->drawgl_prms);
Expand Down Expand Up @@ -631,7 +637,7 @@ int main(int argc, const char* argv[]) {
add_option(cli, "--resolution,-r", apps->drawgl_prms.resolution,
"Image resolution.");
add_option(cli, "--shading", apps->drawgl_prms.shading, "Shading type.",
ogl_shading_names);
gui_shading_names);
add_option(cli, "scenes", filenames, "Scene filenames", true);
parse_cli(cli, argc, argv);

Expand Down
31 changes: 19 additions & 12 deletions apps/ysceneviews/ysceneviews.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#include <yocto/yocto_parallel.h>
#include <yocto/yocto_sceneio.h>
#include <yocto/yocto_shape.h>
#include <yocto_gui/yocto_draw.h>
#include <yocto_gui/yocto_imgui.h>
#include <yocto_gui/yocto_opengl.h>
using namespace yocto;

#include <deque>
Expand All @@ -57,15 +57,15 @@ struct app_state {
string name = "";

// options
ogl_scene_params drawgl_prms = {};
gui_scene_params drawgl_prms = {};

// scene
sceneio_scene* ioscene = new sceneio_scene{};
sceneio_camera* iocamera = nullptr;

// rendering state
ogl_scene* glscene = new ogl_scene{};
ogl_camera* glcamera = nullptr;
gui_scene* glscene = new gui_scene{};
gui_camera* glcamera = nullptr;

// editing
sceneio_camera* selected_camera = nullptr;
Expand All @@ -90,7 +90,7 @@ struct app_state {
}
};

void update_lights(ogl_scene* glscene, sceneio_scene* ioscene) {
void update_lights(gui_scene* glscene, sceneio_scene* ioscene) {
clear_lights(glscene);
for (auto ioobject : ioscene->instances) {
if (has_max_lights(glscene)) break;
Expand Down Expand Up @@ -120,8 +120,8 @@ void update_lights(ogl_scene* glscene, sceneio_scene* ioscene) {
}
}

void init_glscene(ogl_scene* glscene, sceneio_scene* ioscene,
ogl_camera*& glcamera, sceneio_camera* iocamera,
void init_glscene(gui_scene* glscene, sceneio_scene* ioscene,
gui_camera*& glcamera, sceneio_camera* iocamera,
progress_callback progress_cb) {
// handle progress
auto progress = vec2i{
Expand All @@ -133,7 +133,7 @@ void init_glscene(ogl_scene* glscene, sceneio_scene* ioscene,
init_scene(glscene);

// camera
auto camera_map = unordered_map<sceneio_camera*, ogl_camera*>{};
auto camera_map = unordered_map<sceneio_camera*, gui_camera*>{};
camera_map[nullptr] = nullptr;
for (auto iocamera : ioscene->cameras) {
if (progress_cb) progress_cb("convert camera", progress.x++, progress.y);
Expand All @@ -159,7 +159,7 @@ void init_glscene(ogl_scene* glscene, sceneio_scene* ioscene,
}

// material
auto material_map = unordered_map<sceneio_material*, ogl_material*>{};
auto material_map = unordered_map<sceneio_material*, gui_material*>{};
material_map[nullptr] = nullptr;
for (auto iomaterial : ioscene->materials) {
if (progress_cb) progress_cb("convert material", progress.x++, progress.y);
Expand Down Expand Up @@ -209,6 +209,13 @@ void init_glscene(ogl_scene* glscene, sceneio_scene* ioscene,
set_material(globject, material_map.at(ioobject->material));
}

// bake prefiltered environments
// TODO(giacomo): what if there's more than 1 environment?
if (ioscene->environments.size()) {
ibl::init_ibl_data(
glscene, texture_map[ioscene->environments[0]->emission_tex]);
}

// done
if (progress_cb) progress_cb("convert done", progress.x++, progress.y);

Expand All @@ -228,7 +235,7 @@ int main(int argc, const char* argv[]) {
add_option(
cli, "--resolution,-r", app->drawgl_prms.resolution, "Image resolution.");
add_option(cli, "--shading", app->drawgl_prms.shading, "Eyelight rendering.",
ogl_shading_names);
gui_shading_names);
add_option(cli, "scene", app->filename, "Scene filename", true);
parse_cli(cli, argc, argv);

Expand Down Expand Up @@ -257,7 +264,7 @@ int main(int argc, const char* argv[]) {
clear_scene(app->glscene);
};
callbacks.draw_cb = [app](gui_window* win, const gui_input& input) {
if (app->drawgl_prms.shading == ogl_shading_type::lights)
if (app->drawgl_prms.shading == gui_shading_type::eyelight)
update_lights(app->glscene, app->ioscene);
draw_scene(app->glscene, app->glcamera, input.framebuffer_viewport,
app->drawgl_prms);
Expand All @@ -273,7 +280,7 @@ int main(int argc, const char* argv[]) {
auto& params = app->drawgl_prms;
draw_slider(win, "resolution", params.resolution, 0, 4096);
draw_checkbox(win, "wireframe", params.wireframe);
draw_combobox(win, "shading", (int&)params.shading, ogl_shading_names);
draw_combobox(win, "shading", (int&)params.shading, gui_shading_names);
continue_line(win);
draw_checkbox(win, "edges", params.edges);
continue_line(win);
Expand Down
16 changes: 8 additions & 8 deletions apps/yshapeview/yshapeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <yocto/yocto_sceneio.h>
#include <yocto/yocto_shape.h>
#include <yocto_gui/yocto_imgui.h>
#include <yocto_gui/yocto_opengl.h>
#include <yocto_gui/yocto_draw.h>
using namespace yocto;

#include <deque>
Expand All @@ -56,14 +56,14 @@ struct app_state {
string name = "";

// options
ogl_scene_params drawgl_prms = {};
gui_scene_params drawgl_prms = {};

// scene
generic_shape* ioshape = new generic_shape{};

// rendering state
ogl_scene* glscene = new ogl_scene{};
ogl_camera* glcamera = nullptr;
gui_scene* glscene = new gui_scene{};
gui_camera* glcamera = nullptr;

// loading status
std::atomic<bool> ok = false;
Expand All @@ -88,7 +88,7 @@ struct app_states {
std::deque<app_state*> loading = {};

// default options
ogl_scene_params drawgl_prms = {};
gui_scene_params drawgl_prms = {};

// cleanup
~app_states() {
Expand Down Expand Up @@ -170,7 +170,7 @@ quads_shape make_cylinders(const vector<vec2i>& lines,
return shape;
}

void init_glscene(ogl_scene* glscene, const generic_shape* ioshape,
void init_glscene(gui_scene* glscene, const generic_shape* ioshape,
progress_callback progress_cb) {
// handle progress
auto progress = vec2i{0, 4};
Expand Down Expand Up @@ -280,7 +280,7 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
draw_coloredit(win, "color", glmaterial->color);
auto& params = app->drawgl_prms;
draw_slider(win, "resolution", params.resolution, 0, 4096);
draw_combobox(win, "shading", (int&)params.shading, ogl_shading_names);
draw_combobox(win, "shading", (int&)params.shading, gui_shading_names);
draw_checkbox(win, "wireframe", params.wireframe);
continue_line(win);
draw_checkbox(win, "edges", params.edges);
Expand Down Expand Up @@ -364,7 +364,7 @@ int main(int argc, const char* argv[]) {
add_option(cli, "--resolution,-r", apps->drawgl_prms.resolution,
"Image resolution.");
add_option(cli, "--shading", apps->drawgl_prms.shading, "Shading type.",
ogl_shading_names);
gui_shading_names);
add_option(cli, "shapes", filenames, "Shape filenames", true);
parse_cli(cli, argc, argv);

Expand Down
2 changes: 2 additions & 0 deletions libs/yocto_gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ if(YOCTO_OPENGL)
add_library(yocto_gui
yocto_opengl.h
yocto_opengl.cpp
yocto_draw.h
yocto_draw.cpp
yocto_imgui.h
yocto_imgui.cpp
ext/imgui/imgui.cpp
Expand Down
Loading

0 comments on commit 75332ed

Please sign in to comment.