Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many updates to real-time renderers #1040

Merged
merged 10 commits into from
Aug 23, 2020
66 changes: 14 additions & 52 deletions apps/ysceneview/ysceneview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,36 +129,6 @@ void load_scene_async(
if (!apps->selected) apps->selected = app;
}

void update_lights(gui_scene* glscene, sceneio_scene* ioscene) {
clear_lights(glscene);
for (auto ioobject : ioscene->instances) {
if (has_max_lights(glscene)) break;
if (ioobject->material->emission == zero3f) continue;
auto ioshape = ioobject->shape;
auto bbox = invalidb3f;
for (auto p : ioshape->positions) bbox = merge(bbox, p);
auto pos = (bbox.max + bbox.min) / 2;
auto area = 0.0f;
if (!ioshape->triangles.empty()) {
for (auto t : ioshape->triangles)
area += triangle_area(ioshape->positions[t.x], ioshape->positions[t.y],
ioshape->positions[t.z]);
} else if (!ioshape->quads.empty()) {
for (auto q : ioshape->quads)
area += quad_area(ioshape->positions[q.x], ioshape->positions[q.y],
ioshape->positions[q.z], ioshape->positions[q.w]);
} else if (!ioshape->lines.empty()) {
for (auto l : ioshape->lines)
area += line_length(ioshape->positions[l.x], ioshape->positions[l.y]);
} else {
area += ioshape->positions.size();
}
auto ke = ioobject->material->emission * area;
set_light(add_light(glscene), transform_point(ioobject->frame, pos), ke,
ogl_light_type::point, false);
}
}

