Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Draft Material in core #4

Draft
wants to merge 56 commits into
base: material-in-core
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
2afb65d
[core] add sample,pdf, bsdf methods for lambertian model
grandch Jun 13, 2023
efd38e9
[core] add bsdf, sample and pdf implementation for blinnphong model
grandch Jun 15, 2023
15719a5
[core] change randomEngine for a static member
grandch Jun 19, 2023
ec36b7a
[core] first review fix
grandch Jun 20, 2023
0e3ac21
[core] Vector3f to Vector3
grandch Jun 20, 2023
055f30b
[core] fix blinnphong sampling, add roughness conv
grandch Jun 20, 2023
f2b9bcf
[ocre] doc evalBSDF
grandch Jun 20, 2023
8f320af
[core] clang format
grandch Jun 20, 2023
862a60b
[core] return reference to random generator
grandch Jun 21, 2023
151309f
[core] move math methods to LinearAlgebra
grandch Jun 21, 2023
4672268
first uniformgenerator and importancesampler
grandch Jun 27, 2023
1df63e3
wip
grandch Jun 27, 2023
4979046
add blinnphongSphereSampler
grandch Jun 28, 2023
e6b2d5f
wip random module
grandch Jun 29, 2023
937a575
add pdf methods
grandch Jun 29, 2023
f377311
add local frame to sample parameters
grandch Jun 29, 2023
4af8670
remove sampling, pdf methods in linearalgebra
grandch Jun 29, 2023
f0d5661
remove ImportanceSampler class
grandch Jul 3, 2023
a3f48e8
add UniformGenerator as optionnal parameter of material
grandch Jul 3, 2023
a5a6bf4
make samplers CRTP for static methods
grandch Jul 3, 2023
f206a5d
fix include
grandch Jul 4, 2023
1c4efe6
fix crtp
grandch Jul 5, 2023
4d77fbb
example
grandch Jul 5, 2023
b03287c
fix samplers
grandch Jul 7, 2023
1c976db
add plot.py
grandch Jul 7, 2023
5101d68
use blinn-phong exponent instead of roughness
grandch Jul 11, 2023
2ceef6c
fix operators precedence
grandch Jul 11, 2023
0c0195b
fix plot samples path
grandch Jul 11, 2023
9af5643
fix evalbsdf
grandch Jul 19, 2023
f2e323c
fix blinnphong sampling
grandch Jul 24, 2023
f73e500
plot sample
grandch Jul 24, 2023
1f6aca9
remove getRoughness method
grandch Jul 25, 2023
714e748
remove redundant field from blinnphongmaterialmodel
grandch Jul 25, 2023
670adaa
precomputed luminance
grandch Jul 25, 2023
443bd9b
private and protected fields
grandch Jul 25, 2023
7f90533
modify constructors*
grandch Jul 25, 2023
5cec94a
store random generator with shared_ptr
grandch Jul 25, 2023
d20b9f2
remove spherical coordinate based methods
grandch Jul 26, 2023
6b79b65
modify names of methods
grandch Jul 26, 2023
6798fd3
add computeLuminance method
grandch Jul 26, 2023
84fe1c8
fix frame usage in blinnphong specular sampling
grandch Jul 26, 2023
bfb19b7
remove unused u parameter for sampling
grandch Jul 26, 2023
5919435
first doc
grandch Jul 26, 2023
8c3ff9f
quick doc for example app
grandch Jul 26, 2023
09fd139
doc
grandch Jul 27, 2023
86a561d
wip unittests
grandch Jul 31, 2023
bc363ab
fix blinnphong pdf
grandch Jul 31, 2023
b1de2ba
fix reflect method
grandch Jul 31, 2023
326b312
wip unittests
grandch Jul 31, 2023
ecdedef
refix reflect method
grandch Jul 31, 2023
c3a0886
forgot (
grandch Jul 31, 2023
decdc5d
rewrite specular sampling methods
grandch Aug 3, 2023
f1fb47d
first step refactoring
grandch Aug 3, 2023
90eac6d
wip unit tests
grandch Aug 3, 2023
c385d8a
tests
grandch Aug 25, 2023
3f80c49
virtual destructor uniform generator
grandch Aug 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ foreach(
MaterialEdition
ParameterEdition
Picking
PlotImportanceSampler
RawShaderMaterial
SimpleAnimation
SimpleSimulation
Expand Down
61 changes: 61 additions & 0 deletions examples/PlotImportanceSampler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
cmake_minimum_required(VERSION 3.16)
cmake_policy(SET CMP0042 NEW)

project(PlotImportanceSampler)

# ------------------------------------------------------------------------------
# set wanted application defaults for cmake settings
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
# Set default install location to installed-<Compiler_ID> folder in build dir we do not want to
# install to /usr by default
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX
"${CMAKE_CURRENT_BINARY_DIR}/installed-${CMAKE_CXX_COMPILER_ID}-${CMAKE_BUILD_TYPE}"
CACHE PATH "Install path prefix, prepended onto install directories." FORCE
)
message("Set install prefix to ${CMAKE_INSTALL_PREFIX}")
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)

