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

Skip to content

Commit 303043f

Browse files
committed
Merge branch 'master' of github.com:syoyo/tinyobjloader
2 parents 981f7c5 + dcbc8d5 commit 303043f

File tree

7 files changed

+905
-30
lines changed

7 files changed

+905
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Old version is available `v0.9.x` branch https://github.com/syoyo/tinyobjloader/
2626

2727
## What's new
2828

29-
* 20 Aug, 2016 : Bump version v1.0.0. New data strcutre and API!
29+
* 20 Aug, 2016 : Bump version v1.0.0. New data structure and API!
3030

3131
### Old version
3232

@@ -37,7 +37,7 @@ Previous old version is avaiable in `v0.9.x` branch.
3737
![Rungholt](images/rungholt.jpg)
3838

3939
tinyobjloader can successfully load 6M triangles Rungholt scene.
40-
http://graphics.cs.williams.edu/data/meshes.xml
40+
http://casual-effects.com/data/index.html
4141

4242
![](images/sanmugel.png)
4343

examples/viewer/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* glfw3
77
* glew
88

9-
109
## Build on MaCOSX
1110

1211
Install glfw3 and glew using brew.
@@ -35,3 +34,9 @@ Put glfw3 and glew library somewhere and replace include and lib path in `premak
3534
Then,
3635

3736
> premake5.exe vs2013
37+
38+
## TODO
39+
40+
* [ ] Support per-face material.
41+
* [ ] Use shader-based GL rendering.
42+
* [ ] PBR shader support.

examples/viewer/viewer.cc

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class timerutil {
123123
};
124124

125125
typedef struct {
126-
GLuint vb; // vertex buffer
126+
GLuint vb_id; // vertex buffer id
127127
int numTriangles;
128128
size_t material_id;
129129
} DrawObject;
@@ -207,6 +207,9 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
207207
tm.start();
208208

209209
std::string base_dir = GetBaseDir(filename);
210+
if (base_dir.empty()) {
211+
base_dir = ".";
212+
}
210213
#ifdef _WIN32
211214
base_dir += "\\";
212215
#else
@@ -238,6 +241,10 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
238241
// Append `default` material
239242
materials.push_back(tinyobj::material_t());
240243

244+
for (size_t i = 0; i < materials.size(); i++) {
245+
printf("material[%d].diffuse_texname = %s\n", int(i), materials[i].diffuse_texname.c_str());
246+
}
247+
241248
// Load diffuse textures
242249
{
243250
for (size_t m = 0; m < materials.size(); m++) {
@@ -265,15 +272,19 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
265272
std::cerr << "Unable to load texture: " << texture_filename << std::endl;
266273
exit(1);
267274
}
275+
std::cout << "Loaded texture: " << texture_filename << ", w = " << w << ", h = " << h << ", comp = " << comp << std::endl;
276+
268277
glGenTextures(1, &texture_id);
269278
glBindTexture(GL_TEXTURE_2D, texture_id);
270279
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
271-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
280+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
272281
if (comp == 3) {
273282
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
274283
}
275284
else if (comp == 4) {
276285
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
286+
} else {
287+
assert(0); // TODO
277288
}
278289
glBindTexture(GL_TEXTURE_2D, 0);
279290
stbi_image_free(image);
@@ -289,7 +300,7 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
289300
{
290301
for (size_t s = 0; s < shapes.size(); s++) {
291302
DrawObject o;
292-
std::vector<float> vb; // pos(3float), normal(3float), color(3float)
303+
std::vector<float> buffer; // pos(3float), normal(3float), color(3float)
293304
for (size_t f = 0; f < shapes[s].mesh.indices.size() / 3; f++) {
294305
tinyobj::index_t idx0 = shapes[s].mesh.indices[3 * f + 0];
295306
tinyobj::index_t idx1 = shapes[s].mesh.indices[3 * f + 1];
@@ -314,6 +325,8 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
314325
assert(attrib.texcoords.size() > 2 * idx0.texcoord_index + 1);
315326
assert(attrib.texcoords.size() > 2 * idx1.texcoord_index + 1);
316327
assert(attrib.texcoords.size() > 2 * idx2.texcoord_index + 1);
328+
329+
// Flip Y coord.
317330
tc[0][0] = attrib.texcoords[2 * idx0.texcoord_index];
318331
tc[0][1] = 1.0f - attrib.texcoords[2 * idx0.texcoord_index + 1];
319332
tc[1][0] = attrib.texcoords[2 * idx1.texcoord_index];
@@ -374,12 +387,12 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
374387
}
375388

376389
for (int k = 0; k < 3; k++) {
377-
vb.push_back(v[k][0]);
378-
vb.push_back(v[k][1]);
379-
vb.push_back(v[k][2]);
380-
vb.push_back(n[k][0]);
381-
vb.push_back(n[k][1]);
382-
vb.push_back(n[k][2]);
390+
buffer.push_back(v[k][0]);
391+
buffer.push_back(v[k][1]);
392+
buffer.push_back(v[k][2]);
393+
buffer.push_back(n[k][0]);
394+
buffer.push_back(n[k][1]);
395+
buffer.push_back(n[k][2]);
383396
// Combine normal and diffuse to get color.
384397
float normal_factor = 0.2;
385398
float diffuse_factor = 1 - normal_factor;
@@ -396,32 +409,32 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
396409
c[1] /= len;
397410
c[2] /= len;
398411
}
399-
vb.push_back(c[0] * 0.5 + 0.5);
400-
vb.push_back(c[1] * 0.5 + 0.5);
401-
vb.push_back(c[2] * 0.5 + 0.5);
412+
buffer.push_back(c[0] * 0.5 + 0.5);
413+
buffer.push_back(c[1] * 0.5 + 0.5);
414+
buffer.push_back(c[2] * 0.5 + 0.5);
402415

403-
vb.push_back(tc[k][0]);
404-
vb.push_back(tc[k][1]);
416+
buffer.push_back(tc[k][0]);
417+
buffer.push_back(tc[k][1]);
405418
}
406419
}
407420

408-
o.vb = 0;
421+
o.vb_id = 0;
409422
o.numTriangles = 0;
410423

411424
// OpenGL viewer does not support texturing with per-face material.
412425
if (shapes[s].mesh.material_ids.size() > 0 && shapes[s].mesh.material_ids.size() > s) {
413-
// Base case
414-
o.material_id = shapes[s].mesh.material_ids[s];
426+
o.material_id = shapes[s].mesh.material_ids[0]; // use the material ID of the first face.
415427
} else {
416428
o.material_id = materials.size() - 1; // = ID for default material.
417429
}
430+
printf("shape[%d] material_id %d\n", int(s), int(o.material_id));
418431

419-
if (vb.size() > 0) {
420-
glGenBuffers(1, &o.vb);
421-
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
422-
glBufferData(GL_ARRAY_BUFFER, vb.size() * sizeof(float), &vb.at(0),
432+
if (buffer.size() > 0) {
433+
glGenBuffers(1, &o.vb_id);
434+
glBindBuffer(GL_ARRAY_BUFFER, o.vb_id);
435+
glBufferData(GL_ARRAY_BUFFER, buffer.size() * sizeof(float), &buffer.at(0),
423436
GL_STATIC_DRAW);
424-
o.numTriangles = vb.size() / (3 + 3 + 3 + 2) / 3; // 3:vtx, 3:normal, 3:col, 2:texcoord
437+
o.numTriangles = buffer.size() / (3 + 3 + 3 + 2) / 3; // 3:vtx, 3:normal, 3:col, 2:texcoord
425438

426439
printf("shape[%d] # of triangles = %d\n", static_cast<int>(s),
427440
o.numTriangles);
@@ -545,16 +558,17 @@ static void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj
545558
GLsizei stride = (3 + 3 + 3 + 2) * sizeof(float);
546559
for (size_t i = 0; i < drawObjects.size(); i++) {
547560
DrawObject o = drawObjects[i];
548-
if (o.vb < 1) {
561+
if (o.vb_id < 1) {
549562
continue;
550563
}
551564

552-
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
565+
glBindBuffer(GL_ARRAY_BUFFER, o.vb_id);
553566
glEnableClientState(GL_VERTEX_ARRAY);
554567
glEnableClientState(GL_NORMAL_ARRAY);
555568
glEnableClientState(GL_COLOR_ARRAY);
556569
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
557570

571+
glBindTexture(GL_TEXTURE_2D, 0);
558572
if ((o.material_id < materials.size())) {
559573
std::string diffuse_texname = materials[o.material_id].diffuse_texname;
560574
if (textures.find(diffuse_texname) != textures.end()) {
@@ -579,11 +593,11 @@ static void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj
579593
glColor3f(0.0f, 0.0f, 0.4f);
580594
for (size_t i = 0; i < drawObjects.size(); i++) {
581595
DrawObject o = drawObjects[i];
582-
if (o.vb < 1) {
596+
if (o.vb_id < 1) {
583597
continue;
584598
}
585599

586-
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
600+
glBindBuffer(GL_ARRAY_BUFFER, o.vb_id);
587601
glEnableClientState(GL_VERTEX_ARRAY);
588602
glEnableClientState(GL_NORMAL_ARRAY);
589603
glDisableClientState(GL_COLOR_ARRAY);

models/map-bump.mtl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
newmtl Material.001
2+
Ka 0 0 0
3+
Kd 0 0 0
4+
Ks 0 0 0
5+
map_Bump bump.jpg
6+
7+
newmtl Material.003
8+
Ka 0 0 0
9+
Kd 1 1 1
10+
Ks 0 0 0

0 commit comments

Comments
 (0)