This is the main application base class for the MinGfx Toolkit.
Create a Subclass:
To create your own graphics application, you should create a subclass of GraphicsApp and then override some key functions:
- User Input: To get input from the keyboard and mouse, override OnMouseMove() and/or the other On...() functions.
- Drawing Graphics: To draw graphics override one or more of the Draw*() functions.
- DrawUsingNanoVG() is the right place to make 2D drawing calls using the nanovg library.
- DrawUsingOpenGL() is the right place to make 2D or 3D drawing calls using OpenGL. This includes drawing using the Mesh, QuickShapes, DefaultShader, ShaderProgram, and all other MinGfx classes since these are all based on OpenGL.
- InitNanoGUI() is the right place to create nanogui windows to add a 2D user interface to your app.
- InitOpenGL() is the right place to load textures, meshes, shaders, and other graphics objects that can only be created after the OpenGL context exists.
- Physics, Animation, AI, etc.: Override the UpdateSimulation() function to do other non-graphics calculations required by your program. This is called automatically once per frame.
Keep in mind that internally the app uses a rendering loop that looks something like this:
while (!program_ready_to_close) {
internal_get_input_events_from_operating_system();
On*();
internal_render_gui_elements_using_nanogui();
}
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,...
virtual void DrawUsingOpenGL()
Override this to draw graphics using raw OpenGL 2D or 3D graphics calls.
virtual void DrawUsingNanoVG(NVGcontext *ctx)
Override this to draw graphics using the nanovg vector graphics library, which provides an easy way t...
virtual void InitOpenGL()
Override this to initialize the OpenGL context with textures, vertex buffers, etc.
virtual void UpdateSimulation(double dt)
Called once per frame.
virtual void InitNanoGUI()
Called at the beginning of the Run() method.
A Complete Example with GUI Widgets
If you wish to add some buttons, sliders, etc. in your application, you can do this inside GraphicsApp by accessing the NanoGUI library. You will need to pass NanoGUI a nanogui::screen object, which you can get from the screen() function. NanoGui setup should be done in the constructor, like this:
class MyApp : public GraphcisApp {
public:
}
virtual ~MyApp() {}
nanogui::Window *
window =
new nanogui::Window(
screen(),
"My GUI Panel");
window->setPosition(Eigen::Vector2i(10, 10));
window->setSize(Eigen::Vector2i(400,200));
window->setLayout(
new nanogui::GroupLayout());
nanogui::Button pause_btn =
new nanogui::Button(
window,
"Pause");
pause_btn->setCallback(std::bind(&MyApp::OnPauseBtnPressed, this));
pause_btn->setTooltip("Toggle playback.");
}
glClearColor(0.0, 0.0, 0.0, 1);
}
void OnPauseBtnPressed() {
std::cout << "Pause pressed." << std::endl;
}
void OnMouseMove(
const Point2 &pos,
const Vector2 &delta) {
std::cout << "Mouse moved to " << pos << std::endl;
}
Matrix4 view =
Matrix4::LookAt(Point3(0,0,3), Point3(0,0,0), Vector3(0,1,0));
quick_shapes_.DrawCube(model, view, proj, Color(1,1,1));
}
private:
QuickShapes quick_shapes_;
};
int main(int argc, const char *argv[]) {
MyApp app;
app.Run();
return 0;
}
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 nanogui::Screen * screen()
Access to the underlying NanoGUI Screen object.
static Matrix4 Scale(const Vector3 &v)
Returns the scale matrix described by the vector.
static Matrix4 Perspective(float fov_y_in_degrees, float aspect_ratio, float near_plane_dist, float far_plane_dist)
Returns a perspective projection matrix equivalent to the one gluPerspective creates.
static Matrix4 LookAt(Point3 eye, Point3 target, Vector3 up)
Returns a view matrix that centers the camera at the 'eye' position and orients it to look at the des...
static Matrix4 Translation(const Vector3 &v)
Returns the translation matrix described by the vector.
Includes the entire MinGfx library and calls using namespace mingfx.
Namespace for the MinGfx Toolkit.
Definition at line 135 of file graphics_app.h.
|
| GraphicsApp (int width, int height, const std::string &caption) |
| Constructs a new app but does not yet run it. More...
|
|
virtual | ~GraphicsApp () |
| The destructor will shutdown the graphics system and window. More...
|
|
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, then this callback function will be called to report the new position of the mouse to you. More...
|
|
virtual void | OnLeftMouseDown (const Point2 &pos) |
| If the mouse button was pressed down since the last frame, then this function will be called to notify you. More...
|
|
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 called to tell you that a "dragging" operation is happening. More...
|
|
virtual void | OnLeftMouseUp (const Point2 &pos) |
| If the mouse button was released since the last frame, then this function will be called to notify you. More...
|
|
virtual void | OnMiddleMouseDown (const Point2 &pos) |
| If the mouse button was pressed down since the last frame, then this function will be called to notify you. More...
|
|
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 called to tell you that a "dragging" operation is happening. More...
|
|
virtual void | OnMiddleMouseUp (const Point2 &pos) |
| If the mouse button was released since the last frame, then this function will be called to notify you. More...
|
|
virtual void | OnRightMouseDown (const Point2 &pos) |
| If the mouse button was pressed down since the last frame, then this function will be called to notify you. More...
|
|
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 called to tell you that a "dragging" operation is happening. More...
|
|
virtual void | OnRightMouseUp (const Point2 &pos) |
| If the mouse button was released since the last frame, then this function will be called to notify you. More...
|
|
virtual void | OnKeyDown (const char *c, int modifiers) |
| Transforms a keyboard down event into the actual character typed. More...
|
|
virtual void | OnKeyRepeat (const char *c, int modifiers) |
| Transforms a keyboard repeat event into the actual character typed. More...
|
|
virtual void | OnKeyUp (const char *c, int modifiers) |
| Transforms a keyboard up event into the actual character typed. More...
|
|
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/group__keys.html. More...
|
|
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/group__keys.html. More...
|
|
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/group__keys.html. More...
|
|
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 dragging the window or through a call to ResizeWindow(). More...
|
|
virtual void | Run () |
| After creating a new GraphicsApp, call this to start the app's mainloop. More...
|
|
virtual void | InitNanoGUI () |
| Called at the beginning of the Run() method. More...
|
|
virtual void | InitOpenGL () |
| Override this to initialize the OpenGL context with textures, vertex buffers, etc. More...
|
|
virtual void | UpdateSimulation (double dt) |
| Called once per frame. More...
|
|
virtual void | DrawUsingNanoVG (NVGcontext *ctx) |
| Override this to draw graphics using the nanovg vector graphics library, which provides an easy way to draw 2D shapes to the screen. More...
|
|
virtual void | DrawUsingOpenGL () |
| Override this to draw graphics using raw OpenGL 2D or 3D graphics calls. More...
|
|
virtual bool | IsKeyDown (int key) |
| True if the specified is is currently held down. Uses the GLFW key codes found here: http://www.glfw.org/docs/latest/group__keys.html. More...
|
|
virtual bool | IsLeftMouseDown () |
| True if the left mouse button is currently held down. More...
|
|
virtual bool | IsMiddleMouseDown () |
| True if the middle mouse button is currently held down. More...
|
|
virtual bool | IsRightMouseDown () |
| True if the right mouse button is currently held down. More...
|
|
virtual int | window_width () |
| Returns the current width of the client area of the window in pixels. More...
|
|
virtual int | window_height () |
| Returns the current height of the client area of the window in pixels. More...
|
|
virtual int | framebuffer_width () |
| Returns the current width of the framebuffer in pixels. More...
|
|
virtual int | framebuffer_height () |
| Returns the current height of the framebuffer in pixels. More...
|
|
virtual float | aspect_ratio () |
| Returns width/height for the current shape of the window. More...
|
|
virtual Point2 | PixelsToNormalizedDeviceCoords (const Point2 &pointInPixels) |
| Transforms a point in viewport coordinates (pixels where top left = (0,0) and bottom right = (window_width()-1, window_height()-1)) to normalized device coordinates, (top left = (-1,1) bottom right (1,-1)). More...
|
|
virtual Point2 | NormalizedDeviceCoordsToPixels (const Point2 &pointInNDC) |
| Transforms a point in normalized device coordinates (top left = (-1,1) bottom right (1,-1)) to pixels (top left = (0,0), bottom right = (window width-1, window height-1)) More...
|
|
virtual Vector2 | PixelsToNormalizedDeviceCoords (const Vector2 &vectorInPixels) |
| Transforms a vector in viewport coordinates (pixels where top left = (0,0) and bottom right = (window width-1, window height-1)) to normalized device coordinates, (top left = (-1,1) bottom right (1,-1)). More...
|
|
virtual Vector2 | NormalizedDeviceCoordsToPixels (const Vector2 &pointInNDC) |
| Transforms a vector in normalized device coordinates (top left = (-1,1) bottom right (1,-1)) to pixels (top left = (0,0), bottom right = (window width-1, window height-1)) More...
|
|
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 plane. More...
|
|
virtual nanogui::Screen * | screen () |
| Access to the underlying NanoGUI Screen object. More...
|
|
virtual GLFWwindow * | window () |
| Access to the underlying GLFWwindow object. More...
|
|
virtual void | ResizeWindow (int new_width, int new_height) |
| Cause the graphics windows to resize programmatically rather than by dragging on the corner manually. More...
|
|
virtual void | InitGraphicsContext () |
| Users cannot make any graphics calls (e.g., setting the clear color, saving mesh data to the GPU) until the graphics context is initialized by calling this method. More...
|
|