# ------------------------------------------------------------------------------

find_package(Radium REQUIRED Core)

# ------------------------------------------------------------------------------

set(app_sources main.cpp)
set(app_headers)
set(app_resources)

# to install the app as a redistribuable bundle on macos, add MACOSX_BUNDLE when calling
# add_executable
add_executable(${PROJECT_NAME} ${app_sources} ${app_headers} ${app_uis} ${app_resources})

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
target_compile_options(
${PROJECT_NAME}
PRIVATE /MP
/W4
/wd4251
/wd4592
/wd4127
/Zm200
$<$<CONFIG:Release>:
/Gw
/GS-
/GL
/GF
>
PUBLIC
)
endif()

target_link_libraries(${PROJECT_NAME} PUBLIC Radium::Core)

# configure the application
configure_radium_app(NAME ${PROJECT_NAME})
68 changes: 68 additions & 0 deletions examples/PlotImportanceSampler/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <Core/Material/BlinnPhongMaterialModel.hpp>
#include <Core/Random/BlinnPhongSphereSampler.hpp>
#include <Core/Random/CosineWeightedSphereSampler.hpp>
#include <Core/Random/MersenneTwisterGenerator.hpp>
#include <Core/Random/UniformSphereSampler.hpp>

#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <vector>

int main( int /*argc*/, char** /*argv*/ ) {
using namespace Ra::Core;
using json = nlohmann::json;

Random::MersenneTwisterGenerator generator = Random::MersenneTwisterGenerator();

std::vector<Vector3> uSamplesDir, cSamplesDir, bSamplesDir4, bSamplesDir16, bSamplesDir64,
bSamplesDir128, bpmat, lambmat;

Material::BlinnPhongMaterialModel bpmaterial = Material::BlinnPhongMaterialModel();

for ( int i = 0; i < 500; i++ ) {
// uSamplesDir.push_back( Random::UniformSphereSampler::getDir( &generator ).first );
// cSamplesDir.push_back( Random::CosineWeightedSphereSampler::getDir( &generator ).first );
// bSamplesDir4.push_back( Random::BlinnPhongSphereSampler::getDir( &generator, 4 ).first );
// bSamplesDir16.push_back( Random::BlinnPhongSphereSampler::getDir( &generator, 16 ).first
// ); bSamplesDir64.push_back( Random::BlinnPhongSphereSampler::getDir( &generator, 64
// ).first ); bSamplesDir128.push_back(
// Random::BlinnPhongSphereSampler::getDir( &generator, 128 ).first );

bpmaterial.m_ns = 4;
auto sample =
bpmaterial.sample( { -1, 0, 1 }, { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 }, { 0, 0 } );
if ( sample.has_value() ) bSamplesDir4.push_back( sample.value().first );

bpmaterial.m_ns = 16;
sample = bpmaterial.sample( { -1, 0, 1 }, { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 }, { 0, 0 } );
if ( sample.has_value() ) bSamplesDir16.push_back( sample.value().first );

bpmaterial.m_ns = 64;
sample = bpmaterial.sample( { -1, 0, 1 }, { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 }, { 0, 0 } );
if ( sample.has_value() ) bSamplesDir64.push_back( sample.value().first );

bpmaterial.m_ns = 128;
sample = bpmaterial.sample( { -1, 0, 1 }, { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 }, { 0, 0 } );
if ( sample.has_value() ) bSamplesDir128.push_back( sample.value().first );
}

json j = { //{ "UniformSampleDir", uSamplesDir },
//{ "CosineWeightedSampleDir", cSamplesDir },
{ "BlinnPhongSampleDir 4", bSamplesDir4 },
{ "BlinnPhongSampleDir 16", bSamplesDir16 },
{ "BlinnPhongSampleDir 64", bSamplesDir64 },
{ "BlinnPhongSampleDir 128", bSamplesDir128 } };
// {"BlinnPhongMaterial", bpmat}};
// { "UniformSamplePoint", uSamplesPoint },
// { "CosineWeightedSamplePoint", cSamplesPoint },
// { "BlinnPhongSamplePoint", bSamplesPoint }

std::ofstream o( "samples.json" );

o << std::setw( 4 ) << j << std::endl;

o.close();

return 0;
}
29 changes: 29 additions & 0 deletions examples/PlotImportanceSampler/plot-samples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
import math
import sys

