Visual Computing Library  devel
Loading...
Searching...
No Matches
material.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_IO_MESH_OBJ_MATERIAL_H
24#define VCL_IO_MESH_OBJ_MATERIAL_H
25
26#include <vclib/space/core.h>
27
28namespace vcl::detail {
29
30struct ObjMaterial
31{
32 Point3f Ka = Point3f(0.2f, 0.2f, 0.2f); // ambient
33 Point3f Kd = Point3f(1.0f, 1.0f, 1.0f); // diffuse
34 Point3f Ks = Point3f(1.0f, 1.0f, 1.0f); // specular
35
36 float d = 1.0f; // alpha
37
38 int illum = 2; // specular illumination
39 float Ns = 0.f;
40
41 std::string map_Kd; // filename texture
42
43 // id of the texture in the mesh, used when loading materials
44 uint mapId;
45
46 bool hasColor = false;
47 bool hasTexture = false;
48
49 ObjMaterial() = default;
50
51 ObjMaterial(const Color& c) : hasColor(true)
52 {
53 Kd.x() = c.redF();
54 Kd.y() = c.greenF();
55 Kd.z() = c.blueF();
56 d = c.alphaF();
57 }
58
59 ObjMaterial(const std::string& txtName) : map_Kd(txtName), hasTexture(true)
60 {
61 }
62
63 ObjMaterial(const Color& c, const std::string& txtName) :
64 map_Kd(txtName), hasColor(true), hasTexture(true)
65 {
66 Kd.x() = c.redF();
67 Kd.y() = c.greenF();
68 Kd.z() = c.blueF();
69 d = c.alphaF();
70 }
71
72 bool isEmpty() const { return !hasColor && !hasTexture; }
73
74 Color color() const
75 {
76 return Color(Kd.x() * 255, Kd.y() * 255, Kd.z() * 255, d * 255);
77 }
78
79 const std::string& texture() const { return map_Kd; }
80
81 uint textureId() const { return mapId; }
82
92 bool operator<(const ObjMaterial& m) const
93 {
94 if (hasColor) {
95 if (!m.hasColor) // color > no color
96 return false;
97 if (Kd != m.Kd)
98 return Kd < m.Kd;
99 if (d != m.d)
100 return d < m.d;
101 }
102 else if (m.hasColor) { // no color < color
103 return true;
104 }
105 // will arrive here only if:
106 // - this Material and m have both no color
107 // - this Material has the same color of m
108 if (hasTexture) {
109 if (!m.hasTexture) // texture > no texture
110 return false;
111 return map_Kd < m.map_Kd;
112 }
113 else if (m.hasTexture) { // no texture < texture
114 return true;
115 }
116 else { // no color and texture in both materials
117 return false;
118 }
119 }
120
121 bool operator==(const ObjMaterial& m) const
122 {
123 return !(*this < m) && !(m < *this);
124 }
125
126 bool operator!=(const ObjMaterial& m) const { return !(*this == m); }
127};
128
129inline std::ostream& operator<<(std::ostream& out, const ObjMaterial& m)
130{
131 if (m.hasColor) {
132 out << "Kd " << m.Kd.x() << " " << m.Kd.y() << " " << m.Kd.z()
133 << std::endl;
134 out << "d " << m.d << std::endl;
135 }
136 if (m.hasTexture) {
137 out << "map_Kd " << m.map_Kd << std::endl;
138 }
139 return out;
140}
141
142} // namespace vcl::detail
143
144#endif // VCL_IO_MESH_OBJ_MATERIAL_H
Point3< float > Point3f
A convenience alias for a 3-dimensional Point with floating-point components.
Definition point.h:767