Skip to content

Commit

Permalink
add glfw
Browse files Browse the repository at this point in the history
  • Loading branch information
fxia22 committed Feb 13, 2020
1 parent 0531d9b commit efafee7
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "gibson2/core/render/pybind11"]
path = gibson2/core/render/pybind11
url = https://github.com/pybind/pybind11.git
[submodule "gibson2/core/render/glfw"]
path = gibson2/core/render/glfw
url = https://github.com/glfw/glfw
2 changes: 1 addition & 1 deletion examples/demo/mesh_renderer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def change_dir(event, x, y, flags, param):

while True:
with Profiler('Render'):
frame = renderer.render(modes=('rgb', 'normal', '3d'))
frame = renderer.render(modes=('rgb', 'seg', 'normal', '3d'))
cv2.imshow('test', cv2.cvtColor(np.concatenate(frame, axis=1), cv2.COLOR_RGB2BGR))
q = cv2.waitKey(1)
if q == ord('w'):
Expand Down
16 changes: 15 additions & 1 deletion gibson2/core/render/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,39 @@ endif()

add_subdirectory(pybind11)

# Find GLFW and OpenVR
set(GLFW_DIR glfw)
set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE INTERNAL "Generate installation target")
add_subdirectory("${GLFW_DIR}")
include_directories("${GLFW_DIR}/include")

find_package(CUDA REQUIRED)
set(CUDA_LIBRARIES PUBLIC ${CUDA_LIBRARIES})

cuda_add_library(MeshRendererContext MODULE glad/egl.cpp glad/gl.cpp cpp/Mesh_renderer.cpp)
add_library(CGLUtils MODULE glad/egl.cpp glad/gl.cpp cpp/cgl_utils.cpp)
add_library(tinyobjloader MODULE cpp/tinyobjloader/tiny_obj_loader.cc cpp/tinyobjloader/bindings.cc)
add_library(GLFWRendererContext MODULE glad/gl.cpp cpp/glfw_mesh_renderer.cpp)

if (USE_GLAD)
target_link_libraries(MeshRendererContext PRIVATE pybind11::module dl pthread)
target_link_libraries(CGLUtils PRIVATE pybind11::module dl pthread)
target_link_libraries(GLFWRendererContext PRIVATE pybind11::module dl glfw ${GLFW_LIBRARIES})
else ()
target_link_libraries(MeshRendererContext PRIVATE pybind11::module dl pthread EGL ${OPENGL_LIBRARIES})
target_link_libraries(CGLUtils PRIVATE pybind11::module dl pthread EGL ${OPENGL_LIBRARIES})
target_link_libraries(CGLUtils PRIVATE pybind11::module dl pthread EGL ${OPENGL_LIBRARIES})
target_link_libraries(GLFWRendererContext PRIVATE pybind11::module dl glfw ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES})
endif()

target_link_libraries(tinyobjloader PRIVATE pybind11::module)

set_target_properties(MeshRendererContext PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}")
set_target_properties(GLFWRendererContext PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}")
set_target_properties(CGLUtils PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}")
set_target_properties(tinyobjloader PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
Expand Down
76 changes: 76 additions & 0 deletions gibson2/core/render/cpp/glfw_mesh_renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <glad/gl.h>

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

namespace py = pybind11;

class GLFWRendererContext {
public:
GLFWRendererContext(int w, int h) :m_windowHeight(h), m_windowWidth(w) {};

int m_windowWidth;
int m_windowHeight;

int init() {
// Initialize GLFW context and window
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW.\n");
exit(EXIT_FAILURE);
}

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

// Hide GLFW window by default
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

GLFWwindow* window = glfwCreateWindow(m_windowHeight, m_windowHeight, "Gibson VR Renderer", NULL, NULL);
if (window == NULL) {
fprintf(stderr, "Failed to create GLFW window.\n");
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);

// Load all OpenGL function pointers through GLAD
if (!gladLoadGL(glfwGetProcAddress))
{
fprintf(stderr, "Failed to load OpenGL function pointers through GLAD.\n");
exit(EXIT_FAILURE);
}

printf("Succesfully initialized GLFW context and window!\n");

return 0;
}

void release() {
glfwTerminate();
}
};

PYBIND11_MODULE(GLFWRendererContext, m) {
m.doc() = "C++ GLFW bindings";

py::class_<GLFWRendererContext>(m, "GLFWRendererContext")
.def(py::init<int, int>())
.def("init", &GLFWRendererContext::init)
.def("release", &GLFWRendererContext::release);

#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
m.attr("__version__") = "dev";
#endif
}
1 change: 1 addition & 0 deletions gibson2/core/render/glfw
Submodule glfw added at 76406c
2 changes: 1 addition & 1 deletion gibson2/core/render/mesh_renderer/mesh_renderer_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from gibson2.core.render.mesh_renderer.glutils.meshutil import perspective, lookat, xyz2mat, quat2rotmat, mat2xyz, safemat2quat
from transforms3d.quaternions import axangle2quat, mat2quat
from transforms3d.euler import quat2euler, mat2euler
from gibson2.core.render.mesh_renderer import MeshRendererContext, CGLUtils
from gibson2.core.render.mesh_renderer import MeshRendererContext, CGLUtils, GLFWRendererContext
from gibson2.core.render.mesh_renderer.get_available_devices import get_available_devices
from gibson2.core.render.mesh_renderer.glutils.utils import colormap, loadTexture
import gibson2.core.render.mesh_renderer as mesh_renderer
Expand Down

0 comments on commit efafee7

Please sign in to comment.