Skip to content

Commit 58fa260

Browse files
committed
Show normal vector in viewer example.
1 parent 7399aed commit 58fa260

File tree

5 files changed

+88
-39
lines changed

5 files changed

+88
-39
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,13 @@ Features
7979
TODO
8080
----
8181

82-
* [ ] Read .obj/.mtl from memory
82+
* [ ] Read .obj/.mtl from memory.
83+
* [ ] Fix Python binding.
8384

8485
License
8586
-------
8687

87-
Licensed under 2 clause BSD.
88+
Licensed under MIT license.
8889

8990
Usage
9091
-----
@@ -94,11 +95,12 @@ Usage
9495
#include "tiny_obj_loader.h"
9596

9697
std::string inputfile = "cornell_box.obj";
98+
tinyobj::attrib_t attrib;
9799
std::vector<tinyobj::shape_t> shapes;
98100
std::vector<tinyobj::material_t> materials;
99101

100102
std::string err;
101-
bool ret = tinyobj::LoadObj(shapes, materials, err, inputfile.c_str());
103+
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, inputfile.c_str());
102104

103105
if (!err.empty()) { // `err` may contain warning message.
104106
std::cerr << err << std::endl;

examples/viewer/Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
GLFW_INC=-I/usr/local/include
22

3-
# OSX
4-
#GLFW_LIBS=-L/usr/local/lib -lglfw3 -lGLEW
5-
#GL_LIBS=-framework OpenGL
3+
UNAME=$(shell uname -s)
64

7-
# Linux
5+
ifeq ($(UNAME),Darwin)
6+
# OSX
7+
GLFW_LIBS=-L/usr/local/lib -lglfw3 -lGLEW
8+
GL_LIBS=-framework OpenGL
9+
else
10+
# Assume Linux
811
GLFW_LIBS=-L/usr/local/lib -lglfw3 -lGLEW
912
GL_LIBS=-lGL -lGLU -lX11 -lXrandr -lXi -lXxf86vm -lXcursor -lXinerama -ldl -pthread
13+
endif
1014

1115
CXX_FLAGS=-Wno-deprecated-declarations
1216

examples/viewer/viewer.cc

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ typedef struct {
3030

3131
std::vector<DrawObject> gDrawObjects;
3232

33-
int width = 512;
34-
int height = 512;
33+
int width = 768;
34+
int height = 768;
3535

3636
double prevMouseX, prevMouseY;
3737
bool mouseLeftPressed;
@@ -92,16 +92,19 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], std::vector<DrawObject>& dr
9292
return false;
9393
}
9494

95+
printf("# of vertices = %d\n", (int)(attrib.vertices.size()) / 3);
96+
printf("# of normals = %d\n", (int)(attrib.normals.size()) / 3);
97+
printf("# of texcoords = %d\n", (int)(attrib.texcoords.size()) / 2);
9598
printf("# of materials = %d\n", (int)materials.size());
96-
printf("# of shapes = %d\n", (int)shapes.size());
99+
printf("# of shapes = %d\n", (int)shapes.size());
97100

98101
bmin[0] = bmin[1] = bmin[2] = std::numeric_limits<float>::max();
99102
bmax[0] = bmax[1] = bmax[2] = -std::numeric_limits<float>::max();
100103

101104
{
102105
for (size_t s = 0; s < shapes.size(); s++) {
103106
DrawObject o;
104-
std::vector<float> vb; // pos(3float), normal(3float)
107+
std::vector<float> vb; // pos(3float), normal(3float), color(3float)
105108
for (size_t f = 0; f < shapes[s].mesh.indices.size()/3; f++) {
106109

107110
tinyobj::index_t idx0 = shapes[s].mesh.indices[3*f+0];
@@ -150,6 +153,19 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], std::vector<DrawObject>& dr
150153
vb.push_back(n[k][0]);
151154
vb.push_back(n[k][1]);
152155
vb.push_back(n[k][2]);
156+
// Use normal as color.
157+
float c[3] = {n[k][0], n[k][1], n[k][2]};
158+
float len2 = c[0] * c[0] + c[1] * c[1] + c[2] * c[2];
159+
if (len2 > 0.0f) {
160+
float len = sqrtf(len2);
161+
162+
c[0] /= len;
163+
c[1] /= len;
164+
c[2] /= len;
165+
}
166+
vb.push_back(c[0] * 0.5 + 0.5);
167+
vb.push_back(c[1] * 0.5 + 0.5);
168+
vb.push_back(c[2] * 0.5 + 0.5);
153169
}
154170

155171
}
@@ -160,7 +176,8 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], std::vector<DrawObject>& dr
160176
glGenBuffers(1, &o.vb);
161177
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
162178
glBufferData(GL_ARRAY_BUFFER, vb.size() * sizeof(float), &vb.at(0), GL_STATIC_DRAW);
163-
o.numTriangles = vb.size() / 6 / 3;
179+
o.numTriangles = vb.size() / 9 / 3;
180+
printf("shape[%d] # of triangles = %d\n", static_cast<int>(s), o.numTriangles);
164181
}
165182

