Blender V5.3
obj_mtl_parser_tests.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <gtest/gtest.h>
6
7#include "BLI_fileops.hh"
8
9#include "BKE_appdir.hh"
10#include "BKE_gtest_base.hh"
11
12#include "CLG_log.h"
13
14#include "testing/testing.h"
15
16#include "obj_export_mtl.hh"
18
19namespace blender::io::obj {
20
22 public:
23 void check_string(const char *text, const MTLMaterial *expect, size_t expect_count)
24 {
25 BKE_tempdir_init(nullptr);
26 std::string tmp_dir = BKE_tempdir_base();
27 std::string tmp_file_name = "mtl_test.mtl";
28 std::string tmp_file_path = tmp_dir + SEP_STR + tmp_file_name;
29 FILE *tmp_file = BLI_fopen(tmp_file_path.c_str(), "wb");
30 fputs(text, tmp_file);
31 fclose(tmp_file);
32
33 check_impl(tmp_file_name, tmp_dir, expect, expect_count);
34
35 BLI_delete(tmp_file_path.c_str(), false, false);
36 }
37 void check(const char *file, const MTLMaterial *expect, size_t expect_count)
38 {
39 std::string obj_dir = tests::flags_test_asset_dir() +
40 (SEP_STR "io_tests" SEP_STR "obj" SEP_STR);
41 check_impl(file, obj_dir, expect, expect_count);
42 }
43 void check_impl(StringRefNull mtl_file_path,
44 StringRefNull file_dir,
45 const MTLMaterial *expect,
46 size_t expect_count)
47 {
48 MTLParser parser(mtl_file_path, file_dir + "dummy.obj");
50 parser.parse_and_store(materials);
51
52 for (int i = 0; i < expect_count; ++i) {
53 const MTLMaterial &exp = expect[i];
54 if (!materials.contains(exp.name)) {
55 fprintf(stderr, "Material '%s' was expected in parsed result\n", exp.name.c_str());
56 ADD_FAILURE();
57 continue;
58 }
59 const MTLMaterial &got = *materials.lookup(exp.name);
60 const float tol = 0.0001f;
61 EXPECT_V3_NEAR(exp.ambient_color, got.ambient_color, tol);
62 EXPECT_V3_NEAR(exp.color, got.color, tol);
63 EXPECT_V3_NEAR(exp.spec_color, got.spec_color, tol);
64 EXPECT_V3_NEAR(exp.emission_color, got.emission_color, tol);
65 EXPECT_V3_NEAR(exp.transmit_color, got.transmit_color, tol);
66 EXPECT_NEAR(exp.spec_exponent, got.spec_exponent, tol);
67 EXPECT_NEAR(exp.ior, got.ior, tol);
68 EXPECT_NEAR(exp.alpha, got.alpha, tol);
69 EXPECT_NEAR(exp.normal_strength, got.normal_strength, tol);
70 EXPECT_EQ(exp.illum_mode, got.illum_mode);
71 EXPECT_NEAR(exp.roughness, got.roughness, tol);
72 EXPECT_NEAR(exp.metallic, got.metallic, tol);
73 EXPECT_NEAR(exp.sheen, got.sheen, tol);
74 EXPECT_NEAR(exp.cc_thickness, got.cc_thickness, tol);
75 EXPECT_NEAR(exp.cc_roughness, got.cc_roughness, tol);
76 EXPECT_NEAR(exp.aniso, got.aniso, tol);
77 EXPECT_NEAR(exp.aniso_rot, got.aniso_rot, tol);
78 for (int key = 0; key < int(MTLTexMapType::Count); key++) {
79 const MTLTexMap &exp_tex = exp.texture_maps[key];
80 const MTLTexMap &got_tex = got.texture_maps[key];
81 EXPECT_STREQ(exp_tex.image_path.c_str(), got_tex.image_path.c_str());
82 EXPECT_V3_NEAR(exp_tex.translation, got_tex.translation, tol);
83 EXPECT_V3_NEAR(exp_tex.scale, got_tex.scale, tol);
85 }
86 }
87 EXPECT_EQ(materials.size(), expect_count);
88 }
89};
90
91TEST_F(OBJMTLParserTest, string_newlines_whitespace)
92{
93 const char *text =
94 "# a comment\n"
95 " # indented comment\n"
96 "# comment with CRLF line ending\r\n"
97 "\r\n"
98
99 "newmtl simple\n"
100 "Ka 0.1 0.2 0.3\n"
101 "illum 4\n"
102
103 "newmtl\ttab_indentation\n"
104 "Kd\t \t0.2 0.3\t0.4 \t \n"
105
106 "newmtl space_after_name \t \n"
107 "Ks 0.4 0.5 0.6\n"
108
109 "newmtl space_before_name\n"
110
111 "newmtl indented_values\n"
112 " Ka 0.5 0.6 0.7\n"
113 "\t\t\tKd 0.6 0.7 0.8\n"
114
115 "newmtl crlf_ending\r\n"
116 "Ns 5.0\r\n"
117 "map_Kd sometex_d.png\r\n"
118 "map_Ks sometex_s_spaces_after_name.png \t \r\n";
119 MTLMaterial mat[6];
120 mat[0].name = "simple";
121 mat[0].ambient_color = {0.1f, 0.2f, 0.3f};
122 mat[0].illum_mode = 4;
123 mat[1].name = "tab_indentation";
124 mat[1].color = {0.2f, 0.3f, 0.4f};
125 mat[2].name = "space_after_name";
126 mat[2].spec_color = {0.4f, 0.5f, 0.6f};
127 mat[3].name = "space_before_name";
128 mat[4].name = "indented_values";
129 mat[4].ambient_color = {0.5f, 0.6f, 0.7f};
130 mat[4].color = {0.6f, 0.7f, 0.8f};
131 mat[5].name = "crlf_ending";
132 mat[5].spec_exponent = 5.0f;
133 mat[5].tex_map_of_type(MTLTexMapType::Color).image_path = "sometex_d.png";
134 mat[5].tex_map_of_type(MTLTexMapType::Specular).image_path = "sometex_s_spaces_after_name.png";
135 check_string(text, mat, ARRAY_SIZE(mat));
136}
137
139{
140 MTLMaterial mat[6];
141 mat[0].name = "no_textures_red";
142 mat[0].ambient_color = {0.3f, 0.3f, 0.3f};
143 mat[0].color = {0.8f, 0.3f, 0.1f};
144 mat[0].spec_exponent = 5.624998f;
145
146 mat[1].name = "four_maps";
147 mat[1].ambient_color = {1, 1, 1};
148 mat[1].color = {0.8f, 0.8f, 0.8f};
149 mat[1].spec_color = {0.5f, 0.5f, 0.5f};
150 mat[1].emission_color = {0, 0, 0};
151 mat[1].spec_exponent = 1000;
152 mat[1].ior = 1.45f;
153 mat[1].alpha = 1;
154 mat[1].illum_mode = 2;
155 mat[1].normal_strength = 1;
156 {
158 kd.image_path = "texture.png";
160 ns.image_path = "sometexture_Roughness.png";
162 refl.image_path = "sometexture_Metallic.png";
164 bump.image_path = "sometexture_Normal.png";
165 }
166
167 mat[2].name = "Clay";
168 mat[2].ambient_color = {1, 1, 1};
169 mat[2].color = {0.8f, 0.682657f, 0.536371f};
170 mat[2].spec_color = {0.5f, 0.5f, 0.5f};
171 mat[2].emission_color = {0, 0, 0};
172 mat[2].spec_exponent = 440.924042f;
173 mat[2].ior = 1.45f;
174 mat[2].alpha = 1;
175 mat[2].illum_mode = 2;
176
177 mat[3].name = "Hat";
178 mat[3].ambient_color = {1, 1, 1};
179 mat[3].color = {0.8f, 0.8f, 0.8f};
180 mat[3].spec_color = {0.5f, 0.5f, 0.5f};
181 mat[3].spec_exponent = 800;
182 mat[3].normal_strength = 0.5f;
183 {
185 kd.image_path = "someHatTexture_BaseColor.jpg";
187 ns.image_path = "someHatTexture_Roughness.jpg";
189 refl.image_path = "someHatTexture_Metalness.jpg";
191 bump.image_path = "someHatTexture_Normal.jpg";
192 }
193
194 mat[4].name = "Parser_Test";
195 mat[4].ambient_color = {0.1f, 0.2f, 0.3f};
196 mat[4].color = {0.4f, 0.5f, 0.6f};
197 mat[4].spec_color = {0.7f, 0.8f, 0.9f};
198 mat[4].illum_mode = 6;
199 mat[4].spec_exponent = 15.5;
200 mat[4].ior = 1.5;
201 mat[4].alpha = 0.5;
202 mat[4].normal_strength = 0.1f;
203 mat[4].transmit_color = {0.1f, 0.3f, 0.5f};
204 mat[4].normal_strength = 0.1f;
205 mat[4].roughness = 0.2f;
206 mat[4].metallic = 0.3f;
207 mat[4].sheen = 0.4f;
208 mat[4].cc_thickness = 0.5f;
209 mat[4].cc_roughness = 0.6f;
210 mat[4].aniso = 0.7f;
211 mat[4].aniso_rot = 0.8f;
212 {
214 kd.image_path = "sometex_d.png";
216 ns.image_path = "sometex_ns.psd";
218 refl.image_path = "clouds.tiff";
219 refl.scale = {1.5f, 2.5f, 3.5f};
220 refl.translation = {4.5f, 5.5f, 6.5f};
223 bump.image_path = "somebump.tga";
224 bump.scale = {3, 4, 5};
225 }
226
227 mat[5].name = "Parser_ScaleOffset_Test";
228 {
230 kd.translation = {2.5f, 0.0f, 0.0f};
231 kd.image_path = "OffsetOneValue.png";
233 ks.scale = {1.5f, 2.5f, 1.0f};
234 ks.translation = {3.5f, 4.5f, 0.0f};
235 ks.image_path = "ScaleOffsetBothTwovalues.png";
237 ns.scale = {0.5f, 1.0f, 1.0f};
238 ns.image_path = "1.Value.png";
239 }
240
241 check("materials.mtl", mat, ARRAY_SIZE(mat));
242}
243
245{
246 MTLMaterial mat[2];
247 mat[0].name = "Mat1";
248 mat[0].color = {0.8f, 0.276449f, 0.101911f};
249 mat[0].spec_color = {0.25f, 0.25f, 0.25f};
250 mat[0].emission_color = {0, 0, 0};
251 mat[0].ior = 1.45f;
252 mat[0].alpha = 1;
253 mat[0].illum_mode = 3;
254 mat[0].roughness = 0.4f;
255 mat[0].metallic = 0.9f;
256 mat[0].sheen = 0.06f;
257 mat[0].cc_thickness = 0.393182f;
258 mat[0].cc_roughness = 0.05f;
259 mat[0].aniso = 0.2f;
260 mat[0].aniso_rot = 0.0f;
261
262 mat[1].name = "Mat2";
263 mat[1].color = {0.8f, 0.8f, 0.8f};
264 mat[1].spec_color = {0.5f, 0.5f, 0.5f};
265 mat[1].ior = 1.45f;
266 mat[1].alpha = 1;
267 mat[1].illum_mode = 2;
268 mat[1].metallic = 0.0f;
269 mat[1].cc_thickness = 0.3f;
270 mat[1].cc_roughness = 0.4f;
271 mat[1].aniso = 0.8f;
272 mat[1].aniso_rot = 0.7f;
273 {
275 pr.image_path = "../blend_geometry/texture_roughness.png";
277 ps.image_path = "../blend_geometry/texture_checker.png";
279 ke.image_path = "../blend_geometry/texture_illum.png";
280 }
281
282 check("materials_pbr.mtl", mat, ARRAY_SIZE(mat));
283}
284
285} // namespace blender::io::obj
File and directory operations.
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
int64_t size() const
Definition BLI_map.hh:976
bool contains(const Key &key) const
Definition BLI_map.hh:353
void check_string(const char *text, const MTLMaterial *expect, size_t expect_count)
void check_impl(StringRefNull mtl_file_path, StringRefNull file_dir, const MTLMaterial *expect, size_t expect_count)
void check(const char *file, const MTLMaterial *expect, size_t expect_count)
#define exp
#define EXPECT_NEAR(result, expect, threshold)
TEST_F(OBJExportTest, filter_objects_curves_as_mesh)
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1288
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1245
const MTLTexMap & tex_map_of_type(MTLTexMapType key) const
MTLTexMap texture_maps[int(MTLTexMapType::Count)]
#define SEP_STR
Definition unit.cc:41