WindowGLFW.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. // This code contains NVIDIA Confidential Information and is disclosed
  3. // under the Mutual Non-Disclosure Agreement.
  4. //
  5. // Notice
  6. // ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
  7. // NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
  8. // THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
  9. // MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
  10. //
  11. // NVIDIA Corporation assumes no responsibility for the consequences of use of such
  12. // information or for any infringement of patents or other rights of third parties that may
  13. // result from its use. No license is granted by implication or otherwise under any patent
  14. // or patent rights of NVIDIA Corporation. No third party distribution is allowed unless
  15. // expressly authorized by NVIDIA. Details are subject to change without notice.
  16. // This code supersedes and replaces all information previously supplied.
  17. // NVIDIA Corporation products are not authorized for use as critical
  18. // components in life support devices or systems without express written approval of
  19. // NVIDIA Corporation.
  20. //
  21. // Copyright (c) 2014-2016 NVIDIA Corporation. All rights reserved.
  22. //
  23. // NVIDIA Corporation and its licensors retain all intellectual property and proprietary
  24. // rights in and to this software and related documentation and any modifications thereto.
  25. // Any use, reproduction, disclosure or distribution of this software and related
  26. // documentation without an express license agreement from NVIDIA Corporation is
  27. // strictly prohibited.
  28. //
  29. /////////////////////////////////////////////////////////////////////////////////////////
  30. #include "WindowGLFW.hpp"
  31. #include <iostream>
  32. #include <cstring>
  33. #ifdef VIBRANTE
  34. #include <GLFW/glfw3native.h>
  35. #include <EGL/eglext.h>
  36. #endif
  37. // -----------------------------------------------------------------------------
  38. WindowGLFW::WindowGLFW(const char* title, int width, int height, bool invisible)
  39. : WindowBase(width, height)
  40. #ifdef VIBRANTE
  41. , m_display(EGL_NO_DISPLAY)
  42. , m_context(EGL_NO_CONTEXT)
  43. #endif
  44. {/*
  45. if (glfwInit() == 0) {
  46. std::cout << "WindowGLFW: Failed initialize GLFW " << std::endl;
  47. throw std::exception();
  48. }
  49. // Create a windowed mode window and its OpenGL context
  50. glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
  51. glfwWindowHint(GLFW_SAMPLES, 0); // Disable MSAA
  52. glfwWindowHint(GLFW_DEPTH_BITS, 24); // Enable
  53. if (invisible) {
  54. glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
  55. }
  56. #ifdef _GLESMODE
  57. glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
  58. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  59. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
  60. #else
  61. glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
  62. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  63. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  64. #endif
  65. m_hWindow = glfwCreateWindow(width, height, title, NULL, NULL);
  66. if (!m_hWindow) {
  67. glfwTerminate();
  68. std::cout << "WindowGLFW: Failed create window" << std::endl;
  69. throw std::exception();
  70. }
  71. glfwMakeContextCurrent(m_hWindow);
  72. #ifdef USE_GLEW
  73. // dwRenderer requires glewExperimental
  74. // because it calls glGenVertexArrays()
  75. glewExperimental = GL_TRUE;
  76. GLenum err = glewInit();
  77. if (err != GLEW_OK) {
  78. glfwDestroyWindow(m_hWindow);
  79. glfwTerminate();
  80. std::cout << "WindowGLFW: Failed to init GLEW: " << glewGetErrorString(err) << std::endl;
  81. throw std::exception();
  82. }
  83. glGetError(); // clears error on init
  84. #endif
  85. // No vsync
  86. glfwSwapInterval(0);
  87. glfwSetInputMode(m_hWindow, GLFW_STICKY_KEYS, GL_FALSE);
  88. //Callbacks
  89. glfwSetWindowUserPointer(m_hWindow, this);
  90. glfwSetKeyCallback(m_hWindow, [](GLFWwindow *win, int key, int scancode, int action, int mods) {
  91. WindowGLFW *window = reinterpret_cast<WindowGLFW *>(glfwGetWindowUserPointer(win));
  92. window->onKeyCallback(key, scancode, action, mods);
  93. });
  94. glfwSetMouseButtonCallback(m_hWindow, [](GLFWwindow *win, int button, int action, int mods) {
  95. WindowGLFW *window = reinterpret_cast<WindowGLFW *>(glfwGetWindowUserPointer(win));
  96. window->onMouseButtonCallback(button, action, mods);
  97. });
  98. glfwSetCursorPosCallback(m_hWindow, [](GLFWwindow *win, double x, double y) {
  99. WindowGLFW *window = reinterpret_cast<WindowGLFW *>(glfwGetWindowUserPointer(win));
  100. window->onMouseMoveCallback(x, y);
  101. });
  102. glfwSetScrollCallback(m_hWindow, [](GLFWwindow *win, double dx, double dy) {
  103. WindowGLFW *window = reinterpret_cast<WindowGLFW *>(glfwGetWindowUserPointer(win));
  104. window->onMouseWheelCallback(dx, dy);
  105. });
  106. glfwSetFramebufferSizeCallback(m_hWindow, [](GLFWwindow *win, int width, int height) {
  107. WindowGLFW *window = reinterpret_cast<WindowGLFW *>(glfwGetWindowUserPointer(win));
  108. window->onResizeWindowCallback(width, height);
  109. });
  110. #ifdef VIBRANTE
  111. m_display = glfwGetEGLDisplay();
  112. m_context = glfwGetEGLContext(m_hWindow);
  113. // Get configuration
  114. EGLint num_config;
  115. eglGetConfigs(m_display, nullptr, 0, &num_config);
  116. m_config.reset(new EGLConfig[num_config]);
  117. if(eglGetConfigs(m_display, m_config.get(), num_config, &num_config) == EGL_FALSE) {
  118. glfwTerminate();
  119. std::cout << "WindowGLFW: Failed to get configs" << std::endl;
  120. throw std::exception();
  121. }
  122. #endif
  123. */
  124. }
  125. // -----------------------------------------------------------------------------
  126. WindowGLFW::~WindowGLFW(void)
  127. {
  128. /*glfwDestroyWindow(m_hWindow);
  129. glfwTerminate();
  130. */
  131. }
  132. // -----------------------------------------------------------------------------
  133. EGLDisplay WindowGLFW::getEGLDisplay(void)
  134. {
  135. // #ifdef VIBRANTE
  136. // return m_display;
  137. // #else
  138. // return 0;
  139. // #endif
  140. }
  141. // -----------------------------------------------------------------------------
  142. EGLContext WindowGLFW::getEGLContext(void)
  143. {
  144. // #ifdef VIBRANTE
  145. // return m_context;
  146. // #else
  147. // return 0;
  148. // #endif
  149. }
  150. // -----------------------------------------------------------------------------
  151. void WindowGLFW::onKeyCallback(int key, int scancode, int action, int mods)
  152. {
  153. /* if (!m_keyPressCallback)
  154. return;
  155. (void)scancode;
  156. if ((action == GLFW_PRESS || action == GLFW_REPEAT) && mods == 0)
  157. m_keyPressCallback(key); */
  158. }
  159. // -----------------------------------------------------------------------------
  160. void WindowGLFW::onMouseButtonCallback(int button, int action, int mods)
  161. {
  162. /* (void)mods;
  163. double x, y;
  164. glfwGetCursorPos(m_hWindow, &x, &y);
  165. if (action == GLFW_PRESS) {
  166. if (!m_mouseDownCallback)
  167. return;
  168. m_mouseDownCallback(button, (float)x, (float)y);
  169. } else if (action == GLFW_RELEASE) {
  170. if (!m_mouseUpCallback)
  171. return;
  172. m_mouseUpCallback(button, (float)x, (float)y);
  173. } */
  174. }
  175. // -----------------------------------------------------------------------------
  176. void WindowGLFW::onMouseMoveCallback(double x, double y)
  177. {
  178. /* if (!m_mouseMoveCallback)
  179. return;
  180. m_mouseMoveCallback((float)x, (float)y); */
  181. }
  182. // -----------------------------------------------------------------------------
  183. void WindowGLFW::onMouseWheelCallback(double dx, double dy)
  184. {
  185. /* if (!m_mouseWheelCallback)
  186. return;
  187. m_mouseWheelCallback((float)dx, (float)dy); */
  188. }
  189. // -----------------------------------------------------------------------------
  190. void WindowGLFW::onResizeWindowCallback(int width, int height)
  191. {
  192. /* m_width = width;
  193. m_height = height;
  194. if (!m_resizeWindowCallback)
  195. return;
  196. m_resizeWindowCallback(width, height); */
  197. }
  198. // -----------------------------------------------------------------------------
  199. bool WindowGLFW::swapBuffers(void)
  200. {
  201. // glfwPollEvents();
  202. // glfwSwapBuffers(m_hWindow);
  203. return true;
  204. }
  205. // -----------------------------------------------------------------------------
  206. void WindowGLFW::resetContext()
  207. {
  208. }
  209. // -----------------------------------------------------------------------------
  210. EGLContext WindowGLFW::createSharedContext() const {
  211. /* #ifdef VIBRANTE
  212. // -----------------------
  213. std::cout << "WindowGLFW: create shared EGL context" << std::endl;
  214. EGLint ctxAttribs[] = {
  215. EGL_CONTEXT_CLIENT_VERSION, 3,
  216. EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT, EGL_FALSE,
  217. EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT, EGL_NO_RESET_NOTIFICATION_EXT,
  218. EGL_NONE, EGL_NONE};
  219. EGLContext shared = eglCreateContext(m_display, *m_config.get(), m_context, ctxAttribs);
  220. if (shared == EGL_NO_CONTEXT) {
  221. std::cout << "WindowGLFW: Failed to create shared EGL context " << eglGetError() << std::endl;
  222. throw std::exception();
  223. }
  224. EGLBoolean status = eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, shared);
  225. if (status != EGL_TRUE) {
  226. std::cout << "WindowGLFW: Failed to make shared EGL context current: " << eglGetError() << std::endl;
  227. throw std::exception();
  228. }
  229. return shared;
  230. #else
  231. */ return 0;
  232. // #endif
  233. }
  234. // -----------------------------------------------------------------------------
  235. bool WindowGLFW::makeCurrent()
  236. {
  237. // Make the window's context current
  238. // glfwMakeContextCurrent(m_hWindow);
  239. return true;
  240. }
  241. // -----------------------------------------------------------------------------
  242. bool WindowGLFW::resetCurrent()
  243. {
  244. // glfwMakeContextCurrent(nullptr);
  245. return true;
  246. }
  247. // -----------------------------------------------------------------------------
  248. bool WindowGLFW::setWindowSize(int width, int height)
  249. {
  250. // Set the window size
  251. // glfwSetWindowSize(m_hWindow, width, height);
  252. return true;
  253. }