Commit 8006f6a8 authored by Romain Pacanowski's avatar Romain Pacanowski

Merge branch 'wip_previewer' of gitlab.inria.fr:pacanows/MRF into wip_previewer

parents 69715404 9b1ff7eb
......@@ -55,13 +55,25 @@ file(GLOB_RECURSE EXT_SRCS
../../externals/tinyply/*.cpp
../../externals/tinyply/*.c)
# MRF SOURCES
file( GLOB_RECURSE MRF_SRC
../../mrf/geometry/*.c
../../mrf/geometry/*.cpp )
find_package(OpenMP)
add_definitions(-DDATA_DIR="${PROJECT_SOURCE_DIR}/data")
add_executable(previewer ${EXT_SRCS} ${SRC_FILES} )
add_executable(previewer ${MRF_SRC} ${EXT_SRCS} ${SRC_FILES} )
if(OpenMP_CXX_FOUND)
target_link_libraries(previewer glfw ${GLFW_LIBRARIES} glbinding SOIL OpenMP::OpenMP_CXX)
else()
target_link_libraries(previewer glfw ${GLFW_LIBRARIES} glbinding SOIL)
endif()
target_link_libraries(previewer glfw ${GLFW_LIBRARIES} glbinding SOIL)
function(IndicateExternalFile _target)
foreach(_file IN ITEMS ${ARGN})
......
......@@ -28,5 +28,7 @@ void main(void) {
//out_color = vec4(ambient * v_color + blinn(normalize(v_normal),normalize(v_view), lightDir, v_color, spec_color, shininess),1.0);
//out_color = vec4(ambient + blinn(normalize(v_normal),normalize(v_view), lightDir, v_color, spec_color, shininess),1.0);
out_color.xyz = v_normal*vec3(0.5) + vec3(0.5) ;
out_color.xyz = normalize(v_normal)*vec3(0.5) + vec3(0.5) ;
out_color.w = 1.0;
}
#include "geometry/computation.hpp"
#include "mrf/geometry/computation.hpp"
//STL
#include <vector>
#include <iostream>
#include "math/math.hpp"
#include "mrf/math/math.hpp"
using namespace mrf::math;
......@@ -16,7 +16,7 @@ namespace geom
void
Computation::computeNormals( float * vertices,
unsigned int num_of_vertices,
unsigned int * index_faces,
unsigned int const * index_faces,
unsigned int num_of_faces,
float ** normals)
{
......
......@@ -29,7 +29,7 @@ class MRF_EXPORT Computation
public:
static void computeNormals( float * vertices,
unsigned int num_of_vertices,
unsigned int * index_faces,
unsigned int const * index_faces,
unsigned int num_of_faces,
float ** normals);
......
#include "geometry/direction.hpp"
#include "mrf/geometry/direction.hpp"
namespace mrf
{
......
......@@ -11,7 +11,7 @@
#include "mrf/mrf_dll.hpp"
#include "mrf/mrf_types.hpp"
#include "lighting/i_light.hpp"
#include "mrf/lighting/i_light.hpp"
namespace mrf
{
......
......@@ -10,7 +10,7 @@
#include "mrf/mrf_types.hpp"
#include "math/math.hpp"
#include "mrf/math/math.hpp"
namespace mrf
{
......
......@@ -8,6 +8,53 @@ template< class VEC3_TYPE >
_Mesh<VEC3_TYPE>::_Mesh()
{}
template< class VEC3_TYPE >
_Mesh<VEC3_TYPE>::_Mesh( std::vector<VEC3_TYPE> const & vertices,
std::vector< unsigned int > const & face_indexes,
unsigned int unique_material_id_for_all_faces )
{
std::vector< unsigned int> material_per_faces;
material_per_faces.resize(face_indexes.size() );
std::fill( material_per_faces.begin(), material_per_faces.end(), unique_material_id_for_all_faces );
std::vector<VEC3_TYPE> normals;
computeNormals( vertices, face_indexes, normals);
std::vector<VEC3_TYPE> tangents;
generateTangentsPerVertex( vertices, normals, tangents );
std::vector<VEC3_TYPE> uvw;
uvw.resize( vertices.size() );
initializeMeshData( vertices, normals, uvw, tangents, face_indexes, material_per_faces );
}
template< class VEC3_TYPE >
_Mesh<VEC3_TYPE>::_Mesh( std::vector<VEC3_TYPE> const & vertices,
std::vector< unsigned int > const & face_indexes,
std::vector< unsigned int> const & material_per_faces)
{
std::cout << " Constructing Mesh" << std::endl;
std::vector<VEC3_TYPE> normals;
computeNormals( vertices, face_indexes, normals);
std::vector<VEC3_TYPE> tangents;
generateTangentsPerVertex( vertices, normals, tangents );
std::vector<VEC3_TYPE> uvw;
uvw.resize( vertices.size() );
initializeMeshData( vertices, normals, uvw, tangents, face_indexes, material_per_faces );
std::cout << " Mesh constructed" << std::endl;
}
template< class VEC3_TYPE >
_Mesh<VEC3_TYPE>::_Mesh( std::vector<VEC3_TYPE> const & vertices,
std::vector<VEC3_TYPE> const & normals,
......
......@@ -18,6 +18,7 @@
#include "mrf/geometry/intersectable.hpp"
#include "mrf/geometry/meshable.hpp"
#include "mrf/geometry/object.hpp"
#include "mrf/geometry/computation.hpp"
namespace mrf
......@@ -85,6 +86,14 @@ class _Mesh : public Intersectable
**/
_Mesh();
_Mesh( std::vector<VEC3_TYPE> const & vertices,
std::vector< unsigned int > const & face_indexes,
unsigned int unique_material_id_for_all_faces );
_Mesh( std::vector<VEC3_TYPE> const & vertices,
std::vector< unsigned int > const & face_indexes,
std::vector< unsigned int> const & material_per_faces);
_Mesh( std::vector<VEC3_TYPE> const & vertices,
std::vector<VEC3_TYPE> const & normals,
std::vector<VEC3_TYPE> const & uvw,
......@@ -188,6 +197,14 @@ class _Mesh : public Intersectable
virtual unsigned int intersectionCost() const ;
virtual unsigned int memorySize() const ;
/**
* @brief Compute Normals from vertices
*/
static void computeNormals( std::vector<VEC3_TYPE> const & vertices,
std::vector< unsigned int> const & faces,
std::vector<VEC3_TYPE> & normals );
/**
* @brief Compute Tangents for each vertex using Lengyel’s Method
*
......@@ -294,6 +311,69 @@ operator<<( std::ostream & os, _Mesh<VEC3_TYPE> const & mesh )
return os;
}
template< class VEC3_TYPE >
void
_Mesh<VEC3_TYPE>::computeNormals( std::vector<VEC3_TYPE> const & vertices,
std::vector< unsigned int> const & faces,
std::vector<VEC3_TYPE> & normals )
{
using namespace mrf::math;
normals.resize(vertices.size() );
#pragma omp parallel for
for( unsigned int i = 0; i < normals.size(); ++i )
{
normals[i] = VEC3_TYPE::zero();
}
unsigned int const num_faces = faces.size()/3;
// TODO Open MP
#pragma omp parallel for default(none) shared(normals,faces,vertices)
for( unsigned int f=0; f < num_faces; f++ )
{
unsigned int const i1 = faces[3*f];
unsigned int const i2 = faces[3*f+1];
unsigned int const i3 = faces[3*f+2];
Vec3f const & a = vertices[i1];
Vec3f const & b = vertices[i2];
Vec3f const & c = vertices[i3];
Vec3f const ab = (b - a);
Vec3f const ac = (c - a);
Vec3f const triangle_normal = ab.cross(ac);
#pragma omp atomic update
normals[i1][0] += triangle_normal.x();
normals[i1][1] += triangle_normal.y();
normals[i1][2] += triangle_normal.z();
#pragma omp atomic update
normals[i2][0] += triangle_normal.x();
normals[i2][1] += triangle_normal.y();
normals[i2][2] += triangle_normal.z();
#pragma omp atomic update
normals[i3][0] += triangle_normal.x();
normals[i3][1] += triangle_normal.y();
normals[i3][2] += triangle_normal.z();
}
//Normalization Pass
#pragma omp parallel for
for( unsigned int i=0; i < normals.size(); i++)
{
normals[i].normalize();
}
}
template< class VEC3_TYPE >
void
_Mesh<VEC3_TYPE>::computeTangentsFromUV( std::vector<VEC3_TYPE> const & vertices,
......
#include "geometry/object.hpp"
#include "mrf/geometry/object.hpp"
namespace mrf
{
......
#include "geometry/point.hpp"
#include "geometry/aabbox.hpp"
#include "mrf/geometry/point.hpp"
#include "mrf/geometry/aabbox.hpp"
namespace mrf
{
......
......@@ -11,8 +11,8 @@
#include "mrf/mrf_dll.hpp"
#include "mrf/mrf_types.hpp"
#include "math/vec3.hpp"
#include "lighting/i_light.hpp"
#include "mrf/math/vec3.hpp"
#include "mrf/lighting/i_light.hpp"
namespace mrf
......
#include "geometry/quad.hpp"
#include "math/math.hpp"
#include "geometry/mesh.hpp"
#include "mrf/geometry/quad.hpp"
#include "mrf/math/math.hpp"
#include "mrf/geometry/mesh.hpp"
......
......@@ -11,10 +11,10 @@
#include "mrf/mrf_dll.hpp"
#include "mrf/mrf_types.hpp"
#include "math/vec3.hpp"
#include "lighting/i_light.hpp"
#include "geometry/shape.hpp"
#include "geometry/meshable.hpp"
#include "mrf/math/vec3.hpp"
#include "mrf/lighting/i_light.hpp"
#include "mrf/geometry/shape.hpp"
#include "mrf/geometry/meshable.hpp"
namespace mrf
{
......
#include "geometry/shape.hpp"
#include "mrf/geometry/shape.hpp"
......
......@@ -16,10 +16,10 @@
#include <memory>
//MRF
#include "geometry/object.hpp"
#include "geometry/intersectable.hpp"
#include "geometry/mesh.hpp"
#include "math/math.hpp"
#include "mrf/geometry/object.hpp"
#include "mrf/geometry/intersectable.hpp"
#include "mrf/geometry/mesh.hpp"
#include "mrf/math/math.hpp"
namespace mrf
{
......
#include "geometry/sphere.hpp"
#include "math/math.hpp"
#include "mrf/geometry/sphere.hpp"
#include "mrf/math/math.hpp"
using namespace mrf::math;
......
......@@ -13,12 +13,12 @@
//MRF
#include "geometry/shape.hpp"
#include "geometry/meshable.hpp"
#include "geometry/mesh.hpp"
#include "geometry/local_frame.hpp"
#include "lighting/i_light.hpp"
#include "math/vec3.hpp"
#include "mrf/geometry/shape.hpp"
#include "mrf/geometry/meshable.hpp"
#include "mrf/geometry/mesh.hpp"
#include "mrf/geometry/local_frame.hpp"
#include "mrf/lighting/i_light.hpp"
#include "mrf/math/vec3.hpp"
namespace mrf
......
......@@ -76,8 +76,9 @@ inline void loadMeshFromPLY(std::string const &ply_filename, std::shared_ptr< mr
catch (const std::exception &e)
{
// FIX ME ! Allow to construct a mesh even without normals... recompute them
std::cerr << "Caught tinyply exception: " << e.what() << std::endl;
throw std::runtime_error( std::string("Caught tinyply exception NO NORMALS")+e.what() );
std::cout << "No normals per vertex " << std::endl;
// throw std::runtime_error( std::string("Caught tinyply exception NO NORMALS")+e.what() );
tiny_normals = nullptr;
}
try
......@@ -105,8 +106,7 @@ inline void loadMeshFromPLY(std::string const &ply_filename, std::shared_ptr< mr
if (tiny_vertices)
std::cout << "\tRead vertices:" << tiny_vertices->count;
if (tiny_normals)
std::cout << "\tRead normals:" << tiny_normals->count;
if (tiny_texcoords)
std::cout << "\tRead texcoords:" << tiny_texcoords->count;
if (tiny_faces)
......@@ -114,21 +114,17 @@ inline void loadMeshFromPLY(std::string const &ply_filename, std::shared_ptr< mr
const size_t numVerticesBytes = tiny_vertices->buffer.size_bytes();
vertices.resize(tiny_vertices->count);
std::memcpy(vertices.data(), tiny_vertices->buffer.get(), numVerticesBytes);
const size_t numNormalsBytes = tiny_normals->buffer.size_bytes();
normals.resize(tiny_normals->count);
std::memcpy(normals.data(), tiny_normals->buffer.get(), numNormalsBytes);
std::memcpy( (void *) vertices.data(), (const void *) tiny_vertices->buffer.get(), (size_t) numVerticesBytes);
if (tiny_texcoords)
{
const size_t numUVsBytes = tiny_texcoords->buffer.size_bytes();
uvs.resize(tiny_texcoords->count);
std::memcpy(uvs.data(), tiny_texcoords->buffer.get(), numUVsBytes);
std::memcpy((void *) uvs.data(), (const void *) tiny_texcoords->buffer.get(), (size_t) numUVsBytes);
}
else
{
uvs.resize(normals.size(), mrf::math::Vec3f());
uvs.resize(vertices.size(), mrf::math::Vec3f());
}
if (tiny_faces->t != tinyply::Type::UINT32 && tiny_faces->t != tinyply::Type::INT32)
......@@ -163,7 +159,21 @@ inline void loadMeshFromPLY(std::string const &ply_filename, std::shared_ptr< mr
enforced_materials.resize(index_faces.size() / 3);
std::fill(enforced_materials.begin(), enforced_materials.end(), current_material_index);
a_mesh = std::make_shared< mrf::geom::_Mesh<VEC3_TYPE> >( mrf::geom::_Mesh<VEC3_TYPE>(vertices, normals, uvs, index_faces, enforced_materials));
if (tiny_normals)
{
std::cout << "\tRead normals:" << tiny_normals->count;
const size_t numNormalsBytes = tiny_normals->buffer.size_bytes();
normals.resize(tiny_normals->count);
std::memcpy( (void *) normals.data(), (const void *) tiny_normals->buffer.get(), (size_t) numNormalsBytes);
a_mesh = std::make_shared< mrf::geom::_Mesh<VEC3_TYPE> >( mrf::geom::_Mesh<VEC3_TYPE>(vertices, normals, uvs, index_faces, enforced_materials));
}
else // No normals provided
{
a_mesh = std::make_shared< mrf::geom::_Mesh<VEC3_TYPE> >( mrf::geom::_Mesh<VEC3_TYPE>(vertices, index_faces, enforced_materials));
}
}
catch (const std::exception &e)
{
......
......@@ -13,9 +13,9 @@
//MRF
#include "geometry/aabbox.hpp"
#include "geometry/local_frame.hpp"
#include "math/math.hpp"
#include "mrf/geometry/aabbox.hpp"
#include "mrf/geometry/local_frame.hpp"
#include "mrf/math/math.hpp"
namespace mrf
......
......@@ -14,4 +14,4 @@ typedef unsigned int uint;
typedef unsigned char uchar;
typedef std::tuple<uint, uint, uint> Triplet_uint;
typedef std::tuple<int, int, int> Triplet_int;
};
\ No newline at end of file
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment