Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gl version detection #339

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ else()
set(NANOGUI_USE_GLAD_DEFAULT OFF)
endif()

option(NANOGUI_BUILD_EXAMPLE "Build NanoGUI example application?" ON)
option(NANOGUI_BUILD_SHARED "Build NanoGUI as a shared library?" ON)
option(NANOGUI_BUILD_PYTHON "Build a Python plugin for NanoGUI?" ON)
option(NANOGUI_USE_GLAD "Use Glad OpenGL loader library?" ${NANOGUI_USE_GLAD_DEFAULT})
option(NANOGUI_INSTALL "Install NanoGUI on `make install`?" ON)
option(NANOGUI_BUILD_EXAMPLE "Build NanoGUI example application?" ON)
option(NANOGUI_BUILD_SHARED "Build NanoGUI as a shared library?" ON)
option(NANOGUI_BUILD_PYTHON "Build a Python plugin for NanoGUI?" ON)
option(NANOGUI_USE_GLAD "Use Glad OpenGL loader library?" ${NANOGUI_USE_GLAD_DEFAULT})
option(NANOGUI_INSTALL "Install NanoGUI on `make install`?" ON)
option(NANOGUI_DEFAULT_GL_VERSION "Default to GL3 compatability?" ON)

set(NANOGUI_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the Python plugin")

Expand Down Expand Up @@ -86,6 +87,11 @@ add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/ext/glfw" "ext_build/glfw")
# XCode, but preferable for all build systems (reduces build artifacts).
set_target_properties(glfw PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)

# GL Version support: add NANOVG_GL3_IMPLEMENTATION unless disabled
if (NANOGUI_DEFAULT_GL_VERSION)
list(APPEND NANOGUI_EXTRA_DEFS -DNANOVG_GL3_IMPLEMENTATION)
endif()

# Python support: add NANOGUI_PYTHON flag to all targets
if (NANOGUI_BUILD_PYTHON)
list(APPEND NANOGUI_EXTRA_DEFS -DNANOGUI_PYTHON)
Expand Down
6 changes: 6 additions & 0 deletions include/nanogui/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#include <nanogui/widget.h>

#if defined(NANOVG_GL2_IMPLEMENTATION) || defined(NANOVG_GLES2_IMPLEMENTATION)
#define NANOGUI_CURSOR_DISABLED
#endif

NAMESPACE_BEGIN(nanogui)

/**
Expand Down Expand Up @@ -187,8 +191,10 @@ class NANOGUI_EXPORT Screen : public Widget {
protected:
GLFWwindow *mGLFWWindow;
NVGcontext *mNVGContext;
#if !defined(NANOGUI_CURSOR_DISABLED)
GLFWcursor *mCursors[(int) Cursor::CursorCount];
Cursor mCursor;
#endif
std::vector<Widget *> mFocusPath;
Vector2i mFBSize;
float mPixelRatio;
Expand Down
2 changes: 2 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ void mainloop(int refresh) {
std::chrono::milliseconds time(refresh);
while (mainloop_active) {
std::this_thread::sleep_for(time);
#if !defined(NANOVG_GL2_IMPLEMENTATION) && !defined(NANOVG_GLES2_IMPLEMENTATION)
glfwPostEmptyEvent();
#endif
}
}
);
Expand Down
59 changes: 53 additions & 6 deletions src/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# include <GLFW/glfw3native.h>
#endif

/* Allow enforcing the GL2 implementation of NanoVG */
#define NANOVG_GL3_IMPLEMENTATION
#include <nanovg_gl.h>

NAMESPACE_BEGIN(nanogui)
Expand All @@ -43,6 +41,33 @@ std::map<GLFWwindow *, Screen *> __nanogui_screens;
static bool gladInitialized = false;
#endif

inline NVGcontext* nvgCreateContext(int flags) {
#if defined(NANOVG_GL2_IMPLEMENTATION)
return nvgCreateGL2(flags);
#elif defined(NANOVG_GL3_IMPLEMENTATION)
return nvgCreateGL3(flags);
#elif defined(NANOVG_GLES2_IMPLEMENTATION)
return nvgCreateGLES2(flags);
#elif defined(NANOVG_GLES3_IMPLEMENTATION)
return nvgCreateGLES3(flags);
#else
#error No NANOVG_GL*_IMPLEMENTATION macro defined
#endif
}
inline void nvgDeleteContext(NVGcontext* ctx) {
#if defined(NANOVG_GL2_IMPLEMENTATION)
nvgDeleteGL2(ctx);
#elif defined(NANOVG_GL3_IMPLEMENTATION)
nvgDeleteGL3(ctx);
#elif defined(NANOVG_GLES2_IMPLEMENTATION)
nvgDeleteGLES2(ctx);
#elif defined(NANOVG_GLES3_IMPLEMENTATION)
nvgDeleteGLES3(ctx);
#else
#error No NANOVG_GL*_IMPLEMENTATION macro defined
#endif
}

/* Calculate pixel ratio for hi-dpi devices. */
static float get_pixel_ratio(GLFWwindow *window) {
#if defined(_WIN32)
Expand Down Expand Up @@ -118,19 +143,29 @@ static float get_pixel_ratio(GLFWwindow *window) {

Screen::Screen()
: Widget(nullptr), mGLFWWindow(nullptr), mNVGContext(nullptr),
mCursor(Cursor::Arrow), mBackground(0.3f, 0.3f, 0.32f, 1.f),
#if !defined(NANOGUI_CURSOR_DISABLED)
mCursor(Cursor::Arrow),
#endif
mBackground(0.3f, 0.3f, 0.32f, 1.f),
mShutdownGLFWOnDestruct(false), mFullscreen(false) {
#if !defined(NANOGUI_CURSOR_DISABLED)
memset(mCursors, 0, sizeof(GLFWcursor *) * (int) Cursor::CursorCount);
#endif
}

Screen::Screen(const Vector2i &size, const std::string &caption, bool resizable,
bool fullscreen, int colorBits, int alphaBits, int depthBits,
int stencilBits, int nSamples,
unsigned int glMajor, unsigned int glMinor)
: Widget(nullptr), mGLFWWindow(nullptr), mNVGContext(nullptr),
mCursor(Cursor::Arrow), mBackground(0.3f, 0.3f, 0.32f, 1.f), mCaption(caption),
#if !defined(NANOGUI_CURSOR_DISABLED)
mCursor(Cursor::Arrow),
#endif
mBackground(0.3f, 0.3f, 0.32f, 1.f), mCaption(caption),
mShutdownGLFWOnDestruct(false), mFullscreen(fullscreen) {
#if !defined(NANOGUI_CURSOR_DISABLED)
memset(mCursors, 0, sizeof(GLFWcursor *) * (int) Cursor::CursorCount);
#endif

/* Request a forward compatible OpenGL glMajor.glMinor core profile context.
Default value is an OpenGL 3.3 core profile context. */
Expand Down Expand Up @@ -240,6 +275,7 @@ Screen::Screen(const Vector2i &size, const std::string &caption, bool resizable,
}
);

#if !defined(NANOVG_GL2_IMPLEMENTATION) && !defined(NANOVG_GLES2_IMPLEMENTATION)
glfwSetDropCallback(mGLFWWindow,
[](GLFWwindow *w, int count, const char **filenames) {
auto it = __nanogui_screens.find(w);
Expand All @@ -251,6 +287,7 @@ Screen::Screen(const Vector2i &size, const std::string &caption, bool resizable,
s->dropCallbackEvent(count, filenames);
}
);
#endif

glfwSetScrollCallback(mGLFWWindow,
[](GLFWwindow *w, double x, double y) {
Expand Down Expand Up @@ -335,7 +372,7 @@ void Screen::initialize(GLFWwindow *window, bool shutdownGLFWOnDestruct) {
flags |= NVG_DEBUG;
#endif

mNVGContext = nvgCreateGL3(flags);
mNVGContext = nvgCreateContext(flags);
if (mNVGContext == nullptr)
throw std::runtime_error("Could not initialize NanoVG!");

Expand All @@ -348,8 +385,10 @@ void Screen::initialize(GLFWwindow *window, bool shutdownGLFWOnDestruct) {
mProcessEvents = true;
__nanogui_screens[mGLFWWindow] = this;

#if !defined(NANOGUI_CURSOR_DISABLED)
for (int i=0; i < (int) Cursor::CursorCount; ++i)
mCursors[i] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR + i);
#endif

/// Fixes retina display-related font rendering issue (#185)
nvgBeginFrame(mNVGContext, mSize[0], mSize[1], mPixelRatio);
Expand All @@ -358,12 +397,14 @@ void Screen::initialize(GLFWwindow *window, bool shutdownGLFWOnDestruct) {

Screen::~Screen() {
__nanogui_screens.erase(mGLFWWindow);
#if !defined(NANOGUI_CURSOR_DISABLED)
for (int i=0; i < (int) Cursor::CursorCount; ++i) {
if (mCursors[i])
glfwDestroyCursor(mCursors[i]);
}
#endif
if (mNVGContext)
nvgDeleteGL3(mNVGContext);
nvgDeleteContext(mNVGContext);
if (mGLFWWindow && mShutdownGLFWOnDestruct)
glfwDestroyWindow(mGLFWWindow);
}
Expand Down Expand Up @@ -425,7 +466,9 @@ void Screen::drawWidgets() {
#endif

glViewport(0, 0, mFBSize[0], mFBSize[1]);
#if !defined(NANOVG_GL2_IMPLEMENTATION) && !defined(NANOVG_GLES2_IMPLEMENTATION)
glBindSampler(0, 0);
#endif
nvgBeginFrame(mNVGContext, mSize[0], mSize[1], mPixelRatio);

draw(mNVGContext);
Expand Down Expand Up @@ -521,11 +564,13 @@ bool Screen::cursorPosCallbackEvent(double x, double y) {
p -= Vector2i(1, 2);

if (!mDragActive) {
#if !defined(NANOGUI_CURSOR_DISABLED)
Widget *widget = findWidget(p);
if (widget != nullptr && widget->cursor() != mCursor) {
mCursor = widget->cursor();
glfwSetCursor(mGLFWWindow, mCursors[(int) mCursor]);
}
#endif
} else {
ret = mDragWidget->mouseDragEvent(
p - mDragWidget->parent()->absolutePosition(), p - mMousePos,
Expand Down Expand Up @@ -569,10 +614,12 @@ bool Screen::mouseButtonCallbackEvent(int button, int action, int modifiers) {
mMousePos - mDragWidget->parent()->absolutePosition(), button,
false, mModifiers);

#if !defined(NANOGUI_CURSOR_DISABLED)
if (dropWidget != nullptr && dropWidget->cursor() != mCursor) {
mCursor = dropWidget->cursor();
glfwSetCursor(mGLFWWindow, mCursors[(int) mCursor]);
}
#endif

if (action == GLFW_PRESS && (button == GLFW_MOUSE_BUTTON_1 || button == GLFW_MOUSE_BUTTON_2)) {
mDragWidget = findWidget(mMousePos);
Expand Down