def plotDir(array, ax):
for p in array:
ax.scatter(p[0], p[1], p[2], c='#1f77b4', s=1)

with open(sys.argv[1], 'r') as f:
data = json.load(f)

size = len(data)

fig = plt.figure(figsize=plt.figaspect(1/3))
gs = gridspec.GridSpec(math.ceil(size/3), 3)

i = 0
for l in data:
ax = fig.add_subplot(gs[math.floor(i/3), i%3], projection='3d')
ax.set_title(l)
ax.set_zlim(0, 1)
arr = np.array(data[l])
plotDir(arr, ax)
i += 1

plt.show()
63 changes: 62 additions & 1 deletion src/Core/Material/BlinnPhongMaterialModel.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <Core/Material/BlinnPhongMaterialModel.hpp>

#include <Core/Utils/Log.hpp>

#include <algorithm>

namespace Ra {
namespace Core {
namespace Material {
Expand All @@ -24,6 +25,66 @@ void BlinnPhongMaterialModel::displayInfo() const {
print( hasNormalTexture(), " Normal Texture : ", m_texNormal );
print( hasOpacityTexture(), " Alpha Texture : ", m_texOpacity );
}
Utils::Color Material::BlinnPhongMaterialModel::evalBSDF( Vector3 w_i,
Vector3 w_o,
Vector3 normal,
Vector2 uv ) {
// diffuse lambertien component
Utils::Color diffuse = m_kd / M_PI;

// Blinn-Phong specular component
Vector3 halfway = w_i + w_o;
halfway.normalize();
Scalar specularIntensity =
( m_ns + 2.0f ) / ( 2.0f * M_PI ) * std::pow( normal.dot( halfway ), m_ns );
Utils::Color specular = m_ks * specularIntensity;

// Combine the diffuse and specular components
Utils::Color bsdf = diffuse + specular;

return bsdf;
}

std::optional<std::pair<Vector3, Scalar>> BlinnPhongMaterialModel::sample( Vector3 inDir,
grandch marked this conversation as resolved.
Show resolved Hide resolved
Vector3 normal,
Vector3 tangent,
Vector3 bitangent,
Vector2 u ) {
Vector3 halfway;

Scalar distrib = m_generator.get()->get1D();

// diffuse part
if ( distrib < m_diffuseLuminance ) {
std::pair<Vector3, Scalar> smpl =
Core::Random::CosineWeightedSphereSampler::getDir( m_generator.get() );
Vector3 wi(
smpl.first.dot( tangent ), smpl.first.dot( bitangent ), smpl.first.dot( normal ) );
std::pair<Vector3, Scalar> result { wi, smpl.second };
return result;
}
else if ( distrib < m_diffuseLuminance + m_specularLuminance ) { // specular part
std::pair<Vector3, Scalar> smpl =
Core::Random::BlinnPhongSphereSampler::getDir( m_generator.get(), m_ns );
Vector3 wi(
smpl.first.dot( tangent ), smpl.first.dot( bitangent ), smpl.first.dot( normal ) );
std::pair<Vector3, Scalar> result { wi, smpl.second };
return result;
}
else { // no next dir
return {};
}
}

Scalar BlinnPhongMaterialModel::PDF( Vector3 inDir, Vector3 outDir, Vector3 normal ) {
return std::clamp( m_diffuseLuminance *
Core::Random::CosineWeightedSphereSampler::pdf( outDir, normal ) +
m_specularLuminance *
Core::Random::BlinnPhongSphereSampler::pdf( outDir, normal, m_ns ),
0_ra,
1_ra );
}

} // namespace Material
} // namespace Core
} // namespace Ra
78 changes: 64 additions & 14 deletions src/Core/Material/BlinnPhongMaterialModel.hpp
Original file line number Diff line number Diff line change
@@ -1,50 +1,100 @@
#pragma once

#include <Core/Material/MaterialModel.hpp>
#include <Core/Material/SimpleMaterialModel.hpp>
#include <Core/Random/BlinnPhongSphereSampler.hpp>
#include <Core/Random/UniformGenerator.hpp>
#include <Core/Utils/Color.hpp>

#include <ctime>

