From 80fe90c0a531e625270693d32df7e89e5c82d270 Mon Sep 17 00:00:00 2001 From: Nyran Date: Sat, 3 May 2014 10:33:45 +0200 Subject: [PATCH 01/45] initial commit with skeletal pbr classes --- include/gua/renderer/PBRLoader.hpp | 85 +++++++++ include/gua/renderer/PBRRessource.hpp | 115 ++++++++++++ include/gua/renderer/PBRUberShader.hpp | 66 +++++++ include/gua/scenegraph/PBRNode.hpp | 61 +++++++ src/gua/renderer/PBRLoader.cpp | 210 ++++++++++++++++++++++ src/gua/renderer/PBRRessource.cpp | 233 +++++++++++++++++++++++++ src/gua/renderer/PBRUberShader.cpp | 164 +++++++++++++++++ src/gua/scenegraph/PBRNode.cpp | 173 ++++++++++++++++++ 8 files changed, 1107 insertions(+) create mode 100644 include/gua/renderer/PBRLoader.hpp create mode 100644 include/gua/renderer/PBRRessource.hpp create mode 100644 include/gua/renderer/PBRUberShader.hpp create mode 100644 include/gua/scenegraph/PBRNode.hpp create mode 100644 src/gua/renderer/PBRLoader.cpp create mode 100644 src/gua/renderer/PBRRessource.cpp create mode 100644 src/gua/renderer/PBRUberShader.cpp create mode 100644 src/gua/scenegraph/PBRNode.cpp diff --git a/include/gua/renderer/PBRLoader.hpp b/include/gua/renderer/PBRLoader.hpp new file mode 100644 index 000000000..71dac57e2 --- /dev/null +++ b/include/gua/renderer/PBRLoader.hpp @@ -0,0 +1,85 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +#ifndef GUA_PBR_LOADER_HPP +#define GUA_PBR_LOADER_HPP + +#include +#include + +namespace gua{ + +class Node; +class InnerNode; +class GeometryNode; + +class PBRLoader : public LoaderBase{ + public: + + /** + * Default constructor. + * + * Constructs a new and empty PBRLoader. + */ + PBRLoader(); + + /** + * Constructor from a file. + * + * Creates a new PBRLoader from a given file. + * + * \param file_name The file to load the pointclouds data from. + * \param material_name The material name that was set to the parent node + */ + std::shared_ptr load(std::string const& file_name, + unsigned flags); + + /** + * Constructor from memory buffer. + * + * Creates a new MeshLoader from a existing memory buffer. + * + * \param buffer_name The buffer to load the meh's data from. + * \param buffer_size The buffer's size. + */ + std::vector const load_from_buffer(char const* buffer_name, + unsigned buffer_size, + bool build_kd_tree); + + bool is_supported(std::string const& file_name) const; + + private: + + std::shared_ptr get_tree(std::shared_ptr const& importer, + aiScene const* ai_scene, + aiNode* ai_root, + std::string const& file_name, + unsigned flags, unsigned& mesh_count); + + unsigned node_counter_; + + static unsigned mesh_counter_; +}; + + +} + +#endif // GUA_PBR_LOADER_HPP diff --git a/include/gua/renderer/PBRRessource.hpp b/include/gua/renderer/PBRRessource.hpp new file mode 100644 index 000000000..db387dd1d --- /dev/null +++ b/include/gua/renderer/PBRRessource.hpp @@ -0,0 +1,115 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +#ifndef GUA_PBR_RESSOURCE_HPP +#define GUA_PBR_RESSOURCE_HPP + +// guacamole headers +#include +#include +#include +#include + +// external headers +#include + +#include +#include + +#include + +struct aiMesh; + +namespace Assimp { class Importer; } + +namespace gua { + +struct RenderContext; + +/** + * Stores geometry data. + * + * A mesh can be loaded from an Assimp mesh and the draw onto multiple + * contexts. + * Do not use this class directly, it is just used by the Geometry class to + * store the individual meshes of a file. + */ +class PBRRessource : public GeometryRessource { + public: + + /** + * Default constructor. + * + * Creates a new and empty Mesh. + */ + PBRRessource(); + + /** + * Constructor from an Assimp mesh. + * + * Initializes the mesh from a given Assimp mesh. + * + * \param mesh The Assimp mesh to load the data from. + */ + PBRRessource(aiMesh* mesh, std::shared_ptr const& importer, bool build_kd_tree); + + /** + * Draws the Mesh. + * + * Draws the Mesh to the given context. + * + * \param context The RenderContext to draw onto. + */ + void draw(RenderContext const& context) const; + + void ray_test(Ray const& ray, PickResult::Options options, + Node* owner, std::set& hits); + + unsigned int num_vertices() const; + + unsigned int num_faces() const; + + scm::math::vec3 get_vertex(unsigned int i) const; + + std::vector get_face(unsigned int i) const; + + /*virtual*/ GeometryUberShader* get_ubershader() const; + + private: + + void upload_to(RenderContext const& context) const; + + mutable std::vector vertices_; + mutable std::vector indices_; + mutable std::vector vertex_array_; + mutable std::mutex upload_mutex_; + + public: + + KDTree kd_tree_; + + aiMesh* mesh_; + std::shared_ptr importer_; +}; + +} + +#endif // GUA_PBR_RESSOURCE_HPP diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp new file mode 100644 index 000000000..49e061833 --- /dev/null +++ b/include/gua/renderer/PBRUberShader.hpp @@ -0,0 +1,66 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +#ifndef GUA_PBR_UBER_SHADER_HPP +#define GUA_PBR_UBER_SHADER_HPP + +// guacamole headers +#include + +namespace gua { + +class PBRUberShader : public GeometryUberShader { + + public: + + void create (std::set const& material_names); + + /*virtual*/ stage_mask const get_stage_mask() const; + + /*virtual*/ void preframe (RenderContext const& context) const; + + /*virtual*/ void predraw (RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const; + + /*virtual*/ void draw (RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const; + + /*virtual*/ void postdraw (RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const; + + /*virtual*/ void postframe (RenderContext const& context) const; +}; + +} + +#endif // GUA_PBR_UBER_SHADER_HPP diff --git a/include/gua/scenegraph/PBRNode.hpp b/include/gua/scenegraph/PBRNode.hpp new file mode 100644 index 000000000..679a23f10 --- /dev/null +++ b/include/gua/scenegraph/PBRNode.hpp @@ -0,0 +1,61 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +#ifndef GUA_PBR_NODE_HPP +#define GUA_PBR_NODE_HPP + +// guacamole headers +#include + +namespace gua { + +/** + * This class is used to represent pointcloud in the SceneGraph. + * + * \ingroup gua_scenegraph + */ +class GUA_DLL PBRNode : public GeometryNode +{ +public : // member + + PBRNode(std::string const& name, + std::string const& geometry = "gua_default_geometry", + std::string const& material = "gua_default_material", + math::mat4 const& transform = math::mat4::identity()); + + /** + * Implements ray picking for a triangular mesh + */ + /* virtual */ void ray_test_impl(RayNode const& ray, + PickResult::Options options, + Mask const& mask, + std::set& hits); +protected: + + /*virtual*/ std::shared_ptr copy() const; + +private : // attributes e.g. special attributes for drawing + +}; + +} + +#endif // GUA_PBR_NODE_HPP diff --git a/src/gua/renderer/PBRLoader.cpp b/src/gua/renderer/PBRLoader.cpp new file mode 100644 index 000000000..3eb542947 --- /dev/null +++ b/src/gua/renderer/PBRLoader.cpp @@ -0,0 +1,210 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +// class header +#include + +// guacamole headers +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace gua { + + unsigned PBRLoader::mesh_counter_ = 0; + + ///////////////////////////////////////////////////////////////////////////// + + PBRLoader::PBRLoader() + : node_counter_(0) {} + + ///////////////////////////////////////////////////////////////////////////// + + std::shared_ptr PBRLoader::load(std::string const& file_name, + unsigned flags) { + + node_counter_ = 0; + TextFile file(file_name); + + // MESSAGE("Loading mesh file %s", file_name.c_str()); + + if (file.is_valid()) { + auto importer = std::make_shared(); + + importer->SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, + aiPrimitiveType_POINT | aiPrimitiveType_LINE); + + if ((flags & GeometryLoader::OPTIMIZE_GEOMETRY) && + (flags & GeometryLoader::LOAD_MATERIALS)) { + + importer->SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, aiComponent_COLORS); + importer->ReadFile( + file_name, + aiProcessPreset_TargetRealtime_MaxQuality | aiProcess_GenNormals | + aiProcess_RemoveComponent | aiProcess_OptimizeGraph | + aiProcess_PreTransformVertices); + + } else if (flags & GeometryLoader::OPTIMIZE_GEOMETRY) { + + importer->SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, + aiComponent_COLORS | aiComponent_MATERIALS); + importer->ReadFile( + file_name, + aiProcessPreset_TargetRealtime_MaxQuality | aiProcess_GenNormals | + aiProcess_RemoveComponent | aiProcess_OptimizeGraph | + aiProcess_PreTransformVertices); + } else { + + importer->ReadFile( + file_name, + aiProcessPreset_TargetRealtime_Quality | aiProcess_GenNormals); + + } + + aiScene const* scene(importer->GetScene()); + + std::shared_ptr new_node; + + if (scene->mRootNode) { + // new_node = std::make_shared(new GeometryNode("unnamed", + // GeometryNode::Configuration("", ""), + // math::mat4::identity())); + unsigned count(0); + new_node = get_tree(importer, scene, scene->mRootNode, file_name, flags, count); + + } else { + Logger::LOG_WARNING << "Failed to load object \"" << file_name << "\": No valid root node contained!" << std::endl; + } + + return new_node; + + } + + Logger::LOG_WARNING << "Failed to load object \"" << file_name << "\": File does not exist!" << std::endl; + + return nullptr; +} + + ///////////////////////////////////////////////////////////////////////////// + +std::vector const PBRLoader::load_from_buffer(char const* buffer_name, + unsigned buffer_size, + bool build_kd_tree) { + + auto importer = std::make_shared(); + + aiScene const* scene(importer->ReadFileFromMemory( + buffer_name, + buffer_size, + aiProcessPreset_TargetRealtime_Quality | aiProcess_CalcTangentSpace)); + + std::vector meshes; + + for (unsigned int n = 0; n < scene->mNumMeshes; ++n) { + meshes.push_back(new PBRRessource(scene->mMeshes[n], importer, build_kd_tree)); + } + + return meshes; + +} + +bool PBRLoader::is_supported(std::string const& file_name) const { + auto point_pos(file_name.find_last_of(".")); + Assimp::Importer importer; + + if (file_name.substr(point_pos + 1) == "raw"){ + return false; + } + + return importer.IsExtensionSupported(file_name.substr(point_pos + 1)); +} + +std::shared_ptr PBRLoader::get_tree(std::shared_ptr const& importer, + aiScene const* ai_scene, + aiNode* ai_root, + std::string const& file_name, + unsigned flags, unsigned& mesh_count) { + + // creates a geometry node and returns it + auto load_geometry = [&](int i) { + // load geometry + std::string mesh_name("type=file&file=" + file_name + "&id=" + string_utils::to_string(mesh_count++) + "&flags=" + string_utils::to_string(flags)); + GeometryDatabase::instance()->add(mesh_name, std::make_shared(ai_scene->mMeshes[ai_root->mMeshes[i]], importer, flags & GeometryLoader::MAKE_PICKABLE)); + + // load material + std::string material_name(""); + unsigned material_index(ai_scene->mMeshes[ai_root->mMeshes[i]]->mMaterialIndex); + + if (material_index != 0 && flags & GeometryLoader::LOAD_MATERIALS) { + MaterialLoader material_loader; + aiMaterial const* material(ai_scene->mMaterials[material_index]); + material_name = material_loader.load_material(material, file_name); + } + + auto result(std::make_shared(mesh_name)); + result->set_filename(mesh_name); + result->set_material(material_name); + + return result; + }; + + // there is only one child -- skip it! + if (ai_root->mNumChildren == 1 && ai_root->mNumMeshes == 0) { + return get_tree( + importer, ai_scene, ai_root->mChildren[0], + file_name, flags, mesh_count + ); + } + + // there is only one geometry --- return it! + if (ai_root->mNumChildren == 0 && ai_root->mNumMeshes == 1) { + return load_geometry(0); + } + + // else: there are multiple children and meshes + auto group(std::make_shared()); + + for (unsigned i(0); i < ai_root->mNumMeshes; ++i) { + group->add_child(load_geometry(i)); + } + + for (unsigned i(0); i < ai_root->mNumChildren; ++i) { + group->add_child( + get_tree( + importer, ai_scene, ai_root->mChildren[i], + file_name, flags, mesh_count + ) + ); + } + + return group; +} + +} diff --git a/src/gua/renderer/PBRRessource.cpp b/src/gua/renderer/PBRRessource.cpp new file mode 100644 index 000000000..e180c915a --- /dev/null +++ b/src/gua/renderer/PBRRessource.cpp @@ -0,0 +1,233 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +// class header +#include + +// guacamole headers +#include +#include +#include +#include + +// external headers +#if ASSIMP_VERSION == 3 +#include +#include +#else +#include +#include +#include +#endif + +namespace { +struct Vertex { + scm::math::vec3f pos; + scm::math::vec2f tex; + scm::math::vec3f normal; + scm::math::vec3f tangent; + scm::math::vec3f bitangent; +}; +} + +namespace gua { + +//////////////////////////////////////////////////////////////////////////////// + +PBRRessource::PBRRessource() + : vertices_(), indices_(), vertex_array_(), upload_mutex_(), mesh_(nullptr) {} + +//////////////////////////////////////////////////////////////////////////////// + +PBRRessource::PBRRessource(aiMesh* mesh, std::shared_ptr const& importer, + bool build_kd_tree) + : vertices_(), + indices_(), + vertex_array_(), + upload_mutex_(), + mesh_(mesh), + importer_(importer) { + + if (mesh_->HasPositions()) { + bounding_box_ = math::BoundingBox(); + + for (unsigned v(0); v < mesh_->mNumVertices; ++v) { + bounding_box_.expandBy(scm::math::vec3( + mesh_->mVertices[v].x, mesh_->mVertices[v].y, mesh_->mVertices[v].z)); + } + + if (build_kd_tree) { + kd_tree_.generate(mesh_); + } + } +} + +//////////////////////////////////////////////////////////////////////////////// + +void PBRRessource::upload_to(RenderContext const& ctx) const { + + if (!mesh_->HasPositions()) { + Logger::LOG_WARNING << "Unable to load Mesh! Has no vertex data." << std::endl; + return; + } + + std::unique_lock lock(upload_mutex_); + + if (vertices_.size() <= ctx.id) { + vertices_.resize(ctx.id + 1); + indices_.resize(ctx.id + 1); + vertex_array_.resize(ctx.id + 1); + } + + vertices_[ctx.id] = + ctx.render_device->create_buffer(scm::gl::BIND_VERTEX_BUFFER, + scm::gl::USAGE_STATIC_DRAW, + mesh_->mNumVertices * sizeof(Vertex), + 0); + + + + Vertex* data(static_cast(ctx.render_context->map_buffer( + vertices_[ctx.id], scm::gl::ACCESS_WRITE_INVALIDATE_BUFFER))); + + for (unsigned v(0); v < mesh_->mNumVertices; ++v) { + data[v].pos = scm::math::vec3( + mesh_->mVertices[v].x, mesh_->mVertices[v].y, mesh_->mVertices[v].z); + + if (mesh_->HasTextureCoords(0)) { + data[v].tex = scm::math::vec2(mesh_->mTextureCoords[0][v].x, + mesh_->mTextureCoords[0][v].y); + } else { + data[v].tex = scm::math::vec2(0.f, 0.f); + } + + if (mesh_->HasNormals()) { + data[v].normal = scm::math::vec3( + mesh_->mNormals[v].x, mesh_->mNormals[v].y, mesh_->mNormals[v].z); + } else { + data[v].normal = scm::math::vec3(0.f, 0.f, 0.f); + } + + if (mesh_->HasTangentsAndBitangents()) { + data[v].tangent = scm::math::vec3( + mesh_->mTangents[v].x, mesh_->mTangents[v].y, mesh_->mTangents[v].z); + + data[v].bitangent = scm::math::vec3(mesh_->mBitangents[v].x, + mesh_->mBitangents[v].y, + mesh_->mBitangents[v].z); + } else { + data[v].tangent = scm::math::vec3(0.f, 0.f, 0.f); + data[v].bitangent = scm::math::vec3(0.f, 0.f, 0.f); + } + } + + ctx.render_context->unmap_buffer(vertices_[ctx.id]); + + std::vector index_array(mesh_->mNumFaces * 3); + + for (unsigned t = 0; t < mesh_->mNumFaces; ++t) { + const struct aiFace* face = &mesh_->mFaces[t]; + + index_array[t * 3] = face->mIndices[0]; + index_array[t * 3 + 1] = face->mIndices[1]; + index_array[t * 3 + 2] = face->mIndices[2]; + } + + indices_[ctx.id] = + ctx.render_device->create_buffer(scm::gl::BIND_INDEX_BUFFER, + scm::gl::USAGE_STATIC_DRAW, + mesh_->mNumFaces * 3 * sizeof(unsigned), + &index_array[0]); + + std::vector buffer_arrays; + buffer_arrays.push_back(vertices_[ctx.id]); + + vertex_array_[ctx.id] = ctx.render_device->create_vertex_array( + scm::gl::vertex_format(0, 0, scm::gl::TYPE_VEC3F, sizeof(Vertex))( + 0, 1, scm::gl::TYPE_VEC2F, sizeof(Vertex))( + 0, 2, scm::gl::TYPE_VEC3F, sizeof(Vertex))( + 0, 3, scm::gl::TYPE_VEC3F, sizeof(Vertex))( + 0, 4, scm::gl::TYPE_VEC3F, sizeof(Vertex)), + buffer_arrays); + +} + +//////////////////////////////////////////////////////////////////////////////// + +void PBRRessource::draw(RenderContext const& ctx) const { + + // upload to GPU if neccessary + if (vertices_.size() <= ctx.id || vertices_[ctx.id] == nullptr) { + upload_to(ctx); + } + + scm::gl::context_vertex_input_guard vig(ctx.render_context); + + ctx.render_context->bind_vertex_array(vertex_array_[ctx.id]); + + ctx.render_context->bind_index_buffer( + indices_[ctx.id], scm::gl::PRIMITIVE_TRIANGLE_LIST, scm::gl::TYPE_UINT); + + ctx.render_context->apply(); + ctx.render_context->draw_elements(mesh_->mNumFaces * 3); +} + +//////////////////////////////////////////////////////////////////////////////// + +void PBRRessource::ray_test(Ray const& ray, PickResult::Options options, + Node* owner, std::set& hits) { + + kd_tree_.ray_test(ray, mesh_, options, owner, hits); +} + +//////////////////////////////////////////////////////////////////////////////// + +unsigned int PBRRessource::num_vertices() const { return mesh_->mNumVertices; } + +//////////////////////////////////////////////////////////////////////////////// + +unsigned int PBRRessource::num_faces() const { return mesh_->mNumFaces; } + +//////////////////////////////////////////////////////////////////////////////// + +scm::math::vec3 PBRRessource::get_vertex(unsigned int i) const { + + return scm::math::vec3( + mesh_->mVertices[i].x, mesh_->mVertices[i].y, mesh_->mVertices[i].z); +} + +//////////////////////////////////////////////////////////////////////////////// + +std::vector PBRRessource::get_face(unsigned int i) const { + + std::vector face(mesh_->mFaces[i].mNumIndices); + for (unsigned int j = 0; j < mesh_->mFaces[i].mNumIndices; ++j) + face[j] = mesh_->mFaces[i].mIndices[j]; + return face; +} + +//////////////////////////////////////////////////////////////////////////////// + +/*virtual*/ GeometryUberShader* PBRRessource::get_ubershader() const { + return Singleton::instance(); +} + +} diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp new file mode 100644 index 000000000..4a1457beb --- /dev/null +++ b/src/gua/renderer/PBRUberShader.cpp @@ -0,0 +1,164 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +// class header +#include + +// guacamole headers +#include +#include +#include +#include +#include + +namespace gua { + + //////////////////////////////////////////////////////////////////////////////// + + void PBRUberShader::create(std::set const& material_names) + { + // create ubershader factories for given material set + UberShader::create(material_names); + + // VERTEX SHADER ------------------------------------------------------------- + std::string vertex_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_mesh_mesh_vert) + ); + + // material specific uniforms + string_utils::replace(vertex_shader, "@uniform_definition", + get_uniform_mapping()->get_uniform_definition()); + + // output + string_utils::replace(vertex_shader, "@output_definition", + vshader_factory_->get_output_mapping().get_gbuffer_output_definition(false, true)); + + // print material specific methods + string_utils::replace(vertex_shader, "@material_methods", + UberShader::print_material_methods(*vshader_factory_)); + + // print main switch(es) + string_utils::replace(vertex_shader, "@material_switch", + UberShader::print_material_switch(*vshader_factory_)); + + // FRAGMENT SHADER ----------------------------------------------------------- + std::string fragment_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_mesh_mesh_frag) + ); + + // input from vertex shader + string_utils::replace(fragment_shader, "@input_definition", + vshader_factory_->get_output_mapping().get_gbuffer_output_definition(true, true)); + + // material specific uniforms + string_utils::replace(fragment_shader, "@uniform_definition", + get_uniform_mapping()->get_uniform_definition()); + + // outputs + string_utils::replace(fragment_shader, "@output_definition", + get_gbuffer_mapping()->get_gbuffer_output_definition(false, false)); + + // print material specific methods + string_utils::replace(fragment_shader, "@material_methods", + UberShader::print_material_methods(*fshader_factory_)); + + // print main switch(es) + string_utils::replace(fragment_shader, "@material_switch", + UberShader::print_material_switch(*fshader_factory_)); + + auto program = std::make_shared(); + program->create_from_sources(vertex_shader, fragment_shader); + add_program(program); + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ GeometryUberShader::stage_mask const PBRUberShader::get_stage_mask() const + { + return GeometryUberShader::DRAW_STAGE; + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PBRUberShader::preframe(RenderContext const& context) const + { + throw std::runtime_error("PBRUberShader::preframe(): not implemented"); + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PBRUberShader::predraw(RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const + { + throw std::runtime_error("PBRUberShader::predraw(): not implemented"); + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PBRUberShader::draw(RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const + { + auto geometry = std::static_pointer_cast(GeometryDatabase::instance()->lookup(filename)); + auto material = MaterialDatabase::instance()->lookup(material_name); + + get_program()->use(ctx); + { + if (material && geometry) + { + set_uniform(ctx, material->get_id(), "gua_material_id"); + set_uniform(ctx, model_matrix, "gua_model_matrix"); + set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + + geometry->draw(ctx); + } + } + get_program()->unuse(ctx); + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PBRUberShader::postdraw(RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const + { + throw std::runtime_error("PBRUberShader::postdraw(): not implemented"); + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PBRUberShader::postframe(RenderContext const& context) const + { + throw std::runtime_error("PBRUberShader::postframe(): not implemented"); + } + +} + diff --git a/src/gua/scenegraph/PBRNode.cpp b/src/gua/scenegraph/PBRNode.cpp new file mode 100644 index 000000000..69c1270a0 --- /dev/null +++ b/src/gua/scenegraph/PBRNode.cpp @@ -0,0 +1,173 @@ +#/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +// class header +#include + +#include +#include +#include +#include + +// guacamole headers + +namespace gua { + + //////////////////////////////////////////////////////////////////////////////// + PBRNode::PBRNode(std::string const& name, + std::string const& filename, + std::string const& material, + math::mat4 const& transform) + : GeometryNode(name, filename, material, transform) + {} + + //////////////////////////////////////////////////////////////////////////////// + + void PBRNode::ray_test_impl(RayNode const& ray, PickResult::Options options, + Mask const& mask, std::set& hits) { + + // first of all, check bbox + auto box_hits(ray.intersect(bounding_box_)); + + // ray did not intersect bbox -- therefore it wont intersect + if (box_hits.first == RayNode::END && box_hits.second == RayNode::END) { + return; + } + + // return if only first object shall be returned and the current first hit + // is in front of the bbox entry point and the ray does not start inside + // the bbox + if (options & PickResult::PICK_ONLY_FIRST_OBJECT + && hits.size() > 0 && hits.begin()->distance < box_hits.first + && box_hits.first != Ray::END) { + + return; + } + + // bbox is intersected, but check geometry only if mask tells us to check + if (get_filename() != "" && mask.check(get_groups())) { + + auto geometry(GeometryDatabase::instance()->lookup(get_filename())); + + if (geometry) { + + bool check_kd_tree(true); + + math::mat4 world_transform(get_world_transform()); + + // check for bounding box intersection of contained geometry if node + // has children (in this case, the bbox might be larger + // than the actual geometry) + if (has_children()) { + auto geometry_bbox(geometry->get_bounding_box()); + + math::BoundingBox inner_bbox; + inner_bbox.expandBy(world_transform * geometry_bbox.min); + inner_bbox.expandBy(world_transform * geometry_bbox.max); + inner_bbox.expandBy(world_transform * + math::vec3(geometry_bbox.min.x, + geometry_bbox.min.y, + geometry_bbox.max.z)); + inner_bbox.expandBy(world_transform * + math::vec3(geometry_bbox.min.x, + geometry_bbox.max.y, + geometry_bbox.min.z)); + inner_bbox.expandBy(world_transform * + math::vec3(geometry_bbox.min.x, + geometry_bbox.max.y, + geometry_bbox.max.z)); + inner_bbox.expandBy(world_transform * + math::vec3(geometry_bbox.max.x, + geometry_bbox.min.y, + geometry_bbox.max.z)); + inner_bbox.expandBy(world_transform * + math::vec3(geometry_bbox.max.x, + geometry_bbox.max.y, + geometry_bbox.min.z)); + inner_bbox.expandBy(world_transform * + math::vec3(geometry_bbox.max.x, + geometry_bbox.min.y, + geometry_bbox.min.z)); + + auto inner_hits(ray.intersect(inner_bbox)); + if (inner_hits.first == RayNode::END && + inner_hits.second == RayNode::END) + check_kd_tree = false; + } + + if (check_kd_tree) { + Ray world_ray(ray.get_world_ray()); + + math::mat4 ori_transform(scm::math::inverse(world_transform)); + + math::vec4 ori(world_ray.origin_[0], + world_ray.origin_[1], + world_ray.origin_[2], + 1.0); + math::vec4 dir(world_ray.direction_[0], + world_ray.direction_[1], + world_ray.direction_[2], + 0.0); + + ori = ori_transform * ori; + dir = ori_transform * dir; + + Ray object_ray(ori, dir, world_ray.t_max_); + geometry->ray_test(object_ray, options, this, hits); + + float const inf(std::numeric_limits::max()); + + if (options & PickResult::GET_WORLD_POSITIONS) { + + for (auto& hit : hits) { + if (hit.world_position == math::vec3(inf, inf, inf)) { + auto transformed(world_transform * math::vec4(hit.position.x, hit.position.y, hit.position.z, 0.0)); + hit.world_position = scm::math::vec3(transformed.x, transformed.y, transformed.z); + } + } + } + + if (options & PickResult::GET_WORLD_NORMALS) { + + math::mat4 normal_matrix(scm::math::inverse(scm::math::transpose(world_transform))); + for (auto& hit : hits) { + if (hit.world_normal == math::vec3(inf, inf, inf)) { + auto transformed(normal_matrix * math::vec4(hit.normal.x, hit.normal.y, hit.normal.z, 0.0)); + hit.world_normal = scm::math::normalize(scm::math::vec3(transformed.x, transformed.y, transformed.z)); + } + } + } + } + } + } + + for (auto child : get_children()) { + // test for intersection with each child + child->ray_test_impl(ray, options, mask, hits); + } + + } + + //////////////////////////////////////////////////////////////////////////////// + std::shared_ptr PBRNode::copy() const { + return std::make_shared(get_name(), filename_, material_, get_transform()); + } +} From 67e916121489a829aab4716709e19b8f6a197e27 Mon Sep 17 00:00:00 2001 From: Andrey Babanin Date: Sat, 3 May 2014 11:04:32 +0200 Subject: [PATCH 02/45] Fix compilation errors --- include/gua/renderer/PBRLoader.hpp | 6 -- src/gua/renderer/GBufferPass.cpp | 2 +- src/gua/renderer/PBRLoader.cpp | 147 +++-------------------------- 3 files changed, 12 insertions(+), 143 deletions(-) diff --git a/include/gua/renderer/PBRLoader.hpp b/include/gua/renderer/PBRLoader.hpp index 71dac57e2..31476a526 100644 --- a/include/gua/renderer/PBRLoader.hpp +++ b/include/gua/renderer/PBRLoader.hpp @@ -68,12 +68,6 @@ class PBRLoader : public LoaderBase{ private: - std::shared_ptr get_tree(std::shared_ptr const& importer, - aiScene const* ai_scene, - aiNode* ai_root, - std::string const& file_name, - unsigned flags, unsigned& mesh_count); - unsigned node_counter_; static unsigned mesh_counter_; diff --git a/src/gua/renderer/GBufferPass.cpp b/src/gua/renderer/GBufferPass.cpp index ecbde88c0..f28c3a6f2 100644 --- a/src/gua/renderer/GBufferPass.cpp +++ b/src/gua/renderer/GBufferPass.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/gua/renderer/PBRLoader.cpp b/src/gua/renderer/PBRLoader.cpp index 3eb542947..1fe36f0c7 100644 --- a/src/gua/renderer/PBRLoader.cpp +++ b/src/gua/renderer/PBRLoader.cpp @@ -38,17 +38,17 @@ namespace gua { - unsigned PBRLoader::mesh_counter_ = 0; +unsigned PBRLoader::mesh_counter_ = 0; ///////////////////////////////////////////////////////////////////////////// - PBRLoader::PBRLoader() +PBRLoader::PBRLoader() : node_counter_(0) {} ///////////////////////////////////////////////////////////////////////////// - std::shared_ptr PBRLoader::load(std::string const& file_name, - unsigned flags) { +std::shared_ptr PBRLoader::load(std::string const& file_name, + unsigned flags) { node_counter_ = 0; TextFile file(file_name); @@ -56,54 +56,9 @@ namespace gua { // MESSAGE("Loading mesh file %s", file_name.c_str()); if (file.is_valid()) { - auto importer = std::make_shared(); - - importer->SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, - aiPrimitiveType_POINT | aiPrimitiveType_LINE); - - if ((flags & GeometryLoader::OPTIMIZE_GEOMETRY) && - (flags & GeometryLoader::LOAD_MATERIALS)) { - - importer->SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, aiComponent_COLORS); - importer->ReadFile( - file_name, - aiProcessPreset_TargetRealtime_MaxQuality | aiProcess_GenNormals | - aiProcess_RemoveComponent | aiProcess_OptimizeGraph | - aiProcess_PreTransformVertices); - - } else if (flags & GeometryLoader::OPTIMIZE_GEOMETRY) { - - importer->SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, - aiComponent_COLORS | aiComponent_MATERIALS); - importer->ReadFile( - file_name, - aiProcessPreset_TargetRealtime_MaxQuality | aiProcess_GenNormals | - aiProcess_RemoveComponent | aiProcess_OptimizeGraph | - aiProcess_PreTransformVertices); - } else { - - importer->ReadFile( - file_name, - aiProcessPreset_TargetRealtime_Quality | aiProcess_GenNormals); - - } - - aiScene const* scene(importer->GetScene()); - - std::shared_ptr new_node; - - if (scene->mRootNode) { - // new_node = std::make_shared(new GeometryNode("unnamed", - // GeometryNode::Configuration("", ""), - // math::mat4::identity())); - unsigned count(0); - new_node = get_tree(importer, scene, scene->mRootNode, file_name, flags, count); - - } else { - Logger::LOG_WARNING << "Failed to load object \"" << file_name << "\": No valid root node contained!" << std::endl; - } - - return new_node; + + //return new_node; + return nullptr; } @@ -115,96 +70,16 @@ namespace gua { ///////////////////////////////////////////////////////////////////////////// std::vector const PBRLoader::load_from_buffer(char const* buffer_name, - unsigned buffer_size, - bool build_kd_tree) { - - auto importer = std::make_shared(); - - aiScene const* scene(importer->ReadFileFromMemory( - buffer_name, - buffer_size, - aiProcessPreset_TargetRealtime_Quality | aiProcess_CalcTangentSpace)); - - std::vector meshes; - - for (unsigned int n = 0; n < scene->mNumMeshes; ++n) { - meshes.push_back(new PBRRessource(scene->mMeshes[n], importer, build_kd_tree)); - } - - return meshes; + unsigned buffer_size, + bool build_kd_tree) { + return std::vector(); } bool PBRLoader::is_supported(std::string const& file_name) const { auto point_pos(file_name.find_last_of(".")); - Assimp::Importer importer; - - if (file_name.substr(point_pos + 1) == "raw"){ - return false; - } - - return importer.IsExtensionSupported(file_name.substr(point_pos + 1)); -} - -std::shared_ptr PBRLoader::get_tree(std::shared_ptr const& importer, - aiScene const* ai_scene, - aiNode* ai_root, - std::string const& file_name, - unsigned flags, unsigned& mesh_count) { - - // creates a geometry node and returns it - auto load_geometry = [&](int i) { - // load geometry - std::string mesh_name("type=file&file=" + file_name + "&id=" + string_utils::to_string(mesh_count++) + "&flags=" + string_utils::to_string(flags)); - GeometryDatabase::instance()->add(mesh_name, std::make_shared(ai_scene->mMeshes[ai_root->mMeshes[i]], importer, flags & GeometryLoader::MAKE_PICKABLE)); - - // load material - std::string material_name(""); - unsigned material_index(ai_scene->mMeshes[ai_root->mMeshes[i]]->mMaterialIndex); - - if (material_index != 0 && flags & GeometryLoader::LOAD_MATERIALS) { - MaterialLoader material_loader; - aiMaterial const* material(ai_scene->mMaterials[material_index]); - material_name = material_loader.load_material(material, file_name); - } - - auto result(std::make_shared(mesh_name)); - result->set_filename(mesh_name); - result->set_material(material_name); - - return result; - }; - - // there is only one child -- skip it! - if (ai_root->mNumChildren == 1 && ai_root->mNumMeshes == 0) { - return get_tree( - importer, ai_scene, ai_root->mChildren[0], - file_name, flags, mesh_count - ); - } - - // there is only one geometry --- return it! - if (ai_root->mNumChildren == 0 && ai_root->mNumMeshes == 1) { - return load_geometry(0); - } - - // else: there are multiple children and meshes - auto group(std::make_shared()); - - for (unsigned i(0); i < ai_root->mNumMeshes; ++i) { - group->add_child(load_geometry(i)); - } - - for (unsigned i(0); i < ai_root->mNumChildren; ++i) { - group->add_child( - get_tree( - importer, ai_scene, ai_root->mChildren[i], - file_name, flags, mesh_count - ) - ); - } - return group; + return file_name.substr(point_pos + 1) == "kdn"; } } From 4ac1773a859fa2524ff86f8555dbcc724c3b4449 Mon Sep 17 00:00:00 2001 From: Andrey Babanin Date: Sat, 3 May 2014 12:18:09 +0200 Subject: [PATCH 03/45] Link to the PBR library --- CMakeLists.txt | 9 ++++ cmake/modules/FindPBR.cmake | 82 +++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 1 + 3 files changed, 92 insertions(+) create mode 100644 cmake/modules/FindPBR.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index bd9e62e8e..0d230adb5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,9 @@ endif (UNIX) find_package(Bullet REQUIRED) find_package(Json REQUIRED) +set(PBR_ROOT "../gualod/trunk/pbr" CACHE PATH "Set to your PBR install path.") +find_package(PBR REQUIRED) + set(LIB_PATHS ${ASSIMP_LIBRARY_DIRS} ${JSON_LIBRARY_DIRS} @@ -60,6 +63,7 @@ set(LIB_PATHS ${BOOST_LIBRARY_DIRS} ${BULLET_LIBRARY_DIRS} ${SCHISM_LIBRARY_DIRS} + ${PBR_LIBRARY_DIRS} ) set(INCLUDE_PATHS @@ -72,6 +76,7 @@ set(INCLUDE_PATHS ${GL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${BULLET_INCLUDE_DIRS} + ${PBR_INCLUDE_DIRS} ) if (NOT CMAKE_BUILD_TYPE) @@ -211,4 +216,8 @@ message(STATUS " bullet:" ) message(STATUS " library: ${BULLET_LIBRARIES}" ) message(STATUS " library path: ${BULLET_LIBRARY_DIRS}" ) message(STATUS " include: ${BULLET_INCLUDE_DIRS}" ) +message(STATUS " PBR:" ) +message(STATUS " library: ${PBR_LIBRARIES}" ) +message(STATUS " library path: ${PBR_LIBRARY_DIRS}" ) +message(STATUS " include: ${PBR_INCLUDE_DIRS}" ) message(STATUS "" ) diff --git a/cmake/modules/FindPBR.cmake b/cmake/modules/FindPBR.cmake new file mode 100644 index 000000000..b9cee2f51 --- /dev/null +++ b/cmake/modules/FindPBR.cmake @@ -0,0 +1,82 @@ +# - Try to find the point based renderer (PBR) +# +# This module defines the following variables +# +# PBR_FOUND - Was PBR found +# PBR_INCLUDE_DIRS - the PBR include directories +# PBR_LIBRARIES - Link to this, by default it includes rendering lib +# +# This module accepts the following variables +# +# PBR_ROOT - Can be set to PBR install path +# + +#============================================================================= +# Copyright 2009 Kitware, Inc. +# Copyright 2009 Philip Lowman +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +macro(_FIND_PBR_LIBRARY _var) + find_library(${_var} + NAMES + ${ARGN} + HINTS + ${PBR_ROOT} + PATH_SUFFIXES build + ) + mark_as_advanced(${_var}) +endmacro() + +macro(_PBR_APPEND_LIBRARIES _list _release) + set(_debug ${_release}_DEBUG) + if(${_debug}) + set(${_list} ${${_list}} optimized ${${_release}} debug ${${_debug}}) + else() + set(${_list} ${${_list}} ${${_release}}) + endif() +endmacro() + +# Find includes + +find_path(PBR_COMMON_INCLUDE_DIR + NAMES pbr/utils.h + HINTS + ${PBR_ROOT}/common/include + ${PBR_ROOT}/common/src +) + +find_path(PBR_RENDERING_INCLUDE_DIR + NAMES pbr/ren/renderer.h + HINTS + ${PBR_ROOT}/rendering/include + ${PBR_ROOT}/rendering/src +) + +# Find the libraries + +_FIND_PBR_LIBRARY(PBR_COMMON_LIBRARY pbr_common) +_FIND_PBR_LIBRARY(PBR_RENDERING_LIBRARY pbr_rendering) + + +# handle the QUIETLY and REQUIRED arguments and set PBR_FOUND to TRUE if +# all listed variables are TRUE +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(PBR DEFAULT_MSG + PBR_COMMON_LIBRARY PBR_RENDERING_LIBRARY + PBR_COMMON_INCLUDE_DIR PBR_RENDERING_INCLUDE_DIR) + +if(PBR_FOUND) + set(PBR_INCLUDE_DIRS ${PBR_COMMON_INCLUDE_DIR} ${PBR_RENDERING_INCLUDE_DIR}) + _PBR_APPEND_LIBRARIES(PBR_LIBRARIES PBR_COMMON_LIBRARY) + _PBR_APPEND_LIBRARIES(PBR_LIBRARIES PBR_RENDERING_LIBRARY) +endif() + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 03b554e99..29114ee54 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -103,6 +103,7 @@ ENDIF(MSVC) # link static libraries TARGET_LINK_LIBRARIES( guacamole general ${SCHISM_LIBRARIES}) TARGET_LINK_LIBRARIES( guacamole general ${ASSIMP_LIBRARIES}) +TARGET_LINK_LIBRARIES( guacamole general ${PBR_LIBRARIES}) IF (UNIX) TARGET_LINK_LIBRARIES( guacamole general ${BOOST_LIBRARIES}) From 0ee81e8d54d4f08a5eba175da314b455d51da46f Mon Sep 17 00:00:00 2001 From: Andrey Babanin Date: Sat, 3 May 2014 12:53:46 +0200 Subject: [PATCH 04/45] Add new hints in FindPBR.cmake --- cmake/modules/FindPBR.cmake | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/cmake/modules/FindPBR.cmake b/cmake/modules/FindPBR.cmake index b9cee2f51..c95b661bc 100644 --- a/cmake/modules/FindPBR.cmake +++ b/cmake/modules/FindPBR.cmake @@ -25,17 +25,6 @@ # (To distribute this file outside of CMake, substitute the full # License text for the above reference.) -macro(_FIND_PBR_LIBRARY _var) - find_library(${_var} - NAMES - ${ARGN} - HINTS - ${PBR_ROOT} - PATH_SUFFIXES build - ) - mark_as_advanced(${_var}) -endmacro() - macro(_PBR_APPEND_LIBRARIES _list _release) set(_debug ${_release}_DEBUG) if(${_debug}) @@ -63,9 +52,21 @@ find_path(PBR_RENDERING_INCLUDE_DIR # Find the libraries -_FIND_PBR_LIBRARY(PBR_COMMON_LIBRARY pbr_common) -_FIND_PBR_LIBRARY(PBR_RENDERING_LIBRARY pbr_rendering) +find_library(PBR_COMMON_LIBRARY + NAMES pbr_common + HINTS + ${PBR_ROOT}/build/common + ${PBR_ROOT}/build_codeblocks_rel/common + ${PBR_ROOT}/build_codeblocks/common +) +find_library(PBR_RENDERING_LIBRARY + NAMES pbr_rendering + HINTS + ${PBR_ROOT}/build/rendering + ${PBR_ROOT}/build_codeblocks_rel/rendering + ${PBR_ROOT}/build_codeblocks/rendering +) # handle the QUIETLY and REQUIRED arguments and set PBR_FOUND to TRUE if # all listed variables are TRUE From d0b919f12ecdf87b0deaa2201a2db07fc89b81c7 Mon Sep 17 00:00:00 2001 From: Nyran Date: Sat, 3 May 2014 15:40:32 +0200 Subject: [PATCH 05/45] Add basic functions for PBRLoader & Ressource --- include/gua/renderer/PBRLoader.hpp | 2 +- include/gua/renderer/PBRRessource.hpp | 24 ++-- src/gua/renderer/PBRLoader.cpp | 41 ++++--- src/gua/renderer/PBRRessource.cpp | 170 ++++++-------------------- 4 files changed, 72 insertions(+), 165 deletions(-) diff --git a/include/gua/renderer/PBRLoader.hpp b/include/gua/renderer/PBRLoader.hpp index 31476a526..5eda86cda 100644 --- a/include/gua/renderer/PBRLoader.hpp +++ b/include/gua/renderer/PBRLoader.hpp @@ -70,7 +70,7 @@ class PBRLoader : public LoaderBase{ unsigned node_counter_; - static unsigned mesh_counter_; + static unsigned model_counter_; }; diff --git a/include/gua/renderer/PBRRessource.hpp b/include/gua/renderer/PBRRessource.hpp index db387dd1d..f2b7f5c79 100644 --- a/include/gua/renderer/PBRRessource.hpp +++ b/include/gua/renderer/PBRRessource.hpp @@ -22,6 +22,8 @@ #ifndef GUA_PBR_RESSOURCE_HPP #define GUA_PBR_RESSOURCE_HPP + + // guacamole headers #include #include @@ -29,6 +31,7 @@ #include // external headers +#include #include #include @@ -36,9 +39,8 @@ #include -struct aiMesh; -namespace Assimp { class Importer; } + namespace gua { @@ -69,7 +71,7 @@ class PBRRessource : public GeometryRessource { * * \param mesh The Assimp mesh to load the data from. */ - PBRRessource(aiMesh* mesh, std::shared_ptr const& importer, bool build_kd_tree); + PBRRessource(std::shared_ptr point_cloud); /** * Draws the Mesh. @@ -83,13 +85,6 @@ class PBRRessource : public GeometryRessource { void ray_test(Ray const& ray, PickResult::Options options, Node* owner, std::set& hits); - unsigned int num_vertices() const; - - unsigned int num_faces() const; - - scm::math::vec3 get_vertex(unsigned int i) const; - - std::vector get_face(unsigned int i) const; /*virtual*/ GeometryUberShader* get_ubershader() const; @@ -97,17 +92,16 @@ class PBRRessource : public GeometryRessource { void upload_to(RenderContext const& context) const; - mutable std::vector vertices_; - mutable std::vector indices_; + mutable std::vector buffers_; mutable std::vector vertex_array_; mutable std::mutex upload_mutex_; public: - KDTree kd_tree_; + //KDTree kd_tree_; + + std::shared_ptr point_cloud_; - aiMesh* mesh_; - std::shared_ptr importer_; }; } diff --git a/src/gua/renderer/PBRLoader.cpp b/src/gua/renderer/PBRLoader.cpp index 1fe36f0c7..5effefcc5 100644 --- a/src/gua/renderer/PBRLoader.cpp +++ b/src/gua/renderer/PBRLoader.cpp @@ -38,7 +38,7 @@ namespace gua { -unsigned PBRLoader::mesh_counter_ = 0; +unsigned PBRLoader::model_counter_ = 0; ///////////////////////////////////////////////////////////////////////////// @@ -51,20 +51,31 @@ std::shared_ptr PBRLoader::load(std::string const& file_name, unsigned flags) { node_counter_ = 0; - TextFile file(file_name); - - // MESSAGE("Loading mesh file %s", file_name.c_str()); - - if (file.is_valid()) { - - //return new_node; - return nullptr; - - } - - Logger::LOG_WARNING << "Failed to load object \"" << file_name << "\": File does not exist!" << std::endl; - - return nullptr; + + std::shared_ptr new_node; + + std::shared_ptr point_cloud = std::make_shared(/*model_counter_*/); + if (point_cloud->Load(file_name)) + { + + // load point cloud + std::string model_name("type=file&file=" + file_name); + GeometryDatabase::instance()->add(model_name, std::make_shared(point_cloud)); + + ++model_counter_; + + + auto node(std::make_shared(model_name)); + + return node; + + } + else + { + Logger::LOG_WARNING << "Failed to load object \"" << file_name << "\": File does not exist!" << std::endl; + + return nullptr; + } } ///////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/renderer/PBRRessource.cpp b/src/gua/renderer/PBRRessource.cpp index e180c915a..51ba7d5d8 100644 --- a/src/gua/renderer/PBRRessource.cpp +++ b/src/gua/renderer/PBRRessource.cpp @@ -29,143 +29,71 @@ #include // external headers -#if ASSIMP_VERSION == 3 -#include -#include -#else -#include -#include -#include -#endif - -namespace { -struct Vertex { - scm::math::vec3f pos; - scm::math::vec2f tex; - scm::math::vec3f normal; - scm::math::vec3f tangent; - scm::math::vec3f bitangent; -}; -} + + namespace gua { //////////////////////////////////////////////////////////////////////////////// PBRRessource::PBRRessource() - : vertices_(), indices_(), vertex_array_(), upload_mutex_(), mesh_(nullptr) {} + : buffers_(), upload_mutex_(), point_cloud_(nullptr) {} //////////////////////////////////////////////////////////////////////////////// -PBRRessource::PBRRessource(aiMesh* mesh, std::shared_ptr const& importer, - bool build_kd_tree) - : vertices_(), - indices_(), - vertex_array_(), +PBRRessource::PBRRessource(std::shared_ptr point_cloud) + : buffers_(), upload_mutex_(), - mesh_(mesh), - importer_(importer) { + point_cloud_(point_cloud){ - if (mesh_->HasPositions()) { - bounding_box_ = math::BoundingBox(); + //set already created BB - for (unsigned v(0); v < mesh_->mNumVertices; ++v) { - bounding_box_.expandBy(scm::math::vec3( - mesh_->mVertices[v].x, mesh_->mVertices[v].y, mesh_->mVertices[v].z)); + if (!point_cloud_->is_loaded()) + { + Logger::LOG_WARNING << "Point Cloud was not loaded!" << std::endl; } - - if (build_kd_tree) { - kd_tree_.generate(mesh_); + else + { + scm::gl::boxf loaded_bb = point_cloud->aabb(); + bounding_box_.min = loaded_bb.min_vertex(); + bounding_box_.max = loaded_bb.max_vertex(); } - } } //////////////////////////////////////////////////////////////////////////////// void PBRRessource::upload_to(RenderContext const& ctx) const { - if (!mesh_->HasPositions()) { - Logger::LOG_WARNING << "Unable to load Mesh! Has no vertex data." << std::endl; + if (!point_cloud_->is_loaded()) { + Logger::LOG_WARNING << "Point Cloud was not loaded!" << std::endl; return; } std::unique_lock lock(upload_mutex_); - if (vertices_.size() <= ctx.id) { - vertices_.resize(ctx.id + 1); - indices_.resize(ctx.id + 1); - vertex_array_.resize(ctx.id + 1); + if (buffers_.size() <= ctx.id) { + buffers_.resize(ctx.id + 1); } - vertices_[ctx.id] = + buffers_[ctx.id] = ctx.render_device->create_buffer(scm::gl::BIND_VERTEX_BUFFER, - scm::gl::USAGE_STATIC_DRAW, - mesh_->mNumVertices * sizeof(Vertex), - 0); - - - - Vertex* data(static_cast(ctx.render_context->map_buffer( - vertices_[ctx.id], scm::gl::ACCESS_WRITE_INVALIDATE_BUFFER))); - - for (unsigned v(0); v < mesh_->mNumVertices; ++v) { - data[v].pos = scm::math::vec3( - mesh_->mVertices[v].x, mesh_->mVertices[v].y, mesh_->mVertices[v].z); - - if (mesh_->HasTextureCoords(0)) { - data[v].tex = scm::math::vec2(mesh_->mTextureCoords[0][v].x, - mesh_->mTextureCoords[0][v].y); - } else { - data[v].tex = scm::math::vec2(0.f, 0.f); - } - - if (mesh_->HasNormals()) { - data[v].normal = scm::math::vec3( - mesh_->mNormals[v].x, mesh_->mNormals[v].y, mesh_->mNormals[v].z); - } else { - data[v].normal = scm::math::vec3(0.f, 0.f, 0.f); - } - - if (mesh_->HasTangentsAndBitangents()) { - data[v].tangent = scm::math::vec3( - mesh_->mTangents[v].x, mesh_->mTangents[v].y, mesh_->mTangents[v].z); - - data[v].bitangent = scm::math::vec3(mesh_->mBitangents[v].x, - mesh_->mBitangents[v].y, - mesh_->mBitangents[v].z); - } else { - data[v].tangent = scm::math::vec3(0.f, 0.f, 0.f); - data[v].bitangent = scm::math::vec3(0.f, 0.f, 0.f); - } - } - - ctx.render_context->unmap_buffer(vertices_[ctx.id]); + scm::gl::USAGE_DYNAMIC_COPY, + point_cloud_->num_surfels() * sizeof(pbr::ren::RawPointCloud::SerializedSurfel), + &(point_cloud_->data()[0])); - std::vector index_array(mesh_->mNumFaces * 3); - for (unsigned t = 0; t < mesh_->mNumFaces; ++t) { - const struct aiFace* face = &mesh_->mFaces[t]; - - index_array[t * 3] = face->mIndices[0]; - index_array[t * 3 + 1] = face->mIndices[1]; - index_array[t * 3 + 2] = face->mIndices[2]; - } - - indices_[ctx.id] = - ctx.render_device->create_buffer(scm::gl::BIND_INDEX_BUFFER, - scm::gl::USAGE_STATIC_DRAW, - mesh_->mNumFaces * 3 * sizeof(unsigned), - &index_array[0]); std::vector buffer_arrays; - buffer_arrays.push_back(vertices_[ctx.id]); + buffer_arrays.push_back(buffers_[ctx.id]); vertex_array_[ctx.id] = ctx.render_device->create_vertex_array( - scm::gl::vertex_format(0, 0, scm::gl::TYPE_VEC3F, sizeof(Vertex))( - 0, 1, scm::gl::TYPE_VEC2F, sizeof(Vertex))( - 0, 2, scm::gl::TYPE_VEC3F, sizeof(Vertex))( - 0, 3, scm::gl::TYPE_VEC3F, sizeof(Vertex))( - 0, 4, scm::gl::TYPE_VEC3F, sizeof(Vertex)), + scm::gl::vertex_format(0, 0, scm::gl::TYPE_VEC3F, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 1, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 2, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 3, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 4, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 5, scm::gl::TYPE_FLOAT, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 6, scm::gl::TYPE_VEC3F, sizeof(pbr::ren::RawPointCloud::SerializedSurfel)), buffer_arrays); } @@ -175,7 +103,7 @@ void PBRRessource::upload_to(RenderContext const& ctx) const { void PBRRessource::draw(RenderContext const& ctx) const { // upload to GPU if neccessary - if (vertices_.size() <= ctx.id || vertices_[ctx.id] == nullptr) { + if (buffers_.size() <= ctx.id || buffers_[ctx.id] == nullptr) { upload_to(ctx); } @@ -183,46 +111,20 @@ void PBRRessource::draw(RenderContext const& ctx) const { ctx.render_context->bind_vertex_array(vertex_array_[ctx.id]); - ctx.render_context->bind_index_buffer( - indices_[ctx.id], scm::gl::PRIMITIVE_TRIANGLE_LIST, scm::gl::TYPE_UINT); - ctx.render_context->apply(); - ctx.render_context->draw_elements(mesh_->mNumFaces * 3); -} - -//////////////////////////////////////////////////////////////////////////////// -void PBRRessource::ray_test(Ray const& ray, PickResult::Options options, - Node* owner, std::set& hits) { - kd_tree_.ray_test(ray, mesh_, options, owner, hits); + //ctx.render_context->draw_elements(point_cloud_->num_surfels()); } //////////////////////////////////////////////////////////////////////////////// -unsigned int PBRRessource::num_vertices() const { return mesh_->mNumVertices; } - -//////////////////////////////////////////////////////////////////////////////// - -unsigned int PBRRessource::num_faces() const { return mesh_->mNumFaces; } - -//////////////////////////////////////////////////////////////////////////////// - -scm::math::vec3 PBRRessource::get_vertex(unsigned int i) const { +void PBRRessource::ray_test(Ray const& ray, PickResult::Options options, + Node* owner, std::set& hits) { - return scm::math::vec3( - mesh_->mVertices[i].x, mesh_->mVertices[i].y, mesh_->mVertices[i].z); + return; } -//////////////////////////////////////////////////////////////////////////////// - -std::vector PBRRessource::get_face(unsigned int i) const { - - std::vector face(mesh_->mFaces[i].mNumIndices); - for (unsigned int j = 0; j < mesh_->mFaces[i].mNumIndices; ++j) - face[j] = mesh_->mFaces[i].mIndices[j]; - return face; -} //////////////////////////////////////////////////////////////////////////////// From 9ece96ba232571b11365199602de7cab29ce98db Mon Sep 17 00:00:00 2001 From: feoo Date: Sat, 3 May 2014 16:05:56 +0200 Subject: [PATCH 06/45] small fixes --- include/gua/scenegraph/PBRNode.hpp | 3 +- src/gua/renderer/PBRLoader.cpp | 2 +- src/gua/scenegraph/PBRNode.cpp | 102 +---------------------------- 3 files changed, 4 insertions(+), 103 deletions(-) diff --git a/include/gua/scenegraph/PBRNode.hpp b/include/gua/scenegraph/PBRNode.hpp index 679a23f10..1fe6edf83 100644 --- a/include/gua/scenegraph/PBRNode.hpp +++ b/include/gua/scenegraph/PBRNode.hpp @@ -37,8 +37,7 @@ class GUA_DLL PBRNode : public GeometryNode public : // member PBRNode(std::string const& name, - std::string const& geometry = "gua_default_geometry", - std::string const& material = "gua_default_material", + std::string const& filename = "gua_default_point_cloud", math::mat4 const& transform = math::mat4::identity()); /** diff --git a/src/gua/renderer/PBRLoader.cpp b/src/gua/renderer/PBRLoader.cpp index 5effefcc5..955cd616f 100644 --- a/src/gua/renderer/PBRLoader.cpp +++ b/src/gua/renderer/PBRLoader.cpp @@ -54,7 +54,7 @@ std::shared_ptr PBRLoader::load(std::string const& file_name, std::shared_ptr new_node; - std::shared_ptr point_cloud = std::make_shared(/*model_counter_*/); + std::shared_ptr point_cloud = std::make_shared(model_counter_); if (point_cloud->Load(file_name)) { diff --git a/src/gua/scenegraph/PBRNode.cpp b/src/gua/scenegraph/PBRNode.cpp index 69c1270a0..f93555873 100644 --- a/src/gua/scenegraph/PBRNode.cpp +++ b/src/gua/scenegraph/PBRNode.cpp @@ -34,9 +34,8 @@ namespace gua { //////////////////////////////////////////////////////////////////////////////// PBRNode::PBRNode(std::string const& name, std::string const& filename, - std::string const& material, math::mat4 const& transform) - : GeometryNode(name, filename, material, transform) + : GeometryNode(name, filename, "gua_default_material", transform) {} //////////////////////////////////////////////////////////////////////////////// @@ -62,103 +61,6 @@ namespace gua { return; } - // bbox is intersected, but check geometry only if mask tells us to check - if (get_filename() != "" && mask.check(get_groups())) { - - auto geometry(GeometryDatabase::instance()->lookup(get_filename())); - - if (geometry) { - - bool check_kd_tree(true); - - math::mat4 world_transform(get_world_transform()); - - // check for bounding box intersection of contained geometry if node - // has children (in this case, the bbox might be larger - // than the actual geometry) - if (has_children()) { - auto geometry_bbox(geometry->get_bounding_box()); - - math::BoundingBox inner_bbox; - inner_bbox.expandBy(world_transform * geometry_bbox.min); - inner_bbox.expandBy(world_transform * geometry_bbox.max); - inner_bbox.expandBy(world_transform * - math::vec3(geometry_bbox.min.x, - geometry_bbox.min.y, - geometry_bbox.max.z)); - inner_bbox.expandBy(world_transform * - math::vec3(geometry_bbox.min.x, - geometry_bbox.max.y, - geometry_bbox.min.z)); - inner_bbox.expandBy(world_transform * - math::vec3(geometry_bbox.min.x, - geometry_bbox.max.y, - geometry_bbox.max.z)); - inner_bbox.expandBy(world_transform * - math::vec3(geometry_bbox.max.x, - geometry_bbox.min.y, - geometry_bbox.max.z)); - inner_bbox.expandBy(world_transform * - math::vec3(geometry_bbox.max.x, - geometry_bbox.max.y, - geometry_bbox.min.z)); - inner_bbox.expandBy(world_transform * - math::vec3(geometry_bbox.max.x, - geometry_bbox.min.y, - geometry_bbox.min.z)); - - auto inner_hits(ray.intersect(inner_bbox)); - if (inner_hits.first == RayNode::END && - inner_hits.second == RayNode::END) - check_kd_tree = false; - } - - if (check_kd_tree) { - Ray world_ray(ray.get_world_ray()); - - math::mat4 ori_transform(scm::math::inverse(world_transform)); - - math::vec4 ori(world_ray.origin_[0], - world_ray.origin_[1], - world_ray.origin_[2], - 1.0); - math::vec4 dir(world_ray.direction_[0], - world_ray.direction_[1], - world_ray.direction_[2], - 0.0); - - ori = ori_transform * ori; - dir = ori_transform * dir; - - Ray object_ray(ori, dir, world_ray.t_max_); - geometry->ray_test(object_ray, options, this, hits); - - float const inf(std::numeric_limits::max()); - - if (options & PickResult::GET_WORLD_POSITIONS) { - - for (auto& hit : hits) { - if (hit.world_position == math::vec3(inf, inf, inf)) { - auto transformed(world_transform * math::vec4(hit.position.x, hit.position.y, hit.position.z, 0.0)); - hit.world_position = scm::math::vec3(transformed.x, transformed.y, transformed.z); - } - } - } - - if (options & PickResult::GET_WORLD_NORMALS) { - - math::mat4 normal_matrix(scm::math::inverse(scm::math::transpose(world_transform))); - for (auto& hit : hits) { - if (hit.world_normal == math::vec3(inf, inf, inf)) { - auto transformed(normal_matrix * math::vec4(hit.normal.x, hit.normal.y, hit.normal.z, 0.0)); - hit.world_normal = scm::math::normalize(scm::math::vec3(transformed.x, transformed.y, transformed.z)); - } - } - } - } - } - } - for (auto child : get_children()) { // test for intersection with each child child->ray_test_impl(ray, options, mask, hits); @@ -168,6 +70,6 @@ namespace gua { //////////////////////////////////////////////////////////////////////////////// std::shared_ptr PBRNode::copy() const { - return std::make_shared(get_name(), filename_, material_, get_transform()); + return std::make_shared(get_name(), filename_, get_transform()); } } From 92d2049f47363a9a1f988b4acb58143c2fee4650 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Sat, 3 May 2014 16:10:53 +0200 Subject: [PATCH 07/45] added filename --- src/gua/renderer/PBRLoader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gua/renderer/PBRLoader.cpp b/src/gua/renderer/PBRLoader.cpp index 5effefcc5..79ae47cdc 100644 --- a/src/gua/renderer/PBRLoader.cpp +++ b/src/gua/renderer/PBRLoader.cpp @@ -66,7 +66,7 @@ std::shared_ptr PBRLoader::load(std::string const& file_name, auto node(std::make_shared(model_name)); - + node->set_filename(model_name); return node; } @@ -90,7 +90,7 @@ std::vector const PBRLoader::load_from_buffer(char const* buffer_ bool PBRLoader::is_supported(std::string const& file_name) const { auto point_pos(file_name.find_last_of(".")); - return file_name.substr(point_pos + 1) == "kdn"; + return file_name.substr(point_pos + 1) == "xyz_all"; } } From 73345964d214c52fe64626b524578941cf7467dd Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Sat, 3 May 2014 16:15:06 +0200 Subject: [PATCH 08/45] Changes on PBRRessource --- src/gua/renderer/PBRRessource.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gua/renderer/PBRRessource.cpp b/src/gua/renderer/PBRRessource.cpp index 51ba7d5d8..250c2bd89 100644 --- a/src/gua/renderer/PBRRessource.cpp +++ b/src/gua/renderer/PBRRessource.cpp @@ -102,6 +102,7 @@ void PBRRessource::upload_to(RenderContext const& ctx) const { void PBRRessource::draw(RenderContext const& ctx) const { +/* // upload to GPU if neccessary if (buffers_.size() <= ctx.id || buffers_[ctx.id] == nullptr) { upload_to(ctx); @@ -115,6 +116,7 @@ void PBRRessource::draw(RenderContext const& ctx) const { //ctx.render_context->draw_elements(point_cloud_->num_surfels()); +*/ } //////////////////////////////////////////////////////////////////////////////// From 36eb84ad002d23e5f2339194f116f1250bd3b62d Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Sat, 3 May 2014 21:09:15 +0200 Subject: [PATCH 09/45] Add first PBRUberShader implementation for forward rendering --- include/gua/renderer/PBRUberShader.hpp | 10 ++ src/gua/renderer/PBRRessource.cpp | 4 +- src/gua/renderer/PBRUberShader.cpp | 147 ++++++++++++++++--------- 3 files changed, 108 insertions(+), 53 deletions(-) diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp index 49e061833..3e0a3aab5 100644 --- a/include/gua/renderer/PBRUberShader.hpp +++ b/include/gua/renderer/PBRUberShader.hpp @@ -33,6 +33,8 @@ class PBRUberShader : public GeometryUberShader { void create (std::set const& material_names); + bool upload_to (RenderContext const& context) const; + /*virtual*/ stage_mask const get_stage_mask() const; /*virtual*/ void preframe (RenderContext const& context) const; @@ -59,6 +61,14 @@ class PBRUberShader : public GeometryUberShader { Frustum const& /*frustum*/) const; /*virtual*/ void postframe (RenderContext const& context) const; + + private: //auxialiary methods + std::string const forward_point_rendering_vertex_shader() const; + std::string const forward_point_rendering_fragment_shader() const; + + private: //member variables + + mutable std::vector change_point_size_in_shader_state_; }; } diff --git a/src/gua/renderer/PBRRessource.cpp b/src/gua/renderer/PBRRessource.cpp index 250c2bd89..483f14f90 100644 --- a/src/gua/renderer/PBRRessource.cpp +++ b/src/gua/renderer/PBRRessource.cpp @@ -102,7 +102,6 @@ void PBRRessource::upload_to(RenderContext const& ctx) const { void PBRRessource::draw(RenderContext const& ctx) const { -/* // upload to GPU if neccessary if (buffers_.size() <= ctx.id || buffers_[ctx.id] == nullptr) { upload_to(ctx); @@ -114,9 +113,8 @@ void PBRRessource::draw(RenderContext const& ctx) const { ctx.render_context->apply(); + ctx.render_context->draw_arrays(scm::gl::PRIMITIVE_POINT_LIST, 0, point_cloud_->num_surfels()); - //ctx.render_context->draw_elements(point_cloud_->num_surfels()); -*/ } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index 4a1457beb..14f734dcb 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -38,61 +38,102 @@ namespace gua { // create ubershader factories for given material set UberShader::create(material_names); - // VERTEX SHADER ------------------------------------------------------------- - std::string vertex_shader( - Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_mesh_mesh_vert) - ); - - // material specific uniforms - string_utils::replace(vertex_shader, "@uniform_definition", - get_uniform_mapping()->get_uniform_definition()); - - // output - string_utils::replace(vertex_shader, "@output_definition", - vshader_factory_->get_output_mapping().get_gbuffer_output_definition(false, true)); - - // print material specific methods - string_utils::replace(vertex_shader, "@material_methods", - UberShader::print_material_methods(*vshader_factory_)); - - // print main switch(es) - string_utils::replace(vertex_shader, "@material_switch", - UberShader::print_material_switch(*vshader_factory_)); - - // FRAGMENT SHADER ----------------------------------------------------------- - std::string fragment_shader( - Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_mesh_mesh_frag) - ); - - // input from vertex shader - string_utils::replace(fragment_shader, "@input_definition", - vshader_factory_->get_output_mapping().get_gbuffer_output_definition(true, true)); - - // material specific uniforms - string_utils::replace(fragment_shader, "@uniform_definition", - get_uniform_mapping()->get_uniform_definition()); - - // outputs - string_utils::replace(fragment_shader, "@output_definition", - get_gbuffer_mapping()->get_gbuffer_output_definition(false, false)); - - // print material specific methods - string_utils::replace(fragment_shader, "@material_methods", - UberShader::print_material_methods(*fshader_factory_)); - - // print main switch(es) - string_utils::replace(fragment_shader, "@material_switch", - UberShader::print_material_switch(*fshader_factory_)); - - auto program = std::make_shared(); - program->create_from_sources(vertex_shader, fragment_shader); - add_program(program); + // create final ubershader that writes to gbuffer + std::vector shader_stages; + shader_stages.push_back( ShaderProgramStage( scm::gl::STAGE_VERTEX_SHADER, forward_point_rendering_vertex_shader())); + shader_stages.push_back( ShaderProgramStage( scm::gl::STAGE_FRAGMENT_SHADER, forward_point_rendering_fragment_shader())); + + auto final_program = std::make_shared(); + final_program->set_shaders(shader_stages); + add_program(final_program); + } + + //////////////////////////////////////////////////////////////////////////////// + + std::string const PBRUberShader::forward_point_rendering_vertex_shader() const + { + std::stringstream fpr_vertex; + + fpr_vertex << std::string(" \n\ + #version 420 core \n\ + \n\ + // input attributes \n\ + layout (location = 0) in vec3 in_position; \n\ + layout (location = 1) in uint in_r; \n\ + layout (location = 2) in uint in_g; \n\ + layout (location = 3) in uint in_b; \n\ + layout (location = 4) in uint empty; \n\ + layout (location = 5) in float in_radius; \n\ + layout (location = 6) in vec3 in_normal; \n\ + \n\ + uniform mat4 gua_projection_matrix; \n\ + uniform mat4 gua_view_matrix; \n\ + uniform mat4 gua_model_matrix; \n\ + \n\ + out vec3 point_color; \n\ + \n\ + \n\ + void main() \n\ + { \n\ + gl_Position = gua_projection_matrix * \n\ + gua_view_matrix * \n\ + gua_model_matrix * \n\ + vec4(position,1.0); \n\ + \n\ + point_color = vec3(vec3((in_r)/255.0f, \n\ + (in_g)/255.0f, \n\ + (in_b)/255.0f); \n\ + } \n\ + "); + + return fpr_vertex.str(); + } + + //////////////////////////////////////////////////////////////////////////////// + + std::string const PBRUberShader::forward_point_rendering_fragment_shader() const + { + std::stringstream fpr_fragment; + + fpr_fragment << std::string(" \n\ + #version 420 core \n\ + \n\ + layout(location = 0) out vec4 out_color; \n\ + \n\ + \n\ + \n\ + in vec3 point_color; \n\ + \n\ + \n\ + void main() \n\ + { \n\ + out_color = vec4(point_color, 1.0); \n\ + } \n\ + "); + + return fpr_fragment.str(); + } + + + //////////////////////////////////////////////////////////////////////////////// + + bool PBRUberShader::upload_to (RenderContext const& context) const + { + bool upload_succeeded = UberShader::upload_to(context); + + if(context.id >= change_point_size_in_shader_state_.size() ) + { + change_point_size_in_shader_state_.resize(context.id + 1); + + change_point_size_in_shader_state_[context.id] = context.render_device->create_rasterizer_state(scm::gl::FILL_SOLID, scm::gl::CULL_NONE, scm::gl::ORIENT_CCW, false, false, 0.0, false, false, scm::gl::point_raster_state(true)); + } } //////////////////////////////////////////////////////////////////////////////// /*virtual*/ GeometryUberShader::stage_mask const PBRUberShader::get_stage_mask() const { + return GeometryUberShader::DRAW_STAGE; } @@ -124,6 +165,7 @@ namespace gua { scm::math::mat4 const& normal_matrix, Frustum const& /*frustum*/) const { + auto geometry = std::static_pointer_cast(GeometryDatabase::instance()->lookup(filename)); auto material = MaterialDatabase::instance()->lookup(material_name); @@ -131,14 +173,19 @@ namespace gua { { if (material && geometry) { - set_uniform(ctx, material->get_id(), "gua_material_id"); set_uniform(ctx, model_matrix, "gua_model_matrix"); set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + geometry->draw(ctx); + + } } get_program()->unuse(ctx); + + } //////////////////////////////////////////////////////////////////////////////// From d91a5a30fe079ef6cd4f2085d4a39b60d928d6fb Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Mon, 5 May 2014 21:00:44 +0200 Subject: [PATCH 10/45] Model in Buffers,rendering to black screen, don't know about layer mapping and shader outputs yet --- include/gua/scenegraph/PBRNode.hpp | 1 + src/gua/renderer/GBufferPass.cpp | 7 ++++++ src/gua/renderer/GeometryLoader.cpp | 2 ++ src/gua/renderer/PBRLoader.cpp | 6 ++++- src/gua/renderer/PBRRessource.cpp | 16 ++++++++++--- src/gua/renderer/PBRUberShader.cpp | 36 ++++++++++++++++++++++------- src/gua/scenegraph/PBRNode.cpp | 5 ++-- 7 files changed, 59 insertions(+), 14 deletions(-) diff --git a/include/gua/scenegraph/PBRNode.hpp b/include/gua/scenegraph/PBRNode.hpp index 1fe6edf83..4b9b84e3b 100644 --- a/include/gua/scenegraph/PBRNode.hpp +++ b/include/gua/scenegraph/PBRNode.hpp @@ -38,6 +38,7 @@ public : // member PBRNode(std::string const& name, std::string const& filename = "gua_default_point_cloud", + std::string const& material = "gua_default_material", math::mat4 const& transform = math::mat4::identity()); /** diff --git a/src/gua/renderer/GBufferPass.cpp b/src/gua/renderer/GBufferPass.cpp index f28c3a6f2..517495c2d 100644 --- a/src/gua/renderer/GBufferPass.cpp +++ b/src/gua/renderer/GBufferPass.cpp @@ -40,6 +40,7 @@ #include #include + namespace gua { //////////////////////////////////////////////////////////////////////////////// @@ -91,6 +92,7 @@ namespace gua { initialize_state_objects(ctx); } + ctx.render_context->set_rasterizer_state( pipeline_->config.enable_backface_culling() ? bfc_rasterizer_state_ : no_bfc_rasterizer_state_); @@ -124,12 +126,14 @@ namespace gua { ctx); } + // 1. call preframe callback if available for type if (ubershader->get_stage_mask() & GeometryUberShader::PRE_FRAME_STAGE) { ubershader->preframe(ctx); } + // 2. iterate all drawables of current type and call predraw of current ubershader if (ubershader->get_stage_mask() & GeometryUberShader::PRE_DRAW_STAGE) { @@ -150,6 +154,7 @@ namespace gua { } } + // 3. iterate all drawables of current type and call draw of current ubershader if (ubershader->get_stage_mask() & GeometryUberShader::DRAW_STAGE) { @@ -158,6 +163,7 @@ namespace gua { auto const& ressource = GeometryDatabase::instance()->lookup(node->get_filename()); auto const& material = MaterialDatabase::instance()->lookup(node->get_material()); + if (ressource && material) { ubershader->draw(ctx, @@ -207,6 +213,7 @@ namespace gua { display_rays(ctx, scene); ctx.render_context->reset_state_objects(); + } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/renderer/GeometryLoader.cpp b/src/gua/renderer/GeometryLoader.cpp index 6e3f3939d..c10506c24 100644 --- a/src/gua/renderer/GeometryLoader.cpp +++ b/src/gua/renderer/GeometryLoader.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include // external headers @@ -51,6 +52,7 @@ GeometryLoader::GeometryLoader() : fileloaders_() { fileloaders_.push_back(new NURBSLoader); fileloaders_.push_back(new Video3DLoader); fileloaders_.push_back(new VolumeLoader); + fileloaders_.push_back(new PBRLoader); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/renderer/PBRLoader.cpp b/src/gua/renderer/PBRLoader.cpp index 0d86388a7..a57374315 100644 --- a/src/gua/renderer/PBRLoader.cpp +++ b/src/gua/renderer/PBRLoader.cpp @@ -50,6 +50,8 @@ PBRLoader::PBRLoader() std::shared_ptr PBRLoader::load(std::string const& file_name, unsigned flags) { + + node_counter_ = 0; std::shared_ptr new_node; @@ -67,7 +69,9 @@ std::shared_ptr PBRLoader::load(std::string const& file_name, auto node(std::make_shared(model_name)); node->set_filename(model_name); - return node; + node->set_material("data/materials/Red.gmd"); + + return node; } else diff --git a/src/gua/renderer/PBRRessource.cpp b/src/gua/renderer/PBRRessource.cpp index 483f14f90..7c26c6641 100644 --- a/src/gua/renderer/PBRRessource.cpp +++ b/src/gua/renderer/PBRRessource.cpp @@ -30,6 +30,7 @@ // external headers +#include namespace gua { @@ -46,6 +47,7 @@ PBRRessource::PBRRessource(std::shared_ptr point_cloud) upload_mutex_(), point_cloud_(point_cloud){ + //set already created BB if (!point_cloud_->is_loaded()) @@ -58,12 +60,14 @@ PBRRessource::PBRRessource(std::shared_ptr point_cloud) bounding_box_.min = loaded_bb.min_vertex(); bounding_box_.max = loaded_bb.max_vertex(); } + } //////////////////////////////////////////////////////////////////////////////// void PBRRessource::upload_to(RenderContext const& ctx) const { + if (!point_cloud_->is_loaded()) { Logger::LOG_WARNING << "Point Cloud was not loaded!" << std::endl; return; @@ -73,11 +77,13 @@ void PBRRessource::upload_to(RenderContext const& ctx) const { if (buffers_.size() <= ctx.id) { buffers_.resize(ctx.id + 1); + vertex_array_.resize(ctx.id + 1); } + buffers_[ctx.id] = ctx.render_device->create_buffer(scm::gl::BIND_VERTEX_BUFFER, - scm::gl::USAGE_DYNAMIC_COPY, + scm::gl::USAGE_STATIC_DRAW, point_cloud_->num_surfels() * sizeof(pbr::ren::RawPointCloud::SerializedSurfel), &(point_cloud_->data()[0])); @@ -86,15 +92,17 @@ void PBRRessource::upload_to(RenderContext const& ctx) const { std::vector buffer_arrays; buffer_arrays.push_back(buffers_[ctx.id]); + vertex_array_[ctx.id] = ctx.render_device->create_vertex_array( - scm::gl::vertex_format(0, 0, scm::gl::TYPE_VEC3F, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + scm::gl::vertex_format( + 0, 0, scm::gl::TYPE_VEC3F, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( 0, 1, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( 0, 2, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( 0, 3, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( 0, 4, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( 0, 5, scm::gl::TYPE_FLOAT, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( 0, 6, scm::gl::TYPE_VEC3F, sizeof(pbr::ren::RawPointCloud::SerializedSurfel)), - buffer_arrays); + buffer_arrays); } @@ -113,6 +121,8 @@ void PBRRessource::draw(RenderContext const& ctx) const { ctx.render_context->apply(); + + std::cout << point_cloud_->num_surfels()<<"\n"; ctx.render_context->draw_arrays(scm::gl::PRIMITIVE_POINT_LIST, 0, point_cloud_->num_surfels()); } diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index 14f734dcb..6423aacac 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -78,11 +78,11 @@ namespace gua { gl_Position = gua_projection_matrix * \n\ gua_view_matrix * \n\ gua_model_matrix * \n\ - vec4(position,1.0); \n\ + vec4(in_position,1.0); \n\ \n\ - point_color = vec3(vec3((in_r)/255.0f, \n\ - (in_g)/255.0f, \n\ - (in_b)/255.0f); \n\ + point_color = vec3((in_r)/255.0f, \n\ + (in_g)/255.0f, \n\ + (in_b)/255.0f); \n\ } \n\ "); @@ -98,16 +98,21 @@ namespace gua { fpr_fragment << std::string(" \n\ #version 420 core \n\ \n\ - layout(location = 0) out vec4 out_color; \n\ - \n\ - \n\ + // layout(location = 0) out vec4 out_color; \n\ + layout(location = 0) out float out_depth; \n\ + layout(location = 1) out vec3 out_nor; \n\ + layout(location = 2) out int out_mat_id; \n\ \n\ in vec3 point_color; \n\ \n\ \n\ void main() \n\ { \n\ - out_color = vec4(point_color, 1.0); \n\ + //out_color = vec4(point_color, 1.0); \n\ + //out_color = vec4(1.0,0.0,0.0,1.0); \n\ + out_depth = gl_FragDepth; \n\ + out_nor = vec3(-1.0,0.0,0.0); \n\ + out_mat_id = 0; \n\ } \n\ "); @@ -119,6 +124,7 @@ namespace gua { bool PBRUberShader::upload_to (RenderContext const& context) const { + bool upload_succeeded = UberShader::upload_to(context); if(context.id >= change_point_size_in_shader_state_.size() ) @@ -166,17 +172,30 @@ namespace gua { Frustum const& /*frustum*/) const { + + auto geometry = std::static_pointer_cast(GeometryDatabase::instance()->lookup(filename)); auto material = MaterialDatabase::instance()->lookup(material_name); get_program()->use(ctx); + { if (material && geometry) { set_uniform(ctx, model_matrix, "gua_model_matrix"); set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + + + if(ctx.id >= change_point_size_in_shader_state_.size() ) + { + change_point_size_in_shader_state_.resize(ctx.id + 1); + + change_point_size_in_shader_state_[ctx.id] = ctx.render_device->create_rasterizer_state(scm::gl::FILL_SOLID, scm::gl::CULL_NONE, scm::gl::ORIENT_CCW, false, false, 0.0, false, false, scm::gl::point_raster_state(true)); + } + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + ctx.render_context->apply(); geometry->draw(ctx); @@ -186,6 +205,7 @@ namespace gua { get_program()->unuse(ctx); + } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/scenegraph/PBRNode.cpp b/src/gua/scenegraph/PBRNode.cpp index f93555873..4703f4702 100644 --- a/src/gua/scenegraph/PBRNode.cpp +++ b/src/gua/scenegraph/PBRNode.cpp @@ -34,8 +34,9 @@ namespace gua { //////////////////////////////////////////////////////////////////////////////// PBRNode::PBRNode(std::string const& name, std::string const& filename, + std::string const& material, math::mat4 const& transform) - : GeometryNode(name, filename, "gua_default_material", transform) + : GeometryNode(name, filename, material, transform) {} //////////////////////////////////////////////////////////////////////////////// @@ -70,6 +71,6 @@ namespace gua { //////////////////////////////////////////////////////////////////////////////// std::shared_ptr PBRNode::copy() const { - return std::make_shared(get_name(), filename_, get_transform()); + return std::make_shared(get_name(), filename_, material_, get_transform()); } } From 0299cfc22ac7bda5e2f7d4b0a3c0b1eb2531551b Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Thu, 8 May 2014 22:52:28 +0200 Subject: [PATCH 11/45] Change PBRUberShader to be inline --- include/gua/renderer/PBRUberShader.hpp | 31 +- resources/materials/gua_pbr.gmd | 5 + resources/materials/gua_pbr.gsd | 37 ++ .../gbuffer/pbr/normalization.frag | 67 ++++ .../gbuffer/pbr/normalization.vert | 73 ++++ .../gbuffer/pbr/point_forward.frag | 25 ++ .../gbuffer/pbr/point_forward.vert | 39 ++ .../uber_shaders/gbuffer/video3d/.vert | 76 ++++ src/gua/renderer/PBRUberShader.cpp | 353 +++++++++++++++--- 9 files changed, 647 insertions(+), 59 deletions(-) create mode 100644 resources/materials/gua_pbr.gmd create mode 100644 resources/materials/gua_pbr.gsd create mode 100644 resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag create mode 100644 resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert create mode 100644 resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag create mode 100644 resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert create mode 100644 resources/shaders/uber_shaders/gbuffer/video3d/.vert diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp index 3e0a3aab5..197428e13 100644 --- a/include/gua/renderer/PBRUberShader.hpp +++ b/include/gua/renderer/PBRUberShader.hpp @@ -29,6 +29,13 @@ namespace gua { class PBRUberShader : public GeometryUberShader { + public: + + enum pass { + point_forward_pass = 0, + quad_pass = 1 + }; + public: void create (std::set const& material_names); @@ -63,12 +70,32 @@ class PBRUberShader : public GeometryUberShader { /*virtual*/ void postframe (RenderContext const& context) const; private: //auxialiary methods - std::string const forward_point_rendering_vertex_shader() const; - std::string const forward_point_rendering_fragment_shader() const; + std::string const accumulation_pass_vertex_shader() const; + std::string const accumulation_pass_fragment_shader() const; + + std::string const normalization_pass_vertex_shader () const; + std::string const normalization_pass_fragment_shader () const; + + std::string const default_pbr_material_name() const; private: //member variables + mutable std::vector point_forward_depth_result_; + mutable std::vector point_forward_color_result_; + mutable std::vector point_forward_result_fbo_; + + + + + + mutable std::vector fullscreen_quad_; + + mutable std::vector change_point_size_in_shader_state_; + + mutable std::vector linear_sampler_state_; + mutable std::vector nearest_sampler_state_; + mutable std::vector depth_stencil_state_; }; } diff --git a/resources/materials/gua_pbr.gmd b/resources/materials/gua_pbr.gmd new file mode 100644 index 000000000..2dd3614ff --- /dev/null +++ b/resources/materials/gua_pbr.gmd @@ -0,0 +1,5 @@ + +{ + "shading_model" : "gua_pbr", + "uniforms" : null +} diff --git a/resources/materials/gua_pbr.gsd b/resources/materials/gua_pbr.gsd new file mode 100644 index 000000000..0b71a5e22 --- /dev/null +++ b/resources/materials/gua_pbr.gsd @@ -0,0 +1,37 @@ + +{ + "final_shading_stage" : + { + "body" : "gua_color = gua_pbr_output_color;\n", + "functions" : "", + "outputs" : null, + "uniforms" : null + }, + "gbuffer_fragment_stage" : + { + "body" : "gua_normal = varying_normal;\ngua_pbr_output_color = vec3(1.0, 1.0, 0.0);", + "functions" : "", + "outputs" : + { + "gua_pbr_output_color" : "vec3" + }, + "uniforms" : null + }, + "gbuffer_vertex_stage" : + { + "body" : "gua_position = gua_world_position;\nvarying_normal = gua_world_normal;\n", + "functions" : "", + "outputs" : + { + "varying_normal" : "vec3" + }, + "uniforms" : null + }, + "lbuffer_stage" : + { + "body" : "", + "functions" : "", + "outputs" : null, + "uniforms" : null + } +} diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag b/resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag new file mode 100644 index 000000000..dca1b1759 --- /dev/null +++ b/resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag @@ -0,0 +1,67 @@ +@include "shaders/common/header.glsl" + +/////////////////////////////////////////////////////////////////////////////// +// input +/////////////////////////////////////////////////////////////////////////////// +in vec3 gua_position_varying; +in vec2 gua_quad_coords; + +@input_definition + + +/////////////////////////////////////////////////////////////////////////////// +// general uniforms +/////////////////////////////////////////////////////////////////////////////// +@include "shaders/uber_shaders/common/gua_camera_uniforms.glsl" + +@uniform_definition + + +/////////////////////////////////////////////////////////////////////////////// +uniform int using_default_pbr_material; + +uniform sampler2DArray color_texture; + + +/////////////////////////////////////////////////////////////////////////////// +// output +/////////////////////////////////////////////////////////////////////////////// +@output_definition + + +/////////////////////////////////////////////////////////////////////////////// +// main +/////////////////////////////////////////////////////////////////////////////// +void main() { + + vec3 output_color = vec3(0.0); + float output_depth = 1.0f; + vec3 output_normal = vec3(0.0); + + vec3 coords = vec3(gua_quad_coords, 0.0); + + + float maxdist = 1000.0; + float mindist = maxdist; + float ogldepth = 1.0; + + + + + vec4 finalcol = texture2DArray( color_texture, coords); + + output_depth = ogldepth; + output_color = finalcol.rgb; + + + + gua_uint_gbuffer_out_0.x = gua_uint_gbuffer_varying_0.x; + + if ( using_default_video_material != 0 ) + { + @apply_pbr_color + @apply_pbr_normal + } + + gl_FragDepth = output_depth; +} diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert b/resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert new file mode 100644 index 000000000..4130e0fd2 --- /dev/null +++ b/resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert @@ -0,0 +1,73 @@ +@include "shaders/common/header.glsl" + +/////////////////////////////////////////////////////////////////////////////// +// input +/////////////////////////////////////////////////////////////////////////////// +layout(location=0) in vec3 gua_in_position; +layout(location=2) in vec2 gua_in_texcoord; + +/////////////////////////////////////////////////////////////////////////////// +// general uniforms +/////////////////////////////////////////////////////////////////////////////// +@include "shaders/uber_shaders/common/gua_camera_uniforms.glsl" + +uniform uint gua_material_id; + +// material specific uniforms +@uniform_definition + +/////////////////////////////////////////////////////////////////////////////// +// output +/////////////////////////////////////////////////////////////////////////////// +out vec3 gua_position_varying; +out vec2 gua_quad_coords; + +/////////////////////////////////////////////////////////////////////////////// +// varying output +/////////////////////////////////////////////////////////////////////////////// +vec2 gua_texcoords; + +vec3 gua_world_normal; +vec3 gua_world_position; +vec3 gua_world_tangent; +vec3 gua_world_bitangent; + +vec3 gua_object_normal; +vec3 gua_object_position; +vec2 gua_object_texcoords; +vec3 gua_object_tangent; +vec3 gua_object_bitangent; + +@output_definition + +/////////////////////////////////////////////////////////////////////////////// +// methods +/////////////////////////////////////////////////////////////////////////////// + +uint gua_get_material_id() { + return gua_uint_gbuffer_varying_0.x; +} + +@material_methods + +/////////////////////////////////////////////////////////////////////////////// +// main +/////////////////////////////////////////////////////////////////////////////// +void main() { + gua_position_varying = vec3(0); + gua_quad_coords = gua_in_texcoord; + gua_texcoords = gua_in_texcoord; + + gua_object_normal = vec3(0); + gua_object_tangent = vec3(0); + gua_object_bitangent = vec3(0); + gua_object_position = vec3(0); + + gua_world_normal = vec3(0); + gua_world_tangent = vec3(0); + gua_world_bitangent = vec3(0); + gua_world_position = vec3(0); + + gua_uint_gbuffer_varying_0.x = gua_material_id; + gl_Position = vec4(gua_in_position, 1.0); +} diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag new file mode 100644 index 000000000..b9d3d4107 --- /dev/null +++ b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag @@ -0,0 +1,25 @@ +@include "shaders/common/header.glsl" + +/////////////////////////////////////////////////////////////////////////////// +// input +/////////////////////////////////////////////////////////////////////////////// +in vec3 gua_point_color; + + + +/////////////////////////////////////////////////////////////////////////////// +// output +/////////////////////////////////////////////////////////////////////////////// + +layout (location=0) out vec4 out_color; + + + +/////////////////////////////////////////////////////////////////////////////// +// main +/////////////////////////////////////////////////////////////////////////////// +void main() { + + //out_color = vec4(gua_point_color,1.0); + out_color = vec4(1.0,0.0,0.0,1.0); +} diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert new file mode 100644 index 000000000..738a63b93 --- /dev/null +++ b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert @@ -0,0 +1,39 @@ +@include "shaders/common/header.glsl" + +/////////////////////////////////////////////////////////////////////////////// +// general uniforms +/////////////////////////////////////////////////////////////////////////////// +@include "shaders/uber_shaders/common/gua_camera_uniforms.glsl" + + + // input attributes + layout (location = 0) in vec3 in_position; + layout (location = 1) in uint in_r; + layout (location = 2) in uint in_g; + layout (location = 3) in uint in_b; + layout (location = 4) in uint empty; + layout (location = 5) in float in_radius; + layout (location = 6) in vec3 in_normal; + + + uniform uint gua_material_id; + // uniform mat4 gua_projection_matrix; + // uniform mat4 gua_view_matrix; + // uniform mat4 gua_model_matrix; + // uniform mat4 gua_normal_matrix; + + out vec3 gua_point_color; + + + void main() + { + + gl_Position = gua_projection_matrix * + gua_view_matrix * + gua_model_matrix * + vec4(in_position,1.0); + + out_point_color = vec3((in_r)/255.0f, + (in_g)/255.0f, + (in_b)/255.0f); + } diff --git a/resources/shaders/uber_shaders/gbuffer/video3d/.vert b/resources/shaders/uber_shaders/gbuffer/video3d/.vert new file mode 100644 index 000000000..2e6e92b96 --- /dev/null +++ b/resources/shaders/uber_shaders/gbuffer/video3d/.vert @@ -0,0 +1,76 @@ +@include "shaders/common/header.glsl" + +/////////////////////////////////////////////////////////////////////////////// +// input +/////////////////////////////////////////////////////////////////////////////// +layout(location=0) in vec3 gua_in_position; +layout(location=2) in vec2 gua_in_texcoord; + +/////////////////////////////////////////////////////////////////////////////// +// general uniforms +/////////////////////////////////////////////////////////////////////////////// +@include "shaders/uber_shaders/common/gua_camera_uniforms.glsl" + +uniform uint gua_material_id; + +// material specific uniforms +@uniform_definition + +/////////////////////////////////////////////////////////////////////////////// +// output +/////////////////////////////////////////////////////////////////////////////// +out vec3 gua_position_varying; +out vec2 gua_quad_coords; + +/////////////////////////////////////////////////////////////////////////////// +// varying output +/////////////////////////////////////////////////////////////////////////////// +vec2 gua_texcoords; + +vec3 gua_world_normal; +vec3 gua_world_position; +vec3 gua_world_tangent; +vec3 gua_world_bitangent; + +vec3 gua_object_normal; +vec3 gua_object_position; +vec2 gua_object_texcoords; +vec3 gua_object_tangent; +vec3 gua_object_bitangent; + +@output_definition + +/////////////////////////////////////////////////////////////////////////////// +// methods +/////////////////////////////////////////////////////////////////////////////// + +uint gua_get_material_id() { + return gua_uint_gbuffer_varying_0.x; +} + +@material_methods + +/////////////////////////////////////////////////////////////////////////////// +// main +/////////////////////////////////////////////////////////////////////////////// +void main() { + gua_position_varying = vec3(0); + gua_quad_coords = gua_in_texcoord; + gua_texcoords = gua_in_texcoord; + + gua_object_normal = vec3(0); + gua_object_tangent = vec3(0); + gua_object_bitangent = vec3(0); + gua_object_position = vec3(0); + + gua_world_normal = vec3(0); + gua_world_tangent = vec3(0); + gua_world_bitangent = vec3(0); + gua_world_position = vec3(0); + + // big switch, one case for each material + @material_switch + + gua_uint_gbuffer_varying_0.x = gua_material_id; + gl_Position = vec4(gua_in_position, 1.0); +} \ No newline at end of file diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index 6423aacac..f9f625fb3 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -25,6 +25,7 @@ // guacamole headers #include #include +#include #include #include #include @@ -35,21 +36,36 @@ namespace gua { void PBRUberShader::create(std::set const& material_names) { - // create ubershader factories for given material set - UberShader::create(material_names); + UberShader::create(material_names); - // create final ubershader that writes to gbuffer - std::vector shader_stages; - shader_stages.push_back( ShaderProgramStage( scm::gl::STAGE_VERTEX_SHADER, forward_point_rendering_vertex_shader())); - shader_stages.push_back( ShaderProgramStage( scm::gl::STAGE_FRAGMENT_SHADER, forward_point_rendering_fragment_shader())); + // create depth shader + std::vector point_forward_pass_stages; + point_forward_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_VERTEX_SHADER, accumulation_pass_vertex_shader())); + point_forward_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_FRAGMENT_SHADER, accumulation_pass_fragment_shader())); - auto final_program = std::make_shared(); - final_program->set_shaders(shader_stages); - add_program(final_program); + auto point_forward_pass_program = std::make_shared(); + point_forward_pass_program->set_shaders(point_forward_pass_stages); + add_program(point_forward_pass_program); + + // create final shader + std::vector normalization_pass_stages; + normalization_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_VERTEX_SHADER, normalization_pass_vertex_shader())); + normalization_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_FRAGMENT_SHADER, normalization_pass_fragment_shader())); + + auto normalization_pass_program = std::make_shared(); + normalization_pass_program->set_shaders(normalization_pass_stages); + add_program(normalization_pass_program); } + + + + + //////////////////////////////////////////////////////////////////////////////// + +/* std::string const PBRUberShader::forward_point_rendering_vertex_shader() const { std::stringstream fpr_vertex; @@ -98,21 +114,14 @@ namespace gua { fpr_fragment << std::string(" \n\ #version 420 core \n\ \n\ - // layout(location = 0) out vec4 out_color; \n\ - layout(location = 0) out float out_depth; \n\ - layout(location = 1) out vec3 out_nor; \n\ - layout(location = 2) out int out_mat_id; \n\ + layout(location = 0) out vec4 out_color; \n\ \n\ in vec3 point_color; \n\ \n\ \n\ void main() \n\ { \n\ - //out_color = vec4(point_color, 1.0); \n\ - //out_color = vec4(1.0,0.0,0.0,1.0); \n\ - out_depth = gl_FragDepth; \n\ - out_nor = vec3(-1.0,0.0,0.0); \n\ - out_mat_id = 0; \n\ + out_color = vec4(1.0,0.0,0.0,1.0); \n\ } \n\ "); @@ -120,20 +129,7 @@ namespace gua { } - //////////////////////////////////////////////////////////////////////////////// - - bool PBRUberShader::upload_to (RenderContext const& context) const - { - - bool upload_succeeded = UberShader::upload_to(context); - - if(context.id >= change_point_size_in_shader_state_.size() ) - { - change_point_size_in_shader_state_.resize(context.id + 1); - - change_point_size_in_shader_state_[context.id] = context.render_device->create_rasterizer_state(scm::gl::FILL_SOLID, scm::gl::CULL_NONE, scm::gl::ORIENT_CCW, false, false, 0.0, false, false, scm::gl::point_raster_state(true)); - } - } +*/ //////////////////////////////////////////////////////////////////////////////// @@ -162,51 +158,286 @@ namespace gua { throw std::runtime_error("PBRUberShader::predraw(): not implemented"); } + + +//////////////////////////////////////////////////////////////////////////////// + + + +std::string const PBRUberShader::accumulation_pass_vertex_shader() const +{ + std::string vertex_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_point_forward_vert) + ); + return vertex_shader; +} + + + + + + + +//////////////////////////////////////////////////////////////////////////////// + +std::string const PBRUberShader::accumulation_pass_fragment_shader() const +{ + std::string fragment_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_point_forward_frag) + ); + + return fragment_shader; +} + + + //////////////////////////////////////////////////////////////////////////////// + + + +std::string const PBRUberShader::normalization_pass_vertex_shader() const +{ + std::string vertex_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_video3d_blend_pass_vert) + ); + +/* + // material specific uniforms + string_utils::replace(vertex_shader, "@uniform_definition", + get_uniform_mapping()->get_uniform_definition()); + +*/ + // output + string_utils::replace(vertex_shader, "@output_definition", + vshader_factory_->get_output_mapping().get_gbuffer_output_definition(false, true)); + +/* + // print material specific methods + string_utils::replace(vertex_shader, "@material_methods", + UberShader::print_material_methods(*vshader_factory_)); + + // print main switch(es) + string_utils::replace(vertex_shader, "@material_switch", + UberShader::print_material_switch(*vshader_factory_)); +*/ + return vertex_shader; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::string const PBRUberShader::normalization_pass_fragment_shader() const +{ + std::string fragment_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_video3d_blend_pass_frag) + ); + + std::string apply_pbr_color = fshader_factory_->get_output_mapping().get_output_string("gua_pbr", "gua_pbr_output_color"); + apply_pbr_color += " = output_color;\n"; + + std::string apply_pbr_normal = fshader_factory_->get_output_mapping().get_output_string("gua_pbr", "gua_normal"); + + apply_pbr_normal += " = output_normal;\n"; + + string_utils::replace(fragment_shader, "@apply_pbr_color", apply_pbr_color); + string_utils::replace(fragment_shader, "@apply_pbr_normal", apply_pbr_normal); + + // input from vertex shader + string_utils::replace(fragment_shader, "@input_definition", + vshader_factory_->get_output_mapping().get_gbuffer_output_definition(true, true)); + + // material specific uniforms + string_utils::replace(fragment_shader, "@uniform_definition", + get_uniform_mapping()->get_uniform_definition()); + + // outputs + string_utils::replace(fragment_shader, "@output_definition", + get_gbuffer_mapping()->get_gbuffer_output_definition(false, false)); + + // print material specific methods + string_utils::replace(fragment_shader, "@material_methods", + UberShader::print_material_methods(*fshader_factory_)); + + // print main switch(es) + string_utils::replace(fragment_shader, "@material_switch", + UberShader::print_material_switch(*fshader_factory_)); + + return fragment_shader; +} + + + + //////////////////////////////////////////////////////////////////////////////// - /*virtual*/ void PBRUberShader::draw(RenderContext const& ctx, - std::string const& filename, - std::string const& material_name, - scm::math::mat4 const& model_matrix, - scm::math::mat4 const& normal_matrix, - Frustum const& /*frustum*/) const +bool PBRUberShader::upload_to (RenderContext const& context) const +{ + bool upload_succeeded = UberShader::upload_to(context); + + assert(context.render_window->config.get_stereo_mode() == StereoMode::MONO || + ((context.render_window->config.get_left_resolution()[0] == context.render_window->config.get_right_resolution()[0]) && + (context.render_window->config.get_left_resolution()[1] == context.render_window->config.get_right_resolution()[1]))); + + // initialize Texture Arrays (kinect depths & colors) + if ( context.id >= point_forward_color_result_.size() || + context.id >= point_forward_depth_result_.size()) { + point_forward_color_result_.resize(context.id + 1); + point_forward_depth_result_.resize(context.id + 1); + + + point_forward_color_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_RGBA_32F, + 1, + 1, + 1 + ); + + point_forward_depth_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_D32F, + 1, + 1, + 1 + ); + } + if (context.id >= fullscreen_quad_.size()) { + fullscreen_quad_.resize(context.id + 1); + fullscreen_quad_[context.id].reset(new scm::gl::quad_geometry(context.render_device, math::vec2(-1.f, -1.f), math::vec2(1.f, 1.f))); + } + if (context.id >= point_forward_result_fbo_.size()) { + point_forward_result_fbo_.resize(context.id + 1); + point_forward_result_fbo_[context.id] = context.render_device->create_frame_buffer(); + } - auto geometry = std::static_pointer_cast(GeometryDatabase::instance()->lookup(filename)); - auto material = MaterialDatabase::instance()->lookup(material_name); + if (context.id >= linear_sampler_state_.size()) { + linear_sampler_state_.resize(context.id + 1); + linear_sampler_state_[context.id] = context.render_device->create_sampler_state(scm::gl::FILTER_MIN_MAG_LINEAR, scm::gl::WRAP_CLAMP_TO_EDGE); + } - get_program()->use(ctx); + if (context.id >= nearest_sampler_state_.size()) { + nearest_sampler_state_.resize(context.id + 1); + nearest_sampler_state_[context.id] = context.render_device->create_sampler_state(scm::gl::FILTER_MIN_MAG_NEAREST, scm::gl::WRAP_CLAMP_TO_EDGE); + } - { - if (material && geometry) - { - set_uniform(ctx, model_matrix, "gua_model_matrix"); - set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + if (context.id >= depth_stencil_state_.size()) { + depth_stencil_state_.resize(context.id + 1); + depth_stencil_state_[context.id] = context.render_device->create_depth_stencil_state(false, true, scm::gl::COMPARISON_LESS); + } + if (context.id >= change_point_size_in_shader_state_.size()){ + change_point_size_in_shader_state_.resize(context.id + 1); + change_point_size_in_shader_state_[context.id] = context.render_device->create_rasterizer_state(scm::gl::FILL_SOLID, scm::gl::CULL_NONE, scm::gl::ORIENT_CCW, false, false, 0.0, false, false, scm::gl::point_raster_state(true)); + } - if(ctx.id >= change_point_size_in_shader_state_.size() ) - { - change_point_size_in_shader_state_.resize(ctx.id + 1); + + return upload_succeeded; +} - change_point_size_in_shader_state_[ctx.id] = ctx.render_device->create_rasterizer_state(scm::gl::FILL_SOLID, scm::gl::CULL_NONE, scm::gl::ORIENT_CCW, false, false, 0.0, false, false, scm::gl::point_raster_state(true)); - } +//////////////////////////////////////////////////////////////////////////////// + +void PBRUberShader::draw(RenderContext const& ctx, + std::string const& file_name, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const +{ + if (!GeometryDatabase::instance()->is_supported(file_name) || + !MaterialDatabase::instance()->is_supported(material_name)) { + gua::Logger::LOG_WARNING << "PBRUberShader::draw(): No such video or material." << file_name << ", " << material_name << std::endl; + return; + } + + auto pbr_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); + auto material = MaterialDatabase::instance()->lookup(material_name); + + if (!material || !pbr_ressource) { + gua::Logger::LOG_WARNING << "PBRUberShader::draw(): Invalid video or material." << std::endl; + return; + } + + // make sure ressources are on the GPU + upload_to(ctx); + { + // single texture only + scm::gl::context_all_guard guard(ctx.render_context); - ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); - ctx.render_context->apply(); - - geometry->draw(ctx); + //ctx.render_context->bind_texture(video3d_ressource->depth_array(ctx), nearest_sampler_state_[ctx.id], 0); + //get_program(warp_pass)->get_program(ctx)->uniform_sampler("depth_video3d_texture", 0); - + auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); + ctx.render_context->set_depth_stencil_state(ds_state); + + + + // pre passes + // forward rendering, will later be changed to be actually pass 2 (accumulation) + + // configure fbo + point_forward_result_fbo_[ctx.id]->clear_attachments(); + point_forward_result_fbo_[ctx.id]->attach_depth_stencil_buffer(point_forward_depth_result_[ctx.id]); + point_forward_result_fbo_[ctx.id]->attach_color_buffer(0, point_forward_color_result_[ctx.id]); + + // bind and clear fbo + ctx.render_context->set_frame_buffer(point_forward_result_fbo_[ctx.id]); + ctx.render_context->clear_depth_stencil_buffer(point_forward_result_fbo_[ctx.id]); + ctx.render_context->clear_color_buffer(point_forward_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); + + + get_program(point_forward_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(point_forward_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + + if (material && pbr_ressource) + { + + get_program(point_forward_pass)->use(ctx); + { + pbr_ressource->draw(ctx); + } + get_program(point_forward_pass)->unuse(ctx); } + + ctx.render_context->reset_framebuffer(); } - get_program()->unuse(ctx); + + + + + + + { + // single texture only + scm::gl::context_all_guard guard(ctx.render_context); + + // second pass + get_program(quad_pass)->use(ctx); + { + if (material /*&& video3d_ressource*/) + { + get_program(quad_pass)->set_uniform(ctx, material->get_id(), "gua_material_id"); + get_program(quad_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(quad_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + get_program(quad_pass)->set_uniform(ctx, int(material_name == default_pbr_material_name()) , "using_default_pbr_material"); + ctx.render_context->bind_texture(point_forward_color_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); + get_program(quad_pass)->get_program(ctx)->uniform_sampler("color_texture", 0); + + fullscreen_quad_[ctx.id]->draw(ctx.render_context); + } + } + get_program(quad_pass)->unuse(ctx); } +} + + + + + //////////////////////////////////////////////////////////////////////////////// @@ -227,5 +458,13 @@ namespace gua { throw std::runtime_error("PBRUberShader::postframe(): not implemented"); } + +//////////////////////////////////////////////////////////////////////////////// + + std::string const PBRUberShader::default_pbr_material_name() const + { + return "gua_pbr"; + } + } From 1fff04b3f739304a195351489a2f32c824a12ba5 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Fri, 9 May 2014 18:26:05 +0200 Subject: [PATCH 12/45] working forward rendering of points --- include/gua/renderer/.RenderContext.hpp.swp | Bin 0 -> 12288 bytes include/gua/renderer/PBRUberShader.hpp | 3 + include/gua/scenegraph/PBRNode.hpp | 4 +- .../gbuffer/pbr/normalization.frag | 15 +- .../gbuffer/pbr/normalization.vert | 28 +--- .../gbuffer/pbr/point_forward.frag | 4 +- .../gbuffer/pbr/point_forward.vert | 11 +- src/gua/renderer/PBRLoader.cpp | 6 +- src/gua/renderer/PBRRessource.cpp | 2 - src/gua/renderer/PBRUberShader.cpp | 142 ++++++------------ src/gua/renderer/Video3DUberShader.cpp | 4 +- 11 files changed, 75 insertions(+), 144 deletions(-) create mode 100644 include/gua/renderer/.RenderContext.hpp.swp diff --git a/include/gua/renderer/.RenderContext.hpp.swp b/include/gua/renderer/.RenderContext.hpp.swp new file mode 100644 index 0000000000000000000000000000000000000000..7376562cade5ed80dfe6555dc29cbdbf8b4ccb9f GIT binary patch literal 12288 zcmeI2O>Y}T7{{ly1zISr;06btHd2+ki8pD4D!Em`O_Rn_>O_8N2nWn$ch)U?=PbF2GJv`E)CDmq4(z@6C z2CWHav@Lb0rPYkN&0C=_EE(8-?1-eqibUXuNJLg8{bMo=w78)2Uc0@e6Vt#bU=-Lu zfqiV^*!WmdIdteCJ@#mQgLbCaC}0#Y3K#{90!9I&fKk9GU=;W-Dxljt*hNh2zHD-f z+2@WmpV?RQVH7Y57zK<1MggOMQNSo*6fg=H1&jhl0i(dbPyy~RwsR+AZ|%b1@&Et* zfB%2JpRpIf0r1r$jD_Gy@WVdFE&>bOc$l&Gz|-L7LyTPkbKv$~#;$<|*blzk!`K(# z3V0K|0emnE4ub>W_XipK4!jSZ2P5Dpcmg~Q_JTcNH@NWtV?Tnc;6v~ZxC}0VDmVv@ zfMIY5+`6B!U%>a^I`|rV0Nw>Jffs=T4g&(Wb~E-H_!(RSpMuxGtDpnQUD=p$?nTtdmt#StGwXWiSGq6AzzMrK>RnN4bknZ`hH9RmUNLn&X?uiGWEYwo zl7S9aSyOhaP-yyvLT1hp9chb3TSt8dy^?hfl#R)?vPRTvL|m!RmRv)juGHRu0Gl|? zBwFn?E84;>TB+n>*0vpYG3%a0sng9x-0rLrgE; z@9Kj*nnOmsYfrlVRx(jL3Y$pWc`CUSgnERO5OmVhx?QlWZnv9n25~-&nwF0o7$~8v zQ~80hr1loWn3NaBzC#}G2y`Pr>J(g5A*IH_v&pox`7V@dGjsJC70ZjXP^?sn<=Wzj^t=dTO`;=$)R1hoeVH62 zJc^KQQeDsCT%JMu;_1?CskYeDua=y$S!k1^4jWTPaw$$uA=$I#A(z4^N6ymlge0~k zC|1Ia{SiXTz9{!39z0kkA7^4j9c(xx%fBCxbbQs&)euF{G=6^|1;FA62^_a89%!P! z3bQ3QLWtrp=D!@q5l)t*(~&G5FoK>*>@da_&Yk3#*ffwhoZ88XRvsi?T27l!M_2&q z_}0e5O=?s&=t43AvUz}Qvl6HzEIe?mbmii=*m6>U=bpr45 const& material_names); bool upload_to (RenderContext const& context) const; @@ -96,6 +98,7 @@ class PBRUberShader : public GeometryUberShader { mutable std::vector linear_sampler_state_; mutable std::vector nearest_sampler_state_; mutable std::vector depth_stencil_state_; + }; } diff --git a/include/gua/scenegraph/PBRNode.hpp b/include/gua/scenegraph/PBRNode.hpp index 4b9b84e3b..6f11c8929 100644 --- a/include/gua/scenegraph/PBRNode.hpp +++ b/include/gua/scenegraph/PBRNode.hpp @@ -37,12 +37,12 @@ class GUA_DLL PBRNode : public GeometryNode public : // member PBRNode(std::string const& name, - std::string const& filename = "gua_default_point_cloud", + std::string const& filename = "gua_default_geometry", std::string const& material = "gua_default_material", math::mat4 const& transform = math::mat4::identity()); /** - * Implements ray picking for a triangular mesh + * Implements ray picking for a point cloud */ /* virtual */ void ray_test_impl(RayNode const& ray, PickResult::Options options, diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag b/resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag index dca1b1759..eabd36901 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag @@ -20,7 +20,8 @@ in vec2 gua_quad_coords; /////////////////////////////////////////////////////////////////////////////// uniform int using_default_pbr_material; -uniform sampler2DArray color_texture; +uniform sampler2D color_texture; + /////////////////////////////////////////////////////////////////////////////// @@ -34,7 +35,7 @@ uniform sampler2DArray color_texture; /////////////////////////////////////////////////////////////////////////////// void main() { - vec3 output_color = vec3(0.0); + vec3 output_color = vec3(1.0); float output_depth = 1.0f; vec3 output_normal = vec3(0.0); @@ -48,20 +49,20 @@ void main() { - vec4 finalcol = texture2DArray( color_texture, coords); + vec4 finalcol = texture2D( color_texture, coords.xy); output_depth = ogldepth; - output_color = finalcol.rgb; - + //output_color = vec3(finalcol.rg, 1.0);//vec3(0.0,1.0,0.0);//finalcol.rgb; + output_color = finalcol.rgb; gua_uint_gbuffer_out_0.x = gua_uint_gbuffer_varying_0.x; - if ( using_default_video_material != 0 ) + // if ( using_default_pbr_material != 0 ) { @apply_pbr_color @apply_pbr_normal } - gl_FragDepth = output_depth; + gl_FragDepth = 0.5;//output_depth; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert b/resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert index 4130e0fd2..49c6d4a98 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert @@ -22,22 +22,6 @@ uniform uint gua_material_id; out vec3 gua_position_varying; out vec2 gua_quad_coords; -/////////////////////////////////////////////////////////////////////////////// -// varying output -/////////////////////////////////////////////////////////////////////////////// -vec2 gua_texcoords; - -vec3 gua_world_normal; -vec3 gua_world_position; -vec3 gua_world_tangent; -vec3 gua_world_bitangent; - -vec3 gua_object_normal; -vec3 gua_object_position; -vec2 gua_object_texcoords; -vec3 gua_object_tangent; -vec3 gua_object_bitangent; - @output_definition /////////////////////////////////////////////////////////////////////////////// @@ -48,7 +32,7 @@ uint gua_get_material_id() { return gua_uint_gbuffer_varying_0.x; } -@material_methods + /////////////////////////////////////////////////////////////////////////////// // main @@ -56,17 +40,7 @@ uint gua_get_material_id() { void main() { gua_position_varying = vec3(0); gua_quad_coords = gua_in_texcoord; - gua_texcoords = gua_in_texcoord; - - gua_object_normal = vec3(0); - gua_object_tangent = vec3(0); - gua_object_bitangent = vec3(0); - gua_object_position = vec3(0); - gua_world_normal = vec3(0); - gua_world_tangent = vec3(0); - gua_world_bitangent = vec3(0); - gua_world_position = vec3(0); gua_uint_gbuffer_varying_0.x = gua_material_id; gl_Position = vec4(gua_in_position, 1.0); diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag index b9d3d4107..66d710b50 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag @@ -20,6 +20,6 @@ layout (location=0) out vec4 out_color; /////////////////////////////////////////////////////////////////////////////// void main() { - //out_color = vec4(gua_point_color,1.0); - out_color = vec4(1.0,0.0,0.0,1.0); + out_color = vec4(gua_point_color,1.0); + //out_color = vec4(1.0,0.0,0.0,1.0); } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert index 738a63b93..5ae84173f 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert @@ -27,13 +27,18 @@ void main() { - + gl_Position = gua_projection_matrix * gua_view_matrix * gua_model_matrix * vec4(in_position,1.0); - - out_point_color = vec3((in_r)/255.0f, + + + + //gl_PointSize = 10000.0f; + //gl_Position = vec4(0.5,0.5,0.0,1.0); + + gua_point_color = vec3((in_r)/255.0f, (in_g)/255.0f, (in_b)/255.0f); } diff --git a/src/gua/renderer/PBRLoader.cpp b/src/gua/renderer/PBRLoader.cpp index a57374315..6dfc588ba 100644 --- a/src/gua/renderer/PBRLoader.cpp +++ b/src/gua/renderer/PBRLoader.cpp @@ -36,6 +36,8 @@ #include #include +#include + namespace gua { unsigned PBRLoader::model_counter_ = 0; @@ -69,9 +71,9 @@ std::shared_ptr PBRLoader::load(std::string const& file_name, auto node(std::make_shared(model_name)); node->set_filename(model_name); - node->set_material("data/materials/Red.gmd"); + node->set_material(""); - return node; + return node; } else diff --git a/src/gua/renderer/PBRRessource.cpp b/src/gua/renderer/PBRRessource.cpp index 7c26c6641..2db249307 100644 --- a/src/gua/renderer/PBRRessource.cpp +++ b/src/gua/renderer/PBRRessource.cpp @@ -88,7 +88,6 @@ void PBRRessource::upload_to(RenderContext const& ctx) const { &(point_cloud_->data()[0])); - std::vector buffer_arrays; buffer_arrays.push_back(buffers_[ctx.id]); @@ -122,7 +121,6 @@ void PBRRessource::draw(RenderContext const& ctx) const { ctx.render_context->apply(); - std::cout << point_cloud_->num_surfels()<<"\n"; ctx.render_context->draw_arrays(scm::gl::PRIMITIVE_POINT_LIST, 0, point_cloud_->num_surfels()); } diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index f9f625fb3..7d43c1555 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -24,14 +24,34 @@ // guacamole headers #include +#include #include #include #include #include + +#include + #include + namespace gua { + +//////////////////////////////////////////////////////////////////////////////// + +PBRUberShader::PBRUberShader() + : GeometryUberShader() +{ + if (!MaterialDatabase::instance()->is_supported(default_pbr_material_name())) + { + create_resource_material(default_pbr_material_name(), + Resources::materials_gua_pbr_gsd, + Resources::materials_gua_pbr_gmd); + } +} + + //////////////////////////////////////////////////////////////////////////////// void PBRUberShader::create(std::set const& material_names) @@ -61,76 +81,6 @@ namespace gua { - - //////////////////////////////////////////////////////////////////////////////// - - -/* - std::string const PBRUberShader::forward_point_rendering_vertex_shader() const - { - std::stringstream fpr_vertex; - - fpr_vertex << std::string(" \n\ - #version 420 core \n\ - \n\ - // input attributes \n\ - layout (location = 0) in vec3 in_position; \n\ - layout (location = 1) in uint in_r; \n\ - layout (location = 2) in uint in_g; \n\ - layout (location = 3) in uint in_b; \n\ - layout (location = 4) in uint empty; \n\ - layout (location = 5) in float in_radius; \n\ - layout (location = 6) in vec3 in_normal; \n\ - \n\ - uniform mat4 gua_projection_matrix; \n\ - uniform mat4 gua_view_matrix; \n\ - uniform mat4 gua_model_matrix; \n\ - \n\ - out vec3 point_color; \n\ - \n\ - \n\ - void main() \n\ - { \n\ - gl_Position = gua_projection_matrix * \n\ - gua_view_matrix * \n\ - gua_model_matrix * \n\ - vec4(in_position,1.0); \n\ - \n\ - point_color = vec3((in_r)/255.0f, \n\ - (in_g)/255.0f, \n\ - (in_b)/255.0f); \n\ - } \n\ - "); - - return fpr_vertex.str(); - } - - //////////////////////////////////////////////////////////////////////////////// - - std::string const PBRUberShader::forward_point_rendering_fragment_shader() const - { - std::stringstream fpr_fragment; - - fpr_fragment << std::string(" \n\ - #version 420 core \n\ - \n\ - layout(location = 0) out vec4 out_color; \n\ - \n\ - in vec3 point_color; \n\ - \n\ - \n\ - void main() \n\ - { \n\ - out_color = vec4(1.0,0.0,0.0,1.0); \n\ - } \n\ - "); - - return fpr_fragment.str(); - } - - -*/ - //////////////////////////////////////////////////////////////////////////////// /*virtual*/ GeometryUberShader::stage_mask const PBRUberShader::get_stage_mask() const @@ -197,28 +147,19 @@ std::string const PBRUberShader::accumulation_pass_fragment_shader() const std::string const PBRUberShader::normalization_pass_vertex_shader() const { std::string vertex_shader( - Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_video3d_blend_pass_vert) + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_normalization_vert) ); -/* + // material specific uniforms string_utils::replace(vertex_shader, "@uniform_definition", get_uniform_mapping()->get_uniform_definition()); -*/ + // output string_utils::replace(vertex_shader, "@output_definition", vshader_factory_->get_output_mapping().get_gbuffer_output_definition(false, true)); -/* - // print material specific methods - string_utils::replace(vertex_shader, "@material_methods", - UberShader::print_material_methods(*vshader_factory_)); - - // print main switch(es) - string_utils::replace(vertex_shader, "@material_switch", - UberShader::print_material_switch(*vshader_factory_)); -*/ return vertex_shader; } @@ -227,7 +168,7 @@ std::string const PBRUberShader::normalization_pass_vertex_shader() const std::string const PBRUberShader::normalization_pass_fragment_shader() const { std::string fragment_shader( - Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_video3d_blend_pass_frag) + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_normalization_frag) ); std::string apply_pbr_color = fshader_factory_->get_output_mapping().get_output_string("gua_pbr", "gua_pbr_output_color"); @@ -252,13 +193,6 @@ std::string const PBRUberShader::normalization_pass_fragment_shader() const string_utils::replace(fragment_shader, "@output_definition", get_gbuffer_mapping()->get_gbuffer_output_definition(false, false)); - // print material specific methods - string_utils::replace(fragment_shader, "@material_methods", - UberShader::print_material_methods(*fshader_factory_)); - - // print main switch(es) - string_utils::replace(fragment_shader, "@material_switch", - UberShader::print_material_switch(*fshader_factory_)); return fragment_shader; } @@ -347,7 +281,7 @@ void PBRUberShader::draw(RenderContext const& ctx, { if (!GeometryDatabase::instance()->is_supported(file_name) || !MaterialDatabase::instance()->is_supported(material_name)) { - gua::Logger::LOG_WARNING << "PBRUberShader::draw(): No such video or material." << file_name << ", " << material_name << std::endl; + gua::Logger::LOG_WARNING << "PBRUberShader::draw(): No such pbr ressource or material." << file_name << ", " << material_name << std::endl; return; } @@ -355,18 +289,23 @@ void PBRUberShader::draw(RenderContext const& ctx, auto material = MaterialDatabase::instance()->lookup(material_name); if (!material || !pbr_ressource) { - gua::Logger::LOG_WARNING << "PBRUberShader::draw(): Invalid video or material." << std::endl; + gua::Logger::LOG_WARNING << "PBRUberShader::draw(): Invalid pbr ressource or material." << std::endl; return; } // make sure ressources are on the GPU upload_to(ctx); + + + + { // single texture only scm::gl::context_all_guard guard(ctx.render_context); - //ctx.render_context->bind_texture(video3d_ressource->depth_array(ctx), nearest_sampler_state_[ctx.id], 0); - //get_program(warp_pass)->get_program(ctx)->uniform_sampler("depth_video3d_texture", 0); + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + + auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); ctx.render_context->set_depth_stencil_state(ds_state); @@ -376,16 +315,18 @@ void PBRUberShader::draw(RenderContext const& ctx, // pre passes // forward rendering, will later be changed to be actually pass 2 (accumulation) + // configure fbo point_forward_result_fbo_[ctx.id]->clear_attachments(); point_forward_result_fbo_[ctx.id]->attach_depth_stencil_buffer(point_forward_depth_result_[ctx.id]); point_forward_result_fbo_[ctx.id]->attach_color_buffer(0, point_forward_color_result_[ctx.id]); + // bind and clear fbo ctx.render_context->set_frame_buffer(point_forward_result_fbo_[ctx.id]); ctx.render_context->clear_depth_stencil_buffer(point_forward_result_fbo_[ctx.id]); - ctx.render_context->clear_color_buffer(point_forward_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); - + ctx.render_context->clear_color_buffer(point_forward_result_fbo_[ctx.id], 0, scm::math::vec4f(1.0f, 0.0f, 0.0f, 0.0f)); + get_program(point_forward_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(point_forward_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); @@ -400,7 +341,7 @@ void PBRUberShader::draw(RenderContext const& ctx, get_program(point_forward_pass)->unuse(ctx); } - ctx.render_context->reset_framebuffer(); + //ctx.render_context->reset_framebuffer(); } @@ -412,6 +353,9 @@ void PBRUberShader::draw(RenderContext const& ctx, // single texture only scm::gl::context_all_guard guard(ctx.render_context); + auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); + ctx.render_context->set_depth_stencil_state(ds_state); + // second pass get_program(quad_pass)->use(ctx); { @@ -424,9 +368,13 @@ void PBRUberShader::draw(RenderContext const& ctx, get_program(quad_pass)->set_uniform(ctx, int(material_name == default_pbr_material_name()) , "using_default_pbr_material"); + + + ctx.render_context->bind_texture(point_forward_color_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); get_program(quad_pass)->get_program(ctx)->uniform_sampler("color_texture", 0); + fullscreen_quad_[ctx.id]->draw(ctx.render_context); } } diff --git a/src/gua/renderer/Video3DUberShader.cpp b/src/gua/renderer/Video3DUberShader.cpp index 72a4d0395..87fdf49cb 100644 --- a/src/gua/renderer/Video3DUberShader.cpp +++ b/src/gua/renderer/Video3DUberShader.cpp @@ -147,10 +147,10 @@ std::string const Video3DUberShader::_blend_pass_fragment_shader() const Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_video3d_blend_pass_frag) ); - std::string apply_video_color = fshader_factory_->get_output_mapping().get_output_string("gua_video3d", "gua_video_output_color"); + std::string apply_video_color = fshader_factory_->get_output_mapping().get_output_string(default_video_material_name(), "gua_video_output_color"); apply_video_color += " = output_color;\n"; - std::string apply_video_normal = fshader_factory_->get_output_mapping().get_output_string("gua_video3d", "gua_normal"); + std::string apply_video_normal = fshader_factory_->get_output_mapping().get_output_string(default_video_material_name(), "gua_normal"); apply_video_normal += " = output_normal;\n"; string_utils::replace(fragment_shader, "@apply_video3d_color", apply_video_color); From 7a1d491229a3fba9cd55b5293043b81e7840d358 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Fri, 9 May 2014 19:49:41 +0200 Subject: [PATCH 13/45] elliptic splats --- include/gua/renderer/.RenderContext.hpp.swp | Bin 12288 -> 0 bytes .../gbuffer/pbr/point_forward.frag | 78 +++++++++++++++++- .../gbuffer/pbr/point_forward.vert | 30 +++---- src/gua/renderer/PBRUberShader.cpp | 14 +++- 4 files changed, 105 insertions(+), 17 deletions(-) delete mode 100644 include/gua/renderer/.RenderContext.hpp.swp diff --git a/include/gua/renderer/.RenderContext.hpp.swp b/include/gua/renderer/.RenderContext.hpp.swp deleted file mode 100644 index 7376562cade5ed80dfe6555dc29cbdbf8b4ccb9f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2O>Y}T7{{ly1zISr;06btHd2+ki8pD4D!Em`O_Rn_>O_8N2nWn$ch)U?=PbF2GJv`E)CDmq4(z@6C z2CWHav@Lb0rPYkN&0C=_EE(8-?1-eqibUXuNJLg8{bMo=w78)2Uc0@e6Vt#bU=-Lu zfqiV^*!WmdIdteCJ@#mQgLbCaC}0#Y3K#{90!9I&fKk9GU=;W-Dxljt*hNh2zHD-f z+2@WmpV?RQVH7Y57zK<1MggOMQNSo*6fg=H1&jhl0i(dbPyy~RwsR+AZ|%b1@&Et* zfB%2JpRpIf0r1r$jD_Gy@WVdFE&>bOc$l&Gz|-L7LyTPkbKv$~#;$<|*blzk!`K(# z3V0K|0emnE4ub>W_XipK4!jSZ2P5Dpcmg~Q_JTcNH@NWtV?Tnc;6v~ZxC}0VDmVv@ zfMIY5+`6B!U%>a^I`|rV0Nw>Jffs=T4g&(Wb~E-H_!(RSpMuxGtDpnQUD=p$?nTtdmt#StGwXWiSGq6AzzMrK>RnN4bknZ`hH9RmUNLn&X?uiGWEYwo zl7S9aSyOhaP-yyvLT1hp9chb3TSt8dy^?hfl#R)?vPRTvL|m!RmRv)juGHRu0Gl|? zBwFn?E84;>TB+n>*0vpYG3%a0sng9x-0rLrgE; z@9Kj*nnOmsYfrlVRx(jL3Y$pWc`CUSgnERO5OmVhx?QlWZnv9n25~-&nwF0o7$~8v zQ~80hr1loWn3NaBzC#}G2y`Pr>J(g5A*IH_v&pox`7V@dGjsJC70ZjXP^?sn<=Wzj^t=dTO`;=$)R1hoeVH62 zJc^KQQeDsCT%JMu;_1?CskYeDua=y$S!k1^4jWTPaw$$uA=$I#A(z4^N6ymlge0~k zC|1Ia{SiXTz9{!39z0kkA7^4j9c(xx%fBCxbbQs&)euF{G=6^|1;FA62^_a89%!P! z3bQ3QLWtrp=D!@q5l)t*(~&G5FoK>*>@da_&Yk3#*ffwhoZ88XRvsi?T27l!M_2&q z_}0e5O=?s&=t43AvUz}Qvl6HzEIe?mbmii=*m6>U=bpr45 0.0) + normalZ = max(zBound, normalZ); + else + normalZ = -max(zBound, -normalZ); + + xzRatio = (normal.x/normalZ); + yzRatio = (normal.y/normalZ); + +} + + return -(xzRatio)*mappedPointCoord.x - (yzRatio * mappedPointCoord.y); + +} + + + + +float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNormalVec) +{ + + float radius; + //if(ellipsify) + radius = mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y + depth_offset*depth_offset; + //else + // radius = mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y ; + + + if(radius > 1.0) + discard; + else + return 1.0f; +} + + /////////////////////////////////////////////////////////////////////////////// // main /////////////////////////////////////////////////////////////////////////////// void main() { - out_color = vec4(gua_point_color,1.0); + + // out_color = vec4(gua_point_color,1.0); //out_color = vec4(1.0,0.0,0.0,1.0); + + vec2 mappedPointCoord = gl_PointCoord*2 + vec2(-1.0f, -1.0f); + + + float depth_offset = calc_depth_offset(mappedPointCoord) ; + + + //get_gaussianValue(depth_offset, mappedPointCoord, VertexIn.nor.xyz); + get_gaussianValue(depth_offset, mappedPointCoord, gua_normal); + + + out_color = vec4(gua_point_color,1.0); + + } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert index 5ae84173f..b533570c1 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert @@ -17,28 +17,30 @@ uniform uint gua_material_id; - // uniform mat4 gua_projection_matrix; - // uniform mat4 gua_view_matrix; - // uniform mat4 gua_model_matrix; - // uniform mat4 gua_normal_matrix; - - out vec3 gua_point_color; + + uniform float height_divided_by_top_minus_bottom; + uniform float near_plane; + + //output to fragment shader + out vec3 gua_point_color; + out vec3 gua_normal; void main() { - gl_Position = gua_projection_matrix * - gua_view_matrix * - gua_model_matrix * - vec4(in_position,1.0); - + vec4 pos_es = gua_view_matrix * gua_model_matrix * vec4(in_position, 1.0); + + gl_Position = gua_projection_matrix * pos_es; - //gl_PointSize = 10000.0f; - //gl_Position = vec4(0.5,0.5,0.0,1.0); + gl_PointSize = 2.0f * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; + + gua_point_color = vec3((in_r)/255.0f, (in_g)/255.0f, - (in_b)/255.0f); + (in_b)/255.0f); + + gua_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; } diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index 7d43c1555..38450750a 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -277,7 +277,7 @@ void PBRUberShader::draw(RenderContext const& ctx, std::string const& material_name, scm::math::mat4 const& model_matrix, scm::math::mat4 const& normal_matrix, - Frustum const& /*frustum*/) const + Frustum const& frustum) const { if (!GeometryDatabase::instance()->is_supported(file_name) || !MaterialDatabase::instance()->is_supported(material_name)) { @@ -328,9 +328,21 @@ void PBRUberShader::draw(RenderContext const& ctx, ctx.render_context->clear_color_buffer(point_forward_result_fbo_[ctx.id], 0, scm::math::vec4f(1.0f, 0.0f, 0.0f, 0.0f)); + gua::math::mat4 const& projection_matrix = frustum.get_projection(); + + float near_plane_value = frustum.get_clip_near(); + float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; + float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; + float height_divided_by_top_minus_bottom = 800.0 / (top_plane_value - bottom_plane_value); + + //std::cout << "hdbtmb: "<< height_divided_by_top_minus_bottom; + get_program(point_forward_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(point_forward_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + get_program(point_forward_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); + get_program(point_forward_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + if (material && pbr_ressource) { From 307e67fbf37a9770afc7504c448bddcd555b0841 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Fri, 9 May 2014 22:48:38 +0200 Subject: [PATCH 14/45] add depth pass and rename of shader stages --- include/gua/renderer/PBRUberShader.hpp | 15 ++- .../{point_forward.frag => p01_depth.frag} | 9 +- .../{point_forward.vert => p01_depth.vert} | 0 .../gbuffer/pbr/p02_accumulation.frag | 96 ++++++++++++++ .../gbuffer/pbr/p02_accumulation.vert | 46 +++++++ ...malization.frag => p03_normalization.frag} | 0 ...malization.vert => p03_normalization.vert} | 0 src/gua/renderer/PBRUberShader.cpp | 119 ++++++++++++------ 8 files changed, 233 insertions(+), 52 deletions(-) rename resources/shaders/uber_shaders/gbuffer/pbr/{point_forward.frag => p01_depth.frag} (93%) rename resources/shaders/uber_shaders/gbuffer/pbr/{point_forward.vert => p01_depth.vert} (100%) create mode 100644 resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag create mode 100644 resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert rename resources/shaders/uber_shaders/gbuffer/pbr/{normalization.frag => p03_normalization.frag} (100%) rename resources/shaders/uber_shaders/gbuffer/pbr/{normalization.vert => p03_normalization.vert} (100%) diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp index dcb60baae..126cbe57e 100644 --- a/include/gua/renderer/PBRUberShader.hpp +++ b/include/gua/renderer/PBRUberShader.hpp @@ -32,8 +32,9 @@ class PBRUberShader : public GeometryUberShader { public: enum pass { - point_forward_pass = 0, - quad_pass = 1 + depth_pass = 0, + accumulation_pass = 1, + normalization_pass = 2 }; public: @@ -72,6 +73,9 @@ class PBRUberShader : public GeometryUberShader { /*virtual*/ void postframe (RenderContext const& context) const; private: //auxialiary methods + std::string const depth_pass_vertex_shader() const; + std::string const depth_pass_fragment_shader() const; + std::string const accumulation_pass_vertex_shader() const; std::string const accumulation_pass_fragment_shader() const; @@ -82,9 +86,10 @@ class PBRUberShader : public GeometryUberShader { private: //member variables - mutable std::vector point_forward_depth_result_; - mutable std::vector point_forward_color_result_; - mutable std::vector point_forward_result_fbo_; + + mutable std::vector accumulation_pass_depth_result_; + mutable std::vector accumulation_pass_color_result_; + mutable std::vector accumulation_pass_result_fbo_; diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag similarity index 93% rename from resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag rename to resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag index fdddb6102..ea7f05994 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag @@ -37,7 +37,7 @@ float calc_depth_offset(vec2 mappedPointCoord) //if(clamped_normal_mode) { - float zBound = 0.5f;//max_deform_ratio; + float zBound = 0.35f;//max_deform_ratio; float normalZ = normal.z; if(normalZ > 0.0) @@ -77,11 +77,8 @@ float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNorma /////////////////////////////////////////////////////////////////////////////// // main /////////////////////////////////////////////////////////////////////////////// -void main() { - - - // out_color = vec4(gua_point_color,1.0); - //out_color = vec4(1.0,0.0,0.0,1.0); +void main() +{ vec2 mappedPointCoord = gl_PointCoord*2 + vec2(-1.0f, -1.0f); diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert similarity index 100% rename from resources/shaders/uber_shaders/gbuffer/pbr/point_forward.vert rename to resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag new file mode 100644 index 000000000..ea7f05994 --- /dev/null +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -0,0 +1,96 @@ +@include "shaders/common/header.glsl" + +/////////////////////////////////////////////////////////////////////////////// +// input +/////////////////////////////////////////////////////////////////////////////// +in vec3 gua_point_color; +in vec3 gua_normal; + + +/////////////////////////////////////////////////////////////////////////////// +// output +/////////////////////////////////////////////////////////////////////////////// + +layout (location=0) out vec4 out_color; + + + + + +/////////////////////////////////////////////////////////////////////////////// +// splatting methods +/////////////////////////////////////////////////////////////////////////////// + +float calc_depth_offset(vec2 mappedPointCoord) +{ + + vec3 normal = gua_normal; + if(normal.z < 0) + { + + //discard; + normal *= -1; + } + + float xzRatio = (normal.x/normal.z); + float yzRatio = (normal.y/normal.z); + +//if(clamped_normal_mode) +{ + float zBound = 0.35f;//max_deform_ratio; + float normalZ = normal.z; + + if(normalZ > 0.0) + normalZ = max(zBound, normalZ); + else + normalZ = -max(zBound, -normalZ); + + xzRatio = (normal.x/normalZ); + yzRatio = (normal.y/normalZ); + +} + + return -(xzRatio)*mappedPointCoord.x - (yzRatio * mappedPointCoord.y); + +} + + + + +float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNormalVec) +{ + + float radius; + //if(ellipsify) + radius = mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y + depth_offset*depth_offset; + //else + // radius = mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y ; + + + if(radius > 1.0) + discard; + else + return 1.0f; +} + + +/////////////////////////////////////////////////////////////////////////////// +// main +/////////////////////////////////////////////////////////////////////////////// +void main() +{ + + vec2 mappedPointCoord = gl_PointCoord*2 + vec2(-1.0f, -1.0f); + + + float depth_offset = calc_depth_offset(mappedPointCoord) ; + + + //get_gaussianValue(depth_offset, mappedPointCoord, VertexIn.nor.xyz); + get_gaussianValue(depth_offset, mappedPointCoord, gua_normal); + + + out_color = vec4(gua_point_color,1.0); + + +} diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert new file mode 100644 index 000000000..b533570c1 --- /dev/null +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert @@ -0,0 +1,46 @@ +@include "shaders/common/header.glsl" + +/////////////////////////////////////////////////////////////////////////////// +// general uniforms +/////////////////////////////////////////////////////////////////////////////// +@include "shaders/uber_shaders/common/gua_camera_uniforms.glsl" + + + // input attributes + layout (location = 0) in vec3 in_position; + layout (location = 1) in uint in_r; + layout (location = 2) in uint in_g; + layout (location = 3) in uint in_b; + layout (location = 4) in uint empty; + layout (location = 5) in float in_radius; + layout (location = 6) in vec3 in_normal; + + + uniform uint gua_material_id; + + uniform float height_divided_by_top_minus_bottom; + uniform float near_plane; + + //output to fragment shader + out vec3 gua_point_color; + out vec3 gua_normal; + + + void main() + { + + vec4 pos_es = gua_view_matrix * gua_model_matrix * vec4(in_position, 1.0); + + gl_Position = gua_projection_matrix * pos_es; + + + gl_PointSize = 2.0f * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; + + + + gua_point_color = vec3((in_r)/255.0f, + (in_g)/255.0f, + (in_b)/255.0f); + + gua_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; + } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag similarity index 100% rename from resources/shaders/uber_shaders/gbuffer/pbr/normalization.frag rename to resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.vert similarity index 100% rename from resources/shaders/uber_shaders/gbuffer/pbr/normalization.vert rename to resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.vert diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index 38450750a..c606d7749 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -58,14 +58,23 @@ PBRUberShader::PBRUberShader() { UberShader::create(material_names); + // create depth pass shader + std::vector depth_pass_stages; + depth_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_VERTEX_SHADER, depth_pass_vertex_shader())); + depth_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_FRAGMENT_SHADER, depth_pass_fragment_shader())); + + auto depth_pass_program = std::make_shared(); + depth_pass_program->set_shaders(depth_pass_stages); + add_program(depth_pass_program); + // create depth shader - std::vector point_forward_pass_stages; - point_forward_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_VERTEX_SHADER, accumulation_pass_vertex_shader())); - point_forward_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_FRAGMENT_SHADER, accumulation_pass_fragment_shader())); + std::vector accumulation_pass_stages; + accumulation_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_VERTEX_SHADER, accumulation_pass_vertex_shader())); + accumulation_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_FRAGMENT_SHADER, accumulation_pass_fragment_shader())); - auto point_forward_pass_program = std::make_shared(); - point_forward_pass_program->set_shaders(point_forward_pass_stages); - add_program(point_forward_pass_program); + auto accumulation_pass_program = std::make_shared(); + accumulation_pass_program->set_shaders(accumulation_pass_stages); + add_program(accumulation_pass_program); // create final shader std::vector normalization_pass_stages; @@ -110,21 +119,49 @@ PBRUberShader::PBRUberShader() + + //////////////////////////////////////////////////////////////////////////////// -std::string const PBRUberShader::accumulation_pass_vertex_shader() const +std::string const PBRUberShader::depth_pass_vertex_shader() const { + std::string vertex_shader( - Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_point_forward_vert) + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p01_depth_vert) ); + return vertex_shader; } +//////////////////////////////////////////////////////////////////////////////// + +std::string const PBRUberShader::depth_pass_fragment_shader() const +{ + + std::string fragment_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p01_depth_frag) + ); + + return fragment_shader; +} + + + +//////////////////////////////////////////////////////////////////////////////// + + +std::string const PBRUberShader::accumulation_pass_vertex_shader() const +{ + std::string vertex_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p02_accumulation_vert) + ); + return vertex_shader; +} @@ -133,7 +170,7 @@ std::string const PBRUberShader::accumulation_pass_vertex_shader() const std::string const PBRUberShader::accumulation_pass_fragment_shader() const { std::string fragment_shader( - Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_point_forward_frag) + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p02_accumulation_frag) ); return fragment_shader; @@ -147,7 +184,7 @@ std::string const PBRUberShader::accumulation_pass_fragment_shader() const std::string const PBRUberShader::normalization_pass_vertex_shader() const { std::string vertex_shader( - Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_normalization_vert) + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p03_normalization_vert) ); @@ -168,7 +205,7 @@ std::string const PBRUberShader::normalization_pass_vertex_shader() const std::string const PBRUberShader::normalization_pass_fragment_shader() const { std::string fragment_shader( - Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_normalization_frag) + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p03_normalization_frag) ); std::string apply_pbr_color = fshader_factory_->get_output_mapping().get_output_string("gua_pbr", "gua_pbr_output_color"); @@ -211,14 +248,14 @@ bool PBRUberShader::upload_to (RenderContext const& context) const (context.render_window->config.get_left_resolution()[1] == context.render_window->config.get_right_resolution()[1]))); // initialize Texture Arrays (kinect depths & colors) - if ( context.id >= point_forward_color_result_.size() || - context.id >= point_forward_depth_result_.size()) + if ( context.id >= accumulation_pass_color_result_.size() || + context.id >= accumulation_pass_depth_result_.size()) { - point_forward_color_result_.resize(context.id + 1); - point_forward_depth_result_.resize(context.id + 1); + accumulation_pass_color_result_.resize(context.id + 1); + accumulation_pass_depth_result_.resize(context.id + 1); - point_forward_color_result_[context.id] = context.render_device->create_texture_2d( + accumulation_pass_color_result_[context.id] = context.render_device->create_texture_2d( context.render_window->config.get_left_resolution(), scm::gl::FORMAT_RGBA_32F, 1, @@ -226,7 +263,7 @@ bool PBRUberShader::upload_to (RenderContext const& context) const 1 ); - point_forward_depth_result_[context.id] = context.render_device->create_texture_2d( + accumulation_pass_depth_result_[context.id] = context.render_device->create_texture_2d( context.render_window->config.get_left_resolution(), scm::gl::FORMAT_D32F, 1, @@ -240,9 +277,9 @@ bool PBRUberShader::upload_to (RenderContext const& context) const fullscreen_quad_[context.id].reset(new scm::gl::quad_geometry(context.render_device, math::vec2(-1.f, -1.f), math::vec2(1.f, 1.f))); } - if (context.id >= point_forward_result_fbo_.size()) { - point_forward_result_fbo_.resize(context.id + 1); - point_forward_result_fbo_[context.id] = context.render_device->create_frame_buffer(); + if (context.id >= accumulation_pass_result_fbo_.size()) { + accumulation_pass_result_fbo_.resize(context.id + 1); + accumulation_pass_result_fbo_[context.id] = context.render_device->create_frame_buffer(); } if (context.id >= linear_sampler_state_.size()) { @@ -317,15 +354,15 @@ void PBRUberShader::draw(RenderContext const& ctx, // configure fbo - point_forward_result_fbo_[ctx.id]->clear_attachments(); - point_forward_result_fbo_[ctx.id]->attach_depth_stencil_buffer(point_forward_depth_result_[ctx.id]); - point_forward_result_fbo_[ctx.id]->attach_color_buffer(0, point_forward_color_result_[ctx.id]); + accumulation_pass_result_fbo_[ctx.id]->clear_attachments(); + accumulation_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(accumulation_pass_depth_result_[ctx.id]); + accumulation_pass_result_fbo_[ctx.id]->attach_color_buffer(0, accumulation_pass_color_result_[ctx.id]); // bind and clear fbo - ctx.render_context->set_frame_buffer(point_forward_result_fbo_[ctx.id]); - ctx.render_context->clear_depth_stencil_buffer(point_forward_result_fbo_[ctx.id]); - ctx.render_context->clear_color_buffer(point_forward_result_fbo_[ctx.id], 0, scm::math::vec4f(1.0f, 0.0f, 0.0f, 0.0f)); + ctx.render_context->set_frame_buffer(accumulation_pass_result_fbo_[ctx.id]); + ctx.render_context->clear_depth_stencil_buffer(accumulation_pass_result_fbo_[ctx.id]); + ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); gua::math::mat4 const& projection_matrix = frustum.get_projection(); @@ -337,23 +374,23 @@ void PBRUberShader::draw(RenderContext const& ctx, //std::cout << "hdbtmb: "<< height_divided_by_top_minus_bottom; - get_program(point_forward_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); - get_program(point_forward_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); - get_program(point_forward_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); - get_program(point_forward_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + get_program(accumulation_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); + get_program(accumulation_pass)->set_uniform(ctx, near_plane_value, "near_plane"); if (material && pbr_ressource) { - get_program(point_forward_pass)->use(ctx); + get_program(accumulation_pass)->use(ctx); { pbr_ressource->draw(ctx); } - get_program(point_forward_pass)->unuse(ctx); + get_program(accumulation_pass)->unuse(ctx); } - //ctx.render_context->reset_framebuffer(); + ctx.render_context->reset_framebuffer(); } @@ -369,28 +406,28 @@ void PBRUberShader::draw(RenderContext const& ctx, ctx.render_context->set_depth_stencil_state(ds_state); // second pass - get_program(quad_pass)->use(ctx); + get_program(normalization_pass)->use(ctx); { if (material /*&& video3d_ressource*/) { - get_program(quad_pass)->set_uniform(ctx, material->get_id(), "gua_material_id"); - get_program(quad_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); - get_program(quad_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + get_program(normalization_pass)->set_uniform(ctx, material->get_id(), "gua_material_id"); + get_program(normalization_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(normalization_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); - get_program(quad_pass)->set_uniform(ctx, int(material_name == default_pbr_material_name()) , "using_default_pbr_material"); + get_program(normalization_pass)->set_uniform(ctx, int(material_name == default_pbr_material_name()) , "using_default_pbr_material"); - ctx.render_context->bind_texture(point_forward_color_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); - get_program(quad_pass)->get_program(ctx)->uniform_sampler("color_texture", 0); + ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); + get_program(normalization_pass)->get_program(ctx)->uniform_sampler("color_texture", 0); fullscreen_quad_[ctx.id]->draw(ctx.render_context); } } - get_program(quad_pass)->unuse(ctx); + get_program(normalization_pass)->unuse(ctx); } } From cf06ee55bb91cbe302fe1796205b975077d21ce0 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Fri, 9 May 2014 23:07:27 +0200 Subject: [PATCH 15/45] code restructuring --- include/gua/renderer/PBRUberShader.hpp | 12 +++ src/gua/renderer/PBRUberShader.cpp | 101 +++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp index 126cbe57e..a4b0bbca5 100644 --- a/include/gua/renderer/PBRUberShader.hpp +++ b/include/gua/renderer/PBRUberShader.hpp @@ -86,7 +86,14 @@ class PBRUberShader : public GeometryUberShader { private: //member variables + //FBOs: + //depth pass FBO & attachments + mutable std::vector depth_pass_depth_result_; + mutable std::vector depth_pass_color_result_; + mutable std::vector depth_pass_result_fbo_; + + //accumulation pass FBO & attachments mutable std::vector accumulation_pass_depth_result_; mutable std::vector accumulation_pass_color_result_; mutable std::vector accumulation_pass_result_fbo_; @@ -104,6 +111,11 @@ class PBRUberShader : public GeometryUberShader { mutable std::vector nearest_sampler_state_; mutable std::vector depth_stencil_state_; + + //frustum dependent variables + float near_plane_value_; + float height_divided_by_top_minus_bottom_; + }; } diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index c606d7749..b69dd0dcd 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -41,7 +41,7 @@ namespace gua { //////////////////////////////////////////////////////////////////////////////// PBRUberShader::PBRUberShader() - : GeometryUberShader() + : GeometryUberShader(), near_plane_value_(0.0f), height_divided_by_top_minus_bottom_(0.0f) { if (!MaterialDatabase::instance()->is_supported(default_pbr_material_name())) { @@ -247,7 +247,34 @@ bool PBRUberShader::upload_to (RenderContext const& context) const ((context.render_window->config.get_left_resolution()[0] == context.render_window->config.get_right_resolution()[0]) && (context.render_window->config.get_left_resolution()[1] == context.render_window->config.get_right_resolution()[1]))); - // initialize Texture Arrays (kinect depths & colors) + + + // initialize attachments for depth pass + if ( context.id >= depth_pass_color_result_.size() || + context.id >= depth_pass_depth_result_.size()) + { + depth_pass_color_result_.resize(context.id + 1); + depth_pass_depth_result_.resize(context.id + 1); + + + depth_pass_color_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_RGBA_32F, + 1, + 1, + 1 + ); + + depth_pass_depth_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_D32F, + 1, + 1, + 1 + ); + } + + // initialize attachments for accumulation pass if ( context.id >= accumulation_pass_color_result_.size() || context.id >= accumulation_pass_depth_result_.size()) { @@ -277,6 +304,11 @@ bool PBRUberShader::upload_to (RenderContext const& context) const fullscreen_quad_[context.id].reset(new scm::gl::quad_geometry(context.render_device, math::vec2(-1.f, -1.f), math::vec2(1.f, 1.f))); } + if (context.id >= depth_pass_result_fbo_.size()) { + depth_pass_result_fbo_.resize(context.id + 1); + depth_pass_result_fbo_[context.id] = context.render_device->create_frame_buffer(); + } + if (context.id >= accumulation_pass_result_fbo_.size()) { accumulation_pass_result_fbo_.resize(context.id + 1); accumulation_pass_result_fbo_[context.id] = context.render_device->create_frame_buffer(); @@ -334,23 +366,75 @@ void PBRUberShader::draw(RenderContext const& ctx, upload_to(ctx); - - +/* + // pre passes: + ///////////////////// + // begin of depth pass (first) { // single texture only scm::gl::context_all_guard guard(ctx.render_context); ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); - - auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); ctx.render_context->set_depth_stencil_state(ds_state); - // pre passes - // forward rendering, will later be changed to be actually pass 2 (accumulation) + + + + // configure fbo + depth_pass_result_fbo_[ctx.id]->clear_attachments(); + depth_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(depth_pass_depth_result_[ctx.id]); + depth_pass_result_fbo_[ctx.id]->attach_color_buffer(0, depth_pass_color_result_[ctx.id]); + + + // bind and clear fbo + ctx.render_context->set_frame_buffer(depth_pass_result_fbo_[ctx.id]); + ctx.render_context->clear_depth_stencil_buffer(depth_pass_result_fbo_[ctx.id]); + ctx.render_context->clear_color_buffer(depth_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); + + + gua::math::mat4 const& projection_matrix = frustum.get_projection(); + + float near_plane_value = frustum.get_clip_near(); + float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; + float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; + float height_divided_by_top_minus_bottom = 800.0 / (top_plane_value - bottom_plane_value); + + + + get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + + get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); + get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + + if (material && pbr_ressource) + { + + get_program(depth_pass)->use(ctx); + { + pbr_ressource->draw(ctx); + } + get_program(depth_pass)->unuse(ctx); + } + + ctx.render_context->reset_framebuffer(); + } +*/ + // begin of accumulation pass (first) + + { + // single texture only + scm::gl::context_all_guard guard(ctx.render_context); + + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + + //!!!!!!!!!!!!EXCHANGE THIS WITH DEPH TEST NONE AND PUT COLOR ACCUMULATION ALSO SOMEWHERE + auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); + ctx.render_context->set_depth_stencil_state(ds_state); // configure fbo @@ -367,6 +451,7 @@ void PBRUberShader::draw(RenderContext const& ctx, gua::math::mat4 const& projection_matrix = frustum.get_projection(); + //put near_plane_value and height_divided_by_top_minus_bottom as member variables and get these two only 1 per render frame float near_plane_value = frustum.get_clip_near(); float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; From 9d6caffeca8954835f27da02b9c2be8b093e3ae3 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Sun, 11 May 2014 00:16:47 +0200 Subject: [PATCH 16/45] 2 pass multi xyz nodes support --- include/gua/renderer/PBRUberShader.hpp | 8 +- src/gua/renderer/PBRUberShader.cpp | 220 +++++++++++++++---------- 2 files changed, 134 insertions(+), 94 deletions(-) diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp index a4b0bbca5..888be2d55 100644 --- a/include/gua/renderer/PBRUberShader.hpp +++ b/include/gua/renderer/PBRUberShader.hpp @@ -111,10 +111,12 @@ class PBRUberShader : public GeometryUberShader { mutable std::vector nearest_sampler_state_; mutable std::vector depth_stencil_state_; - + //frustum dependent variables - float near_plane_value_; - float height_divided_by_top_minus_bottom_; + + mutable std::vector material_id_; + mutable std::vector near_plane_value_; + mutable std::vector height_divided_by_top_minus_bottom_; }; diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index b69dd0dcd..0f7b01856 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -88,39 +88,6 @@ PBRUberShader::PBRUberShader() - - - //////////////////////////////////////////////////////////////////////////////// - - /*virtual*/ GeometryUberShader::stage_mask const PBRUberShader::get_stage_mask() const - { - - return GeometryUberShader::DRAW_STAGE; - } - - //////////////////////////////////////////////////////////////////////////////// - - /*virtual*/ void PBRUberShader::preframe(RenderContext const& context) const - { - throw std::runtime_error("PBRUberShader::preframe(): not implemented"); - } - - //////////////////////////////////////////////////////////////////////////////// - - /*virtual*/ void PBRUberShader::predraw(RenderContext const& ctx, - std::string const& filename, - std::string const& material_name, - scm::math::mat4 const& model_matrix, - scm::math::mat4 const& normal_matrix, - Frustum const& /*frustum*/) const - { - throw std::runtime_error("PBRUberShader::predraw(): not implemented"); - } - - - - - //////////////////////////////////////////////////////////////////////////////// @@ -334,11 +301,73 @@ bool PBRUberShader::upload_to (RenderContext const& context) const change_point_size_in_shader_state_[context.id] = context.render_device->create_rasterizer_state(scm::gl::FILL_SOLID, scm::gl::CULL_NONE, scm::gl::ORIENT_CCW, false, false, 0.0, false, false, scm::gl::point_raster_state(true)); } + if (context.id >= material_id_.size()){ + material_id_.resize(context.id + 1); + material_id_[context.id] = 0; + } + + if (context.id >= near_plane_value_.size()){ + near_plane_value_.resize(context.id + 1); + near_plane_value_[context.id] = 0; + } + + if (context.id >= height_divided_by_top_minus_bottom_.size()){ + height_divided_by_top_minus_bottom_.resize(context.id + 1); + height_divided_by_top_minus_bottom_[context.id] = 0; + } + return upload_succeeded; } + + + + + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ GeometryUberShader::stage_mask const PBRUberShader::get_stage_mask() const + { + + return GeometryUberShader::PRE_FRAME_STAGE | GeometryUberShader::PRE_DRAW_STAGE | GeometryUberShader::POST_DRAW_STAGE | GeometryUberShader::POST_FRAME_STAGE;//0;//GeometryUberShader::DRAW_STAGE; + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PBRUberShader::preframe(RenderContext const& ctx) const + { + upload_to(ctx); + + + // configure fbo + accumulation_pass_result_fbo_[ctx.id]->clear_attachments(); + accumulation_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(accumulation_pass_depth_result_[ctx.id]); + accumulation_pass_result_fbo_[ctx.id]->attach_color_buffer(0, accumulation_pass_color_result_[ctx.id]); + + ctx.render_context->clear_depth_stencil_buffer(accumulation_pass_result_fbo_[ctx.id]); + ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PBRUberShader::predraw(RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const + { + + + + std::cout<<"PREDRAW\n"; + + } + + + //////////////////////////////////////////////////////////////////////////////// void PBRUberShader::draw(RenderContext const& ctx, @@ -366,6 +395,8 @@ void PBRUberShader::draw(RenderContext const& ctx, upload_to(ctx); + std::cout<<"draw\n"; + /* // pre passes: ///////////////////// @@ -424,30 +455,56 @@ void PBRUberShader::draw(RenderContext const& ctx, ctx.render_context->reset_framebuffer(); } */ - // begin of accumulation pass (first) + + + +} + + + + + + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PBRUberShader::postdraw(RenderContext const& ctx, + std::string const& file_name, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& frustum) const + { + std::cout<<"POSTDRAW\n"; + auto pbr_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); + auto material = MaterialDatabase::instance()->lookup(material_name); { // single texture only scm::gl::context_all_guard guard(ctx.render_context); - ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); //!!!!!!!!!!!!EXCHANGE THIS WITH DEPH TEST NONE AND PUT COLOR ACCUMULATION ALSO SOMEWHERE + +/* auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); - ctx.render_context->set_depth_stencil_state(ds_state); + ctx.render_context->set_depth_stencil_state(ds_state); +*/ +/* // configure fbo accumulation_pass_result_fbo_[ctx.id]->clear_attachments(); - accumulation_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(accumulation_pass_depth_result_[ctx.id]); + // accumulation_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(accumulation_pass_depth_result_[ctx.id]); accumulation_pass_result_fbo_[ctx.id]->attach_color_buffer(0, accumulation_pass_color_result_[ctx.id]); - - + */ + std::cout<<"I did clear the color texture \n"; + + // bind and clear fbo ctx.render_context->set_frame_buffer(accumulation_pass_result_fbo_[ctx.id]); - ctx.render_context->clear_depth_stencil_buffer(accumulation_pass_result_fbo_[ctx.id]); - ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); - + // ctx.render_context->clear_depth_stencil_buffer(accumulation_pass_result_fbo_[ctx.id]); + //ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); + gua::math::mat4 const& projection_matrix = frustum.get_projection(); @@ -462,12 +519,15 @@ void PBRUberShader::draw(RenderContext const& ctx, get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + std::cout<< "M: "<set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); get_program(accumulation_pass)->set_uniform(ctx, near_plane_value, "near_plane"); if (material && pbr_ressource) { - + + material_id_[ctx.id] = material->get_id(); get_program(accumulation_pass)->use(ctx); { pbr_ressource->draw(ctx); @@ -475,69 +535,47 @@ void PBRUberShader::draw(RenderContext const& ctx, get_program(accumulation_pass)->unuse(ctx); } - ctx.render_context->reset_framebuffer(); + // ctx.render_context->reset_framebuffer(); } - - - - + // throw std::runtime_error("PBRUberShader::postdraw(): not implemented"); + } + //////////////////////////////////////////////////////////////////////////////// + /*virtual*/ void PBRUberShader::postframe(RenderContext const& ctx) const { - // single texture only - scm::gl::context_all_guard guard(ctx.render_context); - auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); - ctx.render_context->set_depth_stencil_state(ds_state); + std::cout<<"POSTFRAME\n"; + { + // single texture only + scm::gl::context_all_guard guard(ctx.render_context); - // second pass - get_program(normalization_pass)->use(ctx); - { - if (material /*&& video3d_ressource*/) - { - get_program(normalization_pass)->set_uniform(ctx, material->get_id(), "gua_material_id"); - get_program(normalization_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); - get_program(normalization_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); + ctx.render_context->set_depth_stencil_state(ds_state); + // second pass + get_program(normalization_pass)->use(ctx); + { + // if (/*material /*&& video3d_ressource*/) + { + get_program(normalization_pass)->set_uniform(ctx, material_id_[ctx.id], "gua_material_id"); - get_program(normalization_pass)->set_uniform(ctx, int(material_name == default_pbr_material_name()) , "using_default_pbr_material"); - + get_program(normalization_pass)->set_uniform(ctx, 0 , "using_default_pbr_material"); - ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); - get_program(normalization_pass)->get_program(ctx)->uniform_sampler("color_texture", 0); + - fullscreen_quad_[ctx.id]->draw(ctx.render_context); - } - } - get_program(normalization_pass)->unuse(ctx); - } -} - - - - - - - //////////////////////////////////////////////////////////////////////////////// + ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); + get_program(normalization_pass)->get_program(ctx)->uniform_sampler("color_texture", 0); - /*virtual*/ void PBRUberShader::postdraw(RenderContext const& ctx, - std::string const& filename, - std::string const& material_name, - scm::math::mat4 const& model_matrix, - scm::math::mat4 const& normal_matrix, - Frustum const& /*frustum*/) const - { - throw std::runtime_error("PBRUberShader::postdraw(): not implemented"); - } - - //////////////////////////////////////////////////////////////////////////////// - /*virtual*/ void PBRUberShader::postframe(RenderContext const& context) const - { - throw std::runtime_error("PBRUberShader::postframe(): not implemented"); + fullscreen_quad_[ctx.id]->draw(ctx.render_context); + } + } + get_program(normalization_pass)->unuse(ctx); + } } From b235e4f55d7ff55913a40828c8f7dc5d8a610387 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Sun, 11 May 2014 16:52:14 +0200 Subject: [PATCH 17/45] Three pass blending --- include/gua/renderer/PBRUberShader.hpp | 23 +- .../uber_shaders/gbuffer/pbr/p01_depth.frag | 33 +-- .../uber_shaders/gbuffer/pbr/p01_depth.vert | 18 +- .../gbuffer/pbr/p02_accumulation.frag | 68 +++++- .../gbuffer/pbr/p02_accumulation.vert | 12 +- .../gbuffer/pbr/p03_normalization.frag | 13 +- src/gua/renderer/PBRUberShader.cpp | 200 ++++++++---------- 7 files changed, 206 insertions(+), 161 deletions(-) diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp index 888be2d55..1670cd57e 100644 --- a/include/gua/renderer/PBRUberShader.hpp +++ b/include/gua/renderer/PBRUberShader.hpp @@ -87,24 +87,18 @@ class PBRUberShader : public GeometryUberShader { private: //member variables //FBOs: - + ////////////////////////////////////////////////////////////////////////////////////// //depth pass FBO & attachments mutable std::vector depth_pass_depth_result_; - mutable std::vector depth_pass_color_result_; mutable std::vector depth_pass_result_fbo_; //accumulation pass FBO & attachments - mutable std::vector accumulation_pass_depth_result_; mutable std::vector accumulation_pass_color_result_; mutable std::vector accumulation_pass_result_fbo_; - - - - mutable std::vector fullscreen_quad_; - - + //schism-GL states: + ////////////////////////////////////////////////////////////////////////////////////// mutable std::vector change_point_size_in_shader_state_; mutable std::vector linear_sampler_state_; @@ -112,12 +106,17 @@ class PBRUberShader : public GeometryUberShader { mutable std::vector depth_stencil_state_; - //frustum dependent variables - - mutable std::vector material_id_; + //frustum dependent variables: + ///////////////////////////////////////////////////////////////////////////////////// mutable std::vector near_plane_value_; mutable std::vector height_divided_by_top_minus_bottom_; + //misc: + //////////////////////////////////////////////////////////////////////////////////// + mutable std::vector material_id_; + mutable std::vector fullscreen_quad_; + mutable std::vector render_window_dims_; + }; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag index ea7f05994..b20768de0 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag @@ -3,19 +3,22 @@ /////////////////////////////////////////////////////////////////////////////// // input /////////////////////////////////////////////////////////////////////////////// -in vec3 gua_point_color; -in vec3 gua_normal; - +in vec3 pass_normal; +in float pass_mv_vert_depth; +in float pass_radius; /////////////////////////////////////////////////////////////////////////////// // output /////////////////////////////////////////////////////////////////////////////// -layout (location=0) out vec4 out_color; - - +// No output other than depth texture +/////////////////////////////////////////////////////////////////////////////// +//Uniforms +/////////////////////////////////////////////////////////////////////////////// +uniform float near_plane; +uniform float far_minus_near_plane; /////////////////////////////////////////////////////////////////////////////// // splatting methods @@ -24,19 +27,17 @@ layout (location=0) out vec4 out_color; float calc_depth_offset(vec2 mappedPointCoord) { - vec3 normal = gua_normal; + vec3 normal = pass_normal; if(normal.z < 0) { - - //discard; normal *= -1; } float xzRatio = (normal.x/normal.z); float yzRatio = (normal.y/normal.z); -//if(clamped_normal_mode) -{ + //if (clampedNormalMode) + { float zBound = 0.35f;//max_deform_ratio; float normalZ = normal.z; @@ -48,7 +49,7 @@ float calc_depth_offset(vec2 mappedPointCoord) xzRatio = (normal.x/normalZ); yzRatio = (normal.y/normalZ); -} + } return -(xzRatio)*mappedPointCoord.x - (yzRatio * mappedPointCoord.y); @@ -86,11 +87,13 @@ void main() float depth_offset = calc_depth_offset(mappedPointCoord) ; - //get_gaussianValue(depth_offset, mappedPointCoord, VertexIn.nor.xyz); - get_gaussianValue(depth_offset, mappedPointCoord, gua_normal); + get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); - out_color = vec4(gua_point_color,1.0); + // if(ellipsify) //map a greater far plane range in case the depth correction overshoots + gl_FragDepth = - ( ( (pass_mv_vert_depth + depth_offset * pass_radius ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; + // else + // gl_FragDepth = - ( ( (pass_mv_vert_depth) - near_plane) / (far_minus_near_plane * 1.0f)); } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert index b533570c1..db107d1e2 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert @@ -22,25 +22,23 @@ uniform float near_plane; //output to fragment shader - out vec3 gua_point_color; - out vec3 gua_normal; + out vec3 pass_normal; + out float pass_mv_vert_depth; + out float pass_radius; void main() { - + vec4 pos_es = gua_view_matrix * gua_model_matrix * vec4(in_position, 1.0); gl_Position = gua_projection_matrix * pos_es; - gl_PointSize = 2.0f * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; - - - gua_point_color = vec3((in_r)/255.0f, - (in_g)/255.0f, - (in_b)/255.0f); - gua_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; + pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; + pass_mv_vert_depth = pos_es.z; + pass_radius = in_radius; + } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index ea7f05994..af4508ced 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -3,9 +3,10 @@ /////////////////////////////////////////////////////////////////////////////// // input /////////////////////////////////////////////////////////////////////////////// -in vec3 gua_point_color; -in vec3 gua_normal; - +in vec3 pass_point_color; +in vec3 pass_normal; +in float pass_mv_vert_depth; +in float pass_radius; /////////////////////////////////////////////////////////////////////////////// // output @@ -13,9 +14,20 @@ in vec3 gua_normal; layout (location=0) out vec4 out_color; +/////////////////////////////////////////////////////////////////////////////// +//sampler +/////////////////////////////////////////////////////////////////////////////// +uniform sampler2D p01_depth_texture; +/////////////////////////////////////////////////////////////////////////////// +//Uniforms +/////////////////////////////////////////////////////////////////////////////// +uniform float near_plane; +uniform float far_minus_near_plane; +uniform vec2 win_dims; - +//uniform float win_dim_x; +//uniform float win_dim_y; /////////////////////////////////////////////////////////////////////////////// // splatting methods @@ -24,7 +36,7 @@ layout (location=0) out vec4 out_color; float calc_depth_offset(vec2 mappedPointCoord) { - vec3 normal = gua_normal; + vec3 normal = pass_normal; if(normal.z < 0) { @@ -84,13 +96,55 @@ void main() float depth_offset = calc_depth_offset(mappedPointCoord) ; + + float depthValue = texture2D( p01_depth_texture, gl_FragCoord.xy/win_dims.xy ).r; + //////////depth value of depth pass texture + depthValue = (-depthValue * 1.0 * far_minus_near_plane) + near_plane; //get_gaussianValue(depth_offset, mappedPointCoord, VertexIn.nor.xyz); - get_gaussianValue(depth_offset, mappedPointCoord, gua_normal); + get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); + +float depth_to_compare = 0; + +//if(ellipsify) + depth_to_compare = pass_mv_vert_depth + depth_offset * pass_radius; +//else +// depth_to_compare = pass_mv_vert_depth; + float weight = 1.0; - out_color = vec4(gua_point_color,1.0); + if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_radius /** (1/rad_scale_fac)*/ ) ) + { + +/* + float colorCoding = 1.0f; + + + if(depthValue != 0.0) + { + colorCoding = -depthValue /50.0f; + colorCoding = 1.0 - colorCoding; + } + + colorCoding *= 0.3; + accumulated_colors = vec4(colorCoding * weight, colorCoding * weight, colorCoding * weight, weight); +*/ + //accumulated_colors = vec4(VertexIn.color * weight, weight); + out_color = vec4(pass_point_color,1.0); + } + else + { + //out_color = vec4(1.0,0.0,0.0,1.0); + discard; + } +// discard; + + //if(win_dim_x == 800.0 && win_dim_y == 600.0) + /*if(win_dims.x == 800.0 && win_dims.y == 600.0) + out_color = vec4(0.0,1.0,0.0,1.0); + else */ + // out_color = vec4(pass_point_color,1.0); } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert index b533570c1..70e0efecc 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert @@ -22,8 +22,10 @@ uniform float near_plane; //output to fragment shader - out vec3 gua_point_color; - out vec3 gua_normal; + out vec3 pass_point_color; + out vec3 pass_normal; + out float pass_mv_vert_depth; + out float pass_radius; void main() @@ -38,9 +40,11 @@ - gua_point_color = vec3((in_r)/255.0f, + pass_point_color = vec3((in_r)/255.0f, (in_g)/255.0f, (in_b)/255.0f); - gua_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; + pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; + pass_mv_vert_depth = pos_es.z; + pass_radius = in_radius; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag index eabd36901..dda25e0f1 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag @@ -20,7 +20,9 @@ in vec2 gua_quad_coords; /////////////////////////////////////////////////////////////////////////////// uniform int using_default_pbr_material; -uniform sampler2D color_texture; +uniform sampler2D p01_depth_texture; +uniform sampler2D p02_color_texture; + @@ -48,12 +50,12 @@ void main() { - - vec4 finalcol = texture2D( color_texture, coords.xy); + + vec4 finalcol = texture2D( p02_color_texture, coords.xy); output_depth = ogldepth; //output_color = vec3(finalcol.rg, 1.0);//vec3(0.0,1.0,0.0);//finalcol.rgb; - output_color = finalcol.rgb; + output_color = finalcol.rgb / finalcol.a; gua_uint_gbuffer_out_0.x = gua_uint_gbuffer_varying_0.x; @@ -64,5 +66,6 @@ void main() { @apply_pbr_normal } - gl_FragDepth = 0.5;//output_depth; + gl_FragDepth = output_depth; + gl_FragDepth = texture2D( p01_depth_texture, coords.xy).r; } diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index 0f7b01856..7f6d89095 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -217,20 +217,11 @@ bool PBRUberShader::upload_to (RenderContext const& context) const // initialize attachments for depth pass - if ( context.id >= depth_pass_color_result_.size() || - context.id >= depth_pass_depth_result_.size()) + if (context.id >= depth_pass_depth_result_.size()) { - depth_pass_color_result_.resize(context.id + 1); - depth_pass_depth_result_.resize(context.id + 1); + depth_pass_depth_result_.resize(context.id + 1); - depth_pass_color_result_[context.id] = context.render_device->create_texture_2d( - context.render_window->config.get_left_resolution(), - scm::gl::FORMAT_RGBA_32F, - 1, - 1, - 1 - ); depth_pass_depth_result_[context.id] = context.render_device->create_texture_2d( context.render_window->config.get_left_resolution(), @@ -242,11 +233,9 @@ bool PBRUberShader::upload_to (RenderContext const& context) const } // initialize attachments for accumulation pass - if ( context.id >= accumulation_pass_color_result_.size() || - context.id >= accumulation_pass_depth_result_.size()) + if (context.id >= accumulation_pass_color_result_.size()) { accumulation_pass_color_result_.resize(context.id + 1); - accumulation_pass_depth_result_.resize(context.id + 1); accumulation_pass_color_result_[context.id] = context.render_device->create_texture_2d( @@ -257,15 +246,9 @@ bool PBRUberShader::upload_to (RenderContext const& context) const 1 ); - accumulation_pass_depth_result_[context.id] = context.render_device->create_texture_2d( - context.render_window->config.get_left_resolution(), - scm::gl::FORMAT_D32F, - 1, - 1, - 1 - ); } + if (context.id >= fullscreen_quad_.size()) { fullscreen_quad_.resize(context.id + 1); fullscreen_quad_[context.id].reset(new scm::gl::quad_geometry(context.render_device, math::vec2(-1.f, -1.f), math::vec2(1.f, 1.f))); @@ -316,6 +299,10 @@ bool PBRUberShader::upload_to (RenderContext const& context) const height_divided_by_top_minus_bottom_[context.id] = 0; } + if (context.id >= render_window_dims_.size()){ + render_window_dims_.resize(context.id + 1); + render_window_dims_[context.id] = context.render_window->config.get_left_resolution(); + } return upload_succeeded; @@ -331,7 +318,7 @@ bool PBRUberShader::upload_to (RenderContext const& context) const /*virtual*/ GeometryUberShader::stage_mask const PBRUberShader::get_stage_mask() const { - return GeometryUberShader::PRE_FRAME_STAGE | GeometryUberShader::PRE_DRAW_STAGE | GeometryUberShader::POST_DRAW_STAGE | GeometryUberShader::POST_FRAME_STAGE;//0;//GeometryUberShader::DRAW_STAGE; + return GeometryUberShader::PRE_FRAME_STAGE | GeometryUberShader::PRE_DRAW_STAGE | GeometryUberShader::POST_DRAW_STAGE | GeometryUberShader::POST_FRAME_STAGE; } //////////////////////////////////////////////////////////////////////////////// @@ -340,27 +327,81 @@ bool PBRUberShader::upload_to (RenderContext const& context) const { upload_to(ctx); + // configure depth FBO + depth_pass_result_fbo_[ctx.id]->clear_attachments(); + depth_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(depth_pass_depth_result_[ctx.id]); - // configure fbo + // clear depth FBOs depth attachment + ctx.render_context->clear_depth_stencil_buffer(depth_pass_result_fbo_[ctx.id]); + + // configure accumulation FBO accumulation_pass_result_fbo_[ctx.id]->clear_attachments(); - accumulation_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(accumulation_pass_depth_result_[ctx.id]); accumulation_pass_result_fbo_[ctx.id]->attach_color_buffer(0, accumulation_pass_color_result_[ctx.id]); - ctx.render_context->clear_depth_stencil_buffer(accumulation_pass_result_fbo_[ctx.id]); + // clear accumulation FBOs color attachment ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); } //////////////////////////////////////////////////////////////////////////////// /*virtual*/ void PBRUberShader::predraw(RenderContext const& ctx, - std::string const& filename, + std::string const& file_name, std::string const& material_name, scm::math::mat4 const& model_matrix, scm::math::mat4 const& normal_matrix, - Frustum const& /*frustum*/) const + Frustum const& frustum) const { + auto pbr_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); + auto material = MaterialDatabase::instance()->lookup(material_name); + // begin of depth pass (first) + { + // single texture only + scm::gl::context_all_guard guard(ctx.render_context); + + + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + + + + + + // bind fbo + ctx.render_context->set_frame_buffer(depth_pass_result_fbo_[ctx.id]); + + + + gua::math::mat4 const& projection_matrix = frustum.get_projection(); + + float near_plane_value = frustum.get_clip_near(); + float far_plane_value = frustum.get_clip_far(); + float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; + float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; + + float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); + + + + get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + + get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); + get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + get_program(depth_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); + + if (material && pbr_ressource) + { + + get_program(depth_pass)->use(ctx); + { + pbr_ressource->draw(ctx); + } + get_program(depth_pass)->unuse(ctx); + } + + ctx.render_context->reset_framebuffer(); + } std::cout<<"PREDRAW\n"; @@ -395,68 +436,6 @@ void PBRUberShader::draw(RenderContext const& ctx, upload_to(ctx); - std::cout<<"draw\n"; - -/* - // pre passes: - ///////////////////// - // begin of depth pass (first) - { - // single texture only - scm::gl::context_all_guard guard(ctx.render_context); - - ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); - - auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); - ctx.render_context->set_depth_stencil_state(ds_state); - - - - - - - // configure fbo - depth_pass_result_fbo_[ctx.id]->clear_attachments(); - depth_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(depth_pass_depth_result_[ctx.id]); - depth_pass_result_fbo_[ctx.id]->attach_color_buffer(0, depth_pass_color_result_[ctx.id]); - - - // bind and clear fbo - ctx.render_context->set_frame_buffer(depth_pass_result_fbo_[ctx.id]); - ctx.render_context->clear_depth_stencil_buffer(depth_pass_result_fbo_[ctx.id]); - ctx.render_context->clear_color_buffer(depth_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); - - - gua::math::mat4 const& projection_matrix = frustum.get_projection(); - - float near_plane_value = frustum.get_clip_near(); - float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; - float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; - float height_divided_by_top_minus_bottom = 800.0 / (top_plane_value - bottom_plane_value); - - - - get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); - get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); - - get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); - get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); - - if (material && pbr_ressource) - { - - get_program(depth_pass)->use(ctx); - { - pbr_ressource->draw(ctx); - } - get_program(depth_pass)->unuse(ctx); - } - - ctx.render_context->reset_framebuffer(); - } -*/ - - } @@ -485,44 +464,46 @@ void PBRUberShader::draw(RenderContext const& ctx, //!!!!!!!!!!!!EXCHANGE THIS WITH DEPH TEST NONE AND PUT COLOR ACCUMULATION ALSO SOMEWHERE -/* - auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); + //this is the "NO_DEPTH_TEST"-state. to-do: put it into member variables. + auto ds_state = ctx.render_device->create_depth_stencil_state(false, false, scm::gl::COMPARISON_LESS); - ctx.render_context->set_depth_stencil_state(ds_state); -*/ + ctx.render_context->set_depth_stencil_state(ds_state); + + //also add color accumulation state -/* - // configure fbo - accumulation_pass_result_fbo_[ctx.id]->clear_attachments(); - // accumulation_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(accumulation_pass_depth_result_[ctx.id]); - accumulation_pass_result_fbo_[ctx.id]->attach_color_buffer(0, accumulation_pass_color_result_[ctx.id]); - */ - std::cout<<"I did clear the color texture \n"; + auto acc_col_blend_state = ctx.render_device->create_blend_state(true, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::EQ_FUNC_ADD, scm::gl::EQ_FUNC_ADD); + ctx.render_context->set_blend_state(acc_col_blend_state); - // bind and clear fbo + // bind ctx.render_context->set_frame_buffer(accumulation_pass_result_fbo_[ctx.id]); - // ctx.render_context->clear_depth_stencil_buffer(accumulation_pass_result_fbo_[ctx.id]); - //ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); - gua::math::mat4 const& projection_matrix = frustum.get_projection(); //put near_plane_value and height_divided_by_top_minus_bottom as member variables and get these two only 1 per render frame float near_plane_value = frustum.get_clip_near(); + float far_plane_value = frustum.get_clip_far(); float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; - float height_divided_by_top_minus_bottom = 800.0 / (top_plane_value - bottom_plane_value); + float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); //std::cout << "hdbtmb: "<< height_divided_by_top_minus_bottom; get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); - std::cout<< "M: "<set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); get_program(accumulation_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + get_program(accumulation_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); + + + get_program(accumulation_pass)->set_uniform(ctx, math::vec2(render_window_dims_[ctx.id]),"win_dims"); + + ctx.render_context->bind_texture(depth_pass_depth_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); + get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + if (material && pbr_ressource) { @@ -566,9 +547,12 @@ void PBRUberShader::draw(RenderContext const& ctx, + ctx.render_context->bind_texture(depth_pass_depth_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); + get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + + ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], nearest_sampler_state_[ctx.id], 1); + get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p02_color_texture", 1); - ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); - get_program(normalization_pass)->get_program(ctx)->uniform_sampler("color_texture", 0); fullscreen_quad_[ctx.id]->draw(ctx.render_context); From 352c26cf67d643be168ba096371d29ad848a62d6 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Mon, 12 May 2014 13:35:44 +0200 Subject: [PATCH 18/45] working three pass algorithm with correct depth output --- include/gua/renderer/PBRUberShader.hpp | 10 +- .../uber_shaders/gbuffer/pbr/p01_depth.frag | 4 +- .../gbuffer/pbr/p02_accumulation.frag | 50 +++---- .../gbuffer/pbr/p03_normalization.frag | 16 +-- src/gua/renderer/PBRUberShader.cpp | 128 ++++++++---------- 5 files changed, 85 insertions(+), 123 deletions(-) diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp index 1670cd57e..b419ac008 100644 --- a/include/gua/renderer/PBRUberShader.hpp +++ b/include/gua/renderer/PBRUberShader.hpp @@ -89,7 +89,9 @@ class PBRUberShader : public GeometryUberShader { //FBOs: ////////////////////////////////////////////////////////////////////////////////////// //depth pass FBO & attachments - mutable std::vector depth_pass_depth_result_; + mutable std::vector depth_pass_log_depth_result_; + mutable std::vector depth_pass_linear_depth_result_; + mutable std::vector depth_pass_result_fbo_; //accumulation pass FBO & attachments @@ -102,8 +104,10 @@ class PBRUberShader : public GeometryUberShader { mutable std::vector change_point_size_in_shader_state_; mutable std::vector linear_sampler_state_; - mutable std::vector nearest_sampler_state_; - mutable std::vector depth_stencil_state_; + mutable std::vector no_depth_test_depth_stencil_state_; + + mutable std::vector color_accumulation_state_; + //frustum dependent variables: diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag index b20768de0..b022875e2 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag @@ -12,6 +12,7 @@ in float pass_radius; /////////////////////////////////////////////////////////////////////////////// // No output other than depth texture +layout (location=0) out float out_linear_depth; /////////////////////////////////////////////////////////////////////////////// @@ -91,9 +92,10 @@ void main() // if(ellipsify) //map a greater far plane range in case the depth correction overshoots - gl_FragDepth = - ( ( (pass_mv_vert_depth + depth_offset * pass_radius ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; + //gl_FragDepth = - ( ( (pass_mv_vert_depth + depth_offset * pass_radius ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; // else // gl_FragDepth = - ( ( (pass_mv_vert_depth) - near_plane) / (far_minus_near_plane * 1.0f)); + out_linear_depth = - ( ( (pass_mv_vert_depth + depth_offset * pass_radius ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index af4508ced..cd4d429d2 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -1,5 +1,13 @@ @include "shaders/common/header.glsl" +const float gaussian[32] = float[]( +1.000000, 1.000000, 0.988235, 0.968627, 0.956862, 0.917647, 0.894117, 0.870588, 0.915686, 0.788235, +0.749020, 0.690196, 0.654902, 0.619608, 0.552941, 0.513725, 0.490196, 0.458824, 0.392157, 0.356863, +0.341176, 0.278431, 0.254902, 0.227451, 0.188235, 0.164706, 0.152941, 0.125490, 0.109804, 0.098039, +0.074510, 0.062745 +); + + /////////////////////////////////////////////////////////////////////////////// // input /////////////////////////////////////////////////////////////////////////////// @@ -12,7 +20,7 @@ in float pass_radius; // output /////////////////////////////////////////////////////////////////////////////// -layout (location=0) out vec4 out_color; +layout (location=0) out vec4 out_accumulated_color; /////////////////////////////////////////////////////////////////////////////// //sampler @@ -82,7 +90,8 @@ float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNorma if(radius > 1.0) discard; else - return 1.0f; + return gaussian[(int)(round(radius * 32.0))]; + //return 1.0f; } @@ -103,48 +112,25 @@ void main() depthValue = (-depthValue * 1.0 * far_minus_near_plane) + near_plane; //get_gaussianValue(depth_offset, mappedPointCoord, VertexIn.nor.xyz); - get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); + //get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); float depth_to_compare = 0; -//if(ellipsify) - depth_to_compare = pass_mv_vert_depth + depth_offset * pass_radius; -//else -// depth_to_compare = pass_mv_vert_depth; + //if(ellipsify) + depth_to_compare = pass_mv_vert_depth + depth_offset * pass_radius; + //else + // depth_to_compare = pass_mv_vert_depth; - float weight = 1.0; + float weight = get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_radius /** (1/rad_scale_fac)*/ ) ) { - -/* - float colorCoding = 1.0f; - - - if(depthValue != 0.0) - { - colorCoding = -depthValue /50.0f; - colorCoding = 1.0 - colorCoding; - } - - colorCoding *= 0.3; - accumulated_colors = vec4(colorCoding * weight, colorCoding * weight, colorCoding * weight, weight); -*/ - //accumulated_colors = vec4(VertexIn.color * weight, weight); - out_color = vec4(pass_point_color,1.0); + out_accumulated_color = vec4(pass_point_color * weight, weight); } else { - //out_color = vec4(1.0,0.0,0.0,1.0); discard; } -// discard; - - //if(win_dim_x == 800.0 && win_dim_y == 600.0) - /*if(win_dims.x == 800.0 && win_dims.y == 600.0) - out_color = vec4(0.0,1.0,0.0,1.0); - else */ - // out_color = vec4(pass_point_color,1.0); } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag index dda25e0f1..f20e45a97 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag @@ -44,28 +44,20 @@ void main() { vec3 coords = vec3(gua_quad_coords, 0.0); - float maxdist = 1000.0; - float mindist = maxdist; - float ogldepth = 1.0; - - - - vec4 finalcol = texture2D( p02_color_texture, coords.xy); + vec4 accumulated_color = texture2D( p02_color_texture, coords.xy); - output_depth = ogldepth; - //output_color = vec3(finalcol.rg, 1.0);//vec3(0.0,1.0,0.0);//finalcol.rgb; - output_color = finalcol.rgb / finalcol.a; + output_color = accumulated_color.rgb / accumulated_color.a; gua_uint_gbuffer_out_0.x = gua_uint_gbuffer_varying_0.x; - // if ( using_default_pbr_material != 0 ) + if ( using_default_pbr_material != 0 ) { @apply_pbr_color @apply_pbr_normal } - gl_FragDepth = output_depth; + gl_FragDepth = texture2D( p01_depth_texture, coords.xy).r; } diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index 7f6d89095..120f7068a 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -217,13 +217,13 @@ bool PBRUberShader::upload_to (RenderContext const& context) const // initialize attachments for depth pass - if (context.id >= depth_pass_depth_result_.size()) + if (context.id >= depth_pass_log_depth_result_.size()) { - depth_pass_depth_result_.resize(context.id + 1); + depth_pass_log_depth_result_.resize(context.id + 1); - depth_pass_depth_result_[context.id] = context.render_device->create_texture_2d( + depth_pass_log_depth_result_[context.id] = context.render_device->create_texture_2d( context.render_window->config.get_left_resolution(), scm::gl::FORMAT_D32F, 1, @@ -232,6 +232,22 @@ bool PBRUberShader::upload_to (RenderContext const& context) const ); } + if (context.id >= depth_pass_linear_depth_result_.size()) + { + + depth_pass_linear_depth_result_.resize(context.id + 1); + + + depth_pass_linear_depth_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_R_32F, + 1, + 1, + 1 + ); + } + + // initialize attachments for accumulation pass if (context.id >= accumulation_pass_color_result_.size()) { @@ -269,16 +285,17 @@ bool PBRUberShader::upload_to (RenderContext const& context) const linear_sampler_state_[context.id] = context.render_device->create_sampler_state(scm::gl::FILTER_MIN_MAG_LINEAR, scm::gl::WRAP_CLAMP_TO_EDGE); } - if (context.id >= nearest_sampler_state_.size()) { - nearest_sampler_state_.resize(context.id + 1); - nearest_sampler_state_[context.id] = context.render_device->create_sampler_state(scm::gl::FILTER_MIN_MAG_NEAREST, scm::gl::WRAP_CLAMP_TO_EDGE); + if (context.id >= no_depth_test_depth_stencil_state_.size()) { + no_depth_test_depth_stencil_state_.resize(context.id + 1); + no_depth_test_depth_stencil_state_[context.id] = context.render_device->create_depth_stencil_state(false, false, scm::gl::COMPARISON_LESS); } - if (context.id >= depth_stencil_state_.size()) { - depth_stencil_state_.resize(context.id + 1); - depth_stencil_state_[context.id] = context.render_device->create_depth_stencil_state(false, true, scm::gl::COMPARISON_LESS); + if (context.id >= color_accumulation_state_.size()) { + color_accumulation_state_.resize(context.id + 1); + color_accumulation_state_[context.id] = context.render_device->create_blend_state(true, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::EQ_FUNC_ADD, scm::gl::EQ_FUNC_ADD); } + if (context.id >= change_point_size_in_shader_state_.size()){ change_point_size_in_shader_state_.resize(context.id + 1); change_point_size_in_shader_state_[context.id] = context.render_device->create_rasterizer_state(scm::gl::FILL_SOLID, scm::gl::CULL_NONE, scm::gl::ORIENT_CCW, false, false, 0.0, false, false, scm::gl::point_raster_state(true)); @@ -329,10 +346,12 @@ bool PBRUberShader::upload_to (RenderContext const& context) const // configure depth FBO depth_pass_result_fbo_[ctx.id]->clear_attachments(); - depth_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(depth_pass_depth_result_[ctx.id]); + depth_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(depth_pass_log_depth_result_[ctx.id]); + depth_pass_result_fbo_[ctx.id]->attach_color_buffer(0, depth_pass_linear_depth_result_[ctx.id]); - // clear depth FBOs depth attachment + // clear depth FBOs depth attachments ctx.render_context->clear_depth_stencil_buffer(depth_pass_result_fbo_[ctx.id]); + ctx.render_context->clear_color_buffer(depth_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); // configure accumulation FBO accumulation_pass_result_fbo_[ctx.id]->clear_attachments(); @@ -357,21 +376,17 @@ bool PBRUberShader::upload_to (RenderContext const& context) const // begin of depth pass (first) { - // single texture only - scm::gl::context_all_guard guard(ctx.render_context); - - - ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + scm::gl::context_all_guard guard(ctx.render_context); + //enable dynamic point size in shaders + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); - // bind fbo ctx.render_context->set_frame_buffer(depth_pass_result_fbo_[ctx.id]); - gua::math::mat4 const& projection_matrix = frustum.get_projection(); float near_plane_value = frustum.get_clip_near(); @@ -382,7 +397,6 @@ bool PBRUberShader::upload_to (RenderContext const& context) const float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); - get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); @@ -392,7 +406,6 @@ bool PBRUberShader::upload_to (RenderContext const& context) const if (material && pbr_ressource) { - get_program(depth_pass)->use(ctx); { pbr_ressource->draw(ctx); @@ -403,8 +416,6 @@ bool PBRUberShader::upload_to (RenderContext const& context) const ctx.render_context->reset_framebuffer(); } - std::cout<<"PREDRAW\n"; - } @@ -418,25 +429,7 @@ void PBRUberShader::draw(RenderContext const& ctx, scm::math::mat4 const& normal_matrix, Frustum const& frustum) const { - if (!GeometryDatabase::instance()->is_supported(file_name) || - !MaterialDatabase::instance()->is_supported(material_name)) { - gua::Logger::LOG_WARNING << "PBRUberShader::draw(): No such pbr ressource or material." << file_name << ", " << material_name << std::endl; - return; - } - - auto pbr_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); - auto material = MaterialDatabase::instance()->lookup(material_name); - - if (!material || !pbr_ressource) { - gua::Logger::LOG_WARNING << "PBRUberShader::draw(): Invalid pbr ressource or material." << std::endl; - return; - } - - // make sure ressources are on the GPU - upload_to(ctx); - - - + throw std::runtime_error("PBRUberShader::draw(): not implemented"); } @@ -453,29 +446,24 @@ void PBRUberShader::draw(RenderContext const& ctx, scm::math::mat4 const& normal_matrix, Frustum const& frustum) const { - std::cout<<"POSTDRAW\n"; + auto pbr_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); auto material = MaterialDatabase::instance()->lookup(material_name); { - // single texture only scm::gl::context_all_guard guard(ctx.render_context); - ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); - //!!!!!!!!!!!!EXCHANGE THIS WITH DEPH TEST NONE AND PUT COLOR ACCUMULATION ALSO SOMEWHERE - - //this is the "NO_DEPTH_TEST"-state. to-do: put it into member variables. - auto ds_state = ctx.render_device->create_depth_stencil_state(false, false, scm::gl::COMPARISON_LESS); - - ctx.render_context->set_depth_stencil_state(ds_state); - - //also add color accumulation state + //enable dynamic point size in shaders + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); - auto acc_col_blend_state = ctx.render_device->create_blend_state(true, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::EQ_FUNC_ADD, scm::gl::EQ_FUNC_ADD); - ctx.render_context->set_blend_state(acc_col_blend_state); + //disable depth test + ctx.render_context->set_depth_stencil_state(no_depth_test_depth_stencil_state_[ctx.id]); + + //set blend state to accumulate + ctx.render_context->set_blend_state(color_accumulation_state_[ctx.id]); - // bind + // bind accumulation FBO ctx.render_context->set_frame_buffer(accumulation_pass_result_fbo_[ctx.id]); gua::math::mat4 const& projection_matrix = frustum.get_projection(); @@ -492,16 +480,13 @@ void PBRUberShader::draw(RenderContext const& ctx, get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); - - get_program(accumulation_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); get_program(accumulation_pass)->set_uniform(ctx, near_plane_value, "near_plane"); get_program(accumulation_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); - get_program(accumulation_pass)->set_uniform(ctx, math::vec2(render_window_dims_[ctx.id]),"win_dims"); - ctx.render_context->bind_texture(depth_pass_depth_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); + ctx.render_context->bind_texture(depth_pass_linear_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); @@ -516,9 +501,9 @@ void PBRUberShader::draw(RenderContext const& ctx, get_program(accumulation_pass)->unuse(ctx); } - // ctx.render_context->reset_framebuffer(); + ctx.render_context->reset_framebuffer(); } - // throw std::runtime_error("PBRUberShader::postdraw(): not implemented"); + } //////////////////////////////////////////////////////////////////////////////// @@ -526,35 +511,28 @@ void PBRUberShader::draw(RenderContext const& ctx, /*virtual*/ void PBRUberShader::postframe(RenderContext const& ctx) const { - std::cout<<"POSTFRAME\n"; { - // single texture only + scm::gl::context_all_guard guard(ctx.render_context); - auto ds_state = ctx.render_device->create_depth_stencil_state(true, true, scm::gl::COMPARISON_LESS); - ctx.render_context->set_depth_stencil_state(ds_state); - // second pass get_program(normalization_pass)->use(ctx); { - // if (/*material /*&& video3d_ressource*/) + { get_program(normalization_pass)->set_uniform(ctx, material_id_[ctx.id], "gua_material_id"); + get_program(normalization_pass)->set_uniform(ctx, 1 , "using_default_pbr_material"); - get_program(normalization_pass)->set_uniform(ctx, 0 , "using_default_pbr_material"); - - - - ctx.render_context->bind_texture(depth_pass_depth_result_[ctx.id], nearest_sampler_state_[ctx.id], 0); + //bind logarithmic depth texture for gbuffer + ctx.render_context->bind_texture(depth_pass_log_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); - ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], nearest_sampler_state_[ctx.id], 1); + //bind color output for gbuffer + ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], linear_sampler_state_[ctx.id], 1); get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p02_color_texture", 1); - - fullscreen_quad_[ctx.id]->draw(ctx.render_context); } } From 32caebb5a1873b3eeed899387e1144179ba29646 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Mon, 12 May 2014 23:40:05 +0200 Subject: [PATCH 19/45] FBO Bug fix --- include/gua/renderer/PBRUberShader.hpp | 16 ++ .../uber_shaders/gbuffer/pbr/p01_depth.frag | 5 - .../gbuffer/pbr/p02_accumulation.frag | 9 +- .../gbuffer/pbr/p02_accumulation.vert | 6 +- .../gbuffer/pbr/p03_normalization.frag | 8 +- src/gua/renderer/GBufferPass.cpp | 1 + src/gua/renderer/PBRUberShader.cpp | 161 ++++++++++++------ 7 files changed, 141 insertions(+), 65 deletions(-) diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp index b419ac008..9d2c2e9de 100644 --- a/include/gua/renderer/PBRUberShader.hpp +++ b/include/gua/renderer/PBRUberShader.hpp @@ -37,6 +37,15 @@ class PBRUberShader : public GeometryUberShader { normalization_pass = 2 }; + enum drawing_state{ + pre_frame_state = 0, + pre_draw_state = 1, + draw_state = 2, + post_draw_state = 3, + post_frame_state = 4, + invalid_state = 99999 + }; + public: PBRUberShader(); @@ -121,6 +130,13 @@ class PBRUberShader : public GeometryUberShader { mutable std::vector fullscreen_quad_; mutable std::vector render_window_dims_; + mutable std::vector last_geometry_state_; + + + //////////////////////////////////////////////////////////////////////////////////// + mutable std::vector> context_guard_; + + mutable std::vector framecount_ ; }; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag index b022875e2..dc0dd140c 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag @@ -91,11 +91,6 @@ void main() get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); - // if(ellipsify) //map a greater far plane range in case the depth correction overshoots - //gl_FragDepth = - ( ( (pass_mv_vert_depth + depth_offset * pass_radius ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; - // else - // gl_FragDepth = - ( ( (pass_mv_vert_depth) - near_plane) / (far_minus_near_plane * 1.0f)); - out_linear_depth = - ( ( (pass_mv_vert_depth + depth_offset * pass_radius ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index cd4d429d2..4aef58449 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -88,10 +88,10 @@ float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNorma if(radius > 1.0) - discard; + discard; else return gaussian[(int)(round(radius * 32.0))]; - //return 1.0f; + } @@ -100,7 +100,7 @@ float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNorma /////////////////////////////////////////////////////////////////////////////// void main() { - + vec2 mappedPointCoord = gl_PointCoord*2 + vec2(-1.0f, -1.0f); @@ -121,9 +121,10 @@ float depth_to_compare = 0; //else // depth_to_compare = pass_mv_vert_depth; + float weight = get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); - if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_radius /** (1/rad_scale_fac)*/ ) ) + if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_radius ) ) { out_accumulated_color = vec4(pass_point_color * weight, weight); } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert index 70e0efecc..d0100cb80 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert @@ -21,6 +21,8 @@ uniform float height_divided_by_top_minus_bottom; uniform float near_plane; + + //output to fragment shader out vec3 pass_point_color; out vec3 pass_normal; @@ -35,11 +37,9 @@ gl_Position = gua_projection_matrix * pos_es; - gl_PointSize = 2.0f * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; - - + pass_point_color = vec3((in_r)/255.0f, (in_g)/255.0f, (in_b)/255.0f); diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag index f20e45a97..d033bcb0a 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag @@ -20,8 +20,8 @@ in vec2 gua_quad_coords; /////////////////////////////////////////////////////////////////////////////// uniform int using_default_pbr_material; -uniform sampler2D p01_depth_texture; -uniform sampler2D p02_color_texture; +layout(binding=0) uniform sampler2D p01_depth_texture; +layout(binding=1) uniform sampler2D p02_color_texture; @@ -52,12 +52,12 @@ void main() { gua_uint_gbuffer_out_0.x = gua_uint_gbuffer_varying_0.x; - if ( using_default_pbr_material != 0 ) + //if ( using_default_pbr_material != 0 ) { @apply_pbr_color @apply_pbr_normal } + gl_FragDepth = pow(texture2D( p01_depth_texture, coords.xy).r,50); - gl_FragDepth = texture2D( p01_depth_texture, coords.xy).r; } diff --git a/src/gua/renderer/GBufferPass.cpp b/src/gua/renderer/GBufferPass.cpp index 517495c2d..0ebf3e3f5 100644 --- a/src/gua/renderer/GBufferPass.cpp +++ b/src/gua/renderer/GBufferPass.cpp @@ -41,6 +41,7 @@ #include + namespace gua { //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index 120f7068a..0ec61c0b4 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -225,7 +225,7 @@ bool PBRUberShader::upload_to (RenderContext const& context) const depth_pass_log_depth_result_[context.id] = context.render_device->create_texture_2d( context.render_window->config.get_left_resolution(), - scm::gl::FORMAT_D32F, + scm::gl::FORMAT_D32, 1, 1, 1 @@ -273,11 +273,20 @@ bool PBRUberShader::upload_to (RenderContext const& context) const if (context.id >= depth_pass_result_fbo_.size()) { depth_pass_result_fbo_.resize(context.id + 1); depth_pass_result_fbo_[context.id] = context.render_device->create_frame_buffer(); + + // configure depth FBO + depth_pass_result_fbo_[context.id]->clear_attachments(); + depth_pass_result_fbo_[context.id]->attach_depth_stencil_buffer(depth_pass_log_depth_result_[context.id]); + depth_pass_result_fbo_[context.id]->attach_color_buffer(0, depth_pass_linear_depth_result_[context.id]); } if (context.id >= accumulation_pass_result_fbo_.size()) { accumulation_pass_result_fbo_.resize(context.id + 1); accumulation_pass_result_fbo_[context.id] = context.render_device->create_frame_buffer(); + + // configure accumulation FBO + accumulation_pass_result_fbo_[context.id]->clear_attachments(); + accumulation_pass_result_fbo_[context.id]->attach_color_buffer(0, accumulation_pass_color_result_[context.id]); } if (context.id >= linear_sampler_state_.size()) { @@ -287,7 +296,7 @@ bool PBRUberShader::upload_to (RenderContext const& context) const if (context.id >= no_depth_test_depth_stencil_state_.size()) { no_depth_test_depth_stencil_state_.resize(context.id + 1); - no_depth_test_depth_stencil_state_[context.id] = context.render_device->create_depth_stencil_state(false, false, scm::gl::COMPARISON_LESS); + no_depth_test_depth_stencil_state_[context.id] = context.render_device->create_depth_stencil_state(false, false, scm::gl::COMPARISON_ALWAYS); } if (context.id >= color_accumulation_state_.size()) { @@ -321,7 +330,22 @@ bool PBRUberShader::upload_to (RenderContext const& context) const render_window_dims_[context.id] = context.render_window->config.get_left_resolution(); } - + if (context.id >= context_guard_.size()){ + context_guard_.resize(context.id + 1); + } + + if (context.id >= last_geometry_state_.size()) + { + last_geometry_state_.resize(context.id + 1); + last_geometry_state_[context.id] = invalid_state; + } + + if(context.id >= framecount_.size()) + { + framecount_.resize(context.id + 1); + framecount_[context.id] = 0; + } + return upload_succeeded; } @@ -342,23 +366,34 @@ bool PBRUberShader::upload_to (RenderContext const& context) const /*virtual*/ void PBRUberShader::preframe(RenderContext const& ctx) const { + + + //scm::gl::context_all_guard guard(ctx.render_context); upload_to(ctx); - // configure depth FBO - depth_pass_result_fbo_[ctx.id]->clear_attachments(); - depth_pass_result_fbo_[ctx.id]->attach_depth_stencil_buffer(depth_pass_log_depth_result_[ctx.id]); - depth_pass_result_fbo_[ctx.id]->attach_color_buffer(0, depth_pass_linear_depth_result_[ctx.id]); + last_geometry_state_[ctx.id] = pre_frame_state; + + + context_guard_[ctx.id] = std::make_shared(ctx.render_context); + + + + { // clear depth FBOs depth attachments - ctx.render_context->clear_depth_stencil_buffer(depth_pass_result_fbo_[ctx.id]); - ctx.render_context->clear_color_buffer(depth_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); - // configure accumulation FBO - accumulation_pass_result_fbo_[ctx.id]->clear_attachments(); - accumulation_pass_result_fbo_[ctx.id]->attach_color_buffer(0, accumulation_pass_color_result_[ctx.id]); - - // clear accumulation FBOs color attachment - ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); + ctx.render_context->clear_depth_stencil_buffer(depth_pass_result_fbo_[ctx.id]); + ctx.render_context->clear_color_buffer(depth_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); + + + + + + // clear accumulation FBOs color attachment + + ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f,0.0f)); + + } } //////////////////////////////////////////////////////////////////////////////// @@ -377,7 +412,8 @@ bool PBRUberShader::upload_to (RenderContext const& context) const // begin of depth pass (first) { - scm::gl::context_all_guard guard(ctx.render_context); + if( last_geometry_state_[ctx.id] != pre_draw_state) + { //enable dynamic point size in shaders ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); @@ -387,6 +423,7 @@ bool PBRUberShader::upload_to (RenderContext const& context) const ctx.render_context->set_frame_buffer(depth_pass_result_fbo_[ctx.id]); + gua::math::mat4 const& projection_matrix = frustum.get_projection(); float near_plane_value = frustum.get_clip_near(); @@ -396,13 +433,18 @@ bool PBRUberShader::upload_to (RenderContext const& context) const float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); + get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); + get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + get_program(depth_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + + last_geometry_state_[ctx.id] = pre_draw_state; + + + } - get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); - get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); - get_program(depth_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); if (material && pbr_ressource) { @@ -413,7 +455,7 @@ bool PBRUberShader::upload_to (RenderContext const& context) const get_program(depth_pass)->unuse(ctx); } - ctx.render_context->reset_framebuffer(); + } } @@ -451,44 +493,51 @@ void PBRUberShader::draw(RenderContext const& ctx, auto material = MaterialDatabase::instance()->lookup(material_name); { - scm::gl::context_all_guard guard(ctx.render_context); - //enable dynamic point size in shaders - ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + if( last_geometry_state_[ctx.id] != post_draw_state) + { - //disable depth test - ctx.render_context->set_depth_stencil_state(no_depth_test_depth_stencil_state_[ctx.id]); - - //set blend state to accumulate - ctx.render_context->set_blend_state(color_accumulation_state_[ctx.id]); + ctx.render_context->reset(); + //enable dynamic point size in shaders + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + //disable depth test + ctx.render_context->set_depth_stencil_state(no_depth_test_depth_stencil_state_[ctx.id]); + + //set blend state to accumulate + ctx.render_context->set_blend_state(color_accumulation_state_[ctx.id]); - // bind accumulation FBO - ctx.render_context->set_frame_buffer(accumulation_pass_result_fbo_[ctx.id]); + // ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 1.0f, 0.0f, 0.0f)); + // bind accumulation FBO + ctx.render_context->set_frame_buffer(accumulation_pass_result_fbo_[ctx.id]); - gua::math::mat4 const& projection_matrix = frustum.get_projection(); + gua::math::mat4 const& projection_matrix = frustum.get_projection(); + + //put near_plane_value and height_divided_by_top_minus_bottom as member variables and get these two only 1 per render frame + float near_plane_value = frustum.get_clip_near(); + float far_plane_value = frustum.get_clip_far(); + float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; + float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; + float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); - //put near_plane_value and height_divided_by_top_minus_bottom as member variables and get these two only 1 per render frame - float near_plane_value = frustum.get_clip_near(); - float far_plane_value = frustum.get_clip_far(); - float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; - float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; - float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); + //std::cout << "hdbtmb: "<< height_divided_by_top_minus_bottom; - //std::cout << "hdbtmb: "<< height_divided_by_top_minus_bottom; + get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); - get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); - get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); - get_program(accumulation_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); - get_program(accumulation_pass)->set_uniform(ctx, near_plane_value, "near_plane"); - get_program(accumulation_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); + get_program(accumulation_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); + get_program(accumulation_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + get_program(accumulation_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); - get_program(accumulation_pass)->set_uniform(ctx, math::vec2(render_window_dims_[ctx.id]),"win_dims"); + get_program(accumulation_pass)->set_uniform(ctx, math::vec2(render_window_dims_[ctx.id]),"win_dims"); - ctx.render_context->bind_texture(depth_pass_linear_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); - get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + ctx.render_context->bind_texture(depth_pass_linear_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); + get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + ctx.render_context->apply(); + last_geometry_state_[ctx.id] = post_draw_state; + } if (material && pbr_ressource) { @@ -496,12 +545,12 @@ void PBRUberShader::draw(RenderContext const& ctx, material_id_[ctx.id] = material->get_id(); get_program(accumulation_pass)->use(ctx); { + pbr_ressource->draw(ctx); } get_program(accumulation_pass)->unuse(ctx); } - ctx.render_context->reset_framebuffer(); } } @@ -511,15 +560,20 @@ void PBRUberShader::draw(RenderContext const& ctx, /*virtual*/ void PBRUberShader::postframe(RenderContext const& ctx) const { + + (context_guard_[ctx.id]).reset(); + { scm::gl::context_all_guard guard(ctx.render_context); + get_program(normalization_pass)->use(ctx); { { + get_program(normalization_pass)->set_uniform(ctx, material_id_[ctx.id], "gua_material_id"); @@ -529,15 +583,24 @@ void PBRUberShader::draw(RenderContext const& ctx, ctx.render_context->bind_texture(depth_pass_log_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + //bind color output for gbuffer ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], linear_sampler_state_[ctx.id], 1); get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p02_color_texture", 1); - + + ctx.render_context->apply(); fullscreen_quad_[ctx.id]->draw(ctx.render_context); } } get_program(normalization_pass)->unuse(ctx); - } + + + + ctx.render_context->reset_framebuffer(); + } + + + } From 4bf905d5034be33a820f0d3742fe3e474e3e4e87 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Tue, 13 May 2014 00:01:57 +0200 Subject: [PATCH 20/45] Multiple PBR Models render bug fix --- .../gbuffer/pbr/p03_normalization.frag | 4 ++- src/gua/renderer/PBRUberShader.cpp | 25 +++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag index d033bcb0a..887a93089 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag @@ -49,6 +49,8 @@ void main() { output_color = accumulated_color.rgb / accumulated_color.a; + output_color = pow(output_color,vec3(1.4f)); + gua_uint_gbuffer_out_0.x = gua_uint_gbuffer_varying_0.x; @@ -58,6 +60,6 @@ void main() { @apply_pbr_normal } - gl_FragDepth = pow(texture2D( p01_depth_texture, coords.xy).r,50); + gl_FragDepth = texture2D( p01_depth_texture, coords.xy).r; } diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index 0ec61c0b4..f12fe1283 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -394,6 +394,8 @@ bool PBRUberShader::upload_to (RenderContext const& context) const ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f,0.0f)); } + + } //////////////////////////////////////////////////////////////////////////////// @@ -414,7 +416,7 @@ bool PBRUberShader::upload_to (RenderContext const& context) const if( last_geometry_state_[ctx.id] != pre_draw_state) { - + //enable dynamic point size in shaders ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); @@ -436,15 +438,15 @@ bool PBRUberShader::upload_to (RenderContext const& context) const get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); get_program(depth_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); - - get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); - get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); last_geometry_state_[ctx.id] = pre_draw_state; } + get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + if (material && pbr_ressource) { @@ -522,8 +524,6 @@ void PBRUberShader::draw(RenderContext const& ctx, //std::cout << "hdbtmb: "<< height_divided_by_top_minus_bottom; - get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); - get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); get_program(accumulation_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); @@ -532,13 +532,19 @@ void PBRUberShader::draw(RenderContext const& ctx, get_program(accumulation_pass)->set_uniform(ctx, math::vec2(render_window_dims_[ctx.id]),"win_dims"); - ctx.render_context->bind_texture(depth_pass_linear_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); - get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); - ctx.render_context->apply(); + + last_geometry_state_[ctx.id] = post_draw_state; } + + ctx.render_context->bind_texture(depth_pass_linear_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); + get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + + get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + if (material && pbr_ressource) { @@ -588,7 +594,6 @@ void PBRUberShader::draw(RenderContext const& ctx, ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], linear_sampler_state_[ctx.id], 1); get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p02_color_texture", 1); - ctx.render_context->apply(); fullscreen_quad_[ctx.id]->draw(ctx.render_context); } } From 1af4ebb91f50fc2b096096bb90c3aefb0d8a481c Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Tue, 13 May 2014 20:53:55 +0200 Subject: [PATCH 21/45] add reconstruction stage & make surfel radius scaling dependent --- include/gua/renderer/PBRUberShader.hpp | 13 +- .../uber_shaders/gbuffer/pbr/p01_depth.vert | 5 +- .../gbuffer/pbr/p02_accumulation.frag | 4 +- .../gbuffer/pbr/p02_accumulation.vert | 5 +- .../gbuffer/pbr/p03_normalization.frag | 28 +- .../gbuffer/pbr/p03_normalization.vert | 17 - .../gbuffer/pbr/p04_reconstruction.frag | 344 ++++++++++++++++++ .../gbuffer/pbr/p04_reconstruction.vert | 47 +++ src/gua/renderer/PBRUberShader.cpp | 163 +++++++-- 9 files changed, 550 insertions(+), 76 deletions(-) create mode 100644 resources/shaders/uber_shaders/gbuffer/pbr/p04_reconstruction.frag create mode 100644 resources/shaders/uber_shaders/gbuffer/pbr/p04_reconstruction.vert diff --git a/include/gua/renderer/PBRUberShader.hpp b/include/gua/renderer/PBRUberShader.hpp index 9d2c2e9de..afc490f6d 100644 --- a/include/gua/renderer/PBRUberShader.hpp +++ b/include/gua/renderer/PBRUberShader.hpp @@ -34,7 +34,8 @@ class PBRUberShader : public GeometryUberShader { enum pass { depth_pass = 0, accumulation_pass = 1, - normalization_pass = 2 + normalization_pass = 2, + reconstruction_pass = 3 }; enum drawing_state{ @@ -91,6 +92,9 @@ class PBRUberShader : public GeometryUberShader { std::string const normalization_pass_vertex_shader () const; std::string const normalization_pass_fragment_shader () const; + std::string const reconstruction_pass_vertex_shader () const; + std::string const reconstruction_pass_fragment_shader () const; + std::string const default_pbr_material_name() const; private: //member variables @@ -107,6 +111,10 @@ class PBRUberShader : public GeometryUberShader { mutable std::vector accumulation_pass_color_result_; mutable std::vector accumulation_pass_result_fbo_; + //normalization pass FBO & attachments + mutable std::vector normalization_pass_color_result_; + mutable std::vector normalization_pass_result_fbo_; + //schism-GL states: ////////////////////////////////////////////////////////////////////////////////////// @@ -117,8 +125,6 @@ class PBRUberShader : public GeometryUberShader { mutable std::vector color_accumulation_state_; - - //frustum dependent variables: ///////////////////////////////////////////////////////////////////////////////////// mutable std::vector near_plane_value_; @@ -136,7 +142,6 @@ class PBRUberShader : public GeometryUberShader { //////////////////////////////////////////////////////////////////////////////////// mutable std::vector> context_guard_; - mutable std::vector framecount_ ; }; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert index db107d1e2..fa5194063 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert @@ -21,6 +21,8 @@ uniform float height_divided_by_top_minus_bottom; uniform float near_plane; + uniform float radius_scaling; + //output to fragment shader out vec3 pass_normal; out float pass_mv_vert_depth; @@ -34,7 +36,8 @@ gl_Position = gua_projection_matrix * pos_es; - gl_PointSize = 2.0f * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; + gl_PointSize = 2.0f * radius_scaling * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; + //gl_PointSize = in_radius * 2.0f * -pos_es.z * height_divided_by_top_minus_bottom; pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index 4aef58449..3720e56e1 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -34,6 +34,8 @@ uniform float near_plane; uniform float far_minus_near_plane; uniform vec2 win_dims; +uniform float radius_scaling; + //uniform float win_dim_x; //uniform float win_dim_y; @@ -124,7 +126,7 @@ float depth_to_compare = 0; float weight = get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); - if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_radius ) ) + if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_radius * radius_scaling ) ) { out_accumulated_color = vec4(pass_point_color * weight, weight); } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert index d0100cb80..7d4e1c52b 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert @@ -21,6 +21,7 @@ uniform float height_divided_by_top_minus_bottom; uniform float near_plane; + uniform float radius_scaling; //output to fragment shader @@ -37,8 +38,8 @@ gl_Position = gua_projection_matrix * pos_es; - gl_PointSize = 2.0f * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; - + gl_PointSize = 2.0f * radius_scaling * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; + //gl_PointSize = in_radius * 2.0f * -pos_es.z * height_divided_by_top_minus_bottom; pass_point_color = vec3((in_r)/255.0f, (in_g)/255.0f, diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag index 887a93089..c2d050fe4 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.frag @@ -6,30 +6,22 @@ in vec3 gua_position_varying; in vec2 gua_quad_coords; -@input_definition - - /////////////////////////////////////////////////////////////////////////////// // general uniforms /////////////////////////////////////////////////////////////////////////////// @include "shaders/uber_shaders/common/gua_camera_uniforms.glsl" -@uniform_definition - /////////////////////////////////////////////////////////////////////////////// uniform int using_default_pbr_material; -layout(binding=0) uniform sampler2D p01_depth_texture; -layout(binding=1) uniform sampler2D p02_color_texture; - - +uniform sampler2D p02_color_texture; /////////////////////////////////////////////////////////////////////////////// // output /////////////////////////////////////////////////////////////////////////////// -@output_definition +layout (location=0) out vec3 out_normalized_color; /////////////////////////////////////////////////////////////////////////////// @@ -37,7 +29,7 @@ layout(binding=1) uniform sampler2D p02_color_texture; /////////////////////////////////////////////////////////////////////////////// void main() { - vec3 output_color = vec3(1.0); + vec3 normalized_color = vec3(1.0); float output_depth = 1.0f; vec3 output_normal = vec3(0.0); @@ -47,19 +39,11 @@ void main() { vec4 accumulated_color = texture2D( p02_color_texture, coords.xy); - output_color = accumulated_color.rgb / accumulated_color.a; - - output_color = pow(output_color,vec3(1.4f)); - + normalized_color = accumulated_color.rgb / accumulated_color.a; - gua_uint_gbuffer_out_0.x = gua_uint_gbuffer_varying_0.x; + normalized_color = pow(normalized_color,vec3(1.4f)); - //if ( using_default_pbr_material != 0 ) - { - @apply_pbr_color - @apply_pbr_normal - } + out_normalized_color = normalized_color; - gl_FragDepth = texture2D( p01_depth_texture, coords.xy).r; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.vert index 49c6d4a98..c5b9a1459 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p03_normalization.vert @@ -11,10 +11,6 @@ layout(location=2) in vec2 gua_in_texcoord; /////////////////////////////////////////////////////////////////////////////// @include "shaders/uber_shaders/common/gua_camera_uniforms.glsl" -uniform uint gua_material_id; - -// material specific uniforms -@uniform_definition /////////////////////////////////////////////////////////////////////////////// // output @@ -22,17 +18,6 @@ uniform uint gua_material_id; out vec3 gua_position_varying; out vec2 gua_quad_coords; -@output_definition - -/////////////////////////////////////////////////////////////////////////////// -// methods -/////////////////////////////////////////////////////////////////////////////// - -uint gua_get_material_id() { - return gua_uint_gbuffer_varying_0.x; -} - - /////////////////////////////////////////////////////////////////////////////// // main @@ -41,7 +26,5 @@ void main() { gua_position_varying = vec3(0); gua_quad_coords = gua_in_texcoord; - - gua_uint_gbuffer_varying_0.x = gua_material_id; gl_Position = vec4(gua_in_position, 1.0); } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p04_reconstruction.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p04_reconstruction.frag new file mode 100644 index 000000000..bdec1c346 --- /dev/null +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p04_reconstruction.frag @@ -0,0 +1,344 @@ +@include "shaders/common/header.glsl" + +/////////////////////////////////////////////////////////////////////////////// +// input +/////////////////////////////////////////////////////////////////////////////// +in vec3 gua_position_varying; +in vec2 gua_quad_coords; + +@input_definition + + +/////////////////////////////////////////////////////////////////////////////// +// general uniforms +/////////////////////////////////////////////////////////////////////////////// +@include "shaders/uber_shaders/common/gua_camera_uniforms.glsl" + +@uniform_definition + + +/////////////////////////////////////////////////////////////////////////////// +uniform int using_default_pbr_material; +uniform vec2 win_dims; + + +layout(binding=0) uniform sampler2D p01_depth_texture; +layout(binding=1) uniform sampler2D p02_color_texture; + + + + +/////////////////////////////////////////////////////////////////////////////// +// output +/////////////////////////////////////////////////////////////////////////////// +@output_definition + + +/////////////////////////////////////////////////////////////////////////////// +// main +/////////////////////////////////////////////////////////////////////////////// +void main() +{ + + vec3 output_color = vec3(1.0); + float output_depth = 1.0f; + vec3 output_normal = vec3(0.0); + + vec3 coords = vec3(gua_quad_coords, 0.0); + + + + output_color = texture2D( p02_color_texture, coords.xy).rgb; + + + +/* + output_color = accumulated_color.rgb;// / accumulated_color.a; + + //output_color = pow(output_color,vec3(1.4f)); + + + + + //if ( using_default_pbr_material != 0 ) + + + +*/ + + + float depthValue = texture2D( p01_depth_texture, coords.xy).r; + + + { + + if(depthValue != 1.0f) + { + //out_color = texture2D(p02_color_texture, gl_FragCoord.xy/(win_size.xy)); + //output_color = vec3(0.0,0.0,1.0); + gl_FragDepth = depthValue; + } + else + { + + + float[8] surrounding; + surrounding[0] = texture2D(p01_depth_texture, ( (gl_FragCoord.xy + vec2(-1,+1) )/(win_dims.xy) ) ).r; //upper left pixel + surrounding[1] = texture2D(p01_depth_texture, ( (gl_FragCoord.xy + vec2( 0,+1) )/(win_dims.xy) ) ).r; //upper pixel + surrounding[2] = texture2D(p01_depth_texture, ( (gl_FragCoord.xy + vec2(+1,+1) )/(win_dims.xy) ) ).r; //upper right pixel + surrounding[3] = texture2D(p01_depth_texture, ( (gl_FragCoord.xy + vec2(-1, 0) )/(win_dims.xy) ) ).r; //left pixel + surrounding[4] = texture2D(p01_depth_texture, ( (gl_FragCoord.xy + vec2(+1, 0) )/(win_dims.xy) ) ).r; //right pixel + surrounding[5] = texture2D(p01_depth_texture, ( (gl_FragCoord.xy + vec2(-1,-1) )/(win_dims.xy) ) ).r; //lower left pixel + surrounding[6] = texture2D(p01_depth_texture, ( (gl_FragCoord.xy + vec2( 0,-1) )/(win_dims.xy) ) ).r; //lower pixel + surrounding[7] = texture2D(p01_depth_texture, ( (gl_FragCoord.xy + vec2(+1,-1) )/(win_dims.xy) ) ).r; //lower right pixel + + // surrounding neighbourhood indexing: + // 0 1 2 + // 3 4 + // 5 6 7 + + //pattern symbols: + //b = background pixel + //x = random + //o = center pixel + + //rule 1: + //if all of the b-pixel are actually background pixel: pattern matches + //rule 2: + //if at least 1 pattern matches: don't fill + + //test against pattern 0 + + //x b b x 1 2 + //x o b x 4 + //x b b x 6 7 + + bool pattern0 = (surrounding[1] == 1.0) && (surrounding[2] == 1.0) && (surrounding[4] == 1.0) && (surrounding[6] == 1.0) && (surrounding[7] == 1.0) ; + + //test against pattern 1 + + //b b b 0 1 2 + //b o b 3 4 + //x x x x x x + + bool pattern1 = (surrounding[0] == 1.0) && (surrounding[1] == 1.0) && (surrounding[2] == 1.0) && (surrounding[3] == 1.0) && (surrounding[4] == 1.0) ; + + //test against pattern 2 + + //b b x 0 1 x + //b o x 3 x + //b b x 5 6 x + + bool pattern2 = (surrounding[0] == 1.0) && (surrounding[1] == 1.0) && (surrounding[3] == 1.0) && (surrounding[5] == 1.0) && (surrounding[6] == 1.0) ; + + //test against pattern 3 + + //x x x x x x + //b o b 3 4 + //b b b 5 6 7 + + bool pattern3 = (surrounding[3] == 1.0) && (surrounding[4] == 1.0) && (surrounding[5] == 1.0) && (surrounding[6] == 1.0) && (surrounding[7] == 1.0) ; + + //test against pattern 4 + + //b b b 0 1 2 + //x o b x 4 + //x x b x x 7 + + bool pattern4 = (surrounding[0] == 1.0) && (surrounding[1] == 1.0) && (surrounding[2] == 1.0) && (surrounding[4] == 1.0) && (surrounding[7] == 1.0) ; + + //test against pattern 5 + + //b b b 0 1 2 + //b o x 3 x + //b x x 5 x x + + bool pattern5 = (surrounding[0] == 1.0) && (surrounding[1] == 1.0) && (surrounding[2] == 1.0) && (surrounding[3] == 1.0) && (surrounding[5] == 1.0) ; + + //test against pattern 6 + + //b x x 0 x x + //b o x 3 x + //b b b 5 6 7 + + bool pattern6 = (surrounding[0] == 1.0) && (surrounding[3] == 1.0) && (surrounding[5] == 1.0) && (surrounding[6] == 1.0) && (surrounding[7] == 1.0) ; + + //test against pattern 7 + + //x x b x x 2 + //x o b x 4 + //b b b 5 6 7 + + bool pattern7 = (surrounding[2] == 1.0) && (surrounding[4] == 1.0) && (surrounding[5] == 1.0) && (surrounding[6] == 1.0) && (surrounding[7] == 1.0) ; + + + + if( pattern0 || pattern1 || pattern2 || pattern3 || pattern4 || pattern5 || pattern6 || pattern7 ) + { + output_color = vec3(0.f,0.0f,0.0f); + output_color = vec3(0.0f,1.0f,0.0f); + gl_FragDepth = depthValue; + } + else + { + output_color = vec3(1.0,0.0,0.0); + + + + + + + + //re-fill the surrounding array with luminosity values of the surrounding area + vec3 tempCol = vec3(0.0,0.0,0.0); + tempCol = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(-1,+1) )/(win_dims.xy) ).rgb; //upper left pixel + surrounding[0] = 0.2126 * tempCol.r + 0.7152 * tempCol.g + 0.0722 * tempCol.b; + + tempCol = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(0,+1) )/(win_dims.xy) ).rgb; //upper pixel + surrounding[1] = 0.2126 * tempCol.r + 0.7152 * tempCol.g + 0.0722 * tempCol.b; + + tempCol = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(+1,+1) )/(win_dims.xy) ).rgb; //upper right pixel + surrounding[2] = 0.2126 * tempCol.r + 0.7152 * tempCol.g + 0.0722 * tempCol.b; + + tempCol = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(-1,0) )/(win_dims.xy) ).rgb; //left pixel + surrounding[3] = 0.2126 * tempCol.r + 0.7152 * tempCol.g + 0.0722 * tempCol.b; + + tempCol = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(+1, 0) )/(win_dims.xy) ).rgb; //right pixel + surrounding[4] = 0.2126 * tempCol.r + 0.7152 * tempCol.g + 0.0722 * tempCol.b; + + tempCol = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(-1,-1) )/(win_dims.xy) ).rgb; //lower left pixel + surrounding[5] = 0.2126 * tempCol.r + 0.7152 * tempCol.g + 0.0722 * tempCol.b; + + tempCol = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(0,-1) )/(win_dims.xy) ).rgb; //lower pixel + surrounding[6] = 0.2126 * tempCol.r + 0.7152 * tempCol.g + 0.0722 * tempCol.b; + + tempCol = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(+1,-1) )/(win_dims.xy) ).rgb; //lower right pixel + surrounding[7] = 0.2126 * tempCol.r + 0.7152 * tempCol.g + 0.0722 * tempCol.b; + + //find the median element with index 4 + for(int i = 0; i < 8; ++i) + { + + int sum_smaller_elements = 0; + int sum_equal_elements = 0; + + for(int k = 0; k < 8; ++k) + { + if(i != k) + { + if(surrounding[i] < surrounding[k]) //our current element was smaller, so we don't have to do anything + {//do nothing + } + else if(surrounding[i] > surrounding[k]) + { + sum_smaller_elements += 1; + } + else + { + sum_equal_elements += 1; + } + + } + } + + if((sum_smaller_elements + sum_equal_elements >= 3) ) + { + + //if(renderMode == 0) + { + vec3 tempC; + float tempD; + if( i == 0) + { + tempC = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(-1,+1) )/(win_dims.xy) ).rgb; + tempD = texture2D(p01_depth_texture, (gl_FragCoord.xy + vec2(-1,+1) )/(win_dims.xy) ).r; + + } + else if(i == 1) + { + tempC = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(0,+1) )/(win_dims.xy)).rgb; + tempD = texture2D(p01_depth_texture, (gl_FragCoord.xy + vec2(0,+1) )/(win_dims.xy) ).r; + } + else if(i == 2) + { + tempC = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(+1,+1) )/(win_dims.xy)).rgb; + tempD = texture2D(p01_depth_texture, (gl_FragCoord.xy + vec2(+1,+1) )/(win_dims.xy) ).r; + } + else if(i == 3) + { + tempC = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(-1, 0) )/(win_dims.xy)).rgb; + tempD = texture2D(p01_depth_texture, (gl_FragCoord.xy + vec2(-1, 0) )/(win_dims.xy) ).r; + } + else if(i == 4) + { + tempC = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(+1,0) )/(win_dims.xy) ).rgb; + tempD = texture2D(p01_depth_texture, (gl_FragCoord.xy + vec2(+1,0) )/(win_dims.xy) ).r; + } + else if(i == 5) + { + tempC = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(-1,-1) )/(win_dims.xy)).rgb; + tempD = texture2D(p01_depth_texture, (gl_FragCoord.xy + vec2(-1,-1) )/(win_dims.xy) ).r; + } + else if(i == 6) + { + tempC = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2( 0,-1) )/(win_dims.xy) ).rgb; + tempD = texture2D(p01_depth_texture, (gl_FragCoord.xy + vec2( 0,-1) )/(win_dims.xy) ).r; + } + else if(i == 7) + { + tempC = texture2D(p02_color_texture, (gl_FragCoord.xy + vec2(+1,-1) )/(win_dims.xy)).rgb; + tempD = texture2D(p01_depth_texture, (gl_FragCoord.xy + vec2(+1,-1) )/(win_dims.xy) ).r; + } + + + if( (tempC.rgb == vec3(0.0,0.0,0.0) ) && i != 7 ) + { + continue; + } + else + { + output_color = tempC; + // output_color = vec3(1.0,0.0,0.0); + + gl_FragDepth = tempD; + } + } + + break; + } //endif + + + + + + + } //end for + + + + + + + } + + + + } + + + } + + +///// + { + @apply_pbr_color + @apply_pbr_normal + } + + + gua_uint_gbuffer_out_0.x = gua_uint_gbuffer_varying_0.x; + +//////////////////////////////////////////////////////////////// + + +} diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p04_reconstruction.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p04_reconstruction.vert new file mode 100644 index 000000000..49c6d4a98 --- /dev/null +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p04_reconstruction.vert @@ -0,0 +1,47 @@ +@include "shaders/common/header.glsl" + +/////////////////////////////////////////////////////////////////////////////// +// input +/////////////////////////////////////////////////////////////////////////////// +layout(location=0) in vec3 gua_in_position; +layout(location=2) in vec2 gua_in_texcoord; + +/////////////////////////////////////////////////////////////////////////////// +// general uniforms +/////////////////////////////////////////////////////////////////////////////// +@include "shaders/uber_shaders/common/gua_camera_uniforms.glsl" + +uniform uint gua_material_id; + +// material specific uniforms +@uniform_definition + +/////////////////////////////////////////////////////////////////////////////// +// output +/////////////////////////////////////////////////////////////////////////////// +out vec3 gua_position_varying; +out vec2 gua_quad_coords; + +@output_definition + +/////////////////////////////////////////////////////////////////////////////// +// methods +/////////////////////////////////////////////////////////////////////////////// + +uint gua_get_material_id() { + return gua_uint_gbuffer_varying_0.x; +} + + + +/////////////////////////////////////////////////////////////////////////////// +// main +/////////////////////////////////////////////////////////////////////////////// +void main() { + gua_position_varying = vec3(0); + gua_quad_coords = gua_in_texcoord; + + + gua_uint_gbuffer_varying_0.x = gua_material_id; + gl_Position = vec4(gua_in_position, 1.0); +} diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index f12fe1283..b449c8da3 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -67,7 +67,7 @@ PBRUberShader::PBRUberShader() depth_pass_program->set_shaders(depth_pass_stages); add_program(depth_pass_program); - // create depth shader + // create accumulation pass shader std::vector accumulation_pass_stages; accumulation_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_VERTEX_SHADER, accumulation_pass_vertex_shader())); accumulation_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_FRAGMENT_SHADER, accumulation_pass_fragment_shader())); @@ -76,7 +76,7 @@ PBRUberShader::PBRUberShader() accumulation_pass_program->set_shaders(accumulation_pass_stages); add_program(accumulation_pass_program); - // create final shader + // create normalization pass shader std::vector normalization_pass_stages; normalization_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_VERTEX_SHADER, normalization_pass_vertex_shader())); normalization_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_FRAGMENT_SHADER, normalization_pass_fragment_shader())); @@ -84,6 +84,15 @@ PBRUberShader::PBRUberShader() auto normalization_pass_program = std::make_shared(); normalization_pass_program->set_shaders(normalization_pass_stages); add_program(normalization_pass_program); + + // create reconstruction pass shader + std::vector reconstruction_pass_stages; + reconstruction_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_VERTEX_SHADER, reconstruction_pass_vertex_shader())); + reconstruction_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_FRAGMENT_SHADER, reconstruction_pass_fragment_shader())); + + auto reconstruction_pass_program = std::make_shared(); + reconstruction_pass_program->set_shaders(reconstruction_pass_stages); + add_program(reconstruction_pass_program); } @@ -144,7 +153,7 @@ std::string const PBRUberShader::accumulation_pass_fragment_shader() const } - //////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// @@ -154,6 +163,30 @@ std::string const PBRUberShader::normalization_pass_vertex_shader() const Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p03_normalization_vert) ); + return vertex_shader; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::string const PBRUberShader::normalization_pass_fragment_shader() const +{ + std::string fragment_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p03_normalization_frag) + ); + + return fragment_shader; +} + +//////////////////////////////////////////////////////////////////////////////// + + + +std::string const PBRUberShader::reconstruction_pass_vertex_shader() const +{ + std::string vertex_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p04_reconstruction_vert) + ); + // material specific uniforms string_utils::replace(vertex_shader, "@uniform_definition", @@ -169,10 +202,10 @@ std::string const PBRUberShader::normalization_pass_vertex_shader() const //////////////////////////////////////////////////////////////////////////////// -std::string const PBRUberShader::normalization_pass_fragment_shader() const +std::string const PBRUberShader::reconstruction_pass_fragment_shader() const { std::string fragment_shader( - Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p03_normalization_frag) + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p04_reconstruction_frag) ); std::string apply_pbr_color = fshader_factory_->get_output_mapping().get_output_string("gua_pbr", "gua_pbr_output_color"); @@ -202,8 +235,6 @@ std::string const PBRUberShader::normalization_pass_fragment_shader() const } - - //////////////////////////////////////////////////////////////////////////////// bool PBRUberShader::upload_to (RenderContext const& context) const @@ -247,7 +278,6 @@ bool PBRUberShader::upload_to (RenderContext const& context) const ); } - // initialize attachments for accumulation pass if (context.id >= accumulation_pass_color_result_.size()) { @@ -264,6 +294,23 @@ bool PBRUberShader::upload_to (RenderContext const& context) const } + // initialize attachment for normalization pass + if (context.id >= normalization_pass_color_result_.size()) + { + + normalization_pass_color_result_.resize(context.id + 1); + + + normalization_pass_color_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_RGB_8, + 1, + 1, + 1 + ); + } + + if (context.id >= fullscreen_quad_.size()) { fullscreen_quad_.resize(context.id + 1); @@ -289,6 +336,15 @@ bool PBRUberShader::upload_to (RenderContext const& context) const accumulation_pass_result_fbo_[context.id]->attach_color_buffer(0, accumulation_pass_color_result_[context.id]); } + if (context.id >= normalization_pass_result_fbo_.size()) { + normalization_pass_result_fbo_.resize(context.id + 1); + normalization_pass_result_fbo_[context.id] = context.render_device->create_frame_buffer(); + + // configure normalization FBO + normalization_pass_result_fbo_[context.id]->clear_attachments(); + normalization_pass_result_fbo_[context.id]->attach_color_buffer(0, normalization_pass_color_result_[context.id]); + } + if (context.id >= linear_sampler_state_.size()) { linear_sampler_state_.resize(context.id + 1); linear_sampler_state_[context.id] = context.render_device->create_sampler_state(scm::gl::FILTER_MIN_MAG_LINEAR, scm::gl::WRAP_CLAMP_TO_EDGE); @@ -340,11 +396,6 @@ bool PBRUberShader::upload_to (RenderContext const& context) const last_geometry_state_[context.id] = invalid_state; } - if(context.id >= framecount_.size()) - { - framecount_.resize(context.id + 1); - framecount_[context.id] = 0; - } return upload_succeeded; } @@ -387,15 +438,19 @@ bool PBRUberShader::upload_to (RenderContext const& context) const - - // clear accumulation FBOs color attachment ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f,0.0f)); + + // clear normalization FBOs color attachment + ctx.render_context->clear_color_buffer(normalization_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f,0.0f)); + } + + } //////////////////////////////////////////////////////////////////////////////// @@ -444,6 +499,15 @@ bool PBRUberShader::upload_to (RenderContext const& context) const } + + scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); + + float radius_scaling = scm::math::length(model_matrix * x_unit_vec); + + + get_program(depth_pass)->set_uniform(ctx, radius_scaling, "radius_scaling"); + + get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); @@ -522,10 +586,6 @@ void PBRUberShader::draw(RenderContext const& ctx, float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); - //std::cout << "hdbtmb: "<< height_divided_by_top_minus_bottom; - - - get_program(accumulation_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); get_program(accumulation_pass)->set_uniform(ctx, near_plane_value, "near_plane"); get_program(accumulation_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); @@ -533,15 +593,25 @@ void PBRUberShader::draw(RenderContext const& ctx, get_program(accumulation_pass)->set_uniform(ctx, math::vec2(render_window_dims_[ctx.id]),"win_dims"); - - last_geometry_state_[ctx.id] = post_draw_state; + + } + + + + + scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); + + float radius_scaling = scm::math::length(model_matrix * x_unit_vec); + ctx.render_context->bind_texture(depth_pass_linear_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + get_program(accumulation_pass)->set_uniform(ctx, radius_scaling, "radius_scaling"); + get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); @@ -571,38 +641,73 @@ void PBRUberShader::draw(RenderContext const& ctx, { + + { scm::gl::context_all_guard guard(ctx.render_context); + // bind normalization FBO + ctx.render_context->set_frame_buffer(normalization_pass_result_fbo_[ctx.id]); get_program(normalization_pass)->use(ctx); { { - get_program(normalization_pass)->set_uniform(ctx, material_id_[ctx.id], "gua_material_id"); + get_program(normalization_pass)->set_uniform(ctx, 1 , "using_default_pbr_material"); + + + //bind color output for gbuffer + ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], linear_sampler_state_[ctx.id], 0); + get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p02_color_texture", 0); + + fullscreen_quad_[ctx.id]->draw(ctx.render_context); + } + } + get_program(normalization_pass)->unuse(ctx); + + } + // ctx.render_context->reset_framebuffer(); + + + ///////////////////////////////////////////////////////////////////////////////////////////// + //insert screen space reconstruction pass here + + { + + scm::gl::context_all_guard guard(ctx.render_context); + + get_program(reconstruction_pass)->use(ctx); + { + + { + + get_program(reconstruction_pass)->set_uniform(ctx, material_id_[ctx.id], "gua_material_id"); - get_program(normalization_pass)->set_uniform(ctx, 1 , "using_default_pbr_material"); + get_program(reconstruction_pass)->set_uniform(ctx, 1 , "using_default_pbr_material"); + + get_program(reconstruction_pass)->set_uniform(ctx, math::vec2(render_window_dims_[ctx.id]),"win_dims"); //bind logarithmic depth texture for gbuffer ctx.render_context->bind_texture(depth_pass_log_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); - get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + get_program(reconstruction_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); //bind color output for gbuffer - ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], linear_sampler_state_[ctx.id], 1); - get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p02_color_texture", 1); + ctx.render_context->bind_texture(normalization_pass_color_result_[ctx.id], linear_sampler_state_[ctx.id], 1); + get_program(reconstruction_pass)->get_program(ctx)->uniform_sampler("p02_color_texture", 1); fullscreen_quad_[ctx.id]->draw(ctx.render_context); } } - get_program(normalization_pass)->unuse(ctx); + get_program(reconstruction_pass)->unuse(ctx); + ctx.render_context->reset_framebuffer(); - ctx.render_context->reset_framebuffer(); - } + } + } From 71927f91e6c4065581bfa3ebae3de07215f9ee1a Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Wed, 14 May 2014 16:59:37 +0200 Subject: [PATCH 22/45] Add screen size dependent splat scaling --- .../uber_shaders/gbuffer/pbr/p01_depth.frag | 4 ++-- .../uber_shaders/gbuffer/pbr/p01_depth.vert | 11 ++++++----- .../gbuffer/pbr/p02_accumulation.frag | 7 +++---- .../gbuffer/pbr/p02_accumulation.vert | 16 ++++++++++------ src/gua/renderer/PBRUberShader.cpp | 18 +++++++++++------- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag index dc0dd140c..1552d3530 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag @@ -5,7 +5,7 @@ /////////////////////////////////////////////////////////////////////////////// in vec3 pass_normal; in float pass_mv_vert_depth; -in float pass_radius; +in float pass_scaled_radius; /////////////////////////////////////////////////////////////////////////////// // output @@ -91,6 +91,6 @@ void main() get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); - out_linear_depth = - ( ( (pass_mv_vert_depth + depth_offset * pass_radius ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; + out_linear_depth = - ( ( (pass_mv_vert_depth + depth_offset * pass_scaled_radius ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert index fa5194063..6aaa87711 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert @@ -21,27 +21,28 @@ uniform float height_divided_by_top_minus_bottom; uniform float near_plane; - uniform float radius_scaling; + uniform float radius_model_scaling; + uniform float mVPScalingRatio; //output to fragment shader out vec3 pass_normal; out float pass_mv_vert_depth; - out float pass_radius; + out float pass_scaled_radius; void main() { + float scaled_radius = radius_model_scaling * in_radius; vec4 pos_es = gua_view_matrix * gua_model_matrix * vec4(in_position, 1.0); gl_Position = gua_projection_matrix * pos_es; - gl_PointSize = 2.0f * radius_scaling * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; - //gl_PointSize = in_radius * 2.0f * -pos_es.z * height_divided_by_top_minus_bottom; + gl_PointSize = 2.0f * mVPScalingRatio * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; pass_mv_vert_depth = pos_es.z; - pass_radius = in_radius; + pass_scaled_radius = scaled_radius; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index 3720e56e1..ec3fc644c 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -14,7 +14,7 @@ const float gaussian[32] = float[]( in vec3 pass_point_color; in vec3 pass_normal; in float pass_mv_vert_depth; -in float pass_radius; +in float pass_scaled_radius; /////////////////////////////////////////////////////////////////////////////// // output @@ -34,7 +34,6 @@ uniform float near_plane; uniform float far_minus_near_plane; uniform vec2 win_dims; -uniform float radius_scaling; //uniform float win_dim_x; //uniform float win_dim_y; @@ -119,14 +118,14 @@ void main() float depth_to_compare = 0; //if(ellipsify) - depth_to_compare = pass_mv_vert_depth + depth_offset * pass_radius; + depth_to_compare = pass_mv_vert_depth + depth_offset * pass_scaled_radius; //else // depth_to_compare = pass_mv_vert_depth; float weight = get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); - if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_radius * radius_scaling ) ) + if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_scaled_radius) ) { out_accumulated_color = vec4(pass_point_color * weight, weight); } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert index 7d4e1c52b..cf4e53339 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert @@ -21,25 +21,29 @@ uniform float height_divided_by_top_minus_bottom; uniform float near_plane; - uniform float radius_scaling; - + uniform float radius_model_scaling; + uniform float mVPScalingRatio; //output to fragment shader out vec3 pass_point_color; out vec3 pass_normal; out float pass_mv_vert_depth; - out float pass_radius; + out float pass_scaled_radius; void main() { + + float scaled_radius = radius_model_scaling * in_radius; + vec4 pos_es = gua_view_matrix * gua_model_matrix * vec4(in_position, 1.0); + gl_Position = gua_projection_matrix * pos_es; - gl_PointSize = 2.0f * radius_scaling * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; - //gl_PointSize = in_radius * 2.0f * -pos_es.z * height_divided_by_top_minus_bottom; + gl_PointSize = 2.0f * mVPScalingRatio * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; + pass_point_color = vec3((in_r)/255.0f, (in_g)/255.0f, @@ -47,5 +51,5 @@ pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; pass_mv_vert_depth = pos_es.z; - pass_radius = in_radius; + pass_scaled_radius = scaled_radius; } diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index b449c8da3..87d512266 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -502,11 +502,14 @@ bool PBRUberShader::upload_to (RenderContext const& context) const scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); - float radius_scaling = scm::math::length(model_matrix * x_unit_vec); + float radius_model_scaling = scm::math::length(model_matrix * x_unit_vec); + float modelViewProjectionScalingRatio = scm::math::length(frustum.get_projection()*frustum.get_view()*model_matrix*x_unit_vec) + / 0.8759124279022216797; - get_program(depth_pass)->set_uniform(ctx, radius_scaling, "radius_scaling"); + get_program(depth_pass)->set_uniform(ctx, radius_model_scaling, "radius_model_scaling"); + get_program(depth_pass)->set_uniform(ctx, modelViewProjectionScalingRatio, "mVPScalingRatio"); get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); @@ -598,19 +601,20 @@ void PBRUberShader::draw(RenderContext const& ctx, } + scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); + float radius_model_scaling = scm::math::length(model_matrix * x_unit_vec); + float modelViewProjectionScalingRatio = scm::math::length(frustum.get_projection()*frustum.get_view()*model_matrix*x_unit_vec) + / 0.8759124279022216797; - - scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); - - float radius_scaling = scm::math::length(model_matrix * x_unit_vec); + get_program(accumulation_pass)->set_uniform(ctx, radius_model_scaling, "radius_model_scaling"); + get_program(accumulation_pass)->set_uniform(ctx, modelViewProjectionScalingRatio, "mVPScalingRatio"); ctx.render_context->bind_texture(depth_pass_linear_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); - get_program(accumulation_pass)->set_uniform(ctx, radius_scaling, "radius_scaling"); get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); From b84d4b49c1cc74c035031810d59093877a4cfcac Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Thu, 15 May 2014 22:33:58 +0200 Subject: [PATCH 23/45] Fix blending bug --- .../shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index ec3fc644c..19c847ef9 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -91,7 +91,7 @@ float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNorma if(radius > 1.0) discard; else - return gaussian[(int)(round(radius * 32.0))]; + return gaussian[(int)(round(radius * 31.0))]; } @@ -127,7 +127,7 @@ float depth_to_compare = 0; if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_scaled_radius) ) { - out_accumulated_color = vec4(pass_point_color * weight, weight); + out_accumulated_color = vec4(pass_point_color * weight, weight); } else { From abda6ec0761005b940c31ce9650c0746a47e2887 Mon Sep 17 00:00:00 2001 From: feoo Date: Mon, 19 May 2014 00:41:12 +0200 Subject: [PATCH 24/45] checkin PLOD classes, rendering and loading works --- include/gua/renderer/PLODLoader.hpp | 79 ++ include/gua/renderer/PLODRessource.hpp | 112 +++ include/gua/renderer/PLODUberShader.hpp | 175 ++++ include/gua/scenegraph/PLODNode.hpp | 61 ++ src/gua/renderer/PLODLoader.cpp | 93 ++ src/gua/renderer/PLODRessource.cpp | 151 ++++ src/gua/renderer/PLODUberShader.cpp | 1061 +++++++++++++++++++++++ src/gua/scenegraph/PLODNode.cpp | 76 ++ 8 files changed, 1808 insertions(+) create mode 100644 include/gua/renderer/PLODLoader.hpp create mode 100644 include/gua/renderer/PLODRessource.hpp create mode 100644 include/gua/renderer/PLODUberShader.hpp create mode 100644 include/gua/scenegraph/PLODNode.hpp create mode 100644 src/gua/renderer/PLODLoader.cpp create mode 100644 src/gua/renderer/PLODRessource.cpp create mode 100644 src/gua/renderer/PLODUberShader.cpp create mode 100644 src/gua/scenegraph/PLODNode.cpp diff --git a/include/gua/renderer/PLODLoader.hpp b/include/gua/renderer/PLODLoader.hpp new file mode 100644 index 000000000..2e023daad --- /dev/null +++ b/include/gua/renderer/PLODLoader.hpp @@ -0,0 +1,79 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +#ifndef GUA_PLOD_LOADER_HPP +#define GUA_PLOD_LOADER_HPP + +#include + +namespace gua{ + +class Node; +class InnerNode; +class GeometryNode; +class PLODRessource; + +class PLODLoader : public LoaderBase{ + public: + + /** + * Default constructor. + * + * Constructs a new and empty PBRLoader. + */ + PLODLoader(); + + /** + * Constructor from a file. + * + * Creates a new PBRLoader from a given file. + * + * \param file_name The file to load the pointclouds data from. + * \param material_name The material name that was set to the parent node + */ + std::shared_ptr load(std::string const& file_name, + unsigned flags); + + /** + * Constructor from memory buffer. + * + * Creates a new MeshLoader from a existing memory buffer. + * + * \param buffer_name The buffer to load the meh's data from. + * \param buffer_size The buffer's size. + */ + std::vector const load_from_buffer(char const* buffer_name, + unsigned buffer_size, + bool build_kd_tree); + + bool is_supported(std::string const& file_name) const; + + private: + + unsigned node_counter_; + + static unsigned model_counter_; +}; + + +} + +#endif // GUA_PLOD_LOADER_HPP diff --git a/include/gua/renderer/PLODRessource.hpp b/include/gua/renderer/PLODRessource.hpp new file mode 100644 index 000000000..446564e54 --- /dev/null +++ b/include/gua/renderer/PLODRessource.hpp @@ -0,0 +1,112 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +#ifndef GUA_PLOD_RESSOURCE_HPP +#define GUA_PLOD_RESSOURCE_HPP + + + +// guacamole headers +#include +#include +#include +#include + +// external headers +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + + + + +namespace gua { + +struct RenderContext; + +/** + * Stores geometry data. + * + * A mesh can be loaded from an Assimp mesh and the draw onto multiple + * contexts. + * Do not use this class directly, it is just used by the Geometry class to + * store the individual meshes of a file. + */ +class PLODRessource : public GeometryRessource { + public: + + /** + * Default constructor. + * + * Creates a new and empty Mesh. + */ + PLODRessource(); + + /** + * Constructor from an Assimp mesh. + * + * Initializes the mesh from a given Assimp mesh. + * + * \param mesh The Assimp mesh to load the data from. + */ + PLODRessource(const pbr::ren::LodPointCloud* point_cloud); + + /** + * Draws the Mesh. + * + * Draws the Mesh to the given context. + * + * \param context The RenderContext to draw onto. + */ + void draw(RenderContext const& ctx) const; + + void draw(RenderContext const& ctx, pbr::context_t context_id, pbr::view_t view_id, pbr::model_t model_id, scm::gl::vertex_array_ptr const& vertex_array) const; + + void ray_test(Ray const& ray, PickResult::Options options, + Node* owner, std::set& hits); + + + /*virtual*/ GeometryUberShader* get_ubershader() const; + + private: + + void upload_to(RenderContext const& context) const; +/* + mutable std::vector buffers_; + mutable std::vector vertex_array_;*/ + mutable std::mutex upload_mutex_; + + public: + + +}; + +} + +#endif // GUA_PLOD_RESSOURCE_HPP diff --git a/include/gua/renderer/PLODUberShader.hpp b/include/gua/renderer/PLODUberShader.hpp new file mode 100644 index 000000000..e128241a2 --- /dev/null +++ b/include/gua/renderer/PLODUberShader.hpp @@ -0,0 +1,175 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +#ifndef GUA_PLOD_UBER_SHADER_HPP +#define GUA_PLOD_UBER_SHADER_HPP + +// guacamole headers +#include +#include + +namespace gua { + +class PLODUberShader : public GeometryUberShader { + + public: + + enum pass { + depth_pass = 0, + accumulation_pass = 1, + normalization_pass = 2, + reconstruction_pass = 3 + }; + + enum drawing_state{ + pre_frame_state = 0, + pre_draw_state = 1, + draw_state = 2, + post_draw_state = 3, + post_frame_state = 4, + invalid_state = 99999 + }; + + public: + + PLODUberShader(); + ~PLODUberShader(); + + void create (std::set const& material_names); + + bool upload_to (RenderContext const& context) const; + + /*virtual*/ stage_mask const get_stage_mask() const; + + /*virtual*/ void preframe (RenderContext const& context) const; + + /*virtual*/ void predraw (RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const; + + /*virtual*/ void draw (RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const; + + /*virtual*/ void postdraw (RenderContext const& ctx, + std::string const& filename, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& /*frustum*/) const; + + /*virtual*/ void postframe (RenderContext const& context) const; + + private: //auxialiary methods + + //get shader strings + /////////////////// + std::string const depth_pass_vertex_shader() const; + std::string const depth_pass_fragment_shader() const; + + std::string const accumulation_pass_vertex_shader() const; + std::string const accumulation_pass_fragment_shader() const; + + std::string const normalization_pass_vertex_shader () const; + std::string const normalization_pass_fragment_shader () const; + + std::string const reconstruction_pass_vertex_shader () const; + std::string const reconstruction_pass_fragment_shader () const; + + std::string const default_plod_material_name() const; + + //buffer operations + char* GetMappedTempBufferPtr(RenderContext const& ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer const& buffer) const; + void UnmapTempBufferPtr(RenderContext const& ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer const& buffer) const; + void CopyTempToMainMemory(RenderContext const& ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer const& buffer) const; + + + + private: //member variables + + //FBOs: + ////////////////////////////////////////////////////////////////////////////////////// + //depth pass FBO & attachments + mutable std::vector depth_pass_log_depth_result_; + mutable std::vector depth_pass_linear_depth_result_; + + mutable std::vector depth_pass_result_fbo_; + + //accumulation pass FBO & attachments + mutable std::vector accumulation_pass_color_result_; + mutable std::vector accumulation_pass_result_fbo_; + + //normalization pass FBO & attachments + mutable std::vector normalization_pass_color_result_; + mutable std::vector normalization_pass_result_fbo_; + + //temp buffer front & back + ////////////////////////////////////////////////////////////////////////////////////// + mutable std::vector temp_buffer_A_; + mutable std::vector temp_buffer_B_; + + mutable std::vector render_buffer_; + + mutable std::vector vertex_array_; + + mutable std::vector temp_buffer_A_is_mapped_; + mutable std::vector temp_buffer_B_is_mapped_; + mutable std::vector mapped_temp_buffer_A_; + mutable std::vector mapped_temp_buffer_B_; + mutable std::vector previous_framecount_; + + //schism-GL states: + ////////////////////////////////////////////////////////////////////////////////////// + mutable std::vector change_point_size_in_shader_state_; + + mutable std::vector linear_sampler_state_; + mutable std::vector no_depth_test_depth_stencil_state_; + + mutable std::vector color_accumulation_state_; + + //frustum dependent variables: + ///////////////////////////////////////////////////////////////////////////////////// + mutable std::vector near_plane_value_; + mutable std::vector height_divided_by_top_minus_bottom_; + + //misc: + //////////////////////////////////////////////////////////////////////////////////// + mutable std::vector material_id_; + mutable std::vector fullscreen_quad_; + mutable std::vector render_window_dims_; + + mutable std::vector last_geometry_state_; + + + //////////////////////////////////////////////////////////////////////////////////// + mutable std::vector> context_guard_; + +}; + +} + +#endif // GUA_PLOD_UBER_SHADER_HPP diff --git a/include/gua/scenegraph/PLODNode.hpp b/include/gua/scenegraph/PLODNode.hpp new file mode 100644 index 000000000..96ea04def --- /dev/null +++ b/include/gua/scenegraph/PLODNode.hpp @@ -0,0 +1,61 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +#ifndef GUA_PLOD_NODE_HPP +#define GUA_PLOD_NODE_HPP + +// guacamole headers +#include + +namespace gua { + +/** + * This class is used to represent pointcloud in the SceneGraph. + * + * \ingroup gua_scenegraph + */ +class GUA_DLL PLODNode : public GeometryNode +{ +public : // member + + PLODNode(std::string const& name, + std::string const& filename = "gua_default_geometry", + std::string const& material = "gua_default_material", + math::mat4 const& transform = math::mat4::identity()); + + /** + * Implements ray picking for a point cloud + */ + /* virtual */ void ray_test_impl(RayNode const& ray, + PickResult::Options options, + Mask const& mask, + std::set& hits); +protected: + + /*virtual*/ std::shared_ptr copy() const; + +private : // attributes e.g. special attributes for drawing + +}; + +} + +#endif // GUA_PLOD_NODE_HPP diff --git a/src/gua/renderer/PLODLoader.cpp b/src/gua/renderer/PLODLoader.cpp new file mode 100644 index 000000000..c63a83601 --- /dev/null +++ b/src/gua/renderer/PLODLoader.cpp @@ -0,0 +1,93 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +// class header +#include +#include +#include +#include + +// guacamole headers +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace gua { + +unsigned PLODLoader::model_counter_ = 0; + + ///////////////////////////////////////////////////////////////////////////// + + PLODLoader::PLODLoader() + : node_counter_(0) {} + + ///////////////////////////////////////////////////////////////////////////// + +std::shared_ptr PLODLoader::load(std::string const& file_name, + unsigned flags) { + + std::string model_name("type=file&file=" + file_name); + + auto node(std::make_shared(model_name)); + node->set_filename(model_name); + node->set_material(""); + + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + + // load point cloud + pbr::model_t model_id = database->AddModel(file_name, model_name); + const pbr::ren::LodPointCloud* point_cloud = database->GetModel(model_id); + + GeometryDatabase::instance()->add(model_name, std::make_shared(point_cloud)); + + ++model_counter_; + + return node; + +} + + ///////////////////////////////////////////////////////////////////////////// + +std::vector const PLODLoader::load_from_buffer(char const* buffer_name, + unsigned buffer_size, + bool build_kd_tree) { + + return std::vector(); +} + +bool PLODLoader::is_supported(std::string const& file_name) const { + auto point_pos(file_name.find_last_of(".")); + + return file_name.substr(point_pos + 1) == "kdn"; +} + +} diff --git a/src/gua/renderer/PLODRessource.cpp b/src/gua/renderer/PLODRessource.cpp new file mode 100644 index 000000000..2e77b00f6 --- /dev/null +++ b/src/gua/renderer/PLODRessource.cpp @@ -0,0 +1,151 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +// class header +#include + +// guacamole headers +#include +#include +#include +#include + +// external headers + +#include + + +namespace gua { + +//////////////////////////////////////////////////////////////////////////////// + +PLODRessource::PLODRessource() + : upload_mutex_() {} + +//////////////////////////////////////////////////////////////////////////////// + +PLODRessource::PLODRessource(const pbr::ren::LodPointCloud* point_cloud) + : upload_mutex_() { + + + //set already created BB + scm::gl::boxf loaded_bb = point_cloud->kdn_tree()->bounding_boxes()[0]; + bounding_box_.min = loaded_bb.min_vertex(); + bounding_box_.max = loaded_bb.max_vertex(); +} + +//////////////////////////////////////////////////////////////////////////////// + +void PLODRessource::upload_to(RenderContext const& ctx) const { +/* + + if (!point_cloud_->is_loaded()) { + Logger::LOG_WARNING << "Point Cloud was not loaded!" << std::endl; + return; + } + + std::unique_lock lock(upload_mutex_); + + if (buffers_.size() <= ctx.id) { + buffers_.resize(ctx.id + 1); + vertex_array_.resize(ctx.id + 1); + } + + + buffers_[ctx.id] = + ctx.render_device->create_buffer(scm::gl::BIND_VERTEX_BUFFER, + scm::gl::USAGE_STATIC_DRAW, + point_cloud_->num_surfels() * sizeof(pbr::ren::RawPointCloud::SerializedSurfel), + &(point_cloud_->data()[0])); + + + std::vector buffer_arrays; + buffer_arrays.push_back(buffers_[ctx.id]); + + + vertex_array_[ctx.id] = ctx.render_device->create_vertex_array( + scm::gl::vertex_format( + 0, 0, scm::gl::TYPE_VEC3F, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 1, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 2, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 3, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 4, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 5, scm::gl::TYPE_FLOAT, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 6, scm::gl::TYPE_VEC3F, sizeof(pbr::ren::RawPointCloud::SerializedSurfel)), + buffer_arrays); +*/ +} + +//////////////////////////////////////////////////////////////////////////////// +void PLODRessource::draw(RenderContext const& ctx) const +{/*dummy*/} +//////////////////////////////////////////////////////////////////////////////// + +void PLODRessource::draw(RenderContext const& ctx, pbr::context_t context_id, pbr::view_t view_id, pbr::model_t model_id, scm::gl::vertex_array_ptr const& vertex_array) const +{ + + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); + + pbr::ren::Cut& cut = cuts->GetCut(context_id, view_id, model_id); + std::vector node_list = cut.complete_set(); + const pbr::ren::KdnTree* kdn_tree = database->GetModel(model_id)->kdn_tree(); + + uint32_t surfels_per_node = database->surfels_per_node(); + uint32_t surfels_per_node_of_model = kdn_tree->surfels_per_node(); + + + //if (buffers_.size() <= ctx.id || buffers_[ctx.id] == nullptr) { + // upload_to(ctx); + //} + + scm::gl::context_vertex_input_guard vig(ctx.render_context); + + ctx.render_context->bind_vertex_array(vertex_array); + + ctx.render_context->apply(); + + pbr::node_t node_counter = 0; + + for(std::vector::const_iterator k = node_list.begin(); k != node_list.end(); ++k, ++node_counter) + { + ctx.render_context->draw_arrays(scm::gl::PRIMITIVE_POINT_LIST, (k->slot_id_) * surfels_per_node, surfels_per_node_of_model); + + } + +} + +//////////////////////////////////////////////////////////////////////////////// + +void PLODRessource::ray_test(Ray const& ray, PickResult::Options options, + Node* owner, std::set& hits) { + + return; +} + + +//////////////////////////////////////////////////////////////////////////////// + +/*virtual*/ GeometryUberShader* PLODRessource::get_ubershader() const { + return Singleton::instance(); +} + +} diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp new file mode 100644 index 000000000..87309454f --- /dev/null +++ b/src/gua/renderer/PLODUberShader.cpp @@ -0,0 +1,1061 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +// class header +#include + +// guacamole headers +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include + +#include + +#include + +namespace gua { + + +//////////////////////////////////////////////////////////////////////////////// + +PLODUberShader::PLODUberShader() + : GeometryUberShader(), near_plane_value_(0.0f), height_divided_by_top_minus_bottom_(0.0f) +{ + if (!MaterialDatabase::instance()->is_supported(default_plod_material_name())) + { + create_resource_material(default_plod_material_name(), + Resources::materials_gua_pbr_gsd, + Resources::materials_gua_pbr_gmd); + } +} + + + //////////////////////////////////////////////////////////////////////////////// + + void PLODUberShader::create(std::set const& material_names) + { + UberShader::create(material_names); + + // create depth pass shader + std::vector depth_pass_stages; + depth_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_VERTEX_SHADER, depth_pass_vertex_shader())); + depth_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_FRAGMENT_SHADER, depth_pass_fragment_shader())); + + auto depth_pass_program = std::make_shared(); + depth_pass_program->set_shaders(depth_pass_stages); + add_program(depth_pass_program); + + // create accumulation pass shader + std::vector accumulation_pass_stages; + accumulation_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_VERTEX_SHADER, accumulation_pass_vertex_shader())); + accumulation_pass_stages.push_back( ShaderProgramStage( scm::gl::STAGE_FRAGMENT_SHADER, accumulation_pass_fragment_shader())); + + auto accumulation_pass_program = std::make_shared(); + accumulation_pass_program->set_shaders(accumulation_pass_stages); + add_program(accumulation_pass_program); + + // create normalization pass shader + std::vector normalization_pass_stages; + normalization_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_VERTEX_SHADER, normalization_pass_vertex_shader())); + normalization_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_FRAGMENT_SHADER, normalization_pass_fragment_shader())); + + auto normalization_pass_program = std::make_shared(); + normalization_pass_program->set_shaders(normalization_pass_stages); + add_program(normalization_pass_program); + + // create reconstruction pass shader + std::vector reconstruction_pass_stages; + reconstruction_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_VERTEX_SHADER, reconstruction_pass_vertex_shader())); + reconstruction_pass_stages.push_back(ShaderProgramStage(scm::gl::STAGE_FRAGMENT_SHADER, reconstruction_pass_fragment_shader())); + + auto reconstruction_pass_program = std::make_shared(); + reconstruction_pass_program->set_shaders(reconstruction_pass_stages); + add_program(reconstruction_pass_program); + } + + + +//////////////////////////////////////////////////////////////////////////////// + +PLODUberShader::~PLODUberShader() +{ + std::cout << "memory cleanup...(1)" << std::endl; + delete pbr::ren::Controller::GetInstance(); + std::cout << "deleted controller" << std::endl; + delete pbr::ren::ModelDatabase::GetInstance(); + std::cout << "deleted model database" << std::endl; + delete pbr::ren::MemoryQuery::GetInstance(); + std::cout << "deleted memory query" << std::endl; + delete pbr::ren::CutDatabase::GetInstance(); + std::cout << "deleted cut database" << std::endl; +} + + +//////////////////////////////////////////////////////////////////////////////// + +std::string const PLODUberShader::depth_pass_vertex_shader() const +{ + + std::string vertex_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p01_depth_vert) + ); + + return vertex_shader; +} + + + +//////////////////////////////////////////////////////////////////////////////// + +std::string const PLODUberShader::depth_pass_fragment_shader() const +{ + + std::string fragment_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p01_depth_frag) + ); + + return fragment_shader; +} + + + +//////////////////////////////////////////////////////////////////////////////// + + + +std::string const PLODUberShader::accumulation_pass_vertex_shader() const +{ + std::string vertex_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p02_accumulation_vert) + ); + return vertex_shader; +} + + + +//////////////////////////////////////////////////////////////////////////////// + +std::string const PLODUberShader::accumulation_pass_fragment_shader() const +{ + std::string fragment_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p02_accumulation_frag) + ); + + return fragment_shader; +} + + +//////////////////////////////////////////////////////////////////////////////// + + + +std::string const PLODUberShader::normalization_pass_vertex_shader() const +{ + std::string vertex_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p03_normalization_vert) + ); + + return vertex_shader; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::string const PLODUberShader::normalization_pass_fragment_shader() const +{ + std::string fragment_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p03_normalization_frag) + ); + + return fragment_shader; +} + +//////////////////////////////////////////////////////////////////////////////// + + + +std::string const PLODUberShader::reconstruction_pass_vertex_shader() const +{ + std::string vertex_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p04_reconstruction_vert) + ); + + + // material specific uniforms + string_utils::replace(vertex_shader, "@uniform_definition", + get_uniform_mapping()->get_uniform_definition()); + + + // output + string_utils::replace(vertex_shader, "@output_definition", + vshader_factory_->get_output_mapping().get_gbuffer_output_definition(false, true)); + + return vertex_shader; +} + +//////////////////////////////////////////////////////////////////////////////// + +std::string const PLODUberShader::reconstruction_pass_fragment_shader() const +{ + std::string fragment_shader( + Resources::lookup_shader(Resources::shaders_uber_shaders_gbuffer_pbr_p04_reconstruction_frag) + ); + + std::string apply_pbr_color = fshader_factory_->get_output_mapping().get_output_string("gua_pbr", "gua_pbr_output_color"); + apply_pbr_color += " = output_color;\n"; + + std::string apply_pbr_normal = fshader_factory_->get_output_mapping().get_output_string("gua_pbr", "gua_normal"); + + apply_pbr_normal += " = output_normal;\n"; + + string_utils::replace(fragment_shader, "@apply_pbr_color", apply_pbr_color); + string_utils::replace(fragment_shader, "@apply_pbr_normal", apply_pbr_normal); + + // input from vertex shader + string_utils::replace(fragment_shader, "@input_definition", + vshader_factory_->get_output_mapping().get_gbuffer_output_definition(true, true)); + + // material specific uniforms + string_utils::replace(fragment_shader, "@uniform_definition", + get_uniform_mapping()->get_uniform_definition()); + + // outputs + string_utils::replace(fragment_shader, "@output_definition", + get_gbuffer_mapping()->get_gbuffer_output_definition(false, false)); + + + return fragment_shader; +} + + + //////////////////////////////////////////////////////////////////////////////// + +bool PLODUberShader::upload_to (RenderContext const& context) const +{ + bool upload_succeeded = UberShader::upload_to(context); + + assert(context.render_window->config.get_stereo_mode() == StereoMode::MONO || + ((context.render_window->config.get_left_resolution()[0] == context.render_window->config.get_right_resolution()[0]) && + (context.render_window->config.get_left_resolution()[1] == context.render_window->config.get_right_resolution()[1]))); + + + + // initialize attachments for depth pass + if (context.id >= depth_pass_log_depth_result_.size()) + { + + depth_pass_log_depth_result_.resize(context.id + 1); + + + depth_pass_log_depth_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_D32, + 1, + 1, + 1 + ); + } + + if (context.id >= depth_pass_linear_depth_result_.size()) + { + + depth_pass_linear_depth_result_.resize(context.id + 1); + + + depth_pass_linear_depth_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_R_32F, + 1, + 1, + 1 + ); + } + + // initialize attachments for accumulation pass + if (context.id >= accumulation_pass_color_result_.size()) + { + accumulation_pass_color_result_.resize(context.id + 1); + + + accumulation_pass_color_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_RGBA_32F, + 1, + 1, + 1 + ); + + } + + // initialize attachment for normalization pass + if (context.id >= normalization_pass_color_result_.size()) + { + + normalization_pass_color_result_.resize(context.id + 1); + + + normalization_pass_color_result_[context.id] = context.render_device->create_texture_2d( + context.render_window->config.get_left_resolution(), + scm::gl::FORMAT_RGB_8, + 1, + 1, + 1 + ); + } + + + + if (context.id >= fullscreen_quad_.size()) { + fullscreen_quad_.resize(context.id + 1); + fullscreen_quad_[context.id].reset(new scm::gl::quad_geometry(context.render_device, math::vec2(-1.f, -1.f), math::vec2(1.f, 1.f))); + } + + if (context.id >= depth_pass_result_fbo_.size()) { + depth_pass_result_fbo_.resize(context.id + 1); + depth_pass_result_fbo_[context.id] = context.render_device->create_frame_buffer(); + + // configure depth FBO + depth_pass_result_fbo_[context.id]->clear_attachments(); + depth_pass_result_fbo_[context.id]->attach_depth_stencil_buffer(depth_pass_log_depth_result_[context.id]); + depth_pass_result_fbo_[context.id]->attach_color_buffer(0, depth_pass_linear_depth_result_[context.id]); + } + + if (context.id >= accumulation_pass_result_fbo_.size()) { + accumulation_pass_result_fbo_.resize(context.id + 1); + accumulation_pass_result_fbo_[context.id] = context.render_device->create_frame_buffer(); + + // configure accumulation FBO + accumulation_pass_result_fbo_[context.id]->clear_attachments(); + accumulation_pass_result_fbo_[context.id]->attach_color_buffer(0, accumulation_pass_color_result_[context.id]); + } + + if (context.id >= normalization_pass_result_fbo_.size()) { + normalization_pass_result_fbo_.resize(context.id + 1); + normalization_pass_result_fbo_[context.id] = context.render_device->create_frame_buffer(); + + // configure normalization FBO + normalization_pass_result_fbo_[context.id]->clear_attachments(); + normalization_pass_result_fbo_[context.id]->attach_color_buffer(0, normalization_pass_color_result_[context.id]); + } + + if (context.id >= linear_sampler_state_.size()) { + linear_sampler_state_.resize(context.id + 1); + linear_sampler_state_[context.id] = context.render_device->create_sampler_state(scm::gl::FILTER_MIN_MAG_LINEAR, scm::gl::WRAP_CLAMP_TO_EDGE); + } + + if (context.id >= no_depth_test_depth_stencil_state_.size()) { + no_depth_test_depth_stencil_state_.resize(context.id + 1); + no_depth_test_depth_stencil_state_[context.id] = context.render_device->create_depth_stencil_state(false, false, scm::gl::COMPARISON_ALWAYS); + } + + if (context.id >= color_accumulation_state_.size()) { + color_accumulation_state_.resize(context.id + 1); + color_accumulation_state_[context.id] = context.render_device->create_blend_state(true, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::FUNC_ONE, scm::gl::EQ_FUNC_ADD, scm::gl::EQ_FUNC_ADD); + } + + + if (context.id >= change_point_size_in_shader_state_.size()){ + change_point_size_in_shader_state_.resize(context.id + 1); + change_point_size_in_shader_state_[context.id] = context.render_device->create_rasterizer_state(scm::gl::FILL_SOLID, scm::gl::CULL_NONE, scm::gl::ORIENT_CCW, false, false, 0.0, false, false, scm::gl::point_raster_state(true)); + } + + if (context.id >= material_id_.size()){ + material_id_.resize(context.id + 1); + material_id_[context.id] = 0; + } + + if (context.id >= near_plane_value_.size()){ + near_plane_value_.resize(context.id + 1); + near_plane_value_[context.id] = 0; + } + + if (context.id >= height_divided_by_top_minus_bottom_.size()){ + height_divided_by_top_minus_bottom_.resize(context.id + 1); + height_divided_by_top_minus_bottom_[context.id] = 0; + } + + if (context.id >= render_window_dims_.size()){ + render_window_dims_.resize(context.id + 1); + render_window_dims_[context.id] = context.render_window->config.get_left_resolution(); + } + + if (context.id >= context_guard_.size()){ + context_guard_.resize(context.id + 1); + } + + if (context.id >= last_geometry_state_.size()) + { + last_geometry_state_.resize(context.id + 1); + last_geometry_state_[context.id] = invalid_state; + } + + + if (context.id >= temp_buffer_A_.size()) + { + + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + pbr::ren::MemoryQuery* memory_query = pbr::ren::MemoryQuery::GetInstance(); + + size_t size_of_node_in_bytes = database->size_of_surfel()*database->surfels_per_node(); + + int32_t upload_budget_in_nodes = + (memory_query->upload_budget_in_mb()*1024*1024) / size_of_node_in_bytes; + + + unsigned long long sizeOfNode = database->surfels_per_node() *database->size_of_surfel(); + unsigned long long sizeOfTempBuffers = upload_budget_in_nodes * sizeOfNode; + + temp_buffer_A_.resize(context.id + 1); + temp_buffer_A_[context.id] = context.render_device->create_buffer(scm::gl::BIND_VERTEX_BUFFER, scm::gl::USAGE_DYNAMIC_COPY, sizeOfTempBuffers, NULL); + } + + + if (context.id >= temp_buffer_B_.size()) + { + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + pbr::ren::MemoryQuery* memory_query = pbr::ren::MemoryQuery::GetInstance(); + + size_t size_of_node_in_bytes = database->size_of_surfel()*database->surfels_per_node(); + + int32_t upload_budget_in_nodes = + (memory_query->upload_budget_in_mb()*1024*1024) / size_of_node_in_bytes; + + + unsigned long long sizeOfNode = database->surfels_per_node() *database->size_of_surfel(); + unsigned long long sizeOfTempBuffers = upload_budget_in_nodes * sizeOfNode; + + temp_buffer_B_.resize(context.id + 1); + temp_buffer_B_[context.id] = context.render_device->create_buffer(scm::gl::BIND_VERTEX_BUFFER, scm::gl::USAGE_DYNAMIC_COPY, sizeOfTempBuffers, NULL); + } + + if (context.id >= render_buffer_.size()) + { + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + pbr::ren::MemoryQuery* memory_query = pbr::ren::MemoryQuery::GetInstance(); + + size_t size_of_node_in_bytes = database->size_of_surfel()*database->surfels_per_node(); + + int32_t render_budget_in_nodes = + (memory_query->render_budget_in_mb()*1024*1024) / size_of_node_in_bytes; + + unsigned long long sizeOfNode = database->surfels_per_node() *database->size_of_surfel(); + unsigned long long sizeOfRenderBuffer = render_budget_in_nodes * sizeOfNode; + + + + render_buffer_.resize(context.id + 1); + render_buffer_[context.id] = context.render_device->create_buffer(scm::gl::BIND_VERTEX_BUFFER, scm::gl::USAGE_DYNAMIC_COPY, sizeOfRenderBuffer); + } + + + if (context.id >= vertex_array_.size()) + { + + + vertex_array_.resize(context.id + 1); + + vertex_array_[context.id] = context.render_device->create_vertex_array( + scm::gl::vertex_format( + 0, 0, scm::gl::TYPE_VEC3F, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 1, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 2, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 3, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 4, scm::gl::TYPE_UBYTE, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 5, scm::gl::TYPE_FLOAT, sizeof(pbr::ren::RawPointCloud::SerializedSurfel))( + 0, 6, scm::gl::TYPE_VEC3F, sizeof(pbr::ren::RawPointCloud::SerializedSurfel)), + boost::assign::list_of(render_buffer_[context.id])); + + } + + if (context.id >= temp_buffer_A_is_mapped_.size()) + { + temp_buffer_A_is_mapped_.resize(context.id + 1); + temp_buffer_A_is_mapped_[context.id] = false; + } + + if (context.id >= temp_buffer_B_is_mapped_.size()) + { + temp_buffer_B_is_mapped_.resize(context.id + 1); + temp_buffer_B_is_mapped_[context.id] = false; + } + + + if (context.id >= mapped_temp_buffer_A_.size()) + { + mapped_temp_buffer_A_.resize(context.id + 1); + mapped_temp_buffer_A_[context.id] = nullptr; + } + + if (context.id >= mapped_temp_buffer_B_.size()) + { + mapped_temp_buffer_B_.resize(context.id + 1); + mapped_temp_buffer_B_[context.id] = nullptr; + } + + if (context.id >= previous_framecount_.size()) + { + previous_framecount_.resize(context.id + 1); + previous_framecount_[context.id] = -1; + } + + + return upload_succeeded; +} + + + + + + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ GeometryUberShader::stage_mask const PLODUberShader::get_stage_mask() const + { + + return GeometryUberShader::PRE_FRAME_STAGE | GeometryUberShader::PRE_DRAW_STAGE | GeometryUberShader::POST_DRAW_STAGE | GeometryUberShader::POST_FRAME_STAGE; + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PLODUberShader::preframe(RenderContext const& ctx) const + { + //std::cout<<"Before upload to\n"; + upload_to(ctx); + + //std::cout<<"Starting preframe\n"; + if (previous_framecount_[ctx.id] != ctx.framecount) + { + + previous_framecount_[ctx.id] = ctx.framecount; + + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); + pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); + + pbr::context_t context_id = controller->DeduceContextId(ctx.id); + + cuts->AcceptFront(context_id); + + + if (ctx.framecount > 0) + { + controller->DispatchCutUpdate(context_id); + } + else + { + controller->StoreTemporaryBuffers( + context_id, + GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A), + GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_B)); + + database->set_window_width(render_window_dims_[ctx.id][0]); + database->set_window_height(render_window_dims_[ctx.id][1]); + } + + if (cuts->IsFrontModified(context_id)) + { + UnmapTempBufferPtr(ctx, cuts->GetBuffer(context_id)); + + CopyTempToMainMemory(ctx, cuts->GetBuffer(context_id)); + + cuts->SignalModificationComplete(context_id); + + controller->StoreTemporaryBuffers( + context_id, + GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A), + GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_B)); + + } + + } + + + + + //scm::gl::context_all_guard guard(ctx.render_context); + + + last_geometry_state_[ctx.id] = pre_frame_state; + + + context_guard_[ctx.id] = std::make_shared(ctx.render_context); + + + + { + + // clear depth FBOs depth attachments + + ctx.render_context->clear_depth_stencil_buffer(depth_pass_result_fbo_[ctx.id]); + ctx.render_context->clear_color_buffer(depth_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f, 0.0f)); + + + + // clear accumulation FBOs color attachment + + ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f,0.0f)); + + + // clear normalization FBOs color attachment + ctx.render_context->clear_color_buffer(normalization_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 0.0f, 0.0f,0.0f)); + + } + + + + + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PLODUberShader::predraw(RenderContext const& ctx, + std::string const& file_name, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& frustum) const + { + pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); + pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); + pbr::context_t context_id = controller->DeduceContextId(ctx.id); + pbr::view_t view_id = controller->DeduceViewId(context_id, 0); + cuts->SendCamera(context_id, view_id, pbr::ren::Camera(view_id, frustum.get_view(), frustum.get_projection() )); + + + auto plod_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); + auto material = MaterialDatabase::instance()->lookup(material_name); + + // begin of depth pass (first) + { + + if( last_geometry_state_[ctx.id] != pre_draw_state) + { + + //enable dynamic point size in shaders + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + + + // bind fbo + ctx.render_context->set_frame_buffer(depth_pass_result_fbo_[ctx.id]); + + + + gua::math::mat4 const& projection_matrix = frustum.get_projection(); + + float near_plane_value = frustum.get_clip_near(); + float far_plane_value = frustum.get_clip_far(); + float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; + float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; + + float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); + + get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); + get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + get_program(depth_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); + + last_geometry_state_[ctx.id] = pre_draw_state; + + + } + + + scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); + + float radius_model_scaling = scm::math::length(model_matrix * x_unit_vec); + + float modelViewProjectionScalingRatio = scm::math::length(frustum.get_projection()*frustum.get_view()*model_matrix*x_unit_vec) + / 0.8759124279022216797; + + + get_program(depth_pass)->set_uniform(ctx, radius_model_scaling, "radius_model_scaling"); + get_program(depth_pass)->set_uniform(ctx, modelViewProjectionScalingRatio, "mVPScalingRatio"); + + get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + + pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + pbr::context_t context_id = controller->DeduceContextId(ctx.id); + pbr::model_t model_id = controller->DeduceModelId(file_name); + pbr::view_t view_id = controller->DeduceViewId(context_id, 0); //only one view + + if (material && plod_ressource) + { + get_program(depth_pass)->use(ctx); + { + plod_ressource->draw(ctx, context_id, view_id, model_id, vertex_array_[ctx.id]); + } + get_program(depth_pass)->unuse(ctx); + } + + + } + + } + + + +//////////////////////////////////////////////////////////////////////////////// + +void PLODUberShader::draw(RenderContext const& ctx, + std::string const& file_name, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& frustum) const +{ + throw std::runtime_error("PLODUberShader::draw(): not implemented"); +} + + + + + + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PLODUberShader::postdraw(RenderContext const& ctx, + std::string const& file_name, + std::string const& material_name, + scm::math::mat4 const& model_matrix, + scm::math::mat4 const& normal_matrix, + Frustum const& frustum) const + { + + auto plod_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); + auto material = MaterialDatabase::instance()->lookup(material_name); + + { + + if( last_geometry_state_[ctx.id] != post_draw_state) + { + + ctx.render_context->reset(); + //enable dynamic point size in shaders + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + + //disable depth test + ctx.render_context->set_depth_stencil_state(no_depth_test_depth_stencil_state_[ctx.id]); + + //set blend state to accumulate + ctx.render_context->set_blend_state(color_accumulation_state_[ctx.id]); + + // ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 1.0f, 0.0f, 0.0f)); + // bind accumulation FBO + ctx.render_context->set_frame_buffer(accumulation_pass_result_fbo_[ctx.id]); + + gua::math::mat4 const& projection_matrix = frustum.get_projection(); + + //put near_plane_value and height_divided_by_top_minus_bottom as member variables and get these two only 1 per render frame + float near_plane_value = frustum.get_clip_near(); + float far_plane_value = frustum.get_clip_far(); + float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; + float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; + float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); + + get_program(accumulation_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); + get_program(accumulation_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + get_program(accumulation_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); + + get_program(accumulation_pass)->set_uniform(ctx, math::vec2(render_window_dims_[ctx.id]),"win_dims"); + + + last_geometry_state_[ctx.id] = post_draw_state; + + + } + + scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); + + float radius_model_scaling = scm::math::length(model_matrix * x_unit_vec); + + float modelViewProjectionScalingRatio = scm::math::length(frustum.get_projection()*frustum.get_view()*model_matrix*x_unit_vec) + / 0.8759124279022216797; + + + get_program(accumulation_pass)->set_uniform(ctx, radius_model_scaling, "radius_model_scaling"); + get_program(accumulation_pass)->set_uniform(ctx, modelViewProjectionScalingRatio, "mVPScalingRatio"); + + ctx.render_context->bind_texture(depth_pass_linear_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); + get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + + + get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + + if (material && plod_ressource) + { + + pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + pbr::context_t context_id = controller->DeduceContextId(ctx.id); + pbr::model_t model_id = controller->DeduceModelId(file_name); + pbr::view_t view_id = controller->DeduceViewId(context_id, 0); //only one view + + material_id_[ctx.id] = material->get_id(); + get_program(accumulation_pass)->use(ctx); + { + + plod_ressource->draw(ctx, context_id, view_id, model_id, vertex_array_[ctx.id]); + } + get_program(accumulation_pass)->unuse(ctx); + } + + } + + } + + //////////////////////////////////////////////////////////////////////////////// + + /*virtual*/ void PLODUberShader::postframe(RenderContext const& ctx) const + { + + + (context_guard_[ctx.id]).reset(); + + { + + + { + scm::gl::context_all_guard guard(ctx.render_context); + + + // bind normalization FBO + ctx.render_context->set_frame_buffer(normalization_pass_result_fbo_[ctx.id]); + + get_program(normalization_pass)->use(ctx); + { + + { + + get_program(normalization_pass)->set_uniform(ctx, 1 , "using_default_pbr_material"); + + + //bind color output for gbuffer + ctx.render_context->bind_texture(accumulation_pass_color_result_[ctx.id], linear_sampler_state_[ctx.id], 0); + get_program(normalization_pass)->get_program(ctx)->uniform_sampler("p02_color_texture", 0); + + fullscreen_quad_[ctx.id]->draw(ctx.render_context); + } + } + get_program(normalization_pass)->unuse(ctx); + + } + // ctx.render_context->reset_framebuffer(); + + + ///////////////////////////////////////////////////////////////////////////////////////////// + //insert screen space reconstruction pass here + + { + + scm::gl::context_all_guard guard(ctx.render_context); + + get_program(reconstruction_pass)->use(ctx); + { + + { + + get_program(reconstruction_pass)->set_uniform(ctx, material_id_[ctx.id], "gua_material_id"); + + + get_program(reconstruction_pass)->set_uniform(ctx, 1 , "using_default_pbr_material"); + + get_program(reconstruction_pass)->set_uniform(ctx, math::vec2(render_window_dims_[ctx.id]),"win_dims"); + + //bind logarithmic depth texture for gbuffer + ctx.render_context->bind_texture(depth_pass_log_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); + get_program(reconstruction_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); + + + //bind color output for gbuffer + ctx.render_context->bind_texture(normalization_pass_color_result_[ctx.id], linear_sampler_state_[ctx.id], 1); + get_program(reconstruction_pass)->get_program(ctx)->uniform_sampler("p02_color_texture", 1); + + fullscreen_quad_[ctx.id]->draw(ctx.render_context); + } + } + get_program(reconstruction_pass)->unuse(ctx); + + ctx.render_context->reset_framebuffer(); + + + } + } + + + + } + + +//////////////////////////////////////////////////////////////////////////////// + +char* PLODUberShader::GetMappedTempBufferPtr(RenderContext const& ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer const& buffer) const +{ + switch (buffer) + { + case pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A: + if (!temp_buffer_A_is_mapped_[ctx.id]) + { + mapped_temp_buffer_A_[ctx.id] = (char*)ctx.render_context->map_buffer(temp_buffer_A_[ctx.id], scm::gl::ACCESS_READ_WRITE ); + temp_buffer_A_is_mapped_[ctx.id] = true; + } + return mapped_temp_buffer_A_[ctx.id]; + break; + + case pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_B: + if (!temp_buffer_B_is_mapped_[ctx.id]) + { + mapped_temp_buffer_B_[ctx.id] = (char*)ctx.render_context->map_buffer(temp_buffer_B_[ctx.id], scm::gl::ACCESS_READ_WRITE ); + temp_buffer_B_is_mapped_[ctx.id] = true; + } + return mapped_temp_buffer_B_[ctx.id]; + break; + + default: break; + } + + std::cout << "Failed to map temporary buffer.\n"; + assert(false); + + return nullptr; +} +//////////////////////////////////////////////////////////////////////////////// +void PLODUberShader::UnmapTempBufferPtr(RenderContext const& ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer const& buffer) const +{ + switch (buffer) + { + case pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A: + if (temp_buffer_A_is_mapped_[ctx.id]) + { + ctx.render_context->unmap_buffer(temp_buffer_A_[ctx.id]); + temp_buffer_A_is_mapped_[ctx.id] = false; + } + break; + + case pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_B: + if (temp_buffer_B_is_mapped_[ctx.id]) + { + ctx.render_context->unmap_buffer(temp_buffer_B_[ctx.id]); + temp_buffer_B_is_mapped_[ctx.id] = false; + } + break; + + default: break; + } + +} +//////////////////////////////////////////////////////////////////////////////// + +void PLODUberShader::CopyTempToMainMemory(RenderContext const& ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer const& buffer) const +{ + std::cout<<"uploading something.\n"; + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + + size_t size_of_node_in_bytes = database->surfels_per_node() * database->size_of_surfel(); + + + pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); + + //uploaded_nodes_ = 0; + + pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); + + pbr::context_t context_id = controller->DeduceContextId(ctx.id); //placeholder + + switch (buffer) + { + case pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A: + { + if (temp_buffer_A_is_mapped_[ctx.id]) + { + std::cout << "Failed to transfer nodes into main memory.\nTemp Storage A was still mapped."; + assert(false); + } + std::vector& transfer_descr_list = cuts->GetUpdatedSet(context_id/*, model_id*/); + + if (!transfer_descr_list.empty()) + { + std::cout<<"Update list was not empty\n"; + //uploaded_nodes_ += transfer_descr_list.size(); + + for (const auto& transfer_desc : transfer_descr_list) + { + size_t offset_in_temp_VBO = transfer_desc.src_ * size_of_node_in_bytes; + size_t offset_in_render_VBO = transfer_desc.dst_ * size_of_node_in_bytes; + ctx.render_context->copy_buffer_data(render_buffer_[ctx.id],temp_buffer_A_[ctx.id], offset_in_render_VBO, offset_in_temp_VBO, size_of_node_in_bytes); + } + } + break; + } + + case pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_B: + { + if (temp_buffer_B_is_mapped_[ctx.id]) + { + std::cout << "Failed to transfer nodes into main memory.\nTemp Storage B was still mapped."; + assert(false); + } + std::vector& transfer_descr_list = cuts->GetUpdatedSet(context_id/*, model_id*/); + if (!transfer_descr_list.empty()) + { + //uploaded_nodes_ += transfer_descr_list.size(); + + for (const auto& transfer_desc : transfer_descr_list) + { + size_t offset_in_temp_VBO = transfer_desc.src_ * size_of_node_in_bytes; + size_t offset_in_render_VBO = transfer_desc.dst_ * size_of_node_in_bytes; + ctx.render_context->copy_buffer_data(render_buffer_[ctx.id],temp_buffer_B_[ctx.id], offset_in_render_VBO, offset_in_temp_VBO, size_of_node_in_bytes); + } + } + + break; + } + + default: break; + + } + + +} + +//////////////////////////////////////////////////////////////////////////////// + + std::string const PLODUberShader::default_plod_material_name() const + { + return "gua_pbr"; + } + +} + diff --git a/src/gua/scenegraph/PLODNode.cpp b/src/gua/scenegraph/PLODNode.cpp new file mode 100644 index 000000000..93d23f695 --- /dev/null +++ b/src/gua/scenegraph/PLODNode.cpp @@ -0,0 +1,76 @@ +#/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +// class header +#include + +#include +#include +#include +#include + +// guacamole headers + +namespace gua { + + //////////////////////////////////////////////////////////////////////////////// + PLODNode::PLODNode(std::string const& name, + std::string const& filename, + std::string const& material, + math::mat4 const& transform) + : GeometryNode(name, filename, material, transform) + {} + + //////////////////////////////////////////////////////////////////////////////// + + void PLODNode::ray_test_impl(RayNode const& ray, PickResult::Options options, + Mask const& mask, std::set& hits) { + + // first of all, check bbox + auto box_hits(ray.intersect(bounding_box_)); + + // ray did not intersect bbox -- therefore it wont intersect + if (box_hits.first == RayNode::END && box_hits.second == RayNode::END) { + return; + } + + // return if only first object shall be returned and the current first hit + // is in front of the bbox entry point and the ray does not start inside + // the bbox + if (options & PickResult::PICK_ONLY_FIRST_OBJECT + && hits.size() > 0 && hits.begin()->distance < box_hits.first + && box_hits.first != Ray::END) { + + return; + } + + for (auto child : get_children()) { + // test for intersection with each child + child->ray_test_impl(ray, options, mask, hits); + } + + } + + //////////////////////////////////////////////////////////////////////////////// + std::shared_ptr PLODNode::copy() const { + return std::make_shared(get_name(), filename_, material_, get_transform()); + } +} From 6f53c55b476e881d97c4ace3c70f734ab2f05f23 Mon Sep 17 00:00:00 2001 From: feoo Date: Tue, 20 May 2014 00:17:42 +0200 Subject: [PATCH 25/45] lod rendering for point clouds, integrated cut update --- src/gua/renderer/GeometryLoader.cpp | 2 + src/gua/renderer/PLODUberShader.cpp | 116 +++++++++++++--------------- 2 files changed, 57 insertions(+), 61 deletions(-) diff --git a/src/gua/renderer/GeometryLoader.cpp b/src/gua/renderer/GeometryLoader.cpp index c10506c24..982c6592f 100644 --- a/src/gua/renderer/GeometryLoader.cpp +++ b/src/gua/renderer/GeometryLoader.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include // external headers @@ -53,6 +54,7 @@ GeometryLoader::GeometryLoader() : fileloaders_() { fileloaders_.push_back(new Video3DLoader); fileloaders_.push_back(new VolumeLoader); fileloaders_.push_back(new PBRLoader); + fileloaders_.push_back(new PLODLoader); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp index 87309454f..69a881dc2 100644 --- a/src/gua/renderer/PLODUberShader.cpp +++ b/src/gua/renderer/PLODUberShader.cpp @@ -550,55 +550,54 @@ bool PLODUberShader::upload_to (RenderContext const& context) const /*virtual*/ void PLODUberShader::preframe(RenderContext const& ctx) const { - //std::cout<<"Before upload to\n"; upload_to(ctx); - - //std::cout<<"Starting preframe\n"; - if (previous_framecount_[ctx.id] != ctx.framecount) - { - previous_framecount_[ctx.id] = ctx.framecount; - - pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); - pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); - pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); - - pbr::context_t context_id = controller->DeduceContextId(ctx.id); + if (previous_framecount_[ctx.id] != ctx.framecount) + { - cuts->AcceptFront(context_id); - + previous_framecount_[ctx.id] = ctx.framecount; + + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); + pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); + + pbr::context_t context_id = controller->DeduceContextId(ctx.id); - if (ctx.framecount > 0) - { - controller->DispatchCutUpdate(context_id); - } - else - { - controller->StoreTemporaryBuffers( - context_id, - GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A), - GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_B)); - - database->set_window_width(render_window_dims_[ctx.id][0]); - database->set_window_height(render_window_dims_[ctx.id][1]); - } - - if (cuts->IsFrontModified(context_id)) - { - UnmapTempBufferPtr(ctx, cuts->GetBuffer(context_id)); + + cuts->AcceptFront(context_id); + - CopyTempToMainMemory(ctx, cuts->GetBuffer(context_id)); + if (ctx.framecount > 0) + { + controller->DispatchCutUpdate(context_id); + } + else + { + controller->StoreTemporaryBuffers( + context_id, + GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A), + GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_B)); + + database->set_window_width(render_window_dims_[ctx.id][0]); + database->set_window_height(render_window_dims_[ctx.id][1]); + } + + if (cuts->IsFrontModified(context_id)) + { + UnmapTempBufferPtr(ctx, cuts->GetBuffer(context_id)); - cuts->SignalModificationComplete(context_id); + CopyTempToMainMemory(ctx, cuts->GetBuffer(context_id)); - controller->StoreTemporaryBuffers( - context_id, - GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A), - GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_B)); + cuts->SignalModificationComplete(context_id); - } - - } + controller->StoreTemporaryBuffers( + context_id, + GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A), + GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_B)); + + } + + } @@ -652,17 +651,16 @@ bool PLODUberShader::upload_to (RenderContext const& context) const pbr::view_t view_id = controller->DeduceViewId(context_id, 0); cuts->SendCamera(context_id, view_id, pbr::ren::Camera(view_id, frustum.get_view(), frustum.get_projection() )); - auto plod_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); auto material = MaterialDatabase::instance()->lookup(material_name); - + // begin of depth pass (first) { if( last_geometry_state_[ctx.id] != pre_draw_state) { - //enable dynamic point size in shaders + //enable dynamic point size in shaders ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); @@ -690,16 +688,15 @@ bool PLODUberShader::upload_to (RenderContext const& context) const } - scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); - - float radius_model_scaling = scm::math::length(model_matrix * x_unit_vec); + scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); - float modelViewProjectionScalingRatio = scm::math::length(frustum.get_projection()*frustum.get_view()*model_matrix*x_unit_vec) - / 0.8759124279022216797; + float radius_model_scaling = scm::math::length(model_matrix * x_unit_vec); + float modelViewProjectionScalingRatio = scm::math::length(frustum.get_projection()*frustum.get_view()*model_matrix*x_unit_vec) + / 0.8759124279022216797; - get_program(depth_pass)->set_uniform(ctx, radius_model_scaling, "radius_model_scaling"); - get_program(depth_pass)->set_uniform(ctx, modelViewProjectionScalingRatio, "mVPScalingRatio"); + get_program(depth_pass)->set_uniform(ctx, radius_model_scaling, "radius_model_scaling"); + get_program(depth_pass)->set_uniform(ctx, modelViewProjectionScalingRatio, "mVPScalingRatio"); get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); @@ -709,6 +706,8 @@ bool PLODUberShader::upload_to (RenderContext const& context) const pbr::context_t context_id = controller->DeduceContextId(ctx.id); pbr::model_t model_id = controller->DeduceModelId(file_name); pbr::view_t view_id = controller->DeduceViewId(context_id, 0); //only one view + + database->GetModel(model_id)->kdn_tree()->Transform(model_matrix); if (material && plod_ressource) { @@ -716,7 +715,7 @@ bool PLODUberShader::upload_to (RenderContext const& context) const { plod_ressource->draw(ctx, context_id, view_id, model_id, vertex_array_[ctx.id]); } - get_program(depth_pass)->unuse(ctx); + get_program(depth_pass)->unuse(ctx); } @@ -980,7 +979,7 @@ void PLODUberShader::UnmapTempBufferPtr(RenderContext const& ctx, pbr::ren::CutD void PLODUberShader::CopyTempToMainMemory(RenderContext const& ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer const& buffer) const { - std::cout<<"uploading something.\n"; + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); size_t size_of_node_in_bytes = database->surfels_per_node() * database->size_of_surfel(); @@ -992,7 +991,7 @@ void PLODUberShader::CopyTempToMainMemory(RenderContext const& ctx, pbr::ren::Cu pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); - pbr::context_t context_id = controller->DeduceContextId(ctx.id); //placeholder + pbr::context_t context_id = controller->DeduceContextId(ctx.id); switch (buffer) { @@ -1003,13 +1002,10 @@ void PLODUberShader::CopyTempToMainMemory(RenderContext const& ctx, pbr::ren::Cu std::cout << "Failed to transfer nodes into main memory.\nTemp Storage A was still mapped."; assert(false); } - std::vector& transfer_descr_list = cuts->GetUpdatedSet(context_id/*, model_id*/); + std::vector& transfer_descr_list = cuts->GetUpdatedSet(context_id); if (!transfer_descr_list.empty()) { - std::cout<<"Update list was not empty\n"; - //uploaded_nodes_ += transfer_descr_list.size(); - for (const auto& transfer_desc : transfer_descr_list) { size_t offset_in_temp_VBO = transfer_desc.src_ * size_of_node_in_bytes; @@ -1027,11 +1023,9 @@ void PLODUberShader::CopyTempToMainMemory(RenderContext const& ctx, pbr::ren::Cu std::cout << "Failed to transfer nodes into main memory.\nTemp Storage B was still mapped."; assert(false); } - std::vector& transfer_descr_list = cuts->GetUpdatedSet(context_id/*, model_id*/); + std::vector& transfer_descr_list = cuts->GetUpdatedSet(context_id); if (!transfer_descr_list.empty()) { - //uploaded_nodes_ += transfer_descr_list.size(); - for (const auto& transfer_desc : transfer_descr_list) { size_t offset_in_temp_VBO = transfer_desc.src_ * size_of_node_in_bytes; From 666cc9c6ceab104b35585aeb2563a3af69698a15 Mon Sep 17 00:00:00 2001 From: feoo Date: Tue, 20 May 2014 19:35:47 +0200 Subject: [PATCH 26/45] added point cloud transform (flip flop) --- src/gua/renderer/PLODUberShader.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp index 69a881dc2..c7ba34430 100644 --- a/src/gua/renderer/PLODUberShader.cpp +++ b/src/gua/renderer/PLODUberShader.cpp @@ -563,16 +563,19 @@ bool PLODUberShader::upload_to (RenderContext const& context) const pbr::context_t context_id = controller->DeduceContextId(ctx.id); - + //swap cut database cuts->AcceptFront(context_id); - if (ctx.framecount > 0) + if (temp_buffer_A_is_mapped_[ctx.id]) { + //run cut update controller->DispatchCutUpdate(context_id); } else { + //startup + controller->StoreTemporaryBuffers( context_id, GetMappedTempBufferPtr(ctx, pbr::ren::CutDatabaseRecord::TemporaryBuffer::BUFFER_A), @@ -584,6 +587,8 @@ bool PLODUberShader::upload_to (RenderContext const& context) const if (cuts->IsFrontModified(context_id)) { + //upload data to gpu + UnmapTempBufferPtr(ctx, cuts->GetBuffer(context_id)); CopyTempToMainMemory(ctx, cuts->GetBuffer(context_id)); @@ -645,12 +650,19 @@ bool PLODUberShader::upload_to (RenderContext const& context) const scm::math::mat4 const& normal_matrix, Frustum const& frustum) const { + pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); + pbr::context_t context_id = controller->DeduceContextId(ctx.id); pbr::view_t view_id = controller->DeduceViewId(context_id, 0); + pbr::model_t model_id = controller->DeduceModelId(file_name); + + //send camera and model_matrix to cut update cuts->SendCamera(context_id, view_id, pbr::ren::Camera(view_id, frustum.get_view(), frustum.get_projection() )); - + cuts->SendTransform(context_id, model_id, model_matrix); + + auto plod_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); auto material = MaterialDatabase::instance()->lookup(material_name); @@ -701,14 +713,6 @@ bool PLODUberShader::upload_to (RenderContext const& context) const get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); - pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); - pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); - pbr::context_t context_id = controller->DeduceContextId(ctx.id); - pbr::model_t model_id = controller->DeduceModelId(file_name); - pbr::view_t view_id = controller->DeduceViewId(context_id, 0); //only one view - - database->GetModel(model_id)->kdn_tree()->Transform(model_matrix); - if (material && plod_ressource) { get_program(depth_pass)->use(ctx); From 111f8f87c43c1c50c003a31b447764368fb49c23 Mon Sep 17 00:00:00 2001 From: Andre Schollmeyer Date: Wed, 21 May 2014 13:42:45 +0200 Subject: [PATCH 27/45] bugfixes in gbufferpass::draw_rays() and node::set_dirty() --- src/gua/renderer/GBufferPass.cpp | 2 +- src/gua/scenegraph/Node.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gua/renderer/GBufferPass.cpp b/src/gua/renderer/GBufferPass.cpp index 9122c2e7e..9471728ac 100644 --- a/src/gua/renderer/GBufferPass.cpp +++ b/src/gua/renderer/GBufferPass.cpp @@ -295,7 +295,7 @@ void GBufferPass::display_rays(RenderContext const& ctx, SerializedScene const& for (auto const& ray : scene.rays_) { meshubershader->draw(ctx, - "gua_plane_geometry", + "gua_ray_geometry", "gua_bounding_box", ray->get_cached_world_transform(), scm::math::inverse(ray->get_cached_world_transform()), diff --git a/src/gua/scenegraph/Node.cpp b/src/gua/scenegraph/Node.cpp index eaea881d9..e41912ce6 100644 --- a/src/gua/scenegraph/Node.cpp +++ b/src/gua/scenegraph/Node.cpp @@ -359,7 +359,6 @@ namespace gua { //////////////////////////////////////////////////////////////////////////////// void Node::set_dirty() const { - self_dirty_ = true; set_children_dirty(); set_parent_dirty(); } @@ -380,6 +379,7 @@ namespace gua { self_dirty_ = true; child_dirty_ = true; } + for (auto child : children_) { child->set_children_dirty(); } From fd7484e146422db427e4a7be54b7207d38c61dc4 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Wed, 21 May 2014 14:58:34 +0200 Subject: [PATCH 28/45] implement frustum culling --- include/gua/renderer/PLODRessource.hpp | 6 ++-- include/gua/renderer/PLODUberShader.hpp | 2 ++ src/gua/renderer/PLODRessource.cpp | 32 +++++++++---------- src/gua/renderer/PLODUberShader.cpp | 41 ++++++++++++++++++++++--- 4 files changed, 55 insertions(+), 26 deletions(-) diff --git a/include/gua/renderer/PLODRessource.hpp b/include/gua/renderer/PLODRessource.hpp index 446564e54..c09740e69 100644 --- a/include/gua/renderer/PLODRessource.hpp +++ b/include/gua/renderer/PLODRessource.hpp @@ -86,7 +86,7 @@ class PLODRessource : public GeometryRessource { */ void draw(RenderContext const& ctx) const; - void draw(RenderContext const& ctx, pbr::context_t context_id, pbr::view_t view_id, pbr::model_t model_id, scm::gl::vertex_array_ptr const& vertex_array) const; + void draw(RenderContext const& ctx, pbr::context_t context_id, pbr::view_t view_id, pbr::model_t model_id, scm::gl::vertex_array_ptr const& vertex_array, std::vector const& frustum_culling_results) const; void ray_test(Ray const& ray, PickResult::Options options, Node* owner, std::set& hits); @@ -97,9 +97,7 @@ class PLODRessource : public GeometryRessource { private: void upload_to(RenderContext const& context) const; -/* - mutable std::vector buffers_; - mutable std::vector vertex_array_;*/ + mutable std::mutex upload_mutex_; public: diff --git a/include/gua/renderer/PLODUberShader.hpp b/include/gua/renderer/PLODUberShader.hpp index 1378bcfdb..1359c1ec9 100644 --- a/include/gua/renderer/PLODUberShader.hpp +++ b/include/gua/renderer/PLODUberShader.hpp @@ -159,6 +159,8 @@ class PLODUberShader : public GeometryUberShader { mutable std::vector near_plane_value_; mutable std::vector height_divided_by_top_minus_bottom_; + mutable std::vector > frustum_culling_results_; + //misc: //////////////////////////////////////////////////////////////////////////////////// mutable std::vector material_id_; diff --git a/src/gua/renderer/PLODRessource.cpp b/src/gua/renderer/PLODRessource.cpp index 2e77b00f6..fa70d79a1 100644 --- a/src/gua/renderer/PLODRessource.cpp +++ b/src/gua/renderer/PLODRessource.cpp @@ -99,38 +99,36 @@ void PLODRessource::draw(RenderContext const& ctx) const {/*dummy*/} //////////////////////////////////////////////////////////////////////////////// -void PLODRessource::draw(RenderContext const& ctx, pbr::context_t context_id, pbr::view_t view_id, pbr::model_t model_id, scm::gl::vertex_array_ptr const& vertex_array) const +void PLODRessource::draw(RenderContext const& ctx, pbr::context_t context_id, pbr::view_t view_id, pbr::model_t model_id, scm::gl::vertex_array_ptr const& vertex_array, std::vector const& culling_results) const { pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); pbr::ren::Cut& cut = cuts->GetCut(context_id, view_id, model_id); - std::vector node_list = cut.complete_set(); - const pbr::ren::KdnTree* kdn_tree = database->GetModel(model_id)->kdn_tree(); + std::vector& node_list = cut.complete_set(); + pbr::ren::KdnTree const * kdn_tree = database->GetModel(model_id)->kdn_tree(); uint32_t surfels_per_node = database->surfels_per_node(); uint32_t surfels_per_node_of_model = kdn_tree->surfels_per_node(); + scm::gl::context_vertex_input_guard vig(ctx.render_context); - //if (buffers_.size() <= ctx.id || buffers_[ctx.id] == nullptr) { - // upload_to(ctx); - //} + ctx.render_context->bind_vertex_array(vertex_array); - scm::gl::context_vertex_input_guard vig(ctx.render_context); - - ctx.render_context->bind_vertex_array(vertex_array); - - ctx.render_context->apply(); + ctx.render_context->apply(); - pbr::node_t node_counter = 0; + pbr::node_t node_counter = 0; - for(std::vector::const_iterator k = node_list.begin(); k != node_list.end(); ++k, ++node_counter) - { - ctx.render_context->draw_arrays(scm::gl::PRIMITIVE_POINT_LIST, (k->slot_id_) * surfels_per_node, surfels_per_node_of_model); + for(std::vector::const_iterator k = node_list.begin(); k != node_list.end(); ++k, ++node_counter) + { + //0 = completely inside of frustum, 1 = completely outside of frustum, 2 = intersects frustum + if(culling_results[node_counter] != 1) + { + ctx.render_context->draw_arrays(scm::gl::PRIMITIVE_POINT_LIST, (k->slot_id_) * surfels_per_node, surfels_per_node_of_model); + } + } - } - } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp index 7c09bf3da..d24498546 100644 --- a/src/gua/renderer/PLODUberShader.cpp +++ b/src/gua/renderer/PLODUberShader.cpp @@ -529,6 +529,11 @@ bool PLODUberShader::upload_to (RenderContext const& context) const previous_framecount_[context.id] = -1; } + if(context.id >= frustum_culling_results_.size()) + { + frustum_culling_results_.resize(context.id + 1); + frustum_culling_results_[context.id].clear(); + } return upload_succeeded; } @@ -660,10 +665,37 @@ bool PLODUberShader::upload_to (RenderContext const& context) const pbr::model_t model_id = controller->DeduceModelId(file_name); //send camera and model_matrix to cut update - cuts->SendCamera(context_id, view_id, pbr::ren::Camera(view_id, frustum.get_view(), frustum.get_projection() )); + pbr::ren::Camera cut_update_cam(view_id, frustum.get_view(), frustum.get_projection() ); + + cuts->SendCamera(context_id, view_id, cut_update_cam); cuts->SendTransform(context_id, model_id, model_matrix); - + + //calculate frustum culling results + + pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); + + pbr::ren::KdnTree const* kdn_tree = database->GetModel(model_id)->kdn_tree(); + + scm::gl::frustum culling_frustum = cut_update_cam.GetFrustumByModel(model_matrix); + + std::vector const& model_bounding_boxes = kdn_tree->bounding_boxes(); + + pbr::ren::Cut& cut = cuts->GetCut(context_id, view_id, model_id); + std::vector& node_list = cut.complete_set(); + + unsigned int node_counter = 0; + + frustum_culling_results_[ctx.id].clear(); + frustum_culling_results_[ctx.id].resize(model_bounding_boxes.size()); + + for(std::vector::const_iterator k = node_list.begin(); k != node_list.end(); ++k, ++node_counter) + { + (frustum_culling_results_[ctx.id])[node_counter] = culling_frustum.classify(model_bounding_boxes[k->node_id_]); + } + + + auto plod_ressource = std::static_pointer_cast(GeometryDatabase::instance()->lookup(file_name)); auto material = MaterialDatabase::instance()->lookup(material_name); @@ -700,7 +732,6 @@ bool PLODUberShader::upload_to (RenderContext const& context) const } - scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); float radius_model_scaling = scm::math::length(model_matrix * x_unit_vec); @@ -718,7 +749,7 @@ bool PLODUberShader::upload_to (RenderContext const& context) const { get_program(depth_pass)->use(ctx); { - plod_ressource->draw(ctx, context_id, view_id, model_id, vertex_array_[ctx.id]); + plod_ressource->draw(ctx, context_id, view_id, model_id, vertex_array_[ctx.id], frustum_culling_results_[ctx.id] ); } get_program(depth_pass)->unuse(ctx); } @@ -833,7 +864,7 @@ void PLODUberShader::draw(RenderContext const& ctx, get_program(accumulation_pass)->use(ctx); { - plod_ressource->draw(ctx, context_id, view_id, model_id, vertex_array_[ctx.id]); + plod_ressource->draw(ctx, context_id, view_id, model_id, vertex_array_[ctx.id], frustum_culling_results_[ctx.id]); } get_program(accumulation_pass)->unuse(ctx); } From 3a7b22c49a81046c4dfce3919286378d22028169 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Thu, 22 May 2014 13:34:30 +0200 Subject: [PATCH 29/45] fix point size with and remove dirty old ratio fix --- .../uber_shaders/gbuffer/pbr/p01_depth.frag | 3 +- .../uber_shaders/gbuffer/pbr/p01_depth.vert | 17 +++- .../gbuffer/pbr/p02_accumulation.frag | 8 +- .../gbuffer/pbr/p02_accumulation.vert | 19 ++++- src/gua/renderer/PLODUberShader.cpp | 84 +++++++++---------- 5 files changed, 76 insertions(+), 55 deletions(-) diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag index 1552d3530..83765631f 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag @@ -6,6 +6,7 @@ in vec3 pass_normal; in float pass_mv_vert_depth; in float pass_scaled_radius; +in float pass_view_scaling; /////////////////////////////////////////////////////////////////////////////// // output @@ -91,6 +92,6 @@ void main() get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); - out_linear_depth = - ( ( (pass_mv_vert_depth + depth_offset * pass_scaled_radius ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; + out_linear_depth = - ( ( (pass_mv_vert_depth + depth_offset * pass_view_scaling ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert index 6aaa87711..645a85f99 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert @@ -22,27 +22,36 @@ uniform float near_plane; uniform float radius_model_scaling; - uniform float mVPScalingRatio; //output to fragment shader out vec3 pass_normal; out float pass_mv_vert_depth; - out float pass_scaled_radius; + out float pass_scaled_radius; + out float pass_view_scaling; void main() { float scaled_radius = radius_model_scaling * in_radius; - vec4 pos_es = gua_view_matrix * gua_model_matrix * vec4(in_position, 1.0); + + mat4 model_view_matrix = gua_view_matrix * gua_model_matrix; + + vec4 pos_es = model_view_matrix * vec4(in_position, 1.0); + + vec4 pos_es_ub = model_view_matrix * vec4(in_position + ( 0.5 * in_radius), 1.0); + vec4 pos_es_lb = model_view_matrix * vec4(in_position + (-0.5 * in_radius), 1.0); + + pass_view_scaling = length(vec3(pos_es_ub) - vec3(pos_es_lb)); gl_Position = gua_projection_matrix * pos_es; - gl_PointSize = 2.0f * mVPScalingRatio * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; + gl_PointSize = 2.0f * scaled_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; pass_mv_vert_depth = pos_es.z; pass_scaled_radius = scaled_radius; + } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index 19c847ef9..f5ba7c310 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -15,6 +15,8 @@ in vec3 pass_point_color; in vec3 pass_normal; in float pass_mv_vert_depth; in float pass_scaled_radius; +in float pass_screen_space_splat_size; +in float pass_view_scaling; /////////////////////////////////////////////////////////////////////////////// // output @@ -112,20 +114,18 @@ void main() //////////depth value of depth pass texture depthValue = (-depthValue * 1.0 * far_minus_near_plane) + near_plane; - //get_gaussianValue(depth_offset, mappedPointCoord, VertexIn.nor.xyz); - //get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); float depth_to_compare = 0; //if(ellipsify) - depth_to_compare = pass_mv_vert_depth + depth_offset * pass_scaled_radius; + depth_to_compare = pass_mv_vert_depth + depth_offset * pass_view_scaling; //else // depth_to_compare = pass_mv_vert_depth; float weight = get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); - if( depthValue - (depth_to_compare) < 0.00031 + 3.0*(pass_scaled_radius) ) + if( depthValue - (depth_to_compare) < 0.00031 + 0.001*(pass_screen_space_splat_size) ) { out_accumulated_color = vec4(pass_point_color * weight, weight); } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert index cf4e53339..a45c26f78 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert @@ -22,13 +22,14 @@ uniform float near_plane; uniform float radius_model_scaling; - uniform float mVPScalingRatio; //output to fragment shader out vec3 pass_point_color; out vec3 pass_normal; out float pass_mv_vert_depth; - out float pass_scaled_radius; + out float pass_scaled_radius; + out float pass_screen_space_splat_size; + out float pass_view_scaling; void main() @@ -37,12 +38,20 @@ float scaled_radius = radius_model_scaling * in_radius; - vec4 pos_es = gua_view_matrix * gua_model_matrix * vec4(in_position, 1.0); + mat4 model_view_matrix = gua_view_matrix * gua_model_matrix; + vec4 pos_es = model_view_matrix * vec4(in_position, 1.0); + + vec4 pos_es_ub = model_view_matrix * vec4(in_position + ( 0.5 * in_radius), 1.0); + vec4 pos_es_lb = model_view_matrix * vec4(in_position + (-0.5 * in_radius), 1.0); + + pass_view_scaling = length(vec3(pos_es_ub) - vec3(pos_es_lb)); gl_Position = gua_projection_matrix * pos_es; - gl_PointSize = 2.0f * mVPScalingRatio * in_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; + float splat_size = 2.0f * scaled_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; + + gl_PointSize = splat_size; pass_point_color = vec3((in_r)/255.0f, @@ -52,4 +61,6 @@ pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; pass_mv_vert_depth = pos_es.z; pass_scaled_radius = scaled_radius; + + pass_screen_space_splat_size = splat_size ; } diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp index d24498546..26ef2765e 100644 --- a/src/gua/renderer/PLODUberShader.cpp +++ b/src/gua/renderer/PLODUberShader.cpp @@ -656,7 +656,36 @@ bool PLODUberShader::upload_to (RenderContext const& context) const Frustum const& frustum, std::size_t viewid) const { - + + std::vector corner_values = frustum.get_corners(); + float top_minus_bottom = scm::math::length((corner_values[2]) - (corner_values[0])); + float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / top_minus_bottom; + + if( last_geometry_state_[ctx.id] != pre_draw_state) + { + + //enable dynamic point size in shaders + ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); + + + // bind fbo + ctx.render_context->set_frame_buffer(depth_pass_result_fbo_[ctx.id]); + + + gua::math::mat4 const& projection_matrix = frustum.get_projection(); + + float near_plane_value = frustum.get_clip_near(); + float far_plane_value = frustum.get_clip_far(); + + + get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); + get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); + get_program(depth_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); + + last_geometry_state_[ctx.id] = pre_draw_state; + + } + pbr::ren::Controller* controller = pbr::ren::Controller::GetInstance(); pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); @@ -668,9 +697,13 @@ bool PLODUberShader::upload_to (RenderContext const& context) const pbr::ren::Camera cut_update_cam(view_id, frustum.get_view(), frustum.get_projection() ); cuts->SendCamera(context_id, view_id, cut_update_cam); + cuts->SendHeightDividedByTopMinusBottom(context_id, view_id, height_divided_by_top_minus_bottom); cuts->SendTransform(context_id, model_id, model_matrix); - + + pbr::ren::Cut& cut = cuts->GetCut(context_id, view_id, model_id); + std::vector& node_list = cut.complete_set(); + //calculate frustum culling results pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); @@ -680,9 +713,6 @@ bool PLODUberShader::upload_to (RenderContext const& context) const scm::gl::frustum culling_frustum = cut_update_cam.GetFrustumByModel(model_matrix); std::vector const& model_bounding_boxes = kdn_tree->bounding_boxes(); - - pbr::ren::Cut& cut = cuts->GetCut(context_id, view_id, model_id); - std::vector& node_list = cut.complete_set(); unsigned int node_counter = 0; @@ -702,45 +732,16 @@ bool PLODUberShader::upload_to (RenderContext const& context) const // begin of depth pass (first) { - if( last_geometry_state_[ctx.id] != pre_draw_state) - { - - //enable dynamic point size in shaders - ctx.render_context->set_rasterizer_state(change_point_size_in_shader_state_[ctx.id]); - - - // bind fbo - ctx.render_context->set_frame_buffer(depth_pass_result_fbo_[ctx.id]); - - gua::math::mat4 const& projection_matrix = frustum.get_projection(); - float near_plane_value = frustum.get_clip_near(); - float far_plane_value = frustum.get_clip_far(); - float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; - float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; - - float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); - - get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); - get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); - get_program(depth_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); - - last_geometry_state_[ctx.id] = pre_draw_state; - - - } - scm::math::vec4f x_unit_vec(1.0f,0.f,0.f,0.f); + scm::math::vec4f x_unit_vec(1.0,0.0f,0.f,0.f); float radius_model_scaling = scm::math::length(model_matrix * x_unit_vec); - float modelViewProjectionScalingRatio = scm::math::length(frustum.get_projection()*frustum.get_view()*model_matrix*x_unit_vec) - / 0.8759124279022216797; get_program(depth_pass)->set_uniform(ctx, radius_model_scaling, "radius_model_scaling"); - get_program(depth_pass)->set_uniform(ctx, modelViewProjectionScalingRatio, "mVPScalingRatio"); get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); @@ -808,18 +809,21 @@ void PLODUberShader::draw(RenderContext const& ctx, //set blend state to accumulate ctx.render_context->set_blend_state(color_accumulation_state_[ctx.id]); - // ctx.render_context->clear_color_buffer(accumulation_pass_result_fbo_[ctx.id], 0, scm::math::vec4f(0.0f, 1.0f, 0.0f, 0.0f)); // bind accumulation FBO ctx.render_context->set_frame_buffer(accumulation_pass_result_fbo_[ctx.id]); gua::math::mat4 const& projection_matrix = frustum.get_projection(); - //put near_plane_value and height_divided_by_top_minus_bottom as member variables and get these two only 1 per render frame float near_plane_value = frustum.get_clip_near(); float far_plane_value = frustum.get_clip_far(); float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; - float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / (top_plane_value - bottom_plane_value); + + std::vector corner_values = frustum.get_corners(); + + float top_minus_bottom = scm::math::length((corner_values[2]) - (corner_values[0])); + + float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / top_minus_bottom; get_program(accumulation_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); get_program(accumulation_pass)->set_uniform(ctx, near_plane_value, "near_plane"); @@ -837,12 +841,8 @@ void PLODUberShader::draw(RenderContext const& ctx, float radius_model_scaling = scm::math::length(model_matrix * x_unit_vec); - float modelViewProjectionScalingRatio = scm::math::length(frustum.get_projection()*frustum.get_view()*model_matrix*x_unit_vec) - / 0.8759124279022216797; - get_program(accumulation_pass)->set_uniform(ctx, radius_model_scaling, "radius_model_scaling"); - get_program(accumulation_pass)->set_uniform(ctx, modelViewProjectionScalingRatio, "mVPScalingRatio"); ctx.render_context->bind_texture(depth_pass_linear_depth_result_[ctx.id], linear_sampler_state_[ctx.id], 0); get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); From c24f61dc82bd630dd6e0ce80104982335ff35316 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Thu, 22 May 2014 14:25:06 +0200 Subject: [PATCH 30/45] using correct viewids for cut update --- src/gua/renderer/PLODUberShader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp index 26ef2765e..0a15b70cb 100644 --- a/src/gua/renderer/PLODUberShader.cpp +++ b/src/gua/renderer/PLODUberShader.cpp @@ -690,7 +690,7 @@ bool PLODUberShader::upload_to (RenderContext const& context) const pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); pbr::context_t context_id = controller->DeduceContextId(ctx.id); - pbr::view_t view_id = controller->DeduceViewId(context_id, 0); + pbr::view_t view_id = controller->DeduceViewId(context_id, viewid); pbr::model_t model_id = controller->DeduceModelId(file_name); //send camera and model_matrix to cut update @@ -858,7 +858,7 @@ void PLODUberShader::draw(RenderContext const& ctx, pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); pbr::context_t context_id = controller->DeduceContextId(ctx.id); pbr::model_t model_id = controller->DeduceModelId(file_name); - pbr::view_t view_id = controller->DeduceViewId(context_id, 0); //only one view + pbr::view_t view_id = controller->DeduceViewId(context_id, viewid); material_id_[ctx.id] = material->get_id(); get_program(accumulation_pass)->use(ctx); From 67d3459e3d4dc921d7acf9d82187bf5a67fb61ca Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Thu, 22 May 2014 15:08:05 +0200 Subject: [PATCH 31/45] fix frustum culling bug for multi models --- .../gbuffer/pbr/p02_accumulation.frag | 2 +- src/gua/renderer/PLODUberShader.cpp | 27 ++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index f5ba7c310..306633e8c 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -125,7 +125,7 @@ float depth_to_compare = 0; float weight = get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); - if( depthValue - (depth_to_compare) < 0.00031 + 0.001*(pass_screen_space_splat_size) ) + if( depthValue - (depth_to_compare) < 0.00031 + 3*(pass_scaled_radius) ) { out_accumulated_color = vec4(pass_point_color * weight, weight); } diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp index 0a15b70cb..ba8e91250 100644 --- a/src/gua/renderer/PLODUberShader.cpp +++ b/src/gua/renderer/PLODUberShader.cpp @@ -656,7 +656,6 @@ bool PLODUberShader::upload_to (RenderContext const& context) const Frustum const& frustum, std::size_t viewid) const { - std::vector corner_values = frustum.get_corners(); float top_minus_bottom = scm::math::length((corner_values[2]) - (corner_values[0])); float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / top_minus_bottom; @@ -861,6 +860,32 @@ void PLODUberShader::draw(RenderContext const& ctx, pbr::view_t view_id = controller->DeduceViewId(context_id, viewid); material_id_[ctx.id] = material->get_id(); + + + pbr::ren::CutDatabase* cuts = pbr::ren::CutDatabase::GetInstance(); + pbr::ren::Cut& cut = cuts->GetCut(context_id, view_id, model_id); + std::vector& node_list = cut.complete_set(); + + //calculate frustum culling results + pbr::ren::Camera cut_update_cam(view_id, frustum.get_view(), frustum.get_projection() ); + + pbr::ren::KdnTree const* kdn_tree = database->GetModel(model_id)->kdn_tree(); + + scm::gl::frustum culling_frustum = cut_update_cam.GetFrustumByModel(model_matrix); + + std::vector const& model_bounding_boxes = kdn_tree->bounding_boxes(); + + unsigned int node_counter = 0; + + frustum_culling_results_[ctx.id].clear(); + frustum_culling_results_[ctx.id].resize(model_bounding_boxes.size()); + + for(std::vector::const_iterator k = node_list.begin(); k != node_list.end(); ++k, ++node_counter) + { + (frustum_culling_results_[ctx.id])[node_counter] = culling_frustum.classify(model_bounding_boxes[k->node_id_]); + } + + get_program(accumulation_pass)->use(ctx); { From d965349d237c62f0b5a83284279df599d03edfcc Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Mon, 26 May 2014 19:23:50 +0200 Subject: [PATCH 32/45] changes for new policy class --- .../uber_shaders/gbuffer/pbr/p01_depth.frag | 54 ++++++++++------ .../uber_shaders/gbuffer/pbr/p01_depth.vert | 13 ++-- .../gbuffer/pbr/p02_accumulation.frag | 64 ++++++++++--------- .../gbuffer/pbr/p02_accumulation.vert | 14 ++-- src/gua/renderer/PLODUberShader.cpp | 47 ++++++++------ 5 files changed, 117 insertions(+), 75 deletions(-) diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag index 83765631f..24ab25d83 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag @@ -21,35 +21,38 @@ layout (location=0) out float out_linear_depth; /////////////////////////////////////////////////////////////////////////////// uniform float near_plane; uniform float far_minus_near_plane; +uniform float radius_model_scaling; + /////////////////////////////////////////////////////////////////////////////// // splatting methods /////////////////////////////////////////////////////////////////////////////// -float calc_depth_offset(vec2 mappedPointCoord) +float calc_depth_offset(vec2 mappedPointCoord, vec3 adjustedNormal) { - vec3 normal = pass_normal; - if(normal.z < 0) - { - normal *= -1; - } - - float xzRatio = (normal.x/normal.z); - float yzRatio = (normal.y/normal.z); + float xzRatio = (adjustedNormal.x/adjustedNormal.z); + float yzRatio = (adjustedNormal.y/adjustedNormal.z); - //if (clampedNormalMode) + if (true) { - float zBound = 0.35f;//max_deform_ratio; - float normalZ = normal.z; + float zBound = 0.2f;//max_deform_ratio; + float normalZ = adjustedNormal.z; if(normalZ > 0.0) normalZ = max(zBound, normalZ); else normalZ = -max(zBound, -normalZ); - xzRatio = (normal.x/normalZ); - yzRatio = (normal.y/normalZ); + if(normalZ != 0.0) + { + xzRatio = (adjustedNormal.x/normalZ); + yzRatio = (adjustedNormal.y/normalZ); + } + else + { + discard; + } } @@ -65,7 +68,7 @@ float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNorma float radius; //if(ellipsify) - radius = mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y + depth_offset*depth_offset; + radius = sqrt(mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y + depth_offset*depth_offset); //else // radius = mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y ; @@ -83,15 +86,28 @@ float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNorma void main() { - vec2 mappedPointCoord = gl_PointCoord*2 + vec2(-1.0f, -1.0f); + vec3 adjustedNormal = pass_normal; + + if(pass_normal.z < 0) + { + //discard; + adjustedNormal = pass_normal * -1; + } + else + { + adjustedNormal = pass_normal; + } - float depth_offset = calc_depth_offset(mappedPointCoord) ; + vec2 mappedPointCoord = gl_PointCoord*2 + vec2(-1.0f, -1.0f); - get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); + float depth_offset = calc_depth_offset(mappedPointCoord, adjustedNormal) ; - out_linear_depth = - ( ( (pass_mv_vert_depth + depth_offset * pass_view_scaling ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; + get_gaussianValue(depth_offset, mappedPointCoord, adjustedNormal) ; + + + out_linear_depth = - ( ( (pass_mv_vert_depth + depth_offset * pass_scaled_radius * pass_view_scaling ) - near_plane) / (far_minus_near_plane * 1.0f) ) ; } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert index 645a85f99..460d5b4ab 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.vert @@ -23,6 +23,8 @@ uniform float radius_model_scaling; + uniform mat4 newNormalMatrix; + //output to fragment shader out vec3 pass_normal; out float pass_mv_vert_depth; @@ -39,17 +41,20 @@ vec4 pos_es = model_view_matrix * vec4(in_position, 1.0); - vec4 pos_es_ub = model_view_matrix * vec4(in_position + ( 0.5 * in_radius), 1.0); - vec4 pos_es_lb = model_view_matrix * vec4(in_position + (-0.5 * in_radius), 1.0); + vec4 pos_es_ub = model_view_matrix * vec4(in_position + vec3(0.0, 0.5 * in_radius, 0.0), 1.0); + vec4 pos_es_lb = model_view_matrix * vec4(in_position + vec3(0.0, -0.5 * in_radius, 0.0), 1.0); - pass_view_scaling = length(vec3(pos_es_ub) - vec3(pos_es_lb)); + float view_scaling = length(vec3(pos_es_ub) - vec3(pos_es_lb)); + pass_view_scaling = view_scaling; gl_Position = gua_projection_matrix * pos_es; gl_PointSize = 2.0f * scaled_radius * (near_plane/-pos_es.z)* height_divided_by_top_minus_bottom; - pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; + //pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; + pass_normal = normalize(( /*gua_normal_matrix*/ newNormalMatrix * vec4(in_normal, 0.0)).xyz); + pass_mv_vert_depth = pos_es.z; pass_scaled_radius = scaled_radius; diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index 306633e8c..c1a8543a6 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -36,6 +36,7 @@ uniform float near_plane; uniform float far_minus_near_plane; uniform vec2 win_dims; +uniform float radius_model_scaling; //uniform float win_dim_x; //uniform float win_dim_y; @@ -44,32 +45,24 @@ uniform vec2 win_dims; // splatting methods /////////////////////////////////////////////////////////////////////////////// -float calc_depth_offset(vec2 mappedPointCoord) +float calc_depth_offset(vec2 mappedPointCoord, vec3 adjustedNormal) { - vec3 normal = pass_normal; - if(normal.z < 0) - { - - //discard; - normal *= -1; - } - - float xzRatio = (normal.x/normal.z); - float yzRatio = (normal.y/normal.z); + float xzRatio = (adjustedNormal.x/adjustedNormal.z); + float yzRatio = (adjustedNormal.y/adjustedNormal.z); -//if(clamped_normal_mode) +if(true) { - float zBound = 0.35f;//max_deform_ratio; - float normalZ = normal.z; + float zBound = 0.2f;//max_deform_ratio; + float normalZ = adjustedNormal.z; if(normalZ > 0.0) normalZ = max(zBound, normalZ); else normalZ = -max(zBound, -normalZ); - xzRatio = (normal.x/normalZ); - yzRatio = (normal.y/normalZ); + xzRatio = (adjustedNormal.x/normalZ); + yzRatio = (adjustedNormal.y/normalZ); } @@ -84,14 +77,13 @@ float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNorma { float radius; - //if(ellipsify) - radius = mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y + depth_offset*depth_offset; - //else - // radius = mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y ; + + radius = sqrt(mappedPointCoord.x*mappedPointCoord.x + mappedPointCoord.y*mappedPointCoord.y + depth_offset*depth_offset); + if(radius > 1.0) - discard; + discard; else return gaussian[(int)(round(radius * 31.0))]; @@ -104,10 +96,22 @@ float get_gaussianValue(float depth_offset, vec2 mappedPointCoord, vec3 newNorma void main() { + vec3 adjustedNormal = pass_normal; + + if(pass_normal.z < 0) + { + //discard; + adjustedNormal = pass_normal * -1; + } + else + { + adjustedNormal = pass_normal; + } + vec2 mappedPointCoord = gl_PointCoord*2 + vec2(-1.0f, -1.0f); - float depth_offset = calc_depth_offset(mappedPointCoord) ; + float depth_offset = calc_depth_offset(mappedPointCoord, adjustedNormal) ; float depthValue = texture2D( p01_depth_texture, gl_FragCoord.xy/win_dims.xy ).r; @@ -117,17 +121,18 @@ void main() float depth_to_compare = 0; - //if(ellipsify) - depth_to_compare = pass_mv_vert_depth + depth_offset * pass_view_scaling; - //else - // depth_to_compare = pass_mv_vert_depth; + depth_to_compare = pass_mv_vert_depth + depth_offset * pass_scaled_radius * pass_view_scaling; + + float weight = 0; - float weight = get_gaussianValue(depth_offset, mappedPointCoord, pass_normal); + weight = get_gaussianValue(depth_offset, mappedPointCoord, adjustedNormal) ; - if( depthValue - (depth_to_compare) < 0.00031 + 3*(pass_scaled_radius) ) + + if( depthValue - (depth_to_compare) <= 3 * pass_scaled_radius ) { - out_accumulated_color = vec4(pass_point_color * weight, weight); + + out_accumulated_color = vec4(pass_point_color * weight, weight); } else { @@ -135,4 +140,5 @@ float depth_to_compare = 0; } + } diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert index a45c26f78..d92990efd 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.vert @@ -21,7 +21,10 @@ uniform float height_divided_by_top_minus_bottom; uniform float near_plane; - uniform float radius_model_scaling; + uniform float radius_model_scaling; + + uniform mat4 newNormalMatrix; + //output to fragment shader out vec3 pass_point_color; @@ -42,10 +45,11 @@ vec4 pos_es = model_view_matrix * vec4(in_position, 1.0); - vec4 pos_es_ub = model_view_matrix * vec4(in_position + ( 0.5 * in_radius), 1.0); - vec4 pos_es_lb = model_view_matrix * vec4(in_position + (-0.5 * in_radius), 1.0); + vec4 pos_es_ub = model_view_matrix * vec4(in_position + vec3(0.0, 0.5 * in_radius, 0.0), 1.0); + vec4 pos_es_lb = model_view_matrix * vec4(in_position + vec3(0.0, -0.5 * in_radius, 0.0), 1.0); - pass_view_scaling = length(vec3(pos_es_ub) - vec3(pos_es_lb)); + float view_scaling = length(vec3(pos_es_ub) - vec3(pos_es_lb)); + pass_view_scaling = view_scaling; gl_Position = gua_projection_matrix * pos_es; @@ -58,7 +62,7 @@ (in_g)/255.0f, (in_b)/255.0f); - pass_normal = (gua_normal_matrix * vec4(in_normal, 0.0)).xyz; + pass_normal = normalize(( /*gua_normal_matrix*/ newNormalMatrix * vec4(in_normal, 0.0)).xyz); pass_mv_vert_depth = pos_es.z; pass_scaled_radius = scaled_radius; diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp index ba8e91250..206babf5e 100644 --- a/src/gua/renderer/PLODUberShader.cpp +++ b/src/gua/renderer/PLODUberShader.cpp @@ -32,7 +32,7 @@ #include -#include +#include #include #include #include @@ -116,7 +116,7 @@ PLODUberShader::~PLODUberShader() std::cout << "deleted controller" << std::endl; delete pbr::ren::ModelDatabase::GetInstance(); std::cout << "deleted model database" << std::endl; - delete pbr::ren::MemoryQuery::GetInstance(); + delete pbr::ren::Policy::GetInstance(); std::cout << "deleted memory query" << std::endl; delete pbr::ren::CutDatabase::GetInstance(); std::cout << "deleted cut database" << std::endl; @@ -425,12 +425,12 @@ bool PLODUberShader::upload_to (RenderContext const& context) const { pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); - pbr::ren::MemoryQuery* memory_query = pbr::ren::MemoryQuery::GetInstance(); + pbr::ren::Policy* policy = pbr::ren::Policy::GetInstance(); size_t size_of_node_in_bytes = database->size_of_surfel()*database->surfels_per_node(); int32_t upload_budget_in_nodes = - (memory_query->upload_budget_in_mb()*1024*1024) / size_of_node_in_bytes; + (policy->upload_budget_in_mb()*1024*1024) / size_of_node_in_bytes; unsigned long long sizeOfNode = database->surfels_per_node() *database->size_of_surfel(); @@ -444,12 +444,12 @@ bool PLODUberShader::upload_to (RenderContext const& context) const if (context.id >= temp_buffer_B_.size()) { pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); - pbr::ren::MemoryQuery* memory_query = pbr::ren::MemoryQuery::GetInstance(); + pbr::ren::Policy* policy = pbr::ren::Policy::GetInstance(); size_t size_of_node_in_bytes = database->size_of_surfel()*database->surfels_per_node(); int32_t upload_budget_in_nodes = - (memory_query->upload_budget_in_mb()*1024*1024) / size_of_node_in_bytes; + (policy->upload_budget_in_mb()*1024*1024) / size_of_node_in_bytes; unsigned long long sizeOfNode = database->surfels_per_node() *database->size_of_surfel(); @@ -462,12 +462,12 @@ bool PLODUberShader::upload_to (RenderContext const& context) const if (context.id >= render_buffer_.size()) { pbr::ren::ModelDatabase* database = pbr::ren::ModelDatabase::GetInstance(); - pbr::ren::MemoryQuery* memory_query = pbr::ren::MemoryQuery::GetInstance(); + pbr::ren::Policy* policy = pbr::ren::Policy::GetInstance(); size_t size_of_node_in_bytes = database->size_of_surfel()*database->surfels_per_node(); int32_t render_budget_in_nodes = - (memory_query->render_budget_in_mb()*1024*1024) / size_of_node_in_bytes; + (policy->render_budget_in_mb()*1024*1024) / size_of_node_in_bytes; unsigned long long sizeOfNode = database->surfels_per_node() *database->size_of_surfel(); unsigned long long sizeOfRenderBuffer = render_budget_in_nodes * sizeOfNode; @@ -659,6 +659,8 @@ bool PLODUberShader::upload_to (RenderContext const& context) const std::vector corner_values = frustum.get_corners(); float top_minus_bottom = scm::math::length((corner_values[2]) - (corner_values[0])); float height_divided_by_top_minus_bottom = (render_window_dims_[ctx.id])[1] / top_minus_bottom; + float near_plane_value = frustum.get_clip_near(); + float far_plane_value = frustum.get_clip_far(); if( last_geometry_state_[ctx.id] != pre_draw_state) { @@ -673,10 +675,6 @@ bool PLODUberShader::upload_to (RenderContext const& context) const gua::math::mat4 const& projection_matrix = frustum.get_projection(); - float near_plane_value = frustum.get_clip_near(); - float far_plane_value = frustum.get_clip_far(); - - get_program(depth_pass)->set_uniform(ctx, height_divided_by_top_minus_bottom, "height_divided_by_top_minus_bottom"); get_program(depth_pass)->set_uniform(ctx, near_plane_value, "near_plane"); get_program(depth_pass)->set_uniform(ctx, (far_plane_value - near_plane_value), "far_minus_near_plane"); @@ -693,7 +691,7 @@ bool PLODUberShader::upload_to (RenderContext const& context) const pbr::model_t model_id = controller->DeduceModelId(file_name); //send camera and model_matrix to cut update - pbr::ren::Camera cut_update_cam(view_id, frustum.get_view(), frustum.get_projection() ); + pbr::ren::Camera cut_update_cam(view_id, near_plane_value, frustum.get_view(), frustum.get_projection() ); cuts->SendCamera(context_id, view_id, cut_update_cam); cuts->SendHeightDividedByTopMinusBottom(context_id, view_id, height_divided_by_top_minus_bottom); @@ -742,8 +740,11 @@ bool PLODUberShader::upload_to (RenderContext const& context) const get_program(depth_pass)->set_uniform(ctx, radius_model_scaling, "radius_model_scaling"); - get_program(depth_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + + + get_program(depth_pass)->set_uniform(ctx, transpose(inverse(frustum.get_view()*model_matrix)), "gua_normal_matrix"); get_program(depth_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); + get_program(depth_pass)->set_uniform(ctx, transpose(inverse(frustum.get_view()*model_matrix)), "newNormalMatrix"); if (material && plod_ressource) { @@ -795,6 +796,10 @@ void PLODUberShader::draw(RenderContext const& ctx, { + + float near_plane_value = frustum.get_clip_near(); + float far_plane_value = frustum.get_clip_far(); + if( last_geometry_state_[ctx.id] != post_draw_state) { @@ -813,8 +818,7 @@ void PLODUberShader::draw(RenderContext const& ctx, gua::math::mat4 const& projection_matrix = frustum.get_projection(); - float near_plane_value = frustum.get_clip_near(); - float far_plane_value = frustum.get_clip_far(); + float top_plane_value = near_plane_value * (1.0 + projection_matrix[9]) / projection_matrix[5]; float bottom_plane_value = near_plane_value * (projection_matrix[9] - 1.0) / projection_matrix[5]; @@ -847,7 +851,14 @@ void PLODUberShader::draw(RenderContext const& ctx, get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); - get_program(accumulation_pass)->set_uniform(ctx, normal_matrix, "gua_normal_matrix"); + + scm::math::vec4f testNormal = transpose(inverse(frustum.get_view()*model_matrix))*scm::math::vec4f(0.0,0.0,1.0,0.0); + scm::math::vec3f tN2 = scm::math::normalize(scm::math::vec3f(testNormal[0], testNormal[1], testNormal[2]) ); + + std::cout<<"Normal: "<< tN2 <<" : "<set_uniform(ctx, transpose(inverse(frustum.get_view()*model_matrix)), "gua_normal_matrix"); + get_program(accumulation_pass)->set_uniform(ctx, transpose(inverse(frustum.get_view()*model_matrix)), "newNormalMatrix"); get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); if (material && plod_ressource) @@ -867,7 +878,7 @@ void PLODUberShader::draw(RenderContext const& ctx, std::vector& node_list = cut.complete_set(); //calculate frustum culling results - pbr::ren::Camera cut_update_cam(view_id, frustum.get_view(), frustum.get_projection() ); + pbr::ren::Camera cut_update_cam(view_id, near_plane_value, frustum.get_view(), frustum.get_projection() ); pbr::ren::KdnTree const* kdn_tree = database->GetModel(model_id)->kdn_tree(); From 57a836f76f011b8177d412a74baba64de8b52d1e Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Mon, 26 May 2014 22:01:45 +0200 Subject: [PATCH 33/45] normal fix & remove debug output --- .../uber_shaders/gbuffer/pbr/p01_depth.frag | 14 ++++++++++---- .../uber_shaders/gbuffer/pbr/p02_accumulation.frag | 7 ++++--- src/gua/renderer/PLODRessource.cpp | 1 + src/gua/renderer/PLODUberShader.cpp | 6 ------ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag index 24ab25d83..d3b57cbbb 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag @@ -36,14 +36,19 @@ float calc_depth_offset(vec2 mappedPointCoord, vec3 adjustedNormal) if (true) { - float zBound = 0.2f;//max_deform_ratio; + float zBound = 0.01f;//max_deform_ratio; float normalZ = adjustedNormal.z; + if(normalZ > 0.0) normalZ = max(zBound, normalZ); else normalZ = -max(zBound, -normalZ); + xzRatio = (adjustedNormal.x/normalZ); + yzRatio = (adjustedNormal.y/normalZ); + +/* if(normalZ != 0.0) { xzRatio = (adjustedNormal.x/normalZ); @@ -53,7 +58,7 @@ float calc_depth_offset(vec2 mappedPointCoord, vec3 adjustedNormal) { discard; } - +*/ } return -(xzRatio)*mappedPointCoord.x - (yzRatio * mappedPointCoord.y); @@ -87,16 +92,17 @@ void main() { vec3 adjustedNormal = pass_normal; + // adjustedNormal = vec3(0.0, 1.0, 0.0); if(pass_normal.z < 0) { //discard; - adjustedNormal = pass_normal * -1; + adjustedNormal = adjustedNormal * -1; } else { - adjustedNormal = pass_normal; + adjustedNormal = adjustedNormal; } vec2 mappedPointCoord = gl_PointCoord*2 + vec2(-1.0f, -1.0f); diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index c1a8543a6..7c71a3b58 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -53,7 +53,7 @@ float calc_depth_offset(vec2 mappedPointCoord, vec3 adjustedNormal) if(true) { - float zBound = 0.2f;//max_deform_ratio; + float zBound = 0.0f;//max_deform_ratio; float normalZ = adjustedNormal.z; if(normalZ > 0.0) @@ -97,15 +97,16 @@ void main() { vec3 adjustedNormal = pass_normal; + // adjustedNormal = vec3(0.0, 1.0, 0.0); if(pass_normal.z < 0) { //discard; - adjustedNormal = pass_normal * -1; + adjustedNormal = adjustedNormal * -1; } else { - adjustedNormal = pass_normal; + adjustedNormal = adjustedNormal; } vec2 mappedPointCoord = gl_PointCoord*2 + vec2(-1.0f, -1.0f); diff --git a/src/gua/renderer/PLODRessource.cpp b/src/gua/renderer/PLODRessource.cpp index fa70d79a1..e60361f2f 100644 --- a/src/gua/renderer/PLODRessource.cpp +++ b/src/gua/renderer/PLODRessource.cpp @@ -120,6 +120,7 @@ void PLODRessource::draw(RenderContext const& ctx, pbr::context_t context_id, pb pbr::node_t node_counter = 0; + for(std::vector::const_iterator k = node_list.begin(); k != node_list.end(); ++k, ++node_counter) { //0 = completely inside of frustum, 1 = completely outside of frustum, 2 = intersects frustum diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp index 206babf5e..38a5dae43 100644 --- a/src/gua/renderer/PLODUberShader.cpp +++ b/src/gua/renderer/PLODUberShader.cpp @@ -851,12 +851,6 @@ void PLODUberShader::draw(RenderContext const& ctx, get_program(accumulation_pass)->get_program(ctx)->uniform_sampler("p01_depth_texture", 0); - - scm::math::vec4f testNormal = transpose(inverse(frustum.get_view()*model_matrix))*scm::math::vec4f(0.0,0.0,1.0,0.0); - scm::math::vec3f tN2 = scm::math::normalize(scm::math::vec3f(testNormal[0], testNormal[1], testNormal[2]) ); - - std::cout<<"Normal: "<< tN2 <<" : "<set_uniform(ctx, transpose(inverse(frustum.get_view()*model_matrix)), "gua_normal_matrix"); get_program(accumulation_pass)->set_uniform(ctx, transpose(inverse(frustum.get_view()*model_matrix)), "newNormalMatrix"); get_program(accumulation_pass)->set_uniform(ctx, model_matrix, "gua_model_matrix"); From f5ddef13aff165d265e8b0fcc82df1322ad68e99 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Tue, 27 May 2014 12:37:16 +0200 Subject: [PATCH 34/45] update to wrapping version --- resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag | 2 +- .../shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag index d3b57cbbb..f5b74682f 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p01_depth.frag @@ -36,7 +36,7 @@ float calc_depth_offset(vec2 mappedPointCoord, vec3 adjustedNormal) if (true) { - float zBound = 0.01f;//max_deform_ratio; + float zBound = 0.3f;//max_deform_ratio; float normalZ = adjustedNormal.z; diff --git a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag index 00c5a399f..e51398bdc 100644 --- a/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag +++ b/resources/shaders/uber_shaders/gbuffer/pbr/p02_accumulation.frag @@ -53,7 +53,7 @@ float calc_depth_offset(vec2 mappedPointCoord, vec3 adjustedNormal) if(true) { - float zBound = 0.0f;//max_deform_ratio; + float zBound = 0.3f;//max_deform_ratio; float normalZ = adjustedNormal.z; if(normalZ > 0.0) @@ -130,7 +130,7 @@ float depth_to_compare = 0; weight = get_gaussianValue(depth_offset, mappedPointCoord, adjustedNormal) ; - if( depthValue - (depth_to_compare) <= 2 * pass_scaled_radius ) + if( depthValue - (depth_to_compare) <= 3 * pass_scaled_radius ) { out_accumulated_color = vec4(pass_point_color * weight, weight); From 348879cd71518e87aa55ba6972f64564ffe653fc Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Tue, 27 May 2014 13:43:29 +0200 Subject: [PATCH 35/45] fix standard materials for pbr nodes --- src/gua/renderer/PBRLoader.cpp | 12 ++++++++++-- src/gua/renderer/PBRUberShader.cpp | 9 +-------- src/gua/renderer/PLODLoader.cpp | 16 ++++++++++++---- src/gua/renderer/PLODUberShader.cpp | 9 +-------- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/gua/renderer/PBRLoader.cpp b/src/gua/renderer/PBRLoader.cpp index 7465bdcba..483ab4c30 100644 --- a/src/gua/renderer/PBRLoader.cpp +++ b/src/gua/renderer/PBRLoader.cpp @@ -20,9 +20,11 @@ ******************************************************************************/ // class header -#include + // guacamole headers +#include +#include #include #include #include @@ -49,7 +51,13 @@ unsigned PBRLoader::model_counter_ = 0; _supported_file_extensions(), node_counter_(0) { - _supported_file_extensions.insert("xyz_all"); + _supported_file_extensions.insert("xyz_all"); + if (!MaterialDatabase::instance()->is_supported("gua_pbr")) + { + create_resource_material("gua_pbr", + Resources::materials_gua_pbr_gsd, + Resources::materials_gua_pbr_gmd); + } } ///////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/renderer/PBRUberShader.cpp b/src/gua/renderer/PBRUberShader.cpp index e00dedd25..5bbe00033 100644 --- a/src/gua/renderer/PBRUberShader.cpp +++ b/src/gua/renderer/PBRUberShader.cpp @@ -42,14 +42,7 @@ namespace gua { PBRUberShader::PBRUberShader() : GeometryUberShader(), near_plane_value_(0.0f), height_divided_by_top_minus_bottom_(0.0f) -{ - if (!MaterialDatabase::instance()->is_supported(default_pbr_material_name())) - { - create_resource_material(default_pbr_material_name(), - Resources::materials_gua_pbr_gsd, - Resources::materials_gua_pbr_gmd); - } -} +{} //////////////////////////////////////////////////////////////////////////////// diff --git a/src/gua/renderer/PLODLoader.cpp b/src/gua/renderer/PLODLoader.cpp index 30eeb50b2..4515b03c5 100644 --- a/src/gua/renderer/PLODLoader.cpp +++ b/src/gua/renderer/PLODLoader.cpp @@ -20,12 +20,13 @@ ******************************************************************************/ // class header -#include #include #include -#include // guacamole headers +#include +#include +#include #include #include #include @@ -38,13 +39,12 @@ #include #include #include - +#include #include namespace gua { unsigned PLODLoader::model_counter_ = 0; - ///////////////////////////////////////////////////////////////////////////// PLODLoader::PLODLoader() @@ -53,6 +53,14 @@ unsigned PLODLoader::model_counter_ = 0; node_counter_(0) { _supported_file_extensions.insert("kdn"); + + if (!MaterialDatabase::instance()->is_supported("gua_pbr")) + { + create_resource_material("gua_pbr", + Resources::materials_gua_pbr_gsd, + Resources::materials_gua_pbr_gmd); + } + } diff --git a/src/gua/renderer/PLODUberShader.cpp b/src/gua/renderer/PLODUberShader.cpp index 15f8894e6..ea4dd2fdc 100644 --- a/src/gua/renderer/PLODUberShader.cpp +++ b/src/gua/renderer/PLODUberShader.cpp @@ -52,14 +52,7 @@ namespace gua { PLODUberShader::PLODUberShader() : GeometryUberShader(), near_plane_value_(0.0f), height_divided_by_top_minus_bottom_(0.0f) -{ - if (!MaterialDatabase::instance()->is_supported(default_plod_material_name())) - { - create_resource_material(default_plod_material_name(), - Resources::materials_gua_pbr_gsd, - Resources::materials_gua_pbr_gmd); - } -} +{} //////////////////////////////////////////////////////////////////////////////// From 2c1b31880934fda130b63e6f7680e897cccd88b8 Mon Sep 17 00:00:00 2001 From: Adrian Kreskowski Date: Tue, 27 May 2014 15:33:02 +0200 Subject: [PATCH 36/45] wrap pbr::ren::policy class --- include/gua/renderer/PLODLoader.hpp | 7 +++++-- src/gua/renderer/PLODLoader.cpp | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/gua/renderer/PLODLoader.hpp b/include/gua/renderer/PLODLoader.hpp index 90799c9f4..03cee50a9 100644 --- a/include/gua/renderer/PLODLoader.hpp +++ b/include/gua/renderer/PLODLoader.hpp @@ -39,6 +39,7 @@ class InnerNode; class GeometryNode; class PLODRessource; + class PLODLoader : public GeometryLoader { public: @@ -60,10 +61,12 @@ class PLODLoader : public GeometryLoader { std::shared_ptr create_geometry_from_file(std::string const& nodename, std::string const& kdnfile); - - bool is_supported(std::string const& file_name) const; + void set_upload_budget_in_mb(const size_t upload_budget); + void set_render_budget_in_mb(const size_t render_budget); + void set_out_of_core_budget_in_mb(const size_t out_of_core_budget); + private: unsigned node_counter_; static unsigned model_counter_; diff --git a/src/gua/renderer/PLODLoader.cpp b/src/gua/renderer/PLODLoader.cpp index 4515b03c5..641daf8c1 100644 --- a/src/gua/renderer/PLODLoader.cpp +++ b/src/gua/renderer/PLODLoader.cpp @@ -22,6 +22,7 @@ // class header #include #include +#include // guacamole headers #include @@ -107,5 +108,22 @@ unsigned PLODLoader::model_counter_ = 0; : _supported_file_extensions.count(filename_decomposition.back()) > 0; } + void PLODLoader::set_upload_budget_in_mb(const size_t upload_budget) + { + pbr::ren::Policy* policy = pbr::ren::Policy::GetInstance(); + policy->set_upload_budget_in_mb(upload_budget); + } + + void PLODLoader::set_render_budget_in_mb(const size_t render_budget) + { + pbr::ren::Policy* policy = pbr::ren::Policy::GetInstance(); + policy->set_render_budget_in_mb(render_budget); + } + + void PLODLoader::set_out_of_core_budget_in_mb(const size_t out_of_core_budget) + { + pbr::ren::Policy* policy = pbr::ren::Policy::GetInstance(); + policy->set_out_of_core_budget_in_mb(out_of_core_budget); + } } From 95ab2343edf4206eb2c08bc57285bce228e43696 Mon Sep 17 00:00:00 2001 From: Andreas-Christoph Bernstein Date: Tue, 27 May 2014 15:33:20 +0200 Subject: [PATCH 37/45] Add protobuf. --- CMakeLists.txt | 5 ++++- src/CMakeLists.txt | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 34991282e..ba584786d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ include(find_compiler) include(FindSchism) include(FindBoost) include(FindZMQ) +include(find_protobuf) if (UNIX) set (BULLET_ROOT "/opt/bullet/default" CACHE PATH "Set to your bullet install path.") @@ -66,6 +67,7 @@ set(LIB_PATHS ${SCHISM_LIBRARY_DIRS} ${PBR_LIBRARY_DIRS} ${ZMQ_LIBRARY_DIRS} + ${PROTOBUF_LIBRARY_DIRS} ) set(INCLUDE_PATHS @@ -79,7 +81,8 @@ set(INCLUDE_PATHS ${GLEW_INCLUDE_DIRS} ${BULLET_INCLUDE_DIRS} ${PBR_INCLUDE_DIRS} - ${ZMQ_INCLUDE_DIRS} + ${ZMQ_INCLUDE_DIRS} + ${PROTOBUF_INCLUDE_DIRS} ) if (NOT CMAKE_BUILD_TYPE) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 953fbb368..e7301a837 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -115,6 +115,7 @@ ENDIF (UNIX) # link general libraries TARGET_LINK_LIBRARIES( guacamole general ${ZMQ_LIBRARIES}) +TARGET_LINK_LIBRARIES( guacamole general ${PROTOBUF_LIBRARIES}) # link release libraries TARGET_LINK_LIBRARIES( guacamole optimized ${JSON_LIBRARY}) From 834ec9580f92546b5b6c01b42293de08c61d20ef Mon Sep 17 00:00:00 2001 From: Andre Schollmeyer Date: Tue, 27 May 2014 19:02:19 +0200 Subject: [PATCH 38/45] fixed merge-bug --- src/gua/scenegraph/Node.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/gua/scenegraph/Node.cpp b/src/gua/scenegraph/Node.cpp index e41912ce6..03dd935fb 100644 --- a/src/gua/scenegraph/Node.cpp +++ b/src/gua/scenegraph/Node.cpp @@ -68,13 +68,6 @@ namespace gua { world_transform_ = get_transform(); } else { world_transform_ = parent_->world_transform_ * get_transform(); - } - - if (is_root()) { - world_transform_ = get_transform(); - } - else { - world_transform_ = parent_->world_transform_ * get_transform(); } if (world_transform_ != old_world_trans) { From fbe6a95561a47cb28b76eb5fc05f225666f62baa Mon Sep 17 00:00:00 2001 From: Andreas-Christoph Bernstein Date: Wed, 28 May 2014 11:22:31 +0200 Subject: [PATCH 39/45] Add getter. --- include/gua/renderer/PLODLoader.hpp | 14 ++++++++------ src/gua/renderer/PLODLoader.cpp | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/include/gua/renderer/PLODLoader.hpp b/include/gua/renderer/PLODLoader.hpp index 03cee50a9..9945b5477 100644 --- a/include/gua/renderer/PLODLoader.hpp +++ b/include/gua/renderer/PLODLoader.hpp @@ -32,14 +32,13 @@ #include #include -namespace gua{ +namespace gua { class Node; class InnerNode; class GeometryNode; class PLODRessource; - class PLODLoader : public GeometryLoader { public: @@ -63,9 +62,13 @@ class PLODLoader : public GeometryLoader { bool is_supported(std::string const& file_name) const; - void set_upload_budget_in_mb(const size_t upload_budget); - void set_render_budget_in_mb(const size_t render_budget); - void set_out_of_core_budget_in_mb(const size_t out_of_core_budget); + void set_upload_budget_in_mb(const size_t upload_budget); + void set_render_budget_in_mb(const size_t render_budget); + void set_out_of_core_budget_in_mb(const size_t out_of_core_budget); + + const size_t get_upload_budget_in_mb() const; + const size_t get_render_budget_in_mb() const; + const size_t get_out_of_core_budget_in_mb() const; private: unsigned node_counter_; @@ -73,7 +76,6 @@ class PLODLoader : public GeometryLoader { boost::unordered_set _supported_file_extensions; }; - } #endif // GUA_PLOD_LOADER_HPP diff --git a/src/gua/renderer/PLODLoader.cpp b/src/gua/renderer/PLODLoader.cpp index 641daf8c1..1e88c7ab6 100644 --- a/src/gua/renderer/PLODLoader.cpp +++ b/src/gua/renderer/PLODLoader.cpp @@ -126,4 +126,19 @@ unsigned PLODLoader::model_counter_ = 0; policy->set_out_of_core_budget_in_mb(out_of_core_budget); } +const size_t PLODLoader::get_upload_budget_in_mb() const { + pbr::ren::Policy* policy = pbr::ren::Policy::GetInstance(); + return policy->upload_budget_in_mb(); +} + +const size_t PLODLoader::get_render_budget_in_mb() const { + pbr::ren::Policy* policy = pbr::ren::Policy::GetInstance(); + return policy->render_budget_in_mb(); +} + +const size_t PLODLoader::get_out_of_core_budget_in_mb() const { + pbr::ren::Policy* policy = pbr::ren::Policy::GetInstance(); + return policy->out_of_core_budget_in_mb(); +} + } From 8badf201ae65d0189f682c15a3159094de3b6e63 Mon Sep 17 00:00:00 2001 From: Andreas-Christoph Bernstein Date: Wed, 28 May 2014 13:23:10 +0200 Subject: [PATCH 40/45] Add find_protobuf.cmake. --- cmake/modules/find_protobuf.cmake | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 cmake/modules/find_protobuf.cmake diff --git a/cmake/modules/find_protobuf.cmake b/cmake/modules/find_protobuf.cmake new file mode 100755 index 000000000..2c22523a7 --- /dev/null +++ b/cmake/modules/find_protobuf.cmake @@ -0,0 +1,68 @@ +SET(PROTOBUF_INCLUDE_SEARCH_DIRS + /opt/protobuf/current/src +) + +SET(PROTOBUF_LIBRARY_SEARCH_DIRS + /opt/protobuf/current/src/.libs +) + +message("-- checking for protobuf") + +IF (NOT PROTOBUF_INCLUDE_DIRS) + + SET(_PROTOBUF_FOUND_INC_DIRS "") + + FOREACH(_SEARCH_DIR ${PROTOBUF_INCLUDE_SEARCH_DIRS}) + FIND_PATH(_CUR_SEARCH + NAMES google/protobuf/stubs/common.h + PATHS ${_SEARCH_DIR} + NO_DEFAULT_PATH) + IF (_CUR_SEARCH) + LIST(APPEND _PROTOBUF_FOUND_INC_DIRS ${_CUR_SEARCH}) + ENDIF(_CUR_SEARCH) + SET(_CUR_SEARCH _CUR_SEARCH-NOTFOUND CACHE INTERNAL "internal use") + ENDFOREACH(_SEARCH_DIR ${PROTOBUF_INCLUDE_SEARCH_DIRS}) + + IF (NOT _PROTOBUF_FOUND_INC_DIRS) + MESSAGE(FATAL_ERROR "find_protobuf.cmake: unable to find protobuf headers") + ENDIF (NOT _PROTOBUF_FOUND_INC_DIRS) + + FOREACH(_INC_DIR ${_PROTOBUF_FOUND_INC_DIRS}) + LIST(APPEND PROTOBUF_INCLUDE_DIRS ${_INC_DIR}) + ENDFOREACH(_INC_DIR ${_BOOST_FOUND_INC_DIRS}) + +ENDIF(NOT PROTOBUF_INCLUDE_DIRS) + +IF ( PROTOBUF_INCLUDE_DIRS + AND NOT PROTOBUF_LIBRARIES) + + SET(_PROTOBUF_FOUND_LIB_DIR "") + SET(_PROTOBUF_POSTFIX "") + + FOREACH(_SEARCH_DIR ${PROTOBUF_LIBRARY_SEARCH_DIRS}) + FIND_PATH(_CUR_SEARCH + NAMES libprotobuf.so + PATHS ${_SEARCH_DIR} + NO_DEFAULT_PATH) + IF (_CUR_SEARCH) + LIST(APPEND _PROTOBUF_FOUND_LIB_DIR ${_SEARCH_DIR}) + ENDIF(_CUR_SEARCH) + SET(_CUR_SEARCH _CUR_SEARCH-NOTFOUND CACHE INTERNAL "internal use") + ENDFOREACH(_SEARCH_DIR ${PROTOBUF_LIBRARY_SEARCH_DIRS}) + + IF (NOT _PROTOBUF_FOUND_LIB_DIR) + MESSAGE(FATAL_ERROR "find_protobuf.cmake: unable to find protobuf library") + ELSE (NOT _PROTOBUF_FOUND_LIB_DIR) + message("-- found matching version") + ENDIF (NOT _PROTOBUF_FOUND_LIB_DIR) + + FOREACH(_LIB_DIR ${_PROTOBUF_FOUND_LIB_DIR}) + LIST(APPEND PROTOBUF_LIBRARIES ${_LIB_DIR}/libprotobuf.so) + ENDFOREACH(_LIB_DIR ${_PROTOBUF_FOUND_INC_DIRS}) + + +ENDIF( PROTOBUF_INCLUDE_DIRS + AND NOT PROTOBUF_LIBRARIES) + + + From 9ad4a226141e64eed09c43f62f3465d5e76e7661 Mon Sep 17 00:00:00 2001 From: Andreas-Christoph Bernstein Date: Wed, 28 May 2014 14:30:45 +0200 Subject: [PATCH 41/45] Add GUACAMOLE_PBR_SUPPORT option. --- CMakeLists.txt | 32 ++++++++++++++++++++++++-------- src/CMakeLists.txt | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba584786d..7bf403b02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ set(GUACAMOLE_HOMEPAGE "http://www.GUACAMOLE.org") set(GUACAMOLE_EXENAME "GUACAMOLE") set(GUACAMOLE_PACKAGENAME "GUACAMOLE") +option (GUACAMOLE_PBR_SUPPORT "Set to enable PBR (point based rendering) support.") + # We require at least version 2.8.0 cmake_minimum_required(VERSION 2.8.0) @@ -24,6 +26,13 @@ endif(UNIX) # Location where cmake first looks for modules. list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules) +############################################################################### +# write Config.h +############################################################################### + +configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/include/gua/config.hpp.in + ${CMAKE_CURRENT_SOURCE_DIR}/include/gua/config.hpp ) + ################################################################ # Configure and find libraries ################################################################ @@ -54,8 +63,10 @@ endif (UNIX) find_package(Bullet REQUIRED) find_package(Json REQUIRED) -set(PBR_ROOT "../gualod/trunk/pbr" CACHE PATH "Set to your PBR install path.") -find_package(PBR REQUIRED) +IF (GUACAMOLE_PBR_SUPPORT) + set(PBR_ROOT "/opt/pbr/gualod/trunk/pbr" CACHE PATH "Set to your PBR install path.") + find_package(PBR REQUIRED) +endif (GUACAMOLE_PBR_SUPPORT) set(LIB_PATHS ${ASSIMP_LIBRARY_DIRS} @@ -65,7 +76,6 @@ set(LIB_PATHS ${BOOST_LIBRARY_DIRS} ${BULLET_LIBRARY_DIRS} ${SCHISM_LIBRARY_DIRS} - ${PBR_LIBRARY_DIRS} ${ZMQ_LIBRARY_DIRS} ${PROTOBUF_LIBRARY_DIRS} ) @@ -80,11 +90,15 @@ set(INCLUDE_PATHS ${GL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${BULLET_INCLUDE_DIRS} - ${PBR_INCLUDE_DIRS} ${ZMQ_INCLUDE_DIRS} ${PROTOBUF_INCLUDE_DIRS} ) +IF (GUACAMOLE_PBR_SUPPORT) + LIST(APPEND LIB_PATHS ${PBR_LIBRARY_DIRS}) + LIST(APPEND INCLUDE_PATHS ${PBR_INCLUDE_DIRS}) +ENDIF (GUACAMOLE_PBR_SUPPORT) + if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release") endif() @@ -222,8 +236,10 @@ message(STATUS " bullet:" ) message(STATUS " library: ${BULLET_LIBRARIES}" ) message(STATUS " library path: ${BULLET_LIBRARY_DIRS}" ) message(STATUS " include: ${BULLET_INCLUDE_DIRS}" ) -message(STATUS " PBR:" ) -message(STATUS " library: ${PBR_LIBRARIES}" ) -message(STATUS " library path: ${PBR_LIBRARY_DIRS}" ) -message(STATUS " include: ${PBR_INCLUDE_DIRS}" ) +IF (GUACAMOLE_PBR_SUPPORT) + message(STATUS " PBR:" ) + message(STATUS " library: ${PBR_LIBRARIES}" ) + message(STATUS " library path: ${PBR_LIBRARY_DIRS}" ) + message(STATUS " include: ${PBR_INCLUDE_DIRS}" ) +ENDIF (GUACAMOLE_PBR_SUPPORT) message(STATUS "" ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e7301a837..326678d82 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,10 +3,28 @@ ############################################################################### FILE(GLOB GUACAMOLE_CORE_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} gua/*.cpp) FILE(GLOB GUACAMOLE_RENDERER_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} gua/renderer/*.cpp) +IF (NOT GUACAMOLE_PBR_SUPPORT) +list (REMOVE_ITEM GUACAMOLE_RENDERER_SRC + gua/renderer/PBRLoader.cpp + gua/renderer/PBRRessource.cpp + gua/renderer/PBRUberShader.cpp + gua/renderer/PLODLoader.cpp + gua/renderer/PLODRessource.cpp + gua/renderer/PLODUberShader.cpp +) +ENDIF (NOT GUACAMOLE_PBR_SUPPORT) + FILE(GLOB_RECURSE GUACAMOLE_RENDERER_NURBS_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} gua/renderer/nurbs_geometry/*.cpp) FILE(GLOB_RECURSE GUACAMOLE_RENDERER_VIDEO_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} gua/renderer/video3d_geometry/*.cpp) FILE(GLOB_RECURSE GUACAMOLE_SCENEGRAPH_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} gua/scenegraph/*.cpp) +IF (NOT GUACAMOLE_PBR_SUPPORT) +list (REMOVE_ITEM GUACAMOLE_SCENEGRAPH_SRC + gua/scenegraph/PBRNode.cpp + gua/scenegraph/PLODNode.cpp +) +ENDIF (NOT GUACAMOLE_PBR_SUPPORT) + FILE(GLOB_RECURSE GUACAMOLE_UTILS_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} gua/utils/*.cpp) FILE(GLOB_RECURSE GUACAMOLE_PHYSICS_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} gua/physics/*.cpp) FILE(GLOB_RECURSE GUACAMOLE_MATH_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} gua/math/*.cpp) @@ -19,10 +37,26 @@ FILE(GLOB_RECURSE GUACAMOLE_DATABASES_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_D ############################################################################### FILE(GLOB GUACAMOLE_CORE_INC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ../include/gua/*.hpp) FILE(GLOB GUACAMOLE_RENDERER_INC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ../include/gua/renderer/*.hpp) +IF (NOT GUACAMOLE_PBR_SUPPORT) +list (REMOVE_ITEM GUACAMOLE_RENDERER_INC + ../include/gua/renderer/PLODLoader.hpp + ../include/gua/renderer/PLODRessource.hpp + ../include/gua/renderer/PLODUberShader.hpp + ../include/gua/renderer/PBRLoader.hpp + ../include/gua/renderer/PBRRessource.hpp + ../include/gua/renderer/PBRUberShader.hpp +) +ENDIF (NOT GUACAMOLE_PBR_SUPPORT) FILE(GLOB_RECURSE GUACAMOLE_RENDERER_NURBS_INC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ../include/gua/renderer/nurbs_geometry/*.hpp) FILE(GLOB_RECURSE GUACAMOLE_RENDERER_VIDEO_INC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ../include/gua/renderer/video3d_geometry/*.hpp) FILE(GLOB_RECURSE GUACAMOLE_SCENEGRAPH_INC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ../include/gua/scenegraph/*.hpp) +IF (NOT GUACAMOLE_PBR_SUPPORT) +list (REMOVE_ITEM GUACAMOLE_SCENEGRAPH_INC + ../include/gua/scenegraph/PBRNode.hpp + ../include/gua/scenegraph/PLODNode.hpp +) +ENDIF (NOT GUACAMOLE_PBR_SUPPORT) FILE(GLOB_RECURSE GUACAMOLE_UTILS_INC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ../include/gua/utils/*.hpp) FILE(GLOB_RECURSE GUACAMOLE_PHYSICS_INC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ../include/gua/physics/*.hpp) FILE(GLOB_RECURSE GUACAMOLE_MATH_INC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ../include/gua/math/*.hpp) @@ -103,7 +137,9 @@ ENDIF(MSVC) # link static libraries TARGET_LINK_LIBRARIES( guacamole general ${SCHISM_LIBRARIES}) TARGET_LINK_LIBRARIES( guacamole general ${ASSIMP_LIBRARIES}) -TARGET_LINK_LIBRARIES( guacamole general ${PBR_LIBRARIES}) +IF (GUACAMOLE_PBR_SUPPORT) + TARGET_LINK_LIBRARIES( guacamole general ${PBR_LIBRARIES}) +ENDIF (GUACAMOLE_PBR_SUPPORT) IF (UNIX) TARGET_LINK_LIBRARIES( guacamole general ${BOOST_LIBRARIES}) From ccc3a8df7675fef3360de2a7eca505a978216d66 Mon Sep 17 00:00:00 2001 From: Andreas-Christoph Bernstein Date: Wed, 28 May 2014 14:53:21 +0200 Subject: [PATCH 42/45] Fix documentation. --- include/gua/renderer/PLODLoader.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gua/renderer/PLODLoader.hpp b/include/gua/renderer/PLODLoader.hpp index 9945b5477..faaf7d46c 100644 --- a/include/gua/renderer/PLODLoader.hpp +++ b/include/gua/renderer/PLODLoader.hpp @@ -45,14 +45,14 @@ class PLODLoader : public GeometryLoader { /** * Default constructor. * - * Constructs a new and empty PBRLoader. + * Constructs a new and empty PLODLoader. */ PLODLoader(); /** * Constructor from a file. * - * Creates a new PBRLoader from a given file. + * Creates a new PLODLoader from a given file. * * \param file_name The file to load the pointclouds data from. * \param material_name The material name that was set to the parent node From b8cbff875784060975ce233314c68d772bd81fcc Mon Sep 17 00:00:00 2001 From: Andreas-Christoph Bernstein Date: Wed, 28 May 2014 14:53:43 +0200 Subject: [PATCH 43/45] Add config header. --- include/gua/config.hpp.in | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 include/gua/config.hpp.in diff --git a/include/gua/config.hpp.in b/include/gua/config.hpp.in new file mode 100644 index 000000000..f35bcc057 --- /dev/null +++ b/include/gua/config.hpp.in @@ -0,0 +1,27 @@ +/****************************************************************************** + * guacamole - delicious VR * + * * + * Copyright: (c) 2011-2013 Bauhaus-Universität Weimar * + * Contact: felix.lauer@uni-weimar.de / simon.schneegans@uni-weimar.de * + * * + * This program is free software: you can redistribute it and/or modify it * + * under the terms of the GNU General Public License as published by the Free * + * Software Foundation, either version 3 of the License, or (at your option) * + * any later version. * + * * + * This program is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * + * for more details. * + * * + * You should have received a copy of the GNU General Public License along * + * with this program. If not, see . * + * * + ******************************************************************************/ + +#ifndef GUA_CONFIG_HPP +#define GUA_CONFIG_HPP + +#cmakedefine GUACAMOLE_PBR_SUPPORT + +#endif // GUA_CONFIG_HPP From 7bfc124398c790714acafbbd64610d1aec26c003 Mon Sep 17 00:00:00 2001 From: Andreas-Christoph Bernstein Date: Wed, 28 May 2014 14:54:12 +0200 Subject: [PATCH 44/45] Ignore files. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c31256571..8d3208905 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ editor/guacamole_editor guarc/guarc guacamole.sublime-workspace include/gua/generated/* +include/gua/config.hpp src/gua/generated/* tags .nfs* From f5a275ea96e589ff0b4ddcd81fb453b96da8e15e Mon Sep 17 00:00:00 2001 From: Andreas-Christoph Bernstein Date: Wed, 28 May 2014 15:26:41 +0200 Subject: [PATCH 45/45] Bump version number. --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bf403b02..393a9c5f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,8 @@ PROJECT(GUACAMOLE CXX) # version number set(GUACAMOLE_MAJOR 0) -set(GUACAMOLE_MINOR 7) -set(GUACAMOLE_PATCH 2) +set(GUACAMOLE_MINOR 8) +set(GUACAMOLE_PATCH 0) set(GUACAMOLE_VERSION ${GUACAMOLE_MAJOR}.${GUACAMOLE_MINOR}.${GUACAMOLE_PATCH}) set(GUACAMOLE_DESCRIPTION "GUACAMOLE - an astonishing virtual reality engine") set(GUACAMOLE_HOMEPAGE "http://www.GUACAMOLE.org")