Skip to content

Commit

Permalink
Cleanup yocto_gui (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomonazzaro authored Aug 26, 2020
1 parent f692239 commit eecaa39
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 170 deletions.
45 changes: 25 additions & 20 deletions apps/yshapeview/yshapeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ void init_glscene(app_state* app, gui_scene* glscene, generic_shape* ioshape,
// handle progress
auto progress = vec2i{0, 4};

// create scene
init_scene(glscene);

// compute bounding box
auto bbox = invalidb3f;
for (auto& pos : ioshape->positions) bbox = merge(bbox, pos);
Expand Down Expand Up @@ -207,30 +204,36 @@ void init_glscene(app_state* app, gui_scene* glscene, generic_shape* ioshape,
set_vertex_buffer(model_shape, vec3f{0, 0, 0}, 5);
set_vertex_buffer(model_shape, vec3f{0, 0, 0}, 6);

auto cylinder = make_uvcylinder({4, 1, 1}, {0.0003, 1});
for (auto& p : cylinder.positions) {
p.z = p.z * 0.5 + 0.5;
}

auto edges_shape = add_shape(glscene, {}, {}, {}, cylinder.quads,
cylinder.positions, cylinder.normals, cylinder.texcoords, {});

auto edges = get_edges(ioshape->triangles, ioshape->quads);
auto froms = vector<vec3f>();
auto tos = vector<vec3f>();
froms.reserve(edges.size());
tos.reserve(edges.size());
float avg_edge_length = 0;
for (auto& edge : edges) {
froms.push_back(ioshape->positions[edge.x]);
tos.push_back(ioshape->positions[edge.y]);
auto from = ioshape->positions[edge.x];
auto to = ioshape->positions[edge.y];
froms.push_back(from);
tos.push_back(to);
avg_edge_length += length(from - to);
}
avg_edge_length /= edges.size();
auto cylinder_radius = 0.05f * avg_edge_length;
auto cylinder = make_uvcylinder({4, 1, 1}, {cylinder_radius, 1});
for (auto& p : cylinder.positions) {
p.z = p.z * 0.5 + 0.5;
}
auto edges_shape = add_shape(glscene, {}, {}, {}, cylinder.quads,
cylinder.positions, cylinder.normals, cylinder.texcoords, {});

set_vertex_buffer(edges_shape, froms, 5);
set_instance_buffer(edges_shape, 5);
set_vertex_buffer(edges_shape, tos, 6);
set_instance_buffer(edges_shape, 6);

auto vertices = make_spheres(ioshape->positions, 0.001, 2);
auto vertices_shape = add_shape(glscene, {}, {}, {}, vertices.quads,
auto vertices_radius = 3.0f * cylinder_radius;
auto vertices = make_spheres(ioshape->positions, vertices_radius, 2);
auto vertices_shape = add_shape(glscene, {}, {}, {}, vertices.quads,
vertices.positions, vertices.normals, vertices.texcoords, {});
set_vertex_buffer(vertices_shape, vec3f{0, 0, 0}, 5);
set_vertex_buffer(vertices_shape, vec3f{0, 0, 0}, 6);
Expand All @@ -248,11 +251,13 @@ void init_glscene(app_state* app, gui_scene* glscene, generic_shape* ioshape,
glscene, identity3x4f, vertices_shape, glmaterialv, true);
points_instance->shading = gui_shading_type::constant;

auto error = string{};
auto errorb = string{};
auto vert = draw_instanced_vertex_code();
auto frag = draw_instances_eyelight_fragment_code();
init_program(glscene->eyelight_program, vert, frag, error, errorb);
// init scene
init_scene(glscene);

// override eyelight vertex shader
auto vert = draw_instanced_vertex_code();
auto frag = draw_instances_eyelight_fragment_code();
init_program(glscene->eyelight_program, vert, frag);

// done
if (progress_cb) progress_cb("convert done", progress.x++, progress.y);
Expand Down
54 changes: 16 additions & 38 deletions libs/yocto_gui/yocto_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@

#include <array>

#include "ext/glad/glad.h"

#ifdef _WIN32
#undef near
#undef far
#endif

// -----------------------------------------------------------------------------
// USING DIRECTIVES
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -139,13 +132,14 @@ gui_scene::~gui_scene() {
for (auto shape : shapes) delete shape;
for (auto material : materials) delete material;
for (auto texture : textures) delete texture;
delete eyelight_program;
delete ibl_program;
delete environment_program;
delete environment_shape;
delete environment_cubemap;
delete environment_program;
delete diffuse_cubemap;
delete specular_cubemap;
delete brdf_lut;
delete eyelight_program;
delete ibl_program;
}

static const char* bake_brdf_vertex_code();
Expand Down Expand Up @@ -182,13 +176,14 @@ bool is_initialized(gui_scene* scene) {
void clear_scene(gui_scene* scene) {
for (auto texture : scene->textures) clear_texture(texture);
for (auto shape : scene->shapes) clear_shape(shape);
clear_program(scene->eyelight_program);
clear_program(scene->ibl_program);
clear_program(scene->environment_program);
clear_shape(scene->environment_shape);
clear_cubemap(scene->environment_cubemap);
clear_program(scene->environment_program);
clear_cubemap(scene->diffuse_cubemap);
clear_cubemap(scene->specular_cubemap);
clear_texture(scene->brdf_lut);
clear_program(scene->eyelight_program);
clear_program(scene->ibl_program);
}

// add camera
Expand Down Expand Up @@ -361,7 +356,6 @@ void set_instance_uniforms(ogl_program* program, const frame3f& frame,
// } else {
// set_uniform(program, "highlight", vec4f{0, 0, 0, 0});
// }
assert_ogl_error();

auto mtype = (int)shading;
set_uniform(program, "mtype", mtype);
Expand Down Expand Up @@ -408,7 +402,6 @@ void draw_environment(gui_scene* scene, const gui_scene_view& view) {
}

void set_eyelight_uniforms(ogl_program* program, const gui_scene_view& view) {
// Opengl light
struct gui_light {
vec3f position = {0, 0, 0};
vec3f emission = {0, 0, 0};
Expand Down Expand Up @@ -459,7 +452,11 @@ void draw_instances(gui_scene* scene, const gui_scene_view& view) {
}

bind_program(program);

// set scene uniforms
set_scene_view_uniforms(program, view);

// set lighting uniforms
if (view.params.lighting == gui_lighting_type::eyelight) {
set_eyelight_uniforms(program, view);
} else {
Expand Down Expand Up @@ -498,10 +495,8 @@ gui_scene_view make_scene_view(

void draw_scene(gui_scene* scene, gui_camera* camera, const vec4i& viewport,
const gui_scene_params& params) {
assert_ogl_error();
clear_ogl_framebuffer(params.background);
set_ogl_viewport(viewport);
assert_ogl_error();

auto view = make_scene_view(camera, viewport, params);
draw_instances(scene, view);
Expand Down Expand Up @@ -565,7 +560,7 @@ inline void bake_cubemap(ogl_cubemap* cubemap, const Sampler* environment,
}

inline void bake_specular_brdf_texture(gui_texture* texture) {
auto size = 512;
auto size = vec2i{512, 512};
auto framebuffer = ogl_framebuffer{};
auto screen_quad = ogl_shape{};
set_quad_shape(&screen_quad);
Expand All @@ -576,35 +571,18 @@ inline void bake_specular_brdf_texture(gui_texture* texture) {
auto frag = bake_brdf_fragment_code();
init_program(&program, vert, frag, error, errorlog);

assert_ogl_error();
set_texture(texture, size, 3, (float*)nullptr, true, true, false, false);

texture->is_float = true;
texture->linear = true;
texture->num_channels = 3;
texture->size = {size, size};
glGenTextures(1, &texture->texture_id);

glBindTexture(GL_TEXTURE_2D, texture->texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, size, size, 0, GL_RGB, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// TODO(giacomo): mipmaps?

assert_ogl_error();

set_framebuffer(&framebuffer, {size, size});
set_framebuffer(&framebuffer, size);
set_framebuffer_texture(&framebuffer, texture, 0);

bind_framebuffer(&framebuffer);
bind_program(&program);

set_ogl_viewport(vec2i{size, size});
set_ogl_viewport(size);
clear_ogl_framebuffer({0, 0, 0, 0}, true);

draw_shape(&screen_quad);
assert_ogl_error();

unbind_program();
unbind_framebuffer();
Expand Down
8 changes: 4 additions & 4 deletions libs/yocto_gui/yocto_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,18 @@ struct gui_scene {
vector<gui_texture*> textures = {};

// environment
ogl_cubemap* environment_cubemap = new ogl_cubemap{};
gui_shape* environment_shape = new gui_shape{};
ogl_cubemap* environment_cubemap = new ogl_cubemap{};
ogl_program* environment_program = new ogl_program{};

// IBL baked data
ogl_cubemap* diffuse_cubemap = new ogl_cubemap{};
ogl_cubemap* specular_cubemap = new ogl_cubemap{};
gui_texture* brdf_lut = new gui_texture{};

// programs
ogl_program* eyelight_program = new ogl_program{};
ogl_program* ibl_program = new ogl_program{};
ogl_program* environment_program = new ogl_program{};
ogl_program* eyelight_program = new ogl_program{};
ogl_program* ibl_program = new ogl_program{};

~gui_scene();
};
Expand Down
Loading

0 comments on commit eecaa39

Please sign in to comment.