namespace Ra {
namespace Core {
namespace Material {

// RADIUM SUPPORTED MATERIALS
class RA_CORE_API BlinnPhongMaterialModel : public MaterialModel
class RA_CORE_API BlinnPhongMaterialModel : public SimpleMaterialModel
{
public:
explicit BlinnPhongMaterialModel( const std::string& name = "" ) :
MaterialModel( name, "BlinnPhong" ) {}
explicit BlinnPhongMaterialModel(
const std::string& name = "",
std::shared_ptr<Core::Random::UniformGenerator> generator =
std::make_shared<Core::Random::MersenneTwisterGenerator>() ) :
SimpleMaterialModel( name, "BlinnPhong", generator ) {}

~BlinnPhongMaterialModel() override = default;

/// DEBUG
void displayInfo() const override;

/// QUERY

bool hasDiffuseTexture() const { return m_hasTexDiffuse; }

bool hasSpecularTexture() const { return m_hasTexSpecular; }

bool hasShininessTexture() const { return m_hasTexShininess; }

bool hasNormalTexture() const { return m_hasTexNormal; }

bool hasOpacityTexture() const { return m_hasTexOpacity; }
Utils::Color evalBSDF( Vector3 w_i, Vector3 w_o, Vector3 normal, Vector2 uv ) override;
std::optional<std::pair<Vector3, Scalar>>
sample( Vector3 inDir, Vector3 normal, Vector3 tangent, Vector3 bitangent, Vector2 u ) override;
Scalar PDF( Vector3 inDir, Vector3 outDir, Vector3 normal ) override;

inline Utils::Color getSpecularColor() const { return m_ks; }
inline Scalar getShininess() const { return m_ns; }
inline Scalar getDiffuseLuminance() const { return m_diffuseLuminance; }
inline Scalar getSpecularLuminance() const { return m_specularLuminance; }
inline std::string getTexSpecular() const { return m_texSpecular; }
inline std::string getTexShininess() const { return m_texShininess; }
inline std::string getTexNormal() const { return m_texNormal; }

inline void setDiffuseColor( Utils::Color color ) {
m_kd = color;
Vector3 rgbToLuminance { 0.2126_ra, 0.7152_ra, 0.0722_ra };
Scalar dIntensity = m_kd.rgb().dot( rgbToLuminance );
Scalar sIntensity = m_ks.rgb().dot( rgbToLuminance );
Scalar diffSpecNorm = std::max( 1_ra, dIntensity + sIntensity );

m_diffuseLuminance /= diffSpecNorm;
m_specularLuminance /= diffSpecNorm;
m_alpha = color.alpha();
}

inline void setSpecularColor( Utils::Color color ) {
m_ks = color;
Vector3 rgbToLuminance { 0.2126_ra, 0.7152_ra, 0.0722_ra };
Scalar dIntensity = m_kd.rgb().dot( rgbToLuminance );
Scalar sIntensity = m_ks.rgb().dot( rgbToLuminance );
Scalar diffSpecNorm = std::max( 1_ra, dIntensity + sIntensity );

m_diffuseLuminance /= diffSpecNorm;
m_specularLuminance /= diffSpecNorm;
grandch marked this conversation as resolved.
Show resolved Hide resolved
}

inline void setShininess( Scalar specular ) { m_ns = specular; }
inline void setTexSpecular( std::string texSpecular ) {
m_texSpecular = texSpecular;
m_hasTexSpecular = true;
}
inline void setTexShininess( std::string texShininess ) {
m_texShininess = texShininess;
m_hasTexShininess = true;
}
inline void setTexNormal( std::string texNormal ) {
m_texNormal = texNormal;
m_hasTexNormal = true;
}

/// DATA MEMBERS
Core::Utils::Color m_kd { 0.7_ra, 0.7_ra, 0.7_ra };
private:
Core::Utils::Color m_ks { 0.3_ra, 0.3_ra, 0.3_ra };
Scalar m_ns { 64_ra };
Scalar m_alpha { 1_ra };
std::string m_texDiffuse;
Scalar m_diffuseLuminance;
Scalar m_specularLuminance;
std::string m_texSpecular;
std::string m_texShininess;
std::string m_texNormal;
std::string m_texOpacity;
bool m_hasTexDiffuse { false };
bool m_hasTexSpecular { false };
bool m_hasTexShininess { false };
bool m_hasTexNormal { false };
bool m_hasTexOpacity { false };
};

} // namespace Material
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Material/MaterialModel.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "MaterialModel.hpp"
#include <Core/Material/MaterialModel.hpp>
#include <Core/Utils/Log.hpp>

Expand All @@ -8,6 +9,7 @@ void MaterialModel::displayInfo() const {
using namespace Core::Utils; // log
LOG( logERROR ) << "MaterialModel : unknown material type : " << m_materialType;
}

} // namespace Material
} // namespace Core
} // namespace Ra
Loading