Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcypher89 committed Jun 9, 2024
1 parent 6fd5401 commit 066c57c
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/carcockpit/car_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ car_widget::car_widget(utki::shared_ref<ruis::context> context, all_parameters p

ruis::render::gltf_loader l(*this->context.get().renderer.get().factory, true);
demoscene = l.load(papki::fs_file("../res/samples_gltf/parent_and_children.glb")).to_shared_ptr();
//demoscene = l.load(papki::fs_file("../res/samples_gltf/spray.glb")).to_shared_ptr();
// demoscene = l.load(papki::fs_file("../res/samples_gltf/spray.glb")).to_shared_ptr();

sc_renderer = std::make_shared<ruis::render::scene_renderer>(this->context);
sc_renderer->set_scene(demoscene);
Expand Down
1 change: 1 addition & 0 deletions src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ this_ldlibs += $(this__libmodel_obj) $(this__libruis_render)

this_ldlibs += -lruisapp-opengles-xorg -lpapki -lclargs -ltml -lruis -lutki -lm -l ruis-render-opengles
this_ldlibs += -l jsondom
this_ldlibs += -l rasterimage
this_ldlibs += `pkg-config --libs glesv2`

$(eval $(prorab-build-app))
Expand Down
206 changes: 169 additions & 37 deletions src/ruis/render/scene/gltf_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

#include <jsondom/dom.hpp>
#include <papki/span_file.hpp>
#include <rasterimage/image_variant.hpp>
#include <utki/deserializer.hpp>
#include <utki/string.hpp>
#include <utki/util.hpp>
Expand Down Expand Up @@ -67,6 +68,7 @@ inline bool gltf_loader::read_int_checked(const jsondom::value& json, const std:
}

inline bool gltf_loader::read_uint32_checked(const jsondom::value& json, const std::string& name, uint32_t& value)
// TODO: implement read_string_default instead, return value normally
{
auto it = json.object().find(name);
if (it != json.object().end()) {
Expand Down Expand Up @@ -350,55 +352,58 @@ utki::shared_ref<mesh> gltf_loader::read_mesh(const jsondom::value& mesh_json)
std::cout << accessors.size() << std::endl;

// auto vbo_bitangents = render_factory_.create_vertex_buffer(utki::make_span(bitangents));
//utki::shared_ref<vertex_array> vao;
if(tangent_accessor > 0)
{
// utki::shared_ref<vertex_array> vao;
if (tangent_accessor > 0) {
// auto vao = render_factory_.create_vertex_array(
// {
// utki::shared_ref<ruis::render::vertex_buffer>(accessors[position_accessor].get().vbo),
// utki::shared_ref<ruis::render::vertex_buffer>(accessors[texcoord_0_accessor].get().vbo),
// utki::shared_ref<ruis::render::vertex_buffer>(accessors[normal_accessor].get().vbo),
// utki::shared_ref<ruis::render::vertex_buffer>(accessors[tangent_accessor].get().vbo)
// // , vbo_bitangents
// },
// utki::shared_ref<ruis::render::index_buffer>(accessors[index_accessor].get().ibo),
// ruis::render::vertex_array::mode::triangles
// );
auto vao = render_factory_.create_vertex_array(
{
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[position_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[texcoord_0_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[normal_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[tangent_accessor].get().vbo)
)
// , vbo_bitangents
},
{utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[position_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[texcoord_0_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[normal_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[tangent_accessor].get().vbo)
)},
utki::shared_ref<ruis::render::index_buffer>(
std::shared_ptr<ruis::render::index_buffer>(accessors[index_accessor].get().ibo)
),
ruis::render::vertex_array::mode::triangles
);
auto material_ = utki::make_shared<material>();
primitives.push_back(utki::make_shared<primitive>(vao, material_));
}
else
{
} else {
auto vao = render_factory_.create_vertex_array(
{
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[position_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[texcoord_0_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[normal_accessor].get().vbo)
)
},
{utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[position_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[texcoord_0_accessor].get().vbo)
),
utki::shared_ref<ruis::render::vertex_buffer>(
std::shared_ptr<ruis::render::vertex_buffer>(accessors[normal_accessor].get().vbo)
)},
utki::shared_ref<ruis::render::index_buffer>(
std::shared_ptr<ruis::render::index_buffer>(accessors[index_accessor].get().ibo)
),
ruis::render::vertex_array::mode::triangles
);
);
auto material_ = utki::make_shared<material>();
primitives.push_back(utki::make_shared<primitive>(vao, material_));
}
}
}

auto new_mesh = utki::make_shared<mesh>(primitives, name);
Expand Down Expand Up @@ -447,6 +452,85 @@ utki::shared_ref<scene> gltf_loader::read_scene(const jsondom::value& scene_json
return new_scene;
}

utki::shared_ref<image_l> gltf_loader::read_image(const jsondom::value& image_json)
{
[[maybe_unused]] bool ok;
uint32_t buffer_view_index = 0;
[[maybe_unused]] uint32_t mime_type = 0;
std::string name;
std::string mime_type_string;

ok = read_string_checked(image_json, "name", name);
ok = read_uint32_checked(image_json, "bufferView", buffer_view_index);
ok = read_string_checked(image_json, "mimeType", mime_type_string); // TODO: is a string

auto new_image =
utki::make_shared<image_l>(name, buffer_views[buffer_view_index], static_cast<image_l::mime_type>(0)); //// !!!!
return new_image;
}

utki::shared_ref<sampler_l> gltf_loader::read_sampler(const jsondom::value& sampler_json)
{
[[maybe_unused]] bool ok;
uint32_t min_filter = 0;
uint32_t mag_filter = 0;
uint32_t wrap_s = 0;
uint32_t wrap_t = 0;

ok = read_uint32_checked(sampler_json, "minFilter", min_filter);
ok = read_uint32_checked(sampler_json, "magFilter", mag_filter);
ok = read_uint32_checked(sampler_json, "wrapS", wrap_s);
ok = read_uint32_checked(sampler_json, "wrapT", wrap_t);

auto new_sampler = utki::make_shared<sampler_l>(
static_cast<sampler_l::filter>(min_filter),
static_cast<sampler_l::filter>(mag_filter),
static_cast<sampler_l::wrap>(wrap_s),
static_cast<sampler_l::wrap>(wrap_t)
);
return new_sampler;
}

utki::shared_ref<ruis::render::texture_2d> gltf_loader::read_texture(const jsondom::value& texture_json)
{
[[maybe_unused]] bool ok;
uint32_t image_index = 0;
uint32_t sampler_index = 0;
ok = read_uint32_checked(texture_json, "source", image_index);
ok = read_uint32_checked(texture_json, "sampler", sampler_index);

auto image = images[image_index];
auto sampler = samplers[sampler_index];

auto image_span = glb_binary_buffer.subspan(image.get().bv.get().byte_offset, image.get().bv.get().byte_length);
const papki::span_file fi(image_span);

rasterimage::image_variant imvar;
if (image.get().mime_type_ == image_l::mime_type::image_png) {
imvar = rasterimage::read_png(fi);
} else if (image.get().mime_type_ == image_l::mime_type::image_jpeg) {
imvar = rasterimage::read_jpeg(fi);
} else {
throw std::invalid_argument("gltf: unknown texture image format");
}

ruis::render::render_factory::texture_2d_parameters tex_params; // TODO: fill texparams properly base on gltf file
tex_params.mag_filter = ruis::render::texture_2d::filter::linear;
tex_params.min_filter = ruis::render::texture_2d::filter::linear;
tex_params.mipmap = texture_2d::mipmap::linear;

return render_factory_.create_texture_2d(std::move(imvar), tex_params);

// auto new_texture =
// utki::make_shared<image_l>(name, buffer_views[buffer_view_index], static_cast<image_l::mime_type>(mime_type));
// return new_texture;
}

utki::shared_ref<material> gltf_loader::read_material(const jsondom::value& material_json)
{
return utki::make_shared<material>();
}

utki::shared_ref<scene> gltf_loader::load(const papki::file& fi)
{
auto gltf = fi.load();
Expand Down Expand Up @@ -514,6 +598,56 @@ utki::shared_ref<scene> gltf_loader::load(const papki::file& fi)
}
}

//////////////////////////////////////////////////////////////////////

// std::cout << "loading images" << std::endl;
// {
// auto it = json.object().find("images");
// if (it == json.object().end() || !it->second.is_array()) {
// throw std::invalid_argument("read_gltf(): glTF does not have any valid images");
// } else {
// for (const auto& sub_json : it->second.array()) {
// images.push_back(read_image(sub_json));
// }
// }
// }

// std::cout << "loading samplers" << std::endl;
// {
// auto it = json.object().find("samplers");
// if (it == json.object().end() || !it->second.is_array()) {
// throw std::invalid_argument("read_gltf(): glTF does not have any valid samplers");
// } else {
// for (const auto& sub_json : it->second.array()) {
// samplers.push_back(read_sampler(sub_json));
// }
// }
// }

// std::cout << "loading textures" << std::endl;
// {
// auto it = json.object().find("textures");
// if (it == json.object().end() || !it->second.is_array()) {
// throw std::invalid_argument("read_gltf(): glTF does not have any valid textures");
// } else {
// for (const auto& sub_json : it->second.array()) {
// textures.push_back(read_texture(sub_json));
// }
// }
// }

// std::cout << "loading materials" << std::endl;
// {
// auto it = json.object().find("materials");
// if (it == json.object().end() || !it->second.is_array()) {
// throw std::invalid_argument("read_gltf(): glTF does not have any valid materials");
// } else {
// for (const auto& sub_json : it->second.array()) {
// materials.push_back(read_material(sub_json));
// }
// }
// }

std::cout << "loading accessors" << std::endl;
{
auto it = json.object().find("accessors");
Expand All @@ -530,7 +664,7 @@ utki::shared_ref<scene> gltf_loader::load(const papki::file& fi)
// load meshes
auto meshes_it = json.object().find("meshes");
if (meshes_it == json.object().end() || !meshes_it->second.is_array()) {
throw std::invalid_argument("read_gltf(): glTF does not have any valid nodes");
throw std::invalid_argument("read_gltf(): glTF does not have any valid meshes");
} else {
// load all meshes into the scene object (actually, there should be an uber-object for mesh storage, but we
// currenetly support only one scene)
Expand Down Expand Up @@ -571,8 +705,6 @@ utki::shared_ref<scene> gltf_loader::load(const papki::file& fi)
}
}

// const papki::span_file fi(data);

// create scene
auto active_scene = utki::make_shared<scene>();
int active_scene_index = -1;
Expand Down
64 changes: 64 additions & 0 deletions src/ruis/render/scene/gltf_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace ruis::render {

struct buffer_view;
struct accessor;
struct image_l;
struct sampler_l;
class material;

class gltf_loader
{
Expand All @@ -48,6 +51,12 @@ class gltf_loader
std::vector<utki::shared_ref<accessor>> accessors; // accessors.
std::vector<utki::shared_ref<buffer_view>> buffer_views; // bv's

// std::vector<utki::shared_ref<texture_l>> textures;
std::vector<utki::shared_ref<material>> materials;
std::vector<utki::shared_ref<ruis::render::texture_2d>> textures;
std::vector<utki::shared_ref<sampler_l>> samplers;
std::vector<utki::shared_ref<image_l>> images;

std::vector<std::vector<uint32_t>> child_indices; // storage for node child hierarchy (only during loading stage)

inline int read_int(const jsondom::value& json, const std::string& name);
Expand Down Expand Up @@ -79,6 +88,11 @@ class gltf_loader
utki::shared_ref<node> read_node(const jsondom::value& node_json);
utki::shared_ref<scene> read_scene(const jsondom::value& scene_json);

utki::shared_ref<image_l> read_image(const jsondom::value& image_json);
utki::shared_ref<sampler_l> read_sampler(const jsondom::value& sampler_json);
utki::shared_ref<ruis::render::texture_2d> read_texture(const jsondom::value& texture_json);
utki::shared_ref<material> read_material(const jsondom::value& material_json);

public:
utki::shared_ref<scene> load(const papki::file& fi);
gltf_loader(ruis::render::render_factory& render_factory_, bool use_short_indices);
Expand Down Expand Up @@ -145,4 +159,54 @@ struct accessor {
);
};

struct image_l { // TODO: put loader helper classes into separate namespace
std::string name;
utki::shared_ref<buffer_view> bv;
enum class mime_type {
undefined = 0,
image_jpeg = 1,
image_png = 2
} mime_type_;

image_l(std::string name, utki::shared_ref<buffer_view> bv, mime_type mime_type_) :
name(std::move(name)),
bv(std::move(bv)),
mime_type_(mime_type_)
{}
};

struct sampler_l {
enum class filter {
nearest = 9728,
linear = 9729,
nearest_mipmap_nearest = 9984,
linear_mipmap_nearest = 9985,
nearest_mipmap_linear = 9986,
linear_mipmap_linear = 9987
};

enum class wrap {
clamp_to_edge = 33071,
mirrored_repeat = 33648,
repeat = 10497
};

filter min;
filter mag;
wrap wrap_s;
wrap wrap_t;

sampler_l(filter min, filter mag, wrap wrap_s, wrap wrap_t) :
min(min),
mag(mag),
wrap_s(wrap_s),
wrap_t(wrap_t)
{}
};

// struct texture_l {
// utki::shared_ref<image_l> source;
// utki::shared_ref<sampler_l> sampler;
// };

} // namespace ruis::render
8 changes: 5 additions & 3 deletions src/ruis/render/scene/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once

#include <ruis/render/shader.hpp>
#include <ruis/render/texture_2d.hpp>
#include <ruis/render/vertex_array.hpp>

namespace ruis::render {

class material
{
public:
std::shared_ptr<ruis::render::shader> shader;
// textures resources ?
std::string name;
std::shared_ptr<ruis::render::texture_2d> tex_diffuse;
std::shared_ptr<ruis::render::texture_2d> tex_normal;
std::shared_ptr<ruis::render::texture_2d> tex_arm; // ambient occlusion, roughness, metalness
};

class primitive
Expand Down
Loading

0 comments on commit 066c57c

Please sign in to comment.