MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
graphics_app.h
Go to the documentation of this file.
1 /*
2  This file is part of the MinGfx Project.
3 
4  Copyright (c) 2017,2018 Regents of the University of Minnesota.
5  All Rights Reserved.
6 
7  Original Author(s) of this File:
8  Dan Keefe, 2017, University of Minnesota
9 
10  Author(s) of Significant Updates/Modifications to the File:
11  ...
12  */
13 
14 
15 #ifndef SRC_GRAPHICS_APP_H_
16 #define SRC_GRAPHICS_APP_H_
17 
18 // disable warnings for this 3rd party code
19 #pragma warning ( push, 0 )
20 #include <nanogui/nanogui.h>
21 #pragma warning ( pop )
22 
23 #include <iostream>
24 
25 #include "point2.h"
26 #include "vector2.h"
27 
29 namespace mingfx {
30 
31 
135 class GraphicsApp {
136 public:
137 
144  GraphicsApp(int width, int height, const std::string &caption);
145 
146 
148  virtual ~GraphicsApp();
149 
150 
151  // Callback methods -- override these and fill in to respond to user
152  // input events.
153 
165  virtual void OnMouseMove(const Point2 &pos, const Vector2 &delta) {}
166 
174  virtual void OnLeftMouseDown(const Point2 &pos) {}
175 
187  virtual void OnLeftMouseDrag(const Point2 &pos, const Vector2 &delta) {}
188 
196  virtual void OnLeftMouseUp(const Point2 &pos) {}
197 
198 
200  virtual void OnMiddleMouseDown(const Point2 &pos) {}
201 
203  virtual void OnMiddleMouseDrag(const Point2 &pos, const Vector2 &delta) {}
204 
206  virtual void OnMiddleMouseUp(const Point2 &pos) {}
207 
208 
210  virtual void OnRightMouseDown(const Point2 &pos) {}
211 
213  virtual void OnRightMouseDrag(const Point2 &pos, const Vector2 &delta) {}
214 
216  virtual void OnRightMouseUp(const Point2 &pos) {}
217 
218 
225  virtual void OnKeyDown(const char *c, int modifiers) {}
226 
233  virtual void OnKeyRepeat(const char *c, int modifiers) {}
234 
241  virtual void OnKeyUp(const char *c, int modifiers) {}
242 
243 
244 
247  virtual void OnSpecialKeyDown(int key, int scancode, int modifiers) {}
248 
251  virtual void OnSpecialKeyRepeat(int key, int scancode, int modifiers) {}
252 
255  virtual void OnSpecialKeyUp(int key, int scancode, int modifiers) {}
256 
257 
261  virtual void OnWindowResize(int new_width, int new_height) {}
262 
263 
271  virtual void Run();
272 
273 
284  virtual void InitNanoGUI() {}
285 
297  virtual void InitOpenGL() {}
298 
299 
306  virtual void UpdateSimulation(double dt) {}
307 
308 
311  virtual void DrawUsingNanoVG(NVGcontext *ctx) {}
312 
313 
314 
317  virtual void DrawUsingOpenGL() {}
318 
319 
322  virtual bool IsKeyDown(int key);
323 
325  virtual bool IsLeftMouseDown();
326 
328  virtual bool IsMiddleMouseDown();
329 
331  virtual bool IsRightMouseDown();
332 
334  virtual int window_width();
335 
337  virtual int window_height();
338 
343  virtual int framebuffer_width();
344 
349  virtual int framebuffer_height();
350 
352  virtual float aspect_ratio();
353 
354 
359  virtual Point2 PixelsToNormalizedDeviceCoords(const Point2 &pointInPixels);
360 
365  virtual Point2 NormalizedDeviceCoordsToPixels(const Point2 &pointInNDC);
366 
367 
372  virtual Vector2 PixelsToNormalizedDeviceCoords(const Vector2 &vectorInPixels);
373 
378  virtual Vector2 NormalizedDeviceCoordsToPixels(const Vector2 &pointInNDC);
379 
382  virtual float ReadZValueAtPixel(const Point2 &pointInPixels, unsigned int whichBuffer = GL_BACK);
383 
385  virtual nanogui::Screen* screen();
386 
388  virtual GLFWwindow* window();
389 
390 
393  virtual void ResizeWindow(int new_width, int new_height);
394 
395 
403  virtual void InitGraphicsContext();
404 
405 private:
406 
407  bool cursor_pos_glfw_cb(double x, double y);
408  bool mouse_button_glfw_cb(int button, int action, int modifiers);
409  bool key_glfw_cb(int key, int scancode, int action, int mods);
410  bool char_glfw_cb(unsigned int codepoint);
411  bool drop_glfw_cb(int count, const char **filenames);
412  bool scroll_glfw_cb(double x, double y);
413  bool resize_glfw_cb(int width, int height);
414 
415  virtual void mouse_move(const Point2 &pos, const Vector2 &delta) {
416  OnMouseMove(pos, delta);
417  }
418  virtual void left_mouse_down(const Point2 &pos) {
419  OnLeftMouseDown(pos);
420  }
421  virtual void left_mouse_drag(const Point2 &pos, const Vector2 &delta) {
422  OnLeftMouseDrag(pos, delta);
423  }
424  virtual void left_mouse_up(const Point2 &pos) {
425  OnLeftMouseUp(pos);
426  }
427  virtual void middle_mouse_down(const Point2 &pos) {
428  OnMiddleMouseDown(pos);
429  }
430  virtual void middle_mouse_drag(const Point2 &pos, const Vector2 &delta) {
431  OnMiddleMouseDrag(pos, delta);
432  }
433  virtual void middle_mouse_up(const Point2 &pos) {
434  OnMiddleMouseUp(pos);
435  }
436  virtual void right_mouse_down(const Point2 &pos) {
437  OnRightMouseDown(pos);
438  }
439  virtual void right_mouse_drag(const Point2 &pos, const Vector2 &delta) {
440  OnRightMouseDrag(pos, delta);
441  }
442  virtual void right_mouse_up(const Point2 &pos) {
443  OnRightMouseUp(pos);
444  }
445  virtual void key_down(const char *c, int modifiers) {
446  OnKeyDown(c, modifiers);
447  }
448  virtual void key_repeat(const char *c, int modifiers) {
449  OnKeyRepeat(c, modifiers);
450  }
451  virtual void key_up(const char *c, int modifiers) {
452  OnKeyUp(c, modifiers);
453  }
454  virtual void special_key_down(int key, int scancode, int modifiers) {
455  OnSpecialKeyDown(key, scancode, modifiers);
456  }
457  virtual void special_key_repeat(int key, int scancode, int modifiers) {
458  OnSpecialKeyRepeat(key, scancode, modifiers);
459  }
460  virtual void special_key_up(int key, int scancode, int modifiers) {
461  OnSpecialKeyUp(key, scancode, modifiers);
462  }
463 
464  bool graphicsInitialized_;
465  int width_;
466  int height_;
467  const std::string caption_;
468  nanogui::Screen *screen_;
469  GLFWwindow* window_;
470  double lastDrawT_;
471  Point2 lastMouse_;
472  bool leftDown_;
473  bool middleDown_;
474  bool rightDown_;
475 };
476 
477 
478 } // end namespace
479 
480 #endif
481 
This is the main application base class for the MinGfx Toolkit.
Definition: graphics_app.h:135
virtual void OnKeyDown(const char *c, int modifiers)
Transforms a keyboard down event into the actual character typed.
Definition: graphics_app.h:225
virtual void OnRightMouseDrag(const Point2 &pos, const Vector2 &delta)
If the mouse button is held down and the mouse has moved in the past frame then this function will be...
Definition: graphics_app.h:213
virtual void InitGraphicsContext()
Users cannot make any graphics calls (e.g., setting the clear color, saving mesh data to the GPU) unt...
virtual void ResizeWindow(int new_width, int new_height)
Cause the graphics windows to resize programmatically rather than by dragging on the corner manually.
virtual void OnSpecialKeyUp(int key, int scancode, int modifiers)
The values for key, scancode, and modifiers are documented here: http://www.glfw.org/docs/latest/grou...
Definition: graphics_app.h:255
virtual int window_width()
Returns the current width of the client area of the window in pixels.
virtual void OnKeyRepeat(const char *c, int modifiers)
Transforms a keyboard repeat event into the actual character typed.
Definition: graphics_app.h:233
virtual void OnMouseMove(const Point2 &pos, const Vector2 &delta)
If the mouse has moved in the past frame and no mouse buttons are currently pressed,...
Definition: graphics_app.h:165
virtual void OnMiddleMouseUp(const Point2 &pos)
If the mouse button was released since the last frame, then this function will be called to notify yo...
Definition: graphics_app.h:206
virtual Vector2 NormalizedDeviceCoordsToPixels(const Vector2 &pointInNDC)
Transforms a vector in normalized device coordinates (top left = (-1,1) bottom right (1,...
virtual float ReadZValueAtPixel(const Point2 &pointInPixels, unsigned int whichBuffer=GL_BACK)
Returns the z buffer value under the specified pixel. z will be 0 at the near plane and +1 at the far...
virtual bool IsRightMouseDown()
True if the right mouse button is currently held down.
virtual Vector2 PixelsToNormalizedDeviceCoords(const Vector2 &vectorInPixels)
Transforms a vector in viewport coordinates (pixels where top left = (0,0) and bottom right = (window...
virtual void OnLeftMouseUp(const Point2 &pos)
If the mouse button was released since the last frame, then this function will be called to notify yo...
Definition: graphics_app.h:196
virtual void OnLeftMouseDrag(const Point2 &pos, const Vector2 &delta)
If the mouse button is held down and the mouse has moved in the past frame then this function will be...
Definition: graphics_app.h:187
virtual void OnMiddleMouseDown(const Point2 &pos)
If the mouse button was pressed down since the last frame, then this function will be called to notif...
Definition: graphics_app.h:200
virtual void DrawUsingOpenGL()
Override this to draw graphics using raw OpenGL 2D or 3D graphics calls.
Definition: graphics_app.h:317
virtual bool IsLeftMouseDown()
True if the left mouse button is currently held down.
virtual void Run()
After creating a new GraphicsApp, call this to start the app's mainloop.
virtual bool IsKeyDown(int key)
True if the specified is is currently held down. Uses the GLFW key codes found here: http://www....
virtual Point2 NormalizedDeviceCoordsToPixels(const Point2 &pointInNDC)
Transforms a point in normalized device coordinates (top left = (-1,1) bottom right (1,...
virtual void DrawUsingNanoVG(NVGcontext *ctx)
Override this to draw graphics using the nanovg vector graphics library, which provides an easy way t...
Definition: graphics_app.h:311
virtual void OnSpecialKeyRepeat(int key, int scancode, int modifiers)
The values for key, scancode, and modifiers are documented here: http://www.glfw.org/docs/latest/grou...
Definition: graphics_app.h:251
virtual void OnKeyUp(const char *c, int modifiers)
Transforms a keyboard up event into the actual character typed.
Definition: graphics_app.h:241
virtual void OnRightMouseDown(const Point2 &pos)
If the mouse button was pressed down since the last frame, then this function will be called to notif...
Definition: graphics_app.h:210
virtual bool IsMiddleMouseDown()
True if the middle mouse button is currently held down.
virtual Point2 PixelsToNormalizedDeviceCoords(const Point2 &pointInPixels)
Transforms a point in viewport coordinates (pixels where top left = (0,0) and bottom right = (window_...
virtual void InitOpenGL()
Override this to initialize the OpenGL context with textures, vertex buffers, etc.
Definition: graphics_app.h:297
virtual void UpdateSimulation(double dt)
Called once per frame.
Definition: graphics_app.h:306
virtual ~GraphicsApp()
The destructor will shutdown the graphics system and window.
virtual void OnSpecialKeyDown(int key, int scancode, int modifiers)
The values for key, scancode, and modifiers are documented here: http://www.glfw.org/docs/latest/grou...
Definition: graphics_app.h:247
virtual GLFWwindow * window()
Access to the underlying GLFWwindow object.
GraphicsApp(int width, int height, const std::string &caption)
Constructs a new app but does not yet run it.
virtual float aspect_ratio()
Returns width/height for the current shape of the window.
virtual void OnMiddleMouseDrag(const Point2 &pos, const Vector2 &delta)
If the mouse button is held down and the mouse has moved in the past frame then this function will be...
Definition: graphics_app.h:203
virtual int framebuffer_width()
Returns the current width of the framebuffer in pixels.
virtual int window_height()
Returns the current height of the client area of the window in pixels.
virtual void OnRightMouseUp(const Point2 &pos)
If the mouse button was released since the last frame, then this function will be called to notify yo...
Definition: graphics_app.h:216
virtual void OnWindowResize(int new_width, int new_height)
Override this to respond when the graphics window and/or framebuffer are resized, either by the user ...
Definition: graphics_app.h:261
virtual nanogui::Screen * screen()
Access to the underlying NanoGUI Screen object.
virtual void InitNanoGUI()
Called at the beginning of the Run() method.
Definition: graphics_app.h:284
virtual void OnLeftMouseDown(const Point2 &pos)
If the mouse button was pressed down since the last frame, then this function will be called to notif...
Definition: graphics_app.h:174
virtual int framebuffer_height()
Returns the current height of the framebuffer in pixels.
A 2D Point with floating point coordinates, used for storing 2D texture coordinates,...
Definition: point2.h:28
A 2D Vector with floating point coordinates, used for storing 2D translations, mouse movements,...
Definition: vector2.h:28
Namespace for the MinGfx Toolkit.
Definition: aabb.h:21