Skip to content

Commit

Permalink
set backend at runtime, hide windows on init, missing include (#50)
Browse files Browse the repository at this point in the history
* set backend at init time

* glfw hide windows on init(), no focus on show()

* bugfix: missing include

* include Windows in CI
  • Loading branch information
nmwsharp authored Mar 31, 2020
1 parent e850ca4 commit 107930b
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 57 deletions.
28 changes: 22 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ os:
compiler:
- gcc
- clang


jobs:
include:
- os: windows

addons:
apt:
packages:
Expand All @@ -22,10 +26,22 @@ before_script:
- cd test
- mkdir build
- cd build
- cmake -DCMAKE_BUILD_TYPE=Debug -DPOLYSCOPE_BACKEND=MOCK_OPENGL ..
- cmake -DCMAKE_BUILD_TYPE=Debug -DPOLYSCOPE_BACKEND_OPENGL3_GLFW=ON -DPOLYSCOPE_BACKEND_OPENGL_MOCK=ON ..

script:
- make
- ./bin/polyscope-test --gtest_catch_exceptions=0
- cmake -DCMAKE_BUILD_TYPE=Debug -DPOLYSCOPE_BACKEND=OPENGL3_GLFW ..
- make
- |
if [ "$TRAVIS_OS_NAME" = "linux" ]; then
make
./bin/polyscope-test --gtest_catch_exceptions=0 backend=openGL_mock
fi
- |
if [ "$TRAVIS_OS_NAME" = "osx" ]; then
make
./bin/polyscope-test --gtest_catch_exceptions=0 backend=openGL_mock
fi
- |
if [ "$TRAVIS_OS_NAME" = "windows" ]; then
cmake --build "."
ls
./bin/Debug/polyscope-test.exe --gtest_catch_exceptions=0 backend=openGL_mock
fi
11 changes: 2 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@ cmake_policy(SET CMP0054 NEW) # don't implicitly dereference inside if()
## Project options

# Backend
set(POLYSCOPE_BACKEND "OPENGL3_GLFW" CACHE STRING "Which graphics and windowing backend to use")
set_property(CACHE POLYSCOPE_BACKEND PROPERTY STRINGS "OPENGL3_GLFW" "MOCK_OPENGL")
if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")
message("Using polyscope backend: ${POLYSCOPE_BACKEND}")
elseif("${POLYSCOPE_BACKEND}" STREQUAL "MOCK_OPENGL")
message("Using polyscope backend: ${POLYSCOPE_BACKEND}")
else()
message(FATAL_ERROR "Unrecognized polyscope backend from POLYSCOPE_BACKEND option, value is [${POLYSCOPE_BACKEND}]" )
endif()
set(POLYSCOPE_BACKEND_OPENGL3_GLFW "ON" CACHE BOOL "Enable openGL3_glfw backend")
set(POLYSCOPE_BACKEND_OPENGL_MOCK "ON" CACHE BOOL "Enable openGL_mock backend")

### Do anything needed for dependencies and bring their stuff in to scope
add_subdirectory(deps)
Expand Down
5 changes: 3 additions & 2 deletions deps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 2.8.9)

if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")
if("${POLYSCOPE_BACKEND_OPENGL3_GLFW}")

## Glad
add_subdirectory(glad)
Expand All @@ -11,8 +11,9 @@ if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(glfw)
endif()

elseif("${POLYSCOPE_BACKEND}" STREQUAL "MOCK_OPENGL")
if("${POLYSCOPE_BACKEND_OPENGL_MOCK}")
endif()

## Imgui
Expand Down
4 changes: 2 additions & 2 deletions deps/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STR
endif()


if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")
if("${POLYSCOPE_BACKEND_OPENGL3_GLFW}")

set(SRCS imgui/imgui.cpp imgui/imgui_draw.cpp imgui/imgui_widgets.cpp imgui/imgui_demo.cpp imgui/examples/imgui_impl_glfw.cpp imgui/examples/imgui_impl_opengl3.cpp)

Expand All @@ -32,7 +32,7 @@ if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")
add_definitions(-DIMGUI_IMPL_OPENGL_LOADER_GLAD)
endif()

elseif("${POLYSCOPE_BACKEND}" STREQUAL "MOCK_OPENGL")
elseif("${POLYSCOPE_BACKEND_OPENGL_MOCK}")

