Visual Computing Library
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_LOAD_SAVE_OBJ_MATERIAL_H
24#define VCL_LOAD_SAVE_OBJ_MATERIAL_H
25
26#include <vclib/space/core/color.h>
27#include <vclib/space/core/point.h>
28
29namespace vcl::detail {
30
31struct ObjMaterial
32{
33 Point3f Ka = Point3f(0.2f, 0.2f, 0.2f); // ambient
34 Point3f Kd = Point3f(1.0f, 1.0f, 1.0f); // diffuse
35 Point3f Ks = Point3f(1.0f, 1.0f, 1.0f); // specular
36
37 float d = 1.0f; // alpha
38
39 int illum = 2; // specular illumination
40 float Ns = 0.f;
41
42 std::string map_Kd; // filename texture
43
44 // id of the texture in the mesh, used when loading materials
45 uint mapId;
46
47 bool hasColor = false;
48 bool hasTexture = false;
49
50 ObjMaterial() = default;
51
52 ObjMaterial(const Color& c) : hasColor(true)
53 {
54 Kd.x() = c.redF();
55 Kd.y() = c.greenF();
56 Kd.z() = c.blueF();
57 d = c.alphaF();
58 }
59
60 ObjMaterial(const std::string& txtName) : map_Kd(txtName), hasTexture(true)
61 {
62 }
63
64 ObjMaterial(const Color& c, const std::string& txtName) :
65 map_Kd(txtName), hasColor(true), hasTexture(true)
66 {
67 Kd.x() = c.redF();
68 Kd.y() = c.greenF();
69 Kd.z() = c.blueF();
70 d = c.alphaF();
71 }
72
73 bool isEmpty() const { return !hasColor && !hasTexture; }
74
75 Color color() const
76 {
77 return Color(Kd.x() * 255, Kd.y() * 255, Kd.z() * 255, d * 255);
78 }
79
80 const std::string& texture() const { return map_Kd; }
81
82 uint textureId() const { return mapId; }
83
93 bool operator<(const ObjMaterial& m) const
94 {
95 if (hasColor) {
96 if (!m.hasColor) // color > no color
97 return false;
98 if (Kd != m.Kd)
99 return Kd < m.Kd;
100 if (d != m.d)
101 return d < m.d;
102 }
103 else if (m.hasColor) { // no color < color
104 return true;
105 }
106 // will arrive here only if:
107 // - this Material and m have both no color
108 // - this Material has the same color of m
109 if (hasTexture) {
110 if (!m.hasTexture) // texture > no texture
111 return false;
112 return map_Kd < m.map_Kd;
113 }
114 else if (m.hasTexture) { // no texture < texture
115 return true;
116 }
117 else { // no color and texture in both materials
118 return false;
119 }
120 }
121
122 bool operator==(const ObjMaterial& m) const
123 {
124 return !(*this < m) && !(m < *this);
125 }
126
127 bool operator!=(const ObjMaterial& m) const { return !(*this == m); }
128};
129
130inline std::ostream& operator<<(std::ostream& out, const ObjMaterial& m)
131{
132 if (m.hasColor) {
133 out << "Kd " << m.Kd.x() << " " << m.Kd.y() << " " << m.Kd.z()
134 << std::endl;
135 out << "d " << m.d << std::endl;
136 }
137 if (m.hasTexture) {
138 out << "map_Kd " << m.map_Kd << std::endl;
139 }
140 return out;
141}
142
143} // namespace vcl::detail
144
145#endif // VCL_LOAD_SAVE_OBJ_MATERIAL_H
Point3< float > Point3f
A convenience alias for a 3-dimensional Point with floating-point components.
Definition point.h:792