166183
gDrawObjects.push_back(o);
@@ -243,8 +260,8 @@ void motionFunc(GLFWwindow* window, double mouse_x, double mouse_y){
243260

244261
add_quats(prev_quat, curr_quat, curr_quat);
245262
} else if (mouseMiddlePressed) {
246-
eye[0] += transScale * (mouse_x - prevMouseX) / (float)width;
247-
lookat[0] += transScale * (mouse_x - prevMouseX) / (float)width;
263+
eye[0] -= transScale * (mouse_x - prevMouseX) / (float)width;
264+
lookat[0] -= transScale * (mouse_x - prevMouseX) / (float)width;
248265
eye[1] += transScale * (mouse_y - prevMouseY) / (float)height;
249266
lookat[1] += transScale * (mouse_y - prevMouseY) / (float)height;
250267
} else if (mouseRightPressed) {
@@ -274,8 +291,10 @@ void Draw(const std::vector<DrawObject>& drawObjects)
274291
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
275292
glEnableClientState(GL_VERTEX_ARRAY);
276293
glEnableClientState(GL_NORMAL_ARRAY);
277-
glVertexPointer(3, GL_FLOAT, 24, (const void*)0);
278-
glNormalPointer(GL_FLOAT, 24, (const void*)(sizeof(float)*3));
294+
glEnableClientState(GL_COLOR_ARRAY);
295+
glVertexPointer(3, GL_FLOAT, 36, (const void*)0);
296+
glNormalPointer(GL_FLOAT, 36, (const void*)(sizeof(float)*3));
297+
glColorPointer(3, GL_FLOAT, 36, (const void*)(sizeof(float)*6));
279298

280299
glDrawArrays(GL_TRIANGLES, 0, 3 * o.numTriangles);
281300
CheckErrors("drawarrays");
@@ -296,8 +315,9 @@ void Draw(const std::vector<DrawObject>& drawObjects)
296315
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
297316
glEnableClientState(GL_VERTEX_ARRAY);
298317
glEnableClientState(GL_NORMAL_ARRAY);
299-
glVertexPointer(3, GL_FLOAT, 24, (const void*)0);
300-
glNormalPointer(GL_FLOAT, 24, (const void*)(sizeof(float)*3));
318+
glDisableClientState(GL_COLOR_ARRAY);
319+
glVertexPointer(3, GL_FLOAT, 36, (const void*)0);
320+
glNormalPointer(GL_FLOAT, 36, (const void*)(sizeof(float)*3));
301321

302322
glDrawArrays(GL_TRIANGLES, 0, 3 * o.numTriangles);
303323
CheckErrors("drawarrays");

loader_example.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//
2+
// g++ loader_example.cc
3+
//
14
#define TINYOBJLOADER_IMPLEMENTATION
25
#include "tiny_obj_loader.h"
36

tiny_obj_loader.h

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1-
//
2-
// Copyright 2012-2016, Syoyo Fujita.
3-
//
4-
// Licensed under 2-clause BSD license.
5-
//
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2012-2016 Syoyo Fujita and many contributors.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
*/
624

725
//
8-
// version devel : Change data structure. Support different index for
26+
// version 1.0.0 : Change data structure. Change license from BSD to MIT.
27+
// Support different index for
928
// vertex/normal/texcoord(#73, #39)
1029
// version 0.9.20: Fixes creating per-face material using `usemtl`(#68)
1130
// version 0.9.17: Support n-polygon and crease tag(OpenSubdiv extension)
@@ -117,26 +136,26 @@ typedef struct callback_t_ {
117136
void (*texcoord_cb)(void *user_data, float x, float y);
118137
// -2147483648 will be passed for undefined index
119138
void (*index_cb)(void *user_data, int v_idx, int vn_idx, int vt_idx);
120-
// `name` material name, `materialId` = the array index of material_t[]. -1 if a material not found in .mtl
121-
void (*usemtl_cb)(void *user_data, const char* name, int materialId);
139+
// `name` material name, `materialId` = the array index of material_t[]. -1 if
140+
// a material not found in .mtl
141+
void (*usemtl_cb)(void *user_data, const char *name, int materialId);
122142
// `materials` = parsed material data.
123143
void (*mtllib_cb)(void *user_data, const material_t *materials,
124144
int num_materials);
125145
// There may be multiple group names
126146
void (*group_cb)(void *user_data, const char **names, int num_names);
127147
void (*object_cb)(void *user_data, const char *name);
128148

129-
callback_t_() :
130-
vertex_cb(NULL),
131-
normal_cb(NULL),
132-
texcoord_cb(NULL),
133-
index_cb(NULL),
134-
usemtl_cb(NULL),
135-
mtllib_cb(NULL),
136-
group_cb(NULL),
137-
object_cb(NULL) {
138-
}
139-
149+
callback_t_()
150+
: vertex_cb(NULL),
151+
normal_cb(NULL),
152+
texcoord_cb(NULL),
153+
index_cb(NULL),
154+
usemtl_cb(NULL),
155+
mtllib_cb(NULL),
156+
group_cb(NULL),
157+
object_cb(NULL) {}
158+
140159
} callback_t;
141160

142161
class MaterialReader {
@@ -491,7 +510,8 @@ static vertex_index parseTriple(const char **token, int vsize, int vnsize,
491510

492511
// Parse raw triples: i, i/j/k, i//k, i/j
493512
static vertex_index parseRawTriple(const char **token) {
494-
vertex_index vi(static_cast<int>(0x80000000)); // 0x80000000 = -2147483648 = invalid
513+
vertex_index vi(
514+
static_cast<int>(0x80000000)); // 0x80000000 = -2147483648 = invalid
495515

496516
vi.v_idx = atoi((*token));
497517
(*token) += strcspn((*token), "/ \t\r");
@@ -1179,7 +1199,7 @@ bool LoadObjWithCallback(void *user_data, const callback_t &callback,
11791199

11801200
// material
11811201
std::map<std::string, int> material_map;
1182-
int materialId = -1; // -1 = invalid
1202+
int materialId = -1; // -1 = invalid
11831203

11841204
int maxchars = 8192; // Alloc enough size.
11851205
std::vector<char> buf(static_cast<size_t>(maxchars)); // Alloc enough size.

0 commit comments

Comments
 (0)