23#ifndef VCL_IO_MESH_OBJ_MATERIAL_H
24#define VCL_IO_MESH_OBJ_MATERIAL_H
26#include <vclib/space/core.h>
28namespace vcl::detail {
39 inline static const Point3f Ka_DEFAULT = {0.2f, 0.2f, 0.2f};
40 inline static const Point3f Kd_DEFAULT = {1.0f, 1.0f, 1.0f};
41 inline static const Point3f Ks_DEFAULT = {1.0f, 1.0f, 1.0f};
42 inline static const Point3f Ke_DEFAULT = {0.0f, 0.0f, 0.0f};
44 inline static const float d_DEFAULT = 1.0f;
45 inline static const int illum_DEFAULT = 2;
46 inline static const float Ns_DEFAULT = 0.f;
60 int illum = illum_DEFAULT;
61 float Ns = Ns_DEFAULT;
67 ObjMaterial() =
default;
69 ObjMaterial(
const Material& mat, uint
id)
76 Kd.x() = mat.baseColor().redF();
77 Kd.y() = mat.baseColor().greenF();
78 Kd.z() = mat.baseColor().blueF();
79 Ns = std::pow(1.0f - mat.roughness(), 2) * 1000.f;
81 d = mat.baseColor().alphaF();
84 Ke.x() = mat.emissiveColor().redF();
85 Ke.y() = mat.emissiveColor().greenF();
86 Ke.z() = mat.emissiveColor().blueF();
88 map_Kd = mat.baseColorTextureDescriptor().path();
89 map_Ke = mat.textureDescriptor(EMISSIVE).path();
102 Material toMaterial()
109 vcl::Color(Kd.x() * 255, Kd.y() * 255, Kd.z() * 255, 255);
111 float ns = std::clamp(Ns, 0.f, 1000.f);
112 m.roughness() = 1.0f - std::sqrt(ns) / std::sqrt(1000.f);
115 m.baseColor().alpha() = d * 255;
122 vcl::Color(Ke.x() * 255, Ke.y() * 255, Ke.z() * 255, 255);
124 if (!map_Kd.empty()) {
125 m.baseColorTextureDescriptor().path() = map_Kd;
127 if (!map_Ke.empty()) {
128 m.textureDescriptor(EMISSIVE).path() = map_Ke;
137 bool isValid()
const {
return matId !=
UINT_NULL; }
141 return Color(Kd.x() * 255, Kd.y() * 255, Kd.z() * 255, d * 255);
144 const std::string& texture()
const {
return map_Kd; }
146 uint textureId()
const {
return matId; }
148 bool operator==(
const ObjMaterial& m)
const =
default;
150 friend inline std::ostream& operator<<(
152 const ObjMaterial& m);
155inline std::ostream& operator<<(std::ostream& out,
const ObjMaterial& m)
157 if (m.Ka != m.Ka_DEFAULT)
158 out <<
"Ka " << m.Ka.x() <<
" " << m.Ka.y() <<
" " << m.Ka.z()
161 if (m.Kd != m.Kd_DEFAULT)
162 out <<
"Kd " << m.Kd.x() <<
" " << m.Kd.y() <<
" " << m.Kd.z()
165 if (m.Ks != m.Ks_DEFAULT)
166 out <<
"Ks " << m.Ks.x() <<
" " << m.Ks.y() <<
" " << m.Ks.z()
169 if (m.Ke != m.Ke_DEFAULT)
170 out <<
"Ke " << m.Ke.x() <<
" " << m.Ke.y() <<
" " << m.Ke.z()
173 if (m.d != m.d_DEFAULT)
174 out <<
"d " << m.d << std::endl;
176 if (m.illum != m.illum_DEFAULT)
177 out <<
"illum " << m.illum << std::endl;
179 if (m.Ns != m.Ns_DEFAULT)
180 out <<
"Ns " << m.Ns << std::endl;
182 if (!m.map_Kd.empty()) {
183 out <<
"map_Kd " << m.map_Kd << std::endl;
186 if (m.map_Ke.empty()) {
187 out <<
"map_Ke " << m.map_Ke << std::endl;
The Color class represents a 32 bit color.
Definition color.h:48
TextureType
Defines the types of textures used in the PBR material model.
Definition material.h:62
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48
Point3< float > Point3f
A convenience alias for a 3-dimensional Point with floating-point components.
Definition point.h:768