# Disable every platform-specific thing I can find in imgui
add_definitions(-DIMGUI_DISABLE_OSX_FUNCTIONS)
Expand Down
8 changes: 4 additions & 4 deletions include/polyscope/polyscope.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ namespace polyscope {
// forward declarations
class Structure;

// Initialize polyscope, including windowing system and openGL. Should be
// called exactly once at the beginning of a program. If initialization
// fails in any way, an exception will be thrown.
void init();
// Initialize polyscope, including windowing system and openGL. Should be called exactly once at the beginning of a
// program. If initialization fails in any way, an exception will be thrown.
// The backend string sets which rendering backend to use. If "", a reasonable default backend will be chosen.
void init(std::string backend = "");

// Give control to the polyscope GUI. Blocks until the user returns control via
// the GUI, possibly by exiting the window.
Expand Down
4 changes: 3 additions & 1 deletion include/polyscope/render/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class Engine {

// === Windowing and framework things
virtual void makeContextCurrent() = 0;
virtual void showWindow() = 0;
virtual void updateWindowSize(bool force = false) = 0;
virtual std::tuple<int, int> getWindowPos() = 0;
virtual bool windowRequestsClose() = 0;
Expand Down Expand Up @@ -450,7 +451,8 @@ inline void ShaderProgram::setAttribute(std::string name, const std::vector<std:
// Callers should basically only interact via these methods and variables

// Call once to initialize
void initializeRenderEngine();
// (see render/initialize_backend.cpp)
void initializeRenderEngine(std::string backend="");

// The global render engine
// Gets initialized by initializeRenderEngine() in polyscope::init();
Expand Down
6 changes: 3 additions & 3 deletions include/polyscope/render/mock_opengl/mock_gl_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

// A fake version of the opengl engine, with all of the actual gl calls stubbed out. Useful for testing.

// Macro to construct shader strings. Unforunately, it eats line numbers.
#define POLYSCOPE_GLSL(version, shader) "#version " #version "\n" #shader

namespace polyscope {
namespace render {
namespace backend_openGL_mock {

class GLTextureBuffer : public TextureBuffer {
public:
Expand Down Expand Up @@ -216,6 +214,7 @@ class MockGLEngine : public Engine {

// === Windowing and framework things
void makeContextCurrent() override;
void showWindow() override;
void updateWindowSize(bool force = false) override;
std::tuple<int, int> getWindowPos() override;
bool windowRequestsClose() override;
Expand Down Expand Up @@ -261,5 +260,6 @@ class MockGLEngine : public Engine {
protected:
};

}
} // namespace render
} // namespace polyscope
3 changes: 3 additions & 0 deletions include/polyscope/render/opengl/gl_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

namespace polyscope {
namespace render {
namespace backend_openGL3_glfw {

// Some very nice typdefs
typedef GLuint TextureBufferHandle;
Expand Down Expand Up @@ -262,6 +263,7 @@ class GLEngine : public Engine {

// === Windowing and framework things
void makeContextCurrent() override;
void showWindow() override;
void updateWindowSize(bool force = false) override;
std::tuple<int, int> getWindowPos() override;
bool windowRequestsClose() override;
Expand Down Expand Up @@ -312,5 +314,6 @@ class GLEngine : public Engine {
GLFWwindow* mainWindow = nullptr;
};

} // namespace backend_openGL3_glfw
} // namespace render
} // namespace polyscope
1 change: 1 addition & 0 deletions include/polyscope/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <random>
#include <sstream>
#include <string>
#include <tuple>


#include <glm/glm.hpp>
Expand Down
36 changes: 26 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ endif()

SET(INCLUDE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../include/polyscope/")

# Lists that will be populated for the render backend

# Add the main _engine file no matter what. All of the interesting parts are ifdef'd out, and this
# allows us to resolve stubs.
list (APPEND BACKEND_SRCS
render/opengl/gl_engine.cpp
render/mock_opengl/mock_gl_engine.cpp
)

# Configure the render backend
if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")
if("${POLYSCOPE_BACKEND_OPENGL3_GLFW}")
message("Polyscope backend openGL3_glfw enabled")

SET(BACKEND_SRCS
list (APPEND BACKEND_SRCS
render/opengl/gl_engine.cpp
render/opengl/shaders/texture_draw_shaders.cpp
render/opengl/shaders/lighting_shaders.cpp
Expand All @@ -23,18 +33,18 @@ if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")
render/opengl/shaders/cylinder_shaders.cpp
)

SET(BACKEND_HEADERS
list(APPEND BACKEND_HEADERS
${INCLUDE_ROOT}render/opengl/gl_engine.h
${INCLUDE_ROOT}render/opengl/shaders/common.h
)

SET(BACKEND_INCLUDE_DIRS
list(APPEND BACKEND_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/../deps/glad/include
${CMAKE_CURRENT_SOURCE_DIR}/../deps/glfw/include
)

# Link settings
SET(BACKEND_LIBS
list(APPEND BACKEND_LIBS
glfw ${GLFW_LIBRARIES}
)

Expand All @@ -51,10 +61,14 @@ if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")
else()
list(APPEND BACKEND_LIBS glad)
endif()

add_definitions(-DPOLYSCOPE_BACKEND_OPENGL3_GLFW_ENABLED)
endif()

elseif("${POLYSCOPE_BACKEND}" STREQUAL "MOCK_OPENGL")
if("${POLYSCOPE_BACKEND_OPENGL_MOCK}")
message("Polyscope backend openGL_mock enabled")

SET(BACKEND_SRCS
list (APPEND BACKEND_SRCS
render/mock_opengl/mock_gl_engine.cpp
render/opengl/shaders/texture_draw_shaders.cpp
render/opengl/shaders/lighting_shaders.cpp
Expand All @@ -67,18 +81,19 @@ elseif("${POLYSCOPE_BACKEND}" STREQUAL "MOCK_OPENGL")
render/opengl/shaders/cylinder_shaders.cpp
)

SET(BACKEND_HEADERS
list(APPEND BACKEND_HEADERS
${INCLUDE_ROOT}render/mock_opengl/mock_gl_engine.h
${INCLUDE_ROOT}render/opengl/shaders/common.h
)

SET(BACKEND_INCLUDE_DIRS
list(APPEND BACKEND_INCLUDE_DIRS
)

# Link settings
SET(BACKEND_LIBS
list(APPEND BACKEND_LIBS
)

add_definitions(-DPOLYSCOPE_BACKEND_OPENGL_MOCK_ENABLED)
endif()


Expand All @@ -98,6 +113,7 @@ SET(SRCS
render/color_maps.cpp
render/ground_plane.cpp
render/materials.cpp
render/initialize_backend.cpp

# General utilities
disjoint_sets.cpp
Expand Down
6 changes: 4 additions & 2 deletions src/polyscope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void writePrefsFile() {

// === Core global functions

void init() {
void init(std::string backend) {
if (state::initialized) {
throw std::logic_error(options::printPrefix + "Initialize called twice");
}
Expand All @@ -147,7 +147,7 @@ void init() {
}

// Initialize the rendering engine
render::initializeRenderEngine();
render::initializeRenderEngine(backend);

// Initialie ImGUI
IMGUI_CHECKVERSION();
Expand Down Expand Up @@ -602,6 +602,8 @@ void show(size_t forFrames) {
"must initialize Polyscope with polyscope::init() before calling polyscope::show().");
}

render::engine->showWindow();

// Main loop
while (!render::engine->windowRequestsClose() && forFrames > 0) {
mainLoopIteration();
Expand Down
51 changes: 51 additions & 0 deletions src/render/initialize_backend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "polyscope/render/engine.h"

namespace polyscope {
namespace render {

// Storage for the global engine pointer
Engine* engine = nullptr;

// Forward-declaration of initialize routines
// we don't want to just include the appropriate headers, because they may define conflicting symbols
namespace backend_openGL3_glfw {
void initializeRenderEngine();
}
namespace backend_openGL_mock {
void initializeRenderEngine();
}
// void initializeRenderEngine_openGL_mock();

void initializeRenderEngine(std::string backend) {

// Handle default backends
// (the string is getting overwritten, so lower on the list means higher priority)
if (backend == "") {

#ifdef POLYSCOPE_BACKEND_OPENGL_MOCK_ENABLED
// Don't set it one by default, since it's probably a mistake; better to throw the exception below.
// backend = "mock_openGL";
#endif

#ifdef POLYSCOPE_BACKEND_OPENGL3_GLFW_ENABLED
backend = "openGL3_glfw";
#endif

if (backend == "") {
throw std::runtime_error("no Polyscope backends available");
}
}

// Initialize the appropriate backend
if (backend == "openGL3_glfw") {
backend_openGL3_glfw::initializeRenderEngine();
} else if (backend == "openGL_mock") {
backend_openGL_mock::initializeRenderEngine();
} else {
throw std::runtime_error("unrecognized Polyscope backend " + backend);
}
}

} // namespace render
} // namespace polyscope

Loading

0 comments on commit 107930b

Please sign in to comment.