void init_glscene(gui_scene* glscene, sceneio_scene* ioscene,
gui_camera*& glcamera, sceneio_camera* iocamera,
progress_callback progress_cb) {
Expand All @@ -184,7 +154,7 @@ void init_glscene(gui_scene* glscene, sceneio_scene* ioscene,
}

// textures
auto texture_map = unordered_map<sceneio_texture*, ogl_texture*>{};
auto texture_map = unordered_map<sceneio_texture*, gui_texture*>{};
texture_map[nullptr] = nullptr;
for (auto iotexture : ioscene->textures) {
if (progress_cb) progress_cb("convert texture", progress.x++, progress.y);
Expand Down Expand Up @@ -222,36 +192,30 @@ void init_glscene(gui_scene* glscene, sceneio_scene* ioscene,
}

// shapes
auto shape_map = unordered_map<sceneio_shape*, ogl_shape*>{};
auto shape_map = unordered_map<sceneio_shape*, gui_shape*>{};
shape_map[nullptr] = nullptr;
for (auto ioshape : ioscene->shapes) {
if (progress_cb) progress_cb("convert shape", progress.x++, progress.y);
auto glshape = add_shape(glscene);
set_positions(glshape, ioshape->positions);
set_normals(glshape, ioshape->normals);
set_texcoords(glshape, ioshape->texcoords);
set_colors(glshape, ioshape->colors);
set_points(glshape, ioshape->points);
set_lines(glshape, ioshape->lines);
set_triangles(glshape, ioshape->triangles);
set_quads(glshape, ioshape->quads);
set_edges(glshape, ioshape->triangles, ioshape->quads);
auto glshape = add_shape(glscene, ioshape->points, ioshape->lines,
ioshape->triangles, ioshape->quads, ioshape->positions,
ioshape->normals, ioshape->texcoords, ioshape->colors);
shape_map[ioshape] = glshape;
}

// shapes
for (auto ioinstance : ioscene->instances) {
if (progress_cb) progress_cb("convert instance", progress.x++, progress.y);
auto globject = add_instance(glscene);
set_frame(globject, ioinstance->frame);
set_shape(globject, shape_map.at(ioinstance->shape));
set_material(globject, material_map.at(ioinstance->material));
auto instance = add_instance(glscene);
set_frame(instance, ioinstance->frame);
set_shape(instance, shape_map.at(ioinstance->shape));
set_material(instance, material_map.at(ioinstance->material));
}

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

// done
Expand Down Expand Up @@ -460,7 +424,8 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
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);
draw_checkbox(win, "faceted", params.faceted);
// draw_checkbox(win, "edges", params.edges);
continue_line(win);
draw_checkbox(win, "double sided", params.double_sided);
draw_slider(win, "exposure", params.exposure, -10, 10);
Expand Down Expand Up @@ -590,8 +555,6 @@ 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 == gui_shading_type::eyelight)
update_lights(app->glscene, app->ioscene);
draw_scene(app->glscene, app->glcamera, input.framebuffer_viewport,
app->drawgl_prms);
}
Expand All @@ -615,7 +578,6 @@ void update(gui_window* win, app_states* apps) {
if (app->loader_error.empty()) {
init_glscene(app->glscene, app->ioscene, app->glcamera, app->iocamera,
progress_cb);
update_lights(app->glscene, app->ioscene);
app->ok = true;
app->status = "ok";
} else {
Expand Down
59 changes: 11 additions & 48 deletions apps/ysceneviews/ysceneviews.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,6 @@ struct app_state {
}
};

void update_lights(gui_scene* glscene, sceneio_scene* ioscene) {
clear_lights(glscene);
for (auto ioobject : ioscene->instances) {
if (has_max_lights(glscene)) break;
if (ioobject->material->emission == zero3f) continue;
auto ioshape = ioobject->shape;
auto bbox = invalidb3f;
for (auto p : ioshape->positions) bbox = merge(bbox, p);
auto pos = (bbox.max + bbox.min) / 2;
auto area = 0.0f;
if (!ioshape->triangles.empty()) {
for (auto t : ioshape->triangles)
area += triangle_area(ioshape->positions[t.x], ioshape->positions[t.y],
ioshape->positions[t.z]);
} else if (!ioshape->quads.empty()) {
for (auto q : ioshape->quads)
area += quad_area(ioshape->positions[q.x], ioshape->positions[q.y],
ioshape->positions[q.z], ioshape->positions[q.w]);
} else if (!ioshape->lines.empty()) {
for (auto l : ioshape->lines)
area += line_length(ioshape->positions[l.x], ioshape->positions[l.y]);
} else {
area += ioshape->positions.size();
}
auto ke = ioobject->material->emission * area;
set_light(add_light(glscene), transform_point(ioobject->frame, pos), ke,
ogl_light_type::point, false);
}
}

void init_glscene(gui_scene* glscene, sceneio_scene* ioscene,
gui_camera*& glcamera, sceneio_camera* iocamera,
progress_callback progress_cb) {
Expand All @@ -145,7 +115,7 @@ void init_glscene(gui_scene* glscene, sceneio_scene* ioscene,
}

// textures
auto texture_map = unordered_map<sceneio_texture*, ogl_texture*>{};
auto texture_map = unordered_map<sceneio_texture*, gui_texture*>{};
texture_map[nullptr] = nullptr;
for (auto iotexture : ioscene->textures) {
if (progress_cb) progress_cb("convert texture", progress.x++, progress.y);
Expand Down Expand Up @@ -183,20 +153,13 @@ void init_glscene(gui_scene* glscene, sceneio_scene* ioscene,
}

// shapes
auto shape_map = unordered_map<sceneio_shape*, ogl_shape*>{};
auto shape_map = unordered_map<sceneio_shape*, gui_shape*>{};
shape_map[nullptr] = nullptr;
for (auto ioshape : ioscene->shapes) {
if (progress_cb) progress_cb("convert shape", progress.x++, progress.y);
auto glshape = add_shape(glscene);
set_positions(glshape, ioshape->positions);
set_normals(glshape, ioshape->normals);
set_texcoords(glshape, ioshape->texcoords);
set_colors(glshape, ioshape->colors);
set_points(glshape, ioshape->points);
set_lines(glshape, ioshape->lines);
set_triangles(glshape, ioshape->triangles);
set_quads(glshape, ioshape->quads);
set_edges(glshape, ioshape->triangles, ioshape->quads);
auto glshape = add_shape(glscene, ioshape->points, ioshape->lines,
ioshape->triangles, ioshape->quads, ioshape->positions,
ioshape->normals, ioshape->texcoords, ioshape->colors);
shape_map[ioshape] = glshape;
}

Expand All @@ -212,8 +175,9 @@ void init_glscene(gui_scene* glscene, sceneio_scene* ioscene,
// 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]);
auto environment = ioscene->environments[0];
init_ibl_data(
glscene, texture_map[environment->emission_tex], environment->emission);
}

// done
Expand Down Expand Up @@ -264,8 +228,6 @@ 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 == gui_shading_type::eyelight)
update_lights(app->glscene, app->ioscene);
draw_scene(app->glscene, app->glcamera, input.framebuffer_viewport,
app->drawgl_prms);
};
Expand All @@ -280,11 +242,12 @@ 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, gui_shading_names);
continue_line(win);
draw_checkbox(win, "edges", params.edges);
draw_checkbox(win, "faceted", params.faceted);
continue_line(win);
draw_checkbox(win, "double sided", params.double_sided);
draw_combobox(win, "shading", (int&)params.shading, gui_shading_names);
// draw_checkbox(win, "edges", params.edges);
draw_slider(win, "exposure", params.exposure, -10, 10);
draw_slider(win, "gamma", params.gamma, 0.1f, 4);
draw_slider(win, "near", params.near, 0.01f, 1.0f);
Expand Down