Skip to content

Commit 5a41a78

Browse files
committed
[Viewer example] Add wireframe onoff feature. Add backface cull mode.
1 parent 28a27f7 commit 5a41a78

File tree

4 files changed

+125
-23
lines changed

4 files changed

+125
-23
lines changed

examples/viewer/viewer.cc

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ bool mouseRightPressed;
152152
float curr_quat[4];
153153
float prev_quat[4];
154154
float eye[3], lookat[3], up[3];
155+
bool g_show_wire = true;
156+
bool g_cull_face = false;
155157

156158
GLFWwindow* window;
157159

@@ -833,8 +835,19 @@ static void keyboardFunc(GLFWwindow* window, int key, int scancode, int action,
833835
mv_z += -1;
834836
// camera.move(mv_x * 0.05, mv_y * 0.05, mv_z * 0.05);
835837
// Close window
836-
if (key == GLFW_KEY_Q || key == GLFW_KEY_ESCAPE)
838+
if (key == GLFW_KEY_Q || key == GLFW_KEY_ESCAPE) {
837839
glfwSetWindowShouldClose(window, GL_TRUE);
840+
}
841+
842+
if (key == GLFW_KEY_W) {
843+
// toggle wireframe
844+
g_show_wire = !g_show_wire;
845+
}
846+
847+
if (key == GLFW_KEY_C) {
848+
// cull option
849+
g_cull_face = !g_cull_face;
850+
}
838851

839852
// init_frame = true;
840853
}
@@ -898,7 +911,11 @@ static void Draw(const std::vector<DrawObject>& drawObjects,
898911
std::vector<tinyobj::material_t>& materials,
899912
std::map<std::string, GLuint>& textures) {
900913
glPolygonMode(GL_FRONT, GL_FILL);
901-
glPolygonMode(GL_BACK, GL_FILL);
914+
if (g_cull_face) {
915+
glPolygonMode(GL_BACK, GL_LINE);
916+
} else {
917+
glPolygonMode(GL_BACK, GL_FILL);
918+
}
902919

903920
glEnable(GL_POLYGON_OFFSET_FILL);
904921
glPolygonOffset(1.0, 1.0);
@@ -933,29 +950,31 @@ static void Draw(const std::vector<DrawObject>& drawObjects,
933950
}
934951

935952
// draw wireframe
936-
glDisable(GL_POLYGON_OFFSET_FILL);
937-
glPolygonMode(GL_FRONT, GL_LINE);
938-
glPolygonMode(GL_BACK, GL_LINE);
953+
if (g_show_wire) {
954+
glDisable(GL_POLYGON_OFFSET_FILL);
955+
glPolygonMode(GL_FRONT, GL_LINE);
956+
glPolygonMode(GL_BACK, GL_LINE);
957+
958+
glColor3f(0.0f, 0.0f, 0.4f);
959+
for (size_t i = 0; i < drawObjects.size(); i++) {
960+
DrawObject o = drawObjects[i];
961+
if (o.vb_id < 1) {
962+
continue;
963+
}
939964

940-
glColor3f(0.0f, 0.0f, 0.4f);
941-
for (size_t i = 0; i < drawObjects.size(); i++) {
942-
DrawObject o = drawObjects[i];
943-
if (o.vb_id < 1) {
944-
continue;
965+
glBindBuffer(GL_ARRAY_BUFFER, o.vb_id);
966+
glEnableClientState(GL_VERTEX_ARRAY);
967+
glEnableClientState(GL_NORMAL_ARRAY);
968+
glDisableClientState(GL_COLOR_ARRAY);
969+
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
970+
glVertexPointer(3, GL_FLOAT, stride, (const void*)0);
971+
glNormalPointer(GL_FLOAT, stride, (const void*)(sizeof(float) * 3));
972+
glColorPointer(3, GL_FLOAT, stride, (const void*)(sizeof(float) * 6));
973+
glTexCoordPointer(2, GL_FLOAT, stride, (const void*)(sizeof(float) * 9));
974+
975+
glDrawArrays(GL_TRIANGLES, 0, 3 * o.numTriangles);
976+
CheckErrors("drawarrays");
945977
}
946-
947-
glBindBuffer(GL_ARRAY_BUFFER, o.vb_id);
948-
glEnableClientState(GL_VERTEX_ARRAY);
949-
glEnableClientState(GL_NORMAL_ARRAY);
950-
glDisableClientState(GL_COLOR_ARRAY);
951-
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
952-
glVertexPointer(3, GL_FLOAT, stride, (const void*)0);
953-
glNormalPointer(GL_FLOAT, stride, (const void*)(sizeof(float) * 3));
954-
glColorPointer(3, GL_FLOAT, stride, (const void*)(sizeof(float) * 6));
955-
glTexCoordPointer(2, GL_FLOAT, stride, (const void*)(sizeof(float) * 9));
956-
957-
glDrawArrays(GL_TRIANGLES, 0, 3 * o.numTriangles);
958-
CheckErrors("drawarrays");
959978
}
960979
}
961980

@@ -995,6 +1014,11 @@ int main(int argc, char** argv) {
9951014
return 1;
9961015
}
9971016

1017+
std::cout << "W : Toggle wireframe\n";
1018+
std::cout << "C : Toggle face culling\n";
1019+
//std::cout << "K, J, H, L, P, N : Move camera\n";
1020+
std::cout << "Q, Esc : quit\n";
1021+
9981022
glfwMakeContextCurrent(window);
9991023
glfwSwapInterval(1);
10001024

models/issue-319-002.obj

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
###
2+
#
3+
# OBJ File Generated by Meshlab
4+
#
5+
####
6+
# Object ZH2_001.obj
7+
#
8+
# Vertices: 19
9+
# Faces: 3
10+
#
11+
####
12+
v 8219.830078 6406.934082 9.603000
13+
v 8219.632812 6406.582031 9.603000
14+
v 8219.632812 6406.582031 9.139000
15+
v 8219.973633 6405.420898 9.139000
16+
v 8211.128906 6404.090820 9.139000
17+
v 8211.128906 6404.090820 9.603000
18+
v 8211.469727 6402.930176 9.139000
19+
v 8211.469727 6402.930176 9.603000
20+
v 8211.133789 6402.831055 9.603000
21+
v 8210.793945 6403.992188 9.603000
22+
v 8210.713867 6404.264160 9.603000
23+
v 8211.840820 6403.038086 9.139000
24+
v 8219.899414 6404.861816 9.139000
25+
v 8219.755859 6405.352051 9.139000
26+
v 8211.985352 6402.544922 9.139000
27+
v 8232.911133 6378.534180 55.848999
28+
v 8226.281250 6376.591797 55.848999
29+
v 8226.341797 6376.384766 55.848999
30+
v 8233.450195 6378.466797 55.852001
31+
v 8233.450195 6378.466797 55.852001
32+
# 19 vertices, 0 vertices normals
33+
34+
f 2 1 11 10 9 8 6
35+
f 5 7 12 15 13 14 4 3
36+
f 18 19 20 16 17
37+
# 3 faces, 0 coords texture
38+
39+
# End of File

models/issue-319-003.obj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
####
2+
#
3+
# OBJ File Generated by Meshlab
4+
#
5+
####
6+
# Object new 1.obj
7+
#
8+
# Vertices: 10
9+
# Faces: 1
10+
#
11+
####
12+
v 8434.808594 6083.654785 2.387000
13+
v 8434.808594 6083.654785 71.633003
14+
v 8432.309570 6092.206055 71.633003
15+
v 8432.309570 6092.206055 63.955002
16+
v 8432.309570 6092.206055 2.387000
17+
v 8433.083984 6089.560059 71.633003
18+
v 8433.161133 6089.293945 71.633003
19+
v 8432.309570 6092.206055 64.323997
20+
v 8432.309570 6092.206055 67.152000
21+
v 8432.309570 6092.206055 68.078003
22+
# 10 vertices, 0 vertices normals
23+
24+
f 6 7 2 1 5 4 8 9 10 3
25+
# 1 faces, 0 coords texture
26+
27+
# End of File

models/issue-330.obj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
v -105.342712 40.184242 -16.056709
2+
v -105.463989 40.202003 -16.003181
3+
v -105.564941 40.207558 -15.934708
4+
v -105.722252 40.151146 -16.112091
5+
v -105.610237 40.191372 -16.176643
6+
v -105.667282 40.189800 -15.864197
7+
v -105.751717 40.125790 -15.794304
8+
# 7 vertices, 0 vertices normals
9+
10+
f 2 5 4 3
11+
f 4 6 3
12+
# 2 faces, 0 coords texture

0 commit comments

Comments
 (0)