Added camera!
- Can be translated locally or moved to a point in the world - Rotates locally around local x- and z-axis and around global y-axis. Added key checks to test camera movement. Added Quaternions (Inherits from CoordinatesVector<double,4>) with a view to handling the camera orientation. Added equality operators for CoordinatesVectors. Removed slow `glutGet()` calls. Added custom reshape callback as we now store the window parameters to avoid some `glutGet()` calls. Renamed `KeyStateManager` to `InputStatus` as it handles more than key states now. Mouse movement callback now captures the cursor, handles its re-centering and when the cursor enters the window far away from its last position. TODO : Consider limiting rotations for the camera (Prevents flipping the head around)
This commit is contained in:
parent
b8036a1c36
commit
459ad7e6c3
11 changed files with 494 additions and 174 deletions
66
src/main.cpp
66
src/main.cpp
|
@ -4,24 +4,27 @@
|
|||
#include "displayers.h"
|
||||
#include "DataHandling/Texture.h"
|
||||
#include "DataHandling/Model3D.h"
|
||||
#include "KeyStateManager.h"
|
||||
#include "InputStatus.h"
|
||||
#include "Camera.h"
|
||||
|
||||
volatile unsigned long long int timer_ticks = 0;
|
||||
static double aspect_ratio;
|
||||
static Texture tree_texture;
|
||||
static Model3D raptor;
|
||||
static Texture raptor_texture;
|
||||
static Camera camera({10,0,10},{-10,0,-10},{0,1,0});
|
||||
|
||||
void manage_inputs()
|
||||
{
|
||||
if (KeyStateManager::is_mouse_button_pressed(GLUT_LEFT_BUTTON))
|
||||
if (InputStatus::is_mouse_button_pressed(GLUT_LEFT_BUTTON))
|
||||
{
|
||||
glClearColor(0,0,0.5,1);
|
||||
}
|
||||
else if (KeyStateManager::is_key_pressed(' '))
|
||||
else if (InputStatus::is_key_pressed(' '))
|
||||
{
|
||||
glClearColor(0.5,0,0,1);
|
||||
}
|
||||
else if (KeyStateManager::is_key_pressed(0x1B))
|
||||
else if (InputStatus::is_key_pressed(0x1B))
|
||||
{
|
||||
glutDestroyWindow(glutGetWindow());
|
||||
exit(EXIT_SUCCESS);
|
||||
|
@ -31,13 +34,43 @@ void manage_inputs()
|
|||
glClearColor(0,0,0,1);
|
||||
}
|
||||
|
||||
if (KeyStateManager::is_special_key_pressed(GLUT_KEY_RIGHT))
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_RIGHT))
|
||||
{
|
||||
timer_ticks += 5;
|
||||
camera.translate({0,0,1});
|
||||
}
|
||||
if (KeyStateManager::is_special_key_pressed(GLUT_KEY_LEFT))
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_LEFT))
|
||||
{
|
||||
timer_ticks -= 5;
|
||||
camera.translate({0,0,-1});
|
||||
}
|
||||
if (InputStatus::is_key_pressed(' '))
|
||||
{
|
||||
camera.translate({0,1,0});
|
||||
}
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_PAGE_DOWN))
|
||||
{
|
||||
camera.translate({0,-1,0});
|
||||
}
|
||||
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_UP))
|
||||
{
|
||||
camera.translate({1,0,0});
|
||||
}
|
||||
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_DOWN))
|
||||
{
|
||||
camera.translate({-1,0,0});
|
||||
}
|
||||
|
||||
// Get mouse delta since last frame
|
||||
static Vec2i mouse_delta;
|
||||
mouse_delta = InputStatus::get_mouse_delta(true);
|
||||
if (mouse_delta.x != 0 || mouse_delta.y != 0)
|
||||
{
|
||||
camera.rotate({0,
|
||||
-mouse_delta.x / InputStatus::mouse_sensitivity.x,
|
||||
-mouse_delta.y / InputStatus::mouse_sensitivity.y});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,14 +84,11 @@ void display()
|
|||
glMatrixMode(GL_PROJECTION);
|
||||
// Prepare view
|
||||
glLoadIdentity();
|
||||
gluPerspective(60,(double)glutGet(GLUT_WINDOW_WIDTH)/(double)glutGet(GLUT_WINDOW_HEIGHT),10,30000);
|
||||
gluPerspective(60,aspect_ratio,0.1,30000);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
gluLookAt(10, 7.5, 10,
|
||||
0, 0, 1,
|
||||
0, 1, 0);
|
||||
|
||||
camera.look();
|
||||
|
||||
display_rotating_pyramid(-5,-2,-5,2,4,angleY);
|
||||
display_rotating_pyramid(5,-2,0,1,5,angleY);
|
||||
|
@ -74,6 +104,15 @@ void display()
|
|||
glutSwapBuffers();
|
||||
}
|
||||
|
||||
void reshape(int new_x, int new_y)
|
||||
{
|
||||
glViewport(0,0,new_x,new_y);
|
||||
|
||||
aspect_ratio = (double)new_x/new_y;
|
||||
InputStatus::window_center.x = new_x / 2;
|
||||
InputStatus::window_center.y = new_y / 2;
|
||||
}
|
||||
|
||||
void update_angle(int value)
|
||||
{
|
||||
timer_ticks++;
|
||||
|
@ -85,7 +124,7 @@ int main(int argc, char** argv)
|
|||
glutInit(&argc,argv);
|
||||
// Generate window with GLUT
|
||||
glutInitDisplayMode(GLUT_RGB| GLUT_DEPTH | GLUT_DOUBLE);
|
||||
glutCreateWindow("Hello");
|
||||
glutCreateWindow("OpenGL custom engine tests");
|
||||
glutIgnoreKeyRepeat(true);
|
||||
|
||||
// Init OpenGL
|
||||
|
@ -129,8 +168,9 @@ int main(int argc, char** argv)
|
|||
|
||||
glutDisplayFunc(display);
|
||||
glutIdleFunc(display);
|
||||
glutReshapeFunc(reshape);
|
||||
glutTimerFunc(50,update_angle,0);
|
||||
KeyStateManager::register_glut_callbacks();
|
||||
InputStatus::register_glut_callbacks();
|
||||
|
||||
// Enters main loop, managed by GLUT
|
||||
glutMainLoop();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue