From a32fa80102a5474b966c24b570331f960f64f7c2 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Sun, 27 Jan 2019 00:38:34 +0900 Subject: [PATCH] Add support for building glview with draco. Fix out-of-bounds access when calling DrawMesh(). Fix potential out-of-bounds access when filling window title string. --- examples/glview/CMakeLists.txt | 7 ++++++- examples/glview/README.md | 11 +++++++++++ examples/glview/glview.cc | 14 ++++++++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/examples/glview/CMakeLists.txt b/examples/glview/CMakeLists.txt index 58e9152..cc94b29 100644 --- a/examples/glview/CMakeLists.txt +++ b/examples/glview/CMakeLists.txt @@ -25,8 +25,12 @@ set(CMAKE_CXX_STANDARD 11) if (${DRACO_DIR} STREQUAL "") else () - add_definitions(-DTINYGLTF_USE_DRACO) + # TODO(syoyo): better CMake script for draco + add_definitions(-DTINYGLTF_ENABLE_DRACO) include_directories(${DRACO_DIR}/include) + + link_directories(${DRACO_DIR}/lib) + set(DRACO_LIBRARY draco) endif () include_directories( @@ -43,6 +47,7 @@ add_executable(glview ) target_link_libraries ( glview + ${DRACO_LIBRARY} ${GLFW3_UNIX_LINK_LIBRARIES} ${GLEW_LIBRARY} ${GLFW3_glfw_LIBRARY} diff --git a/examples/glview/README.md b/examples/glview/README.md index 34a0c3b..0482942 100644 --- a/examples/glview/README.md +++ b/examples/glview/README.md @@ -27,6 +27,17 @@ Open .sln in Visual Studio 2013 When running .exe, glew and glfw dll must exist in the working directory. +#### Build with Draco(optional) + +Assume CMake build. + +``` +$ mkdir build +$ cd build +$ cmake -DDRACO_DIR=/path/to/draco ../ +$ make +``` + ## TODO * [ ] PBR Material diff --git a/examples/glview/glview.cc b/examples/glview/glview.cc index de393d5..4c462b4 100644 --- a/examples/glview/glview.cc +++ b/examples/glview/glview.cc @@ -677,10 +677,14 @@ static void DrawNode(tinygltf::Model &model, const tinygltf::Node &node) { // std::cout << it->first << std::endl; // FIXME(syoyo): Refactor. // DrawCurves(scene, it->second); - DrawMesh(model, model.meshes[node.mesh]); + if (node.mesh > -1) { + assert(node.mesh < model.meshes.size()); + DrawMesh(model, model.meshes[node.mesh]); + } // Draw child nodes. for (size_t i = 0; i < node.children.size(); i++) { + assert(node.children[i] < model.nodes.size()); DrawNode(model, model.nodes[node.children[i]]); } @@ -786,10 +790,12 @@ int main(int argc, char **argv) { return -1; } - char title[1024]; - sprintf(title, "Simple glTF viewer: %s", input_filename.c_str()); + std::stringstream ss; + ss << "Simple glTF viewer: " << input_filename; - window = glfwCreateWindow(width, height, title, NULL, NULL); + std::string title = ss.str(); + + window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL); if (window == NULL) { std::cerr << "Failed to open GLFW window. " << std::endl; glfwTerminate();