diff --git a/.gitignore b/.gitignore index f2884bd19..dc07ec022 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build* _builds _logs .vscode +.vs \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index ee4999956..b386e1369 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,8 +117,12 @@ set(component_list "") foreach(component_cmake ${components_cmake}) get_filename_component(component_dir ${component_cmake} DIRECTORY) get_filename_component(component_name ${component_dir} NAME) - add_library(${component_name} "") - list(APPEND component_list ${component_name}) + list(FIND component_list ${component_name} index) + + if(index EQUAL -1) + add_library(${component_name} "") + list(APPEND component_list ${component_name}) + endif() endforeach() foreach(component_cmake ${components_cmake}) diff --git a/components/pango_display/src/pangolin_gl.h b/components/pango_display/include/pangolin/display/pangolin_gl.h similarity index 93% rename from components/pango_display/src/pangolin_gl.h rename to components/pango_display/include/pangolin/display/pangolin_gl.h index a13d9e18f..7f86c4d75 100644 --- a/components/pango_display/src/pangolin_gl.h +++ b/components/pango_display/include/pangolin/display/pangolin_gl.h @@ -51,6 +51,7 @@ typedef std::map > KeyhookMap; struct PANGOLIN_EXPORT PangolinGl { PangolinGl(); + PangolinGl(PangolinGl& other); ~PangolinGl(); void Run(); @@ -88,15 +89,15 @@ struct PANGOLIN_EXPORT PangolinGl std::shared_ptr window; std::shared_ptr font; - std::unique_ptr console_view; + std::shared_ptr console_view; }; PangolinGl* GetCurrentContext(); void SetCurrentContext(PangolinGl* context); void RegisterNewContext(const std::string& name, std::shared_ptr newcontext); void DeleteContext(const std::string& name); -PangolinGl *FindContext(const std::string& name); -void SetCurrentContext(PangolinGl* newcontext); +PangolinGl* FindContext(const std::string& name); +void SetCurrentContext(std::shared_ptr newcontext); } diff --git a/components/pango_display/src/default_font.cpp b/components/pango_display/src/default_font.cpp index 7a90ee3c0..b52eb0ad2 100644 --- a/components/pango_display/src/default_font.cpp +++ b/components/pango_display/src/default_font.cpp @@ -1,6 +1,6 @@ #include -#include "pangolin_gl.h" +#include extern const unsigned char AnonymousPro_ttf[]; diff --git a/components/pango_display/src/display.cpp b/components/pango_display/src/display.cpp index a03dc793b..e1c7e4c84 100644 --- a/components/pango_display/src/display.cpp +++ b/components/pango_display/src/display.cpp @@ -48,7 +48,7 @@ #include #include -#include "pangolin_gl.h" +#include extern const unsigned char AnonymousPro_ttf[]; @@ -63,7 +63,6 @@ std::recursive_mutex contexts_mutex; // Context active for current thread __thread PangolinGl* context = 0; - void SetCurrentContext(PangolinGl* newcontext) { context = newcontext; } @@ -85,7 +84,6 @@ PangolinGl *FindContext(const std::string& name) WindowInterface& CreateWindowAndBind(std::string window_title, int w, int h, const Params& params) { std::unique_lock l(contexts_mutex); - pangolin::Uri win_uri; if(const char* extra_params = std::getenv("PANGOLIN_WINDOW_URI")) @@ -178,7 +176,6 @@ void RegisterNewContext(const std::string& name, std::shared_ptr new throw std::runtime_error("Context already exists."); } contexts[name] = newcontext; - // Process the following as if this context is now current. PangolinGl *oldContext = context; context = newcontext.get(); @@ -264,7 +261,7 @@ View& DisplayBase() View& CreateDisplay() { - int iguid = rand(); + int iguid =std::chrono::high_resolution_clock::now().time_since_epoch().count(); std::stringstream ssguid; ssguid << iguid; return Display(ssguid.str()); diff --git a/components/pango_display/src/handler.cpp b/components/pango_display/src/handler.cpp index 53e422f5f..39be7582d 100644 --- a/components/pango_display/src/handler.cpp +++ b/components/pango_display/src/handler.cpp @@ -29,7 +29,7 @@ #include #include -#include "pangolin_gl.h" +#include namespace pangolin { diff --git a/components/pango_display/src/pangolin_gl.cpp b/components/pango_display/src/pangolin_gl.cpp index 74f0b4cf9..d7ede2511 100644 --- a/components/pango_display/src/pangolin_gl.cpp +++ b/components/pango_display/src/pangolin_gl.cpp @@ -25,7 +25,7 @@ * OTHER DEALINGS IN THE SOFTWARE. */ -#include "pangolin_gl.h" +#include #include #include @@ -36,7 +36,6 @@ PangolinGl::PangolinGl() : user_app(0), quit(false), mouse_state(0),activeDisplay(0) { } - PangolinGl::~PangolinGl() { // Free displays owned by named_managed_views diff --git a/components/pango_display/src/process.cpp b/components/pango_display/src/process.cpp index 4e954fb04..dcf5245ea 100644 --- a/components/pango_display/src/process.cpp +++ b/components/pango_display/src/process.cpp @@ -28,7 +28,7 @@ #include #include #include -#include "pangolin_gl.h" +#include namespace pangolin { @@ -47,7 +47,7 @@ void Resize( int width, int height ) void Keyboard(unsigned char key, int x, int y, bool pressed, KeyModifierBitmask /*key_modifiers*/) { - PangolinGl* context = GetCurrentContext(); + PangolinGl* context = GetCurrentContext(); // Force coords to match OpenGl Window Coords y = context->base.v.h - y; diff --git a/components/pango_display/src/view.cpp b/components/pango_display/src/view.cpp index 7e0db7163..8f085c48d 100644 --- a/components/pango_display/src/view.cpp +++ b/components/pango_display/src/view.cpp @@ -33,7 +33,7 @@ #include #include -#include "pangolin_gl.h" +#include namespace pangolin { diff --git a/components/pango_display/src/widgets.cpp b/components/pango_display/src/widgets.cpp index 2469fc681..8157da9d0 100644 --- a/components/pango_display/src/widgets.cpp +++ b/components/pango_display/src/widgets.cpp @@ -37,7 +37,7 @@ #include #include -#include "pangolin_gl.h" +#include using namespace std; diff --git a/components/pango_geometry/src/geometry_obj.cpp b/components/pango_geometry/src/geometry_obj.cpp index 37ea47f7c..05ab3f55d 100644 --- a/components/pango_geometry/src/geometry_obj.cpp +++ b/components/pango_geometry/src/geometry_obj.cpp @@ -101,12 +101,11 @@ pangolin::Geometry LoadGeometryObj(const std::string& filename) PANGO_ASSERT(attrib.texcoords.size() % 2 == 0); // Load textures - a bit of a hack for now. - for(size_t i=0; i < materials.size(); ++i) { - if(!materials[i].diffuse_texname.empty()) { - const std::string tex_name = FormatString("texture_%",i); + for(const auto& material : materials) { + if(!material.diffuse_texname.empty()) { try { - TypedImage& tex_image = geom.textures[tex_name]; - tex_image = LoadImage(PathParent(filename) + "/" + materials[i].diffuse_texname); + TypedImage& tex_image = geom.textures[material.name]; + tex_image = LoadImage(PathParent(filename) + "/" + material.diffuse_texname); const int row_bytes = tex_image.w * tex_image.fmt.bpp / 8; std::vector tmp_row(row_bytes); for (std::size_t y=0; y < (tex_image.h >> 1); ++y) { @@ -115,8 +114,8 @@ pangolin::Geometry LoadGeometryObj(const std::string& filename) std::memcpy(tex_image.RowPtr(tex_image.h - 1 - y), tmp_row.data(), row_bytes); } } catch(const std::exception&) { - pango_print_warn("Unable to read texture '%s'\n", tex_name.c_str()); - geom.textures.erase(tex_name); + pango_print_warn("Unable to read texture '%s'\n", material.name.c_str()); + geom.textures.erase(material.name); } } } diff --git a/components/pango_glgeometry/src/glgeometry.cpp b/components/pango_glgeometry/src/glgeometry.cpp index 18618781f..12aa3ece0 100644 --- a/components/pango_glgeometry/src/glgeometry.cpp +++ b/components/pango_glgeometry/src/glgeometry.cpp @@ -96,47 +96,50 @@ void GlDraw(GlSlProgram& prog, const GlGeometry& geom, const GlTexture* matcap) { // Bind textures int num_tex_bound = 0; - for(auto& tex : geom.textures) { - glActiveTexture(GL_TEXTURE0 + num_tex_bound); - tex.second.Bind(); - prog.SetUniform(tex.first, (int)num_tex_bound); - ++num_tex_bound; + for (const auto& buffer: geom.buffers) { + BindGlElement(prog, buffer.second); } - if(matcap) { glActiveTexture(GL_TEXTURE0 + num_tex_bound); matcap->Bind(); prog.SetUniform("matcap", (int)num_tex_bound); ++num_tex_bound; - } - - // Bind all attribute buffers - for(auto& buffer : geom.buffers) { - BindGlElement(prog, buffer.second); - } - - // Draw all geometry - for(auto& buffer : geom.objects) { - auto it_indices = buffer.second.attributes.find("vertex_indices"); - if(it_indices != buffer.second.attributes.end()) { - buffer.second.Bind(); - auto& attrib = it_indices->second; - glDrawElements( - GL_TRIANGLES, attrib.count_per_element * attrib.num_elements, - attrib.gltype, reinterpret_cast(attrib.offset) - ); - buffer.second.Unbind(); + }else{ + std::vector texturesNames; + for (auto& tex: geom.textures) { + texturesNames.emplace_back(tex.first); } - } + for (auto& texName: texturesNames) { + glActiveTexture(GL_TEXTURE0 + num_tex_bound); + geom.textures.at(texName).Bind(); + prog.SetUniform("texture", (int) num_tex_bound); + + auto& buffer = *geom.objects.equal_range(texName).first; + auto it_indices = buffer.second.attributes.find("vertex_indices"); + if (it_indices != buffer.second.attributes.end()) { + buffer.second.Bind(); + auto& attrib = it_indices->second; + glDrawElements( + GL_TRIANGLES, attrib.count_per_element * attrib.num_elements, + attrib.gltype, reinterpret_cast(attrib.offset) + ); + buffer.second.Unbind(); + + } + + ++num_tex_bound; - // Unbind attribute buffers - for(auto& buffer : geom.buffers) { - UnbindGlElements(prog, buffer.second); + } } // Unbind textures glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0); + + // Unbind attribute buffers + for (const auto& buffer: geom.buffers) { + UnbindGlElements(prog, buffer.second); + } } } diff --git a/components/pango_windowing/include/pangolin/windowing/WinWindow.h b/components/pango_windowing/include/pangolin/windowing/WinWindow.h index ed3806011..ad7d1ff0c 100644 --- a/components/pango_windowing/include/pangolin/windowing/WinWindow.h +++ b/components/pango_windowing/include/pangolin/windowing/WinWindow.h @@ -31,10 +31,9 @@ #include #include #include - namespace pangolin { - + struct WinWindow : public WindowInterface { WinWindow( @@ -70,7 +69,7 @@ struct WinWindow : public WindowInterface LRESULT HandleWinMessages(UINT message, WPARAM wParam, LPARAM lParam); - void RegisterThisClass(HMODULE hCurrentInst); + void RegisterThisClass(HMODULE hCurrentInst,const std::string &window_title); void SetupPixelFormat(HDC hdc); diff --git a/components/pango_windowing/src/display_win.cpp b/components/pango_windowing/src/display_win.cpp index 5707d9fd3..5f6f280ad 100644 --- a/components/pango_windowing/src/display_win.cpp +++ b/components/pango_windowing/src/display_win.cpp @@ -50,9 +50,7 @@ inline void _CheckWLDieOnError( const char *sFile, const int nLine ) namespace pangolin { - const char *className = "Pangolin"; - //////////////////////////////////////////////////////////////////////// // Utils //////////////////////////////////////////////////////////////////////// @@ -214,10 +212,10 @@ WinWindow::WinWindow( std::cerr << "GetModuleHandle() failed" << std::endl; CheckWGLDieOnError(); } - RegisterThisClass(hCurrentInst); + RegisterThisClass(hCurrentInst,window_title); HWND thishwnd = CreateWindowA( - className, window_title.c_str(), + window_title.c_str(), window_title.c_str(), WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, width, height, NULL, NULL, hCurrentInst, this); @@ -229,7 +227,6 @@ WinWindow::WinWindow( if( thishwnd != hWnd ) { throw std::runtime_error("Pangolin Window Creation Failed."); } - // Display Window ShowWindow(hWnd, SW_SHOW); } @@ -242,7 +239,7 @@ WinWindow::~WinWindow() } } -void WinWindow::RegisterThisClass(HMODULE hCurrentInst) +void WinWindow::RegisterThisClass(HMODULE hCurrentInst,const std::string &window_title) { WNDCLASSA wndClass; wndClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; @@ -254,7 +251,7 @@ void WinWindow::RegisterThisClass(HMODULE hCurrentInst) wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wndClass.lpszMenuName = NULL; - wndClass.lpszClassName = className; + wndClass.lpszClassName = window_title.c_str(); if(!RegisterClassA(&wndClass)) { std::cerr << "RegisterClass() failed" << std::endl; CheckWGLDieOnError(); @@ -537,7 +534,6 @@ void WinWindow::Resize(unsigned int w, unsigned int h) CheckWGLDieOnError(); } } - void WinWindow::MakeCurrent() { if(wglMakeCurrent(hDC, hGLRC)==FALSE) { diff --git a/examples/HelloPangolinThreads/CMakeLists.txt b/examples/HelloPangolinThreads/CMakeLists.txt index 8073645a4..b7cfabce0 100644 --- a/examples/HelloPangolinThreads/CMakeLists.txt +++ b/examples/HelloPangolinThreads/CMakeLists.txt @@ -1,9 +1,10 @@ # Find Pangolin (https://github.com/stevenlovegrove/Pangolin) -find_package(Pangolin 0.5 REQUIRED) +find_package(Pangolin 0.8 REQUIRED) + find_package(Threads QUIET) -if(Theads_FOUND) +if(Threads_FOUND) + include_directories(${Pangolin_INCLUDE_DIRS}) add_executable(HelloPangolinThreads main.cpp) - target_include_directories(HelloPangolinThreads ${Pangolin_INCLUDE_DIRS}) target_link_libraries(HelloPangolinThreads pango_display Threads::Threads) endif() diff --git a/examples/HelloPangolinThreads/main.cpp b/examples/HelloPangolinThreads/main.cpp index b1e90179a..2f5cc340b 100644 --- a/examples/HelloPangolinThreads/main.cpp +++ b/examples/HelloPangolinThreads/main.cpp @@ -1,43 +1,43 @@ #include #include +#include +#include #include - +#include #include -static const std::string window_name = "HelloPangolinThreads"; - -void setup() { +std::mutex lock; +void setup(std::string &window_name) { // create a window and bind its context to the main thread - pangolin::CreateWindowAndBind(window_name, 640, 480); - + // enable depth - glEnable(GL_DEPTH_TEST); - + //glEnable(GL_DEPTH_TEST); // unset the current context from the main thread - pangolin::GetBoundWindow()->RemoveCurrent(); + //pangolin::GetBoundWindow()->RemoveCurrent(); } -void run() { +void run(std::string &window_name) { // fetch the context and bind it to this thread - pangolin::BindToContext(window_name); - + pangolin::CreateWindowAndBind(window_name, 640, 480); + auto context = pangolin::GetCurrentContext(); // we manually need to restore the properties of the context glEnable(GL_DEPTH_TEST); // Define Projection and initial ModelView matrix - pangolin::OpenGlRenderState s_cam( + pangolin::OpenGlRenderState s_cam( pangolin::ProjectionMatrix(640,480,420,420,320,240,0.2,100), pangolin::ModelViewLookAt(-2,2,-2, 0,0,0, pangolin::AxisY) ); - + pangolin::RegisterKeyPressCallback('0', [&]() + { std::cout <