Number5
Visualisierung 2 Project - Florian Schober (0828151, f.schober@live.com), Andreas Walch (0926780, walch.andreas89@gmail.com)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Window.cpp
Go to the documentation of this file.
1 #include "Application.hpp"
2 
3 void glfwOnWindowSizeChanged(GLFWwindow * window, int width, int height);
4 void glfwOnCursorMove(GLFWwindow * window, double x, double y);
5 void glfwOnKeyStateChanged(GLFWwindow * window, int key, int scancode, int action, int mods);
6 void glfwOnError(int errorCode, const char* desc)
7 {
8  cerr << "ERROR: (GLFW) " << desc << endl;
9 }
10 
11 
12 
14 {
15  set<string> extensions;
16  int nExtensions;
17  glGetIntegerv(GL_NUM_EXTENSIONS, &nExtensions);
18  for (int i = 0; i < nExtensions; i++)
19  extensions.insert(reinterpret_cast<char const *>(glGetStringi(GL_EXTENSIONS, i)));
20 
21 
22  cout << endl;
23  cout << "OpenGL Caps:" << endl;
24  cout << " GL Version: " << glGetString(GL_VERSION) << endl;
25  cout << " Vendor: " << glGetString(GL_VENDOR) << endl;
26  cout << " Renderer: " << glGetString(GL_RENDERER) << endl;
27  cout << " GLSL Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl;
28  cout << " Extensions:" << endl;
29  for (auto& extension : extensions)
30  cout << " " << extension << endl;
31 
32  cout << endl;
33 }
34 
35 void APIENTRY debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam)
36 {
37  stringstream ss;
38 
39  ss << "Source:";
40  if(source == GL_DEBUG_SOURCE_API_ARB) ss << "OpenGL";
41  else if(source == GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB) ss << "Windows";
42  else if(source == GL_DEBUG_SOURCE_SHADER_COMPILER_ARB) ss << "Shader Compiler";
43  else if(source == GL_DEBUG_SOURCE_THIRD_PARTY_ARB) ss << "Third Party";
44  else if(source == GL_DEBUG_SOURCE_APPLICATION_ARB) ss << "Application";
45  else if(source == GL_DEBUG_SOURCE_OTHER_ARB) ss << "Other";
46  else ss << "Unknown";
47  string sourceStr = ss.str();
48 
49  ss.str(string());
50  ss << "Type:";
51  if(source == GL_DEBUG_TYPE_ERROR_ARB) ss << "Error";
52  else if(source == GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB) ss << "Deprecated behavior";
53  else if(source == GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB) ss << "Undefined behavior";
54  else if(source == GL_DEBUG_TYPE_PORTABILITY_ARB) ss << "Portability";
55  else if(source == GL_DEBUG_TYPE_PERFORMANCE_ARB) ss << "Performance";
56  else if(source == GL_DEBUG_TYPE_OTHER_ARB) ss << "Other";
57  else ss << "Unknown";
58  string typeStr = ss.str();
59 
60  ss.str(string());
61  ss << "Severity:";
62  if(source == GL_DEBUG_SEVERITY_HIGH_ARB) ss << "High";
63  else if(source == GL_DEBUG_SEVERITY_MEDIUM_ARB) ss << "Medium";
64  else if(source == GL_DEBUG_SEVERITY_LOW_ARB) ss << "Low";
65  else ss << "Unknown";
66  string severityStr = ss.str();
67 
68  ss.str(string());
69  ss << "ID:";
70  ss << id;
71  string idStr = ss.str();
72 
73  ss.str(string());
74  ss << "Message:";
75  ss << message;
76  string msgStr = ss.str();
77 
78 
79  ss.str(string());
80  cerr << "ERROR: An OpenGL error occurred(" << sourceStr << ";" << typeStr << ";" << severityStr << ";" << idStr << ";" << msgStr << ")" << endl;
81 }
82 
83 
85  : m_app(app)
86  , m_window(nullptr)
87  , m_size(m_app->getConfig().resolution())
88  , m_cursorInit(false)
89  , m_hasProjChanged(2,true)
90 {
91 
92  if (!glfwInit())
93  {
94  cerr << "ERROR: Failed to init GLFW." << endl;
95  exit(EXIT_FAILURE);
96  }
97 
98  glfwSetErrorCallback(glfwOnError);
99 
100  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
101  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
102  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
103 #ifdef _DEBUG
104  glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
105 #endif
106 
107  GLFWmonitor* monitor = nullptr;
108  if (m_app->getConfig().isFullScreen())
109  {
110  monitor = glfwGetPrimaryMonitor();
111 
112  if (!m_app->getConfig().hasResolution())
113  {
114  auto mode = glfwGetVideoMode(monitor);
115  m_size.x = (float)mode->width;
116  m_size.y = (float)mode->height;
117  }
118  }
119 
120  m_window = glfwCreateWindow((int)m_size.x, (int)m_size.y, m_app->getConfig().windowTitle().c_str(), monitor, nullptr);
121  if (!m_window)
122  {
123  cerr << "ERROR: Failed to open window." << endl;
124  exit(EXIT_FAILURE);
125  }
126 
127  glfwMakeContextCurrent(m_window);
128  glfwSetWindowUserPointer(m_window,this);
129 
130  glfwSetInputMode(m_window, GLFW_STICKY_KEYS, GL_TRUE);
131  glfwSetInputMode(m_window, GLFW_STICKY_MOUSE_BUTTONS, GL_TRUE);
132 
133  // HIDE original cursor
134  glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
135 
136 
137  glfwSetWindowSizeCallback(m_window, glfwOnWindowSizeChanged);
138  glfwSetCursorPosCallback(m_window, glfwOnCursorMove);
139  glfwSetKeyCallback(m_window, glfwOnKeyStateChanged);
140 
141  glewExperimental = true;
142  if (glewInit() != GLEW_OK)
143  {
144  cerr << "ERROR: Failed to init GLEW." << endl;
145  exit(EXIT_FAILURE);
146  }
147 
148 
149 #ifdef _DEBUG
150  if ( glDebugMessageCallbackARB )
151  {
152  glDebugMessageCallbackARB(debugCallback, nullptr);
153  glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
154  }
155 #endif
156 
158 
159  onWindowSizeChanged(true);
160 }
162 {
163  glfwTerminate();
164 }
165 
166 
167 
168 
169 void glfwOnWindowSizeChanged(GLFWwindow * window, int width, int height)
170 {
171  auto wnd = (Window*)glfwGetWindowUserPointer(window);
172  wnd->m_size = vec2(width,height);
173  wnd->onWindowSizeChanged();
174 }
175 void Window::onWindowSizeChanged(bool initialization)
176 {
177  glViewport(0, 0, (GLsizei)m_size.x, (GLsizei)m_size.y);
178  m_projection = glm::perspective(glm::radians(60.0f), m_size.x/m_size.y, 0.1f, 1000.0f);
179 
180  m_hasProjChanged[1] = true;
181  double x,y;
182  glfwGetCursorPos(m_window, &x, &y);
183 
184  m_cursorDelta = vec2(0);
185  m_cursor = vec2((float)x,(float)y);
186 
187  if (!initialization)
189 }
190 
191 
192 void glfwOnKeyStateChanged(GLFWwindow * window, int key, int scancode, int action, int mods)
193 {
194  auto wnd = (Window*)glfwGetWindowUserPointer(window);
195  wnd->onKeyStateChanged(key, action == GLFW_PRESS);
196 }
197 void Window::onKeyStateChanged(int key, bool pressed)
198 {
199  m_app->onKeyStateChanged(key, pressed);
200 }
201 
202 void glfwOnCursorMove(GLFWwindow * window, double x, double y)
203 {
204  auto wnd = (Window*)glfwGetWindowUserPointer(window);
205  wnd->m_cursor = vec2((float)x,(float)y);
206 }
207 
208 
210 {
211  glfwSwapBuffers(m_window);
212 }
214 {
215  auto cursor = m_cursor;
216 
217  glfwPollEvents();
218 
219  auto cursorDeltaInMotoSpace = vec2(0, 0);
220 
221  if (!m_cursorInit)
222  {
223  cursorDeltaInMotoSpace = vec2(0,0);
224  m_cursorInit = true;
225  }
226  else
227  cursorDeltaInMotoSpace = m_cursor - cursor;
228 
229  auto newCursor = m_app->getOwnCursor().calculateCursorPosInVisualSpace(cursor, cursorDeltaInMotoSpace);
230 
231  if (newCursor != m_cursor)
232  {
233  m_cursor = newCursor;
235  m_cursorDelta = m_cursor - cursor;
236  }
237 
238  return !glfwWindowShouldClose(m_window);
239 }
240 
241 
242 
243 bool Window::keyDown(int key)
244 {
245  return glfwGetKey(m_window, key) == GLFW_PRESS;
246 }
247 bool Window::mouseDown(int button)
248 {
249  return glfwGetMouseButton(m_window, button) == GLFW_PRESS;
250 }
251 
253 {
254  glfwSetWindowShouldClose(m_window, true);
255 }
256 
257 
258 void Window::update(double time, double timeDelta)
259 {
261  m_hasProjChanged[1] = false;
262 }
263 
265 {
266  glfwSetCursorPos(m_window, pos.x, pos.y);
267 }
vec2 calculateCursorPosInVisualSpace(vec2 cursorPosBeforeMove, vec2 const &deltaMotorSpace)
Definition: OwnCursor.cpp:48
void close()
Definition: Window.cpp:252
Application * m_app
Definition: Window.hpp:16
Configuration & getConfig()
Definition: Application.hpp:36
void APIENTRY debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *userParam)
Definition: Window.cpp:35
void glfwOnKeyStateChanged(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: Window.cpp:192
vector< bool > m_hasProjChanged
Definition: Window.hpp:23
GLFWwindow * m_window
Definition: Window.hpp:17
bool keyDown(int key)
Definition: Window.cpp:243
vec2 m_cursor
Definition: Window.hpp:19
void onWindowSizeChanged(bool initialization=false)
Definition: Window.cpp:175
void onKeyStateChanged(int key, bool pressed)
Definition: Application.cpp:81
Window(Application *app)
Definition: Window.cpp:84
friend void glfwOnKeyStateChanged(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: Window.cpp:192
bool hasResolution() const
void glfwOnError(int errorCode, const char *desc)
Definition: Window.cpp:6
void swapBuffers()
Definition: Window.cpp:209
bool doEvents()
Definition: Window.cpp:213
void updatePointerPosition(vec2 pos)
Definition: Window.cpp:264
void update(double time, double timeDelta)
Definition: Window.cpp:258
vec2 m_cursorDelta
Definition: Window.hpp:20
void glfwOnCursorMove(GLFWwindow *window, double x, double y)
Definition: Window.cpp:202
OwnCursor & getOwnCursor()
bool mouseDown(int key)
Definition: Window.cpp:247
void onWindowSizeChanged(vec2 const &size)
Definition: Application.cpp:75
bool m_cursorInit
Definition: Window.hpp:22
bool isFullScreen() const
void onKeyStateChanged(int key, bool pressed)
Definition: Window.cpp:197
string const & windowTitle() const
mat4 m_projection
Definition: Window.hpp:21
friend void glfwOnWindowSizeChanged(GLFWwindow *window, int width, int height)
Definition: Window.cpp:169
void glfwOnWindowSizeChanged(GLFWwindow *window, int width, int height)
Definition: Window.cpp:169
~Window()
Definition: Window.cpp:161
friend void glfwOnCursorMove(GLFWwindow *window, double x, double y)
Definition: Window.cpp:202
vec2 m_size
Definition: Window.hpp:18
void reportOpenGlCaps()
Definition: Window.cpp:13