@@ -152,6 +152,8 @@ bool mouseRightPressed;
152152float curr_quat[4 ];
153153float prev_quat[4 ];
154154float eye[3 ], lookat[3 ], up[3 ];
155+ bool g_show_wire = true ;
156+ bool g_cull_face = false ;
155157
156158GLFWwindow* 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
0 commit comments