Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cc429b

Browse files
committed
Support -texres resolution texture option. Fixes tinyobjloader#248
1 parent e52dfdb commit 9cc429b

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

models/issue-248-texres-texopt.mtl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
newmtl white
2+
Ka 0 0 0
3+
Kd 1 1 1
4+
Ks 0 0 0
5+
map_Kd -texres 512 input.jpg
6+
7+
newmtl red
8+
Ka 0 0 0
9+
Kd 1 0 0
10+
Ks 0 0 0
11+
12+
newmtl green
13+
Ka 0 0 0
14+
Kd 0 1 0
15+
Ks 0 0 0
16+
17+
newmtl blue
18+
Ka 0 0 0
19+
Kd 0 0 1
20+
Ks 0 0 0
21+
22+
newmtl light
23+
Ka 20 20 20
24+
Kd 1 1 1
25+
Ks 0 0 0

models/issue-248-texres-texopt.obj

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
mtllib issue-248-texres-texopt.mtl
2+
3+
v 0.000000 2.000000 2.000000
4+
v 0.000000 0.000000 2.000000
5+
v 2.000000 0.000000 2.000000
6+
v 2.000000 2.000000 2.000000
7+
v 0.000000 2.000000 0.000000
8+
v 0.000000 0.000000 0.000000
9+
v 2.000000 0.000000 0.000000
10+
v 2.000000 2.000000 0.000000
11+
# 8 vertices
12+
13+
g front cube
14+
usemtl white
15+
f 1 2 3 4
16+
# two white spaces between 'back' and 'cube'
17+
g back cube
18+
# expects white material
19+
f 8 7 6 5
20+
g right cube
21+
usemtl red
22+
f 4 3 7 8
23+
g top cube
24+
usemtl white
25+
f 5 1 4 8
26+
g left cube
27+
usemtl green
28+
f 5 6 2 1
29+
g bottom cube
30+
usemtl white
31+
f 2 6 7 3
32+
# 6 elements

tests/tester.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,34 @@ void test_usemtl_whitespace_issue246() {
12801280
TEST_CHECK(0 == shapes[0].mesh.material_ids[0]);
12811281
}
12821282

1283+
void test_texres_texopt_issue248() {
1284+
tinyobj::attrib_t attrib;
1285+
std::vector<tinyobj::shape_t> shapes;
1286+
std::vector<tinyobj::material_t> materials;
1287+
1288+
std::string warn;
1289+
std::string err;
1290+
bool ret = tinyobj::LoadObj(
1291+
&attrib, &shapes, &materials, &warn, &err,
1292+
"../models/issue-248-texres-texopt.obj",
1293+
gMtlBasePath);
1294+
1295+
TEST_CHECK(warn.empty());
1296+
1297+
if (!warn.empty()) {
1298+
std::cout << "WARN: " << warn << std::endl;
1299+
}
1300+
1301+
if (!err.empty()) {
1302+
std::cerr << "ERR: " << err << std::endl;
1303+
}
1304+
1305+
TEST_CHECK(true == ret);
1306+
TEST_CHECK(1 < materials.size());
1307+
TEST_CHECK(512 == materials[0].diffuse_texopt.texture_resolution);
1308+
TEST_CHECK("input.jpg" == materials[0].diffuse_texname);
1309+
}
1310+
12831311
// Fuzzer test.
12841312
// Just check if it does not crash.
12851313
// Disable by default since Windows filesystem can't create filename of afl
@@ -1377,4 +1405,6 @@ TEST_LIST = {
13771405
test_mtl_searchpaths_issue244},
13781406
{"usemtl_whitespece_issue246",
13791407
test_usemtl_whitespace_issue246},
1408+
{"texres_texopt_issue248",
1409+
test_texres_texopt_issue248},
13801410
{NULL, NULL}};

tiny_obj_loader.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ typedef struct {
155155
real_t origin_offset[3]; // -o u [v [w]] (default 0 0 0)
156156
real_t scale[3]; // -s u [v [w]] (default 1 1 1)
157157
real_t turbulence[3]; // -t u [v [w]] (default 0 0 0)
158-
// int texture_resolution; // -texres resolution (default = ?) TODO
158+
int texture_resolution; // -texres resolution (No default value in the spec. We'll use -1)
159159
bool clamp; // -clamp (default false)
160160
char imfchan; // -imfchan (the default for bump is 'l' and for decal is 'm')
161161
bool blendu; // -blendu (default on)
@@ -1194,6 +1194,10 @@ bool ParseTextureNameAndOption(std::string *texname, texture_option_t *texopt,
11941194
} else if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) {
11951195
token += 5;
11961196
texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE);
1197+
} else if ((0 == strncmp(token, "-texres", 7)) && IS_SPACE((token[7]))) {
1198+
token += 7;
1199+
// TODO(syoyo): Check if arg is int type.
1200+
texopt->texture_resolution = parseInt(&token);
11971201
} else if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) {
11981202
token += 9;
11991203
token += strspn(token, " \t");
@@ -1258,6 +1262,7 @@ static void InitTexOpt(texture_option_t *texopt, const bool is_bump) {
12581262
texopt->turbulence[0] = static_cast<real_t>(0.0);
12591263
texopt->turbulence[1] = static_cast<real_t>(0.0);
12601264
texopt->turbulence[2] = static_cast<real_t>(0.0);
1265+
texopt->texture_resolution = -1;
12611266
texopt->type = TEXTURE_TYPE_NONE;
12621267
}
12631268

@@ -2354,7 +2359,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
23542359

23552360
// use mtl
23562361
if ((0 == strncmp(token, "usemtl", 6))) {
2357-
token += 6;
2362+
token += 6;
23582363
std::string namebuf = parseString(&token);
23592364

23602365
int newMaterialId = -1;

0 commit comments

Comments
 (0)