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

Update nanogui and use our own mainloop #221

Open
wants to merge 4 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
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "dependencies/nanogui"]
path = dependencies/nanogui
url = https://github.com/konstructs/nanogui.git
[submodule "dependencies/lodepng"]
path = dependencies/lodepng
url = https://github.com/lvandeve/lodepng.git
Expand All @@ -10,3 +7,6 @@
[submodule "dependencies/tinyobjloader"]
path = dependencies/tinyobjloader
url = https://github.com/syoyo/tinyobjloader.git
[submodule "dependencies/nanogui"]
path = dependencies/nanogui
url = https://github.com/wjakob/nanogui.git
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ find_package(ZLIB REQUIRED)
include_directories(
dependencies/nanogui/include
dependencies/nanogui/ext/glfw/include
dependencies/nanogui/ext/glew/include
dependencies/nanogui/ext/glad/include
dependencies/nanogui/ext/eigen
dependencies/nanogui/ext/nanovg/src
dependencies/optional-lite
Expand All @@ -59,15 +59,17 @@ include_directories(

FILE(
GLOB SOURCE_FILES
src/*.cpp)
src/*.cpp
dependencies/nanogui/ext/glad/src/glad.c
)

add_executable(konstructs ${SOURCE_FILES})

set(konstructs_LIBS
konstructs-lib
${ZLIB_LIBRARIES}
nanogui ${NANOGUI_EXTRA_LIBS}
glfw
glfw
)

if(WIN32 OR MINGW)
Expand Down
2 changes: 1 addition & 1 deletion dependencies/nanogui
Submodule nanogui updated 144 files
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ find_package(ZLIB REQUIRED)
include_directories(
${ZLIB_INCLUDE_DIRS}
../dependencies/nanogui/ext/glfw/include
../dependencies/nanogui/ext/glew/include
../dependencies/nanogui/ext/glad/include
../dependencies/nanogui/ext/eigen
../dependencies/lodepng
../dependencies/optional-lite
Expand Down
4 changes: 2 additions & 2 deletions lib/include/gl_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#if defined(__APPLE__)
#define GLFW_INCLUDE_GLCOREARB
#elif defined(WIN32)
#define GLEW_STATIC
#include <GL/glew.h>
#include <windows.h>
#include <glad/glad.h>
#else
#define GL_GLEXT_PROTOTYPES
#endif
Expand Down
99 changes: 89 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include <nanogui/nanogui.h>

#if defined(WIN32)
#define _WINSOCKAPI_
#include <windows.h>
Expand Down Expand Up @@ -63,10 +63,8 @@ class Konstructs: public nanogui::Screen {
Konstructs(const string &hostname,
const string &username,
const string &password,
bool debug_mode) :
nanogui::Screen(Eigen::Vector2i(KONSTRUCTS_APP_WIDTH,
KONSTRUCTS_APP_HEIGHT),
KONSTRUCTS_APP_TITLE),
bool debug_mode,
GLFWwindow* window) :
hostname(hostname),
username(username),
password(password),
Expand All @@ -92,6 +90,12 @@ class Konstructs: public nanogui::Screen {
debug_mode(debug_mode),
frame(0),
click_delay(0) {
initialize(window, true);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glfwSwapInterval(1);
glfwSwapBuffers(window);

using namespace nanogui;
performLayout(mNVGContext);
Expand Down Expand Up @@ -735,6 +739,7 @@ void glfw_error(int error_code, const char *error_string) {
cout << "GLFW Error[" << error_code << "]: " << error_string << endl;
}

Konstructs *app;

int main(int argc, char ** argv) {
std::string hostname = "play.konstructs.org";
Expand Down Expand Up @@ -784,17 +789,91 @@ int main(int argc, char ** argv) {
}

try {

glfwSetErrorCallback(glfw_error);
nanogui::init();

glfwInit();

glfwSetTime(0);

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwWindowHint(GLFW_SAMPLES, 0);
glfwWindowHint(GLFW_RED_BITS, 8);
glfwWindowHint(GLFW_GREEN_BITS, 8);
glfwWindowHint(GLFW_BLUE_BITS, 8);
glfwWindowHint(GLFW_ALPHA_BITS, 8);
glfwWindowHint(GLFW_STENCIL_BITS, 8);
glfwWindowHint(GLFW_DEPTH_BITS, 24);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

// Create a GLFWwindow object
GLFWwindow* window = glfwCreateWindow(KONSTRUCTS_APP_WIDTH, KONSTRUCTS_APP_HEIGHT,
KONSTRUCTS_APP_TITLE, nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glClearColor(0.2f, 0.25f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
{
nanogui::ref<Konstructs> app = new Konstructs(hostname, username, password, debug_mode);
app = new Konstructs(hostname, username, password, debug_mode, window);
glfwSetCursorPosCallback(window,
[](GLFWwindow *, double x, double y) {
app->cursorPosCallbackEvent(x, y);
});

glfwSetMouseButtonCallback(window,
[](GLFWwindow *, int button, int action, int modifiers) {
app->mouseButtonCallbackEvent(button, action, modifiers);
});

glfwSetKeyCallback(window,
[](GLFWwindow *, int key, int scancode, int action, int mods) {
app->keyCallbackEvent(key, scancode, action, mods);
});

glfwSetCharCallback(window,
[](GLFWwindow *, unsigned int codepoint) {
app->charCallbackEvent(codepoint);
});

glfwSetDropCallback(window,
[](GLFWwindow *, int count, const char **filenames) {
app->dropCallbackEvent(count, filenames);
});

glfwSetScrollCallback(window,
[](GLFWwindow *, double x, double y) {
app->scrollCallbackEvent(x, y);
});

glfwSetFramebufferSizeCallback(window,
[](GLFWwindow *, int width, int height) {
app->resizeCallbackEvent(width, height);
});

app->drawAll();
app->setVisible(true);
nanogui::mainloop();
}
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();

nanogui::shutdown();
glClearColor(0.2f, 0.25f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

app->drawContents();
app->drawWidgets();

glfwSwapBuffers(window);
}
delete app;
}
glfwTerminate();
} catch (const std::runtime_error &e) {
std::string error_msg = std::string("Caught a fatal error: ") + std::string(e.what());
#if defined(WIN32)
Expand Down