MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
Public Member Functions | List of all members
mingfx::CraftCam Class Reference

Detailed Description

This implements a user interface for controlling the camera with the mouse.

This interface is appropriate for "first person" camera control, as in games like Minecraft.

Use the arrow keys or A,S,W,Z keys to move around in the virtual world. UP/DOWN and W/Z move forward and back. LEFT/RIGHT or A/S rotate your body to face left or right. You can rotate and tilt your head to look left/right/up/down by moving the mouse. In Minecraft, that movement happens whenever you move the mouse, regardless of whether you are holding down a mouse button, but in some games you want to hold the camera still while you use the mouse to draw on the screen or do something else. It's possible to use this interface both ways by calling the MouseMove() function either every time the mouse moves, or only when the mouse is in a dragging mode.

Example usage:

// Create a global or member variable in your MyGraphicsApp class:
CraftCam cam_;
// If you want to always rotate the view with the mouse, use this:
void MyGraphicsApp::OnMouseMove(const Point2 &pos, const Vector2 &delta) {
Vector2 delta_ndc = PixelsToNormalizedDeviceCoords(pos);
cam_.OnMouseMove(delta_ndc);
}
// Alternatively, if you want to only rotate the view when the mouse button is
// held down, use this instead. Call cam_.OnMouseMove() in either one function
// or the other, but not both!
// void MyGraphicsApp::OnLeftMouseDrag(const Point2 &pos, const Vector2 &delta) {
// Vector2 delta_ndc = PixelsToNormalizedDeviceCoords(pos);
// cam_.OnMouseMove(delta_ndc);
// }
// This tells the camera to simulate walking based on the keyboard keys currently
// pressed. You need to pass a pointer to the underlying GLFW window created by
// GraphicsApp.
void MyGraphicsApp::UpdateSimulation(double dt) {
cam_.UpdateSimulation(dt, window());
}
void MyGraphicsApp::InitOpenGL() {
cam_.set_view_matrix(Matrix4::lookAt(Point3(0,2,2), Point3(0,2,0), Vector3(0,1,0)););
}
void MyGraphicsApp::DrawOpenGL() {
// draw your scene using the view matrix from the camera
Matrix4 proj_matrix = Matrix4::perspective(60, aspect_ratio(), 1, 200);
Matrix4 view_matrix = cam_.view_matrix();
Matrix4 model_matrix = Matrix4::RotateY(to_radians(45.0));
quick_shapes.DrawCube(model_matrix, view_matirx, proj_matrix, Color(1,1,1));
}
Matrix4 view_matrix()
Access the camera view matrix created by the CraftCam interactions via this method and use it to draw...
CraftCam()
Creates a CraftCam object with an initial view matrix = identity.

Definition at line 79 of file craft_cam.h.

#include <craft_cam.h>

Public Member Functions

 CraftCam ()
 Creates a CraftCam object with an initial view matrix = identity. More...
 
 CraftCam (const Matrix4 &initial_view_matrix)
 Creates a CraftCam object with the supplied initial view matrix. More...
 
virtual ~CraftCam ()
 
void UpdateSimulation (double dt, GLFWwindow *window_ptr)
 Call this from your app's UpdateSimulation() method. This tells the camera to simulate walking based on the keyboard keys currently pressed. You need to pass a pointer to the underlying GLFW window created by GraphicsApp. Example: More...
 
void OnMouseMove (const Vector2 &normalized_mouse_delta)
 Call this from your app's OnMouseMove() or On*MouseDrag() method. Use OnMouseMove() if you want to always rotate the view with the mouse. Remember to convert the mouse coordinates (usually reported by window managers in pixels) into normalized device coordinates: More...
 
Matrix4 view_matrix ()
 Access the camera view matrix created by the CraftCam interactions via this method and use it to draw the geometry in your scence. For example, within GraphicsApp::DrawUsingOpenGL(), you might have: More...
 
Point3 eye ()
 Returns the "eye" point (i.e., focal point) of the camera in world space coordinates. More...
 
Vector3 look ()
 Returns the look direction (i.e., -Z axis of the camera matrix) in world space coordinates. More...
 
void UpdateHeight (float new_y_value)
 Sets the y value of the camera (i.e., the height). If you want to set the entire view matrix, then use set_view_matrix(), but if you just want to update the height, e.g., while walking around a bumpy terrain, then use this. More...
 
void set_view_matrix (Matrix4 view_matrix)
 This is not required, but you may use this if you wish to set an initial view matrix or reset the view matrix. More...
 
float translation_scale ()
 This is the scale factor used to speed up / slow down forward/backward translation when walking for the UP / DOWN keys. It defaults to 1.0, smaller values will make the camera walk slower, larger values will speed it up. More...
 
void set_translation_scale (float s)
 This is the scale factor used to speed up / slow down forward/backward translation when walking for the UP / DOWN keys. It defaults to 1.0, smaller values will make the camera walk slower, larger values will speed it up. More...
 
float rotation_scale ()
 This is the scale factor used to speed up / slow down left/right rotation when walking for the LEFT / RIGHT keys. It defaults to 1.0, smaller values will make the camera turn slower, larger values will speed it up. More...
 
void set_rotation_scale (float s)
 This is the scale factor used to speed up / slow down left/right rotation when walking for the LEFT / RIGHT keys. It defaults to 1.0, smaller values will make the camera turn slower, larger values will speed it up. More...
 
float look_scale ()
 This is the scale factor used to speed up / slow down looking around when moving the head with the mouse. It defaults to 1.0, smaller values will make the camera turn slower, larger values will speed it up. More...
 
void set_look_scale (float s)
 This is the scale factor used to speed up / slow down looking around when moving the head with the mouse. It defaults to 1.0, smaller values will make the camera turn slower, larger values will speed it up. More...
 

Constructor & Destructor Documentation

◆ CraftCam() [1/2]

mingfx::CraftCam::CraftCam ( )

Creates a CraftCam object with an initial view matrix = identity.

◆ CraftCam() [2/2]

mingfx::CraftCam::CraftCam ( const Matrix4 initial_view_matrix)

Creates a CraftCam object with the supplied initial view matrix.

◆ ~CraftCam()

virtual mingfx::CraftCam::~CraftCam ( )
virtual

Member Function Documentation

◆ eye()

Point3 mingfx::CraftCam::eye ( )

Returns the "eye" point (i.e., focal point) of the camera in world space coordinates.

◆ look()

Vector3 mingfx::CraftCam::look ( )

Returns the look direction (i.e., -Z axis of the camera matrix) in world space coordinates.

◆ look_scale()

float mingfx::CraftCam::look_scale ( )

This is the scale factor used to speed up / slow down looking around when moving the head with the mouse. It defaults to 1.0, smaller values will make the camera turn slower, larger values will speed it up.

◆ OnMouseMove()

void mingfx::CraftCam::OnMouseMove ( const Vector2 normalized_mouse_delta)

Call this from your app's OnMouseMove() or On*MouseDrag() method. Use OnMouseMove() if you want to always rotate the view with the mouse. Remember to convert the mouse coordinates (usually reported by window managers in pixels) into normalized device coordinates:

void MyGraphicsApp::OnMouseMove(const Point2 &pos, const Vector2 &delta) {
Vector2 delta_ndc = PixelsToNormalizedDeviceCoords(pos);
cam_.OnMouseMove(delta_ndc);
}

Alternatively, if you want to only rotate the view when the mouse button is held down, use On*MouseDrag() instead:

void MyGraphicsApp::OnLeftMouseDrag(const Point2 &pos, const Vector2 &delta) {
Vector2 delta_ndc = PixelsToNormalizedDeviceCoords(pos);
cam_.OnMouseMove(delta_ndc);
}

◆ rotation_scale()

float mingfx::CraftCam::rotation_scale ( )

This is the scale factor used to speed up / slow down left/right rotation when walking for the LEFT / RIGHT keys. It defaults to 1.0, smaller values will make the camera turn slower, larger values will speed it up.

◆ set_look_scale()

void mingfx::CraftCam::set_look_scale ( float  s)

This is the scale factor used to speed up / slow down looking around when moving the head with the mouse. It defaults to 1.0, smaller values will make the camera turn slower, larger values will speed it up.

◆ set_rotation_scale()

void mingfx::CraftCam::set_rotation_scale ( float  s)

This is the scale factor used to speed up / slow down left/right rotation when walking for the LEFT / RIGHT keys. It defaults to 1.0, smaller values will make the camera turn slower, larger values will speed it up.

◆ set_translation_scale()

void mingfx::CraftCam::set_translation_scale ( float  s)

This is the scale factor used to speed up / slow down forward/backward translation when walking for the UP / DOWN keys. It defaults to 1.0, smaller values will make the camera walk slower, larger values will speed it up.

◆ set_view_matrix()

void mingfx::CraftCam::set_view_matrix ( Matrix4  view_matrix)

This is not required, but you may use this if you wish to set an initial view matrix or reset the view matrix.

◆ translation_scale()

float mingfx::CraftCam::translation_scale ( )

This is the scale factor used to speed up / slow down forward/backward translation when walking for the UP / DOWN keys. It defaults to 1.0, smaller values will make the camera walk slower, larger values will speed it up.

◆ UpdateHeight()

void mingfx::CraftCam::UpdateHeight ( float  new_y_value)

Sets the y value of the camera (i.e., the height). If you want to set the entire view matrix, then use set_view_matrix(), but if you just want to update the height, e.g., while walking around a bumpy terrain, then use this.

◆ UpdateSimulation()

void mingfx::CraftCam::UpdateSimulation ( double  dt,
GLFWwindow *  window_ptr 
)

Call this from your app's UpdateSimulation() method. This tells the camera to simulate walking based on the keyboard keys currently pressed. You need to pass a pointer to the underlying GLFW window created by GraphicsApp. Example:

void MyGraphicsApp::UpdateSimulation(double dt) {
cam_.UpdateSimulation(dt, window());
}

◆ view_matrix()

Matrix4 mingfx::CraftCam::view_matrix ( )

Access the camera view matrix created by the CraftCam interactions via this method and use it to draw the geometry in your scence. For example, within GraphicsApp::DrawUsingOpenGL(), you might have:

Matrix4 P = Matrix4::Perspective(30, aspect_ratio(), 1, 20);
Matrix4 V = cam.view_matrix();
Matrix4 M = Matrix4::RotateY(GfxMath::ToRadians(45.0));
quick_shapes.DrawCube(M, V, P, Color(1,1,1));
static float ToRadians(float degrees)
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.

The documentation for this class was generated from the following file: