So here's the deal, I am trying to make a class that will handle the view of the scene using keyboard input. Just like in an aircraft, there is thrust which causes the camera to move forward(if we are sitting in a cockpit). Let us say, I am using W and S keys to control the thrust of this hypothetical camera and A and D keys to control my heading(the direction at which I am looking) and Up and Down keys to control the Pitch and Left and Right to control Roll.

If you have ever played Descent, there are actually ten ways to control the ship. Thrust forward, backward.. slide left, right, slide up and down, Pitch up and down, and roll left and right.

This is what I am trying to do here. By using basic opengl transformation functions I want to build my own camera class that I can control the view the scene very easily and how I want to view it.

Here is the code so far that I was able to do:

#ifndef CAMERA_H_INCLUDED #define CAMERA_H_INCLUDED #include <SDL.h> #include <GLEW\glew.h> #include <math.h> #include "Misc.h" const GLfloat INCREMENT_HEADING = 0.1f; const GLfloat INCREMENT_PITCH = 0.1f; const GLfloat INCREMENT_ROLL = 0.1f; const GLfloat INCREMENT_FORWARD = 1.0f; // From Right to Left, in Bits, the keys are // right left down up d a s w class EyeCam { private: GLfloat Angle_Heading; GLfloat Angle_Pitch; GLfloat Angle_Roll; Uint8 KeyState; public: GLfloat eyeX, eyeY, eyeZ; EyeCam(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat aimX, GLfloat aimY, GLfloat aimZ); void onEvent(SDL_Event* event); void update(); }; #endif // CAMERA_H_INCLUDED

#include "EyeCam.h" #include <iostream> EyeCam::EyeCam(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat aimX, GLfloat aimY, GLfloat aimZ) { this->eyeX = eyeX; this->eyeY = eyeY; this->eyeZ = eyeZ; Angle_Heading = Misc::toDegrees(atan2f((eyeX - aimX), (eyeZ - aimZ))); GLfloat base = sqrt(pow(eyeZ-aimZ, 2) + pow(eyeX - aimX, 2)); Angle_Pitch = Misc::toDegrees(atan2f( -(eyeY - aimY), base)); //Angle_Roll = 0.0; KeyState = 0x00; } void EyeCam::onEvent(SDL_Event* event) { if (event->type == SDL_KEYDOWN) { if (event->key.keysym.sym == SDLK_RETURN) { system("cls"); std::cout << "X: " << eyeX << '\n'; std::cout << "Y: " << eyeY << '\n'; std::cout << "Z: " << eyeZ << "\n\n"; std::cout << "Heading: " << Angle_Heading << '\n'; std::cout << "Pitch: " << Angle_Pitch << '\n'; //std::cout << "Roll: " << Angle_Roll << '\n'; } if (event->key.keysym.sym == SDLK_w) KeyState = KeyState | 0x01; if (event->key.keysym.sym == SDLK_s) KeyState = KeyState | 0x02; if (event->key.keysym.sym == SDLK_a) KeyState = KeyState | 0x04; if (event->key.keysym.sym == SDLK_d) KeyState = KeyState | 0x08; if (event->key.keysym.sym == SDLK_UP) KeyState = KeyState | 0x10; if (event->key.keysym.sym == SDLK_DOWN) KeyState = KeyState | 0x20; if (event->key.keysym.sym == SDLK_LEFT) KeyState = KeyState | 0x40; if (event->key.keysym.sym == SDLK_RIGHT) KeyState = KeyState | 0x80; } else if (event->type == SDL_KEYUP) { if (event->key.keysym.sym == SDLK_w) KeyState = KeyState & 0xFE; if (event->key.keysym.sym == SDLK_s) KeyState = KeyState & 0xFD; if (event->key.keysym.sym == SDLK_a) KeyState = KeyState & 0xFB; if (event->key.keysym.sym == SDLK_d) KeyState = KeyState & 0xF7; if (event->key.keysym.sym == SDLK_UP) KeyState = KeyState & 0xEF; if (event->key.keysym.sym == SDLK_DOWN) KeyState = KeyState & 0xDF; if (event->key.keysym.sym == SDLK_LEFT) KeyState = KeyState & 0xBF; if (event->key.keysym.sym == SDLK_RIGHT) KeyState = KeyState & 0x7F; } } void EyeCam::update() { if (KeyState & 0x01) // w { eyeX = eyeX - INCREMENT_FORWARD * sin(Misc::toRadians(Angle_Heading)); eyeY = eyeY + INCREMENT_FORWARD * sin(Misc::toRadians(Angle_Pitch)); eyeZ = eyeZ - INCREMENT_FORWARD * cos(Misc::toRadians(Angle_Heading)); } if (KeyState & 0x02) // s { eyeX = eyeX + INCREMENT_FORWARD * sin(Misc::toRadians(Angle_Heading)); eyeY = eyeY - INCREMENT_FORWARD * sin(Misc::toRadians(Angle_Pitch)); eyeZ = eyeZ + INCREMENT_FORWARD * cos(Misc::toRadians(Angle_Heading)); } if (KeyState & 0x04) // a { Angle_Heading = Angle_Heading + INCREMENT_HEADING; } if (KeyState & 0x08) // d { Angle_Heading = Angle_Heading - INCREMENT_HEADING; } if (KeyState & 0x10) // Up { Angle_Pitch = Angle_Pitch + INCREMENT_PITCH; } if (KeyState & 0x20) { Angle_Pitch = Angle_Pitch - INCREMENT_PITCH; } if (KeyState & 0x40) // Left { Angle_Roll = Angle_Roll + INCREMENT_ROLL; } if (KeyState & 0x80) // Right { Angle_Roll = Angle_Roll - INCREMENT_ROLL; } glRotatef(-Angle_Pitch, 1.0, 0, 0); glRotatef(-Angle_Heading, 0, 1.0, 0); glTranslatef(-eyeX, -eyeY, -eyeZ); glRotatef(-Angle_Roll, 0.0, 0.0, 1.0); }

The constructor of the class takes the same arguments as gluLookAt() function, except for the Up vector (I've not implemented it yet in my class). The position of the eye in x, y, z coordinates and a point of aim in x, y, z coordinates.

In the constructor, I am calculating two angles, the Heading and the Pitch by using spherical trigonometry and then using these angles to rotate the view accordingly. Please note that, I am not using gluLookAt() function at all in my class.

Then, by using keyboard commands, I am just making changes to the angles to adjust my aim and calculating eye coordinates to adjust my position.

Now, the problem I am having here is that, If I am originally looking down the -ve z- axis and my up is +ve y axis. then I know to adjust my heading I have to Rotate the scene around the y - axis, if I want to adjust pitch I have to Rotate around the x - axis and if I want to adjust roll I have to Rotate around the z-axis. Hence, I am using equation of circle with heading angle to calculate my eyeX and eyeZ and pitch angle to calculate y - axis. But unfortunately, this will only work till I stay in the XZ plane. Any change in the plane, I will not get the desired results.

For example, at the start if I make a 90 degree Roll such that my up now is -ve x-axis, then if I want to Pitch up relative to the camera, I have rotate around the y-axis. Originally I was rotating about the x-axis when roll was 0. Similarly, If I am looking down the -ve z-axis and then roll will perfectly fine because I am rotating around the +ve z axis, however, If I move the camera to aim at the +ve x-axis and then roll it will not work, in that case I have to rotate on the -ve x-axis to make the roll work. I was able to solve this problem by changing the glRotate function for Roll

from:

glRotatef(Angle_Roll, 0, 0, 1);

to:

glRotatef(Angle_Roll, sin(toRadians(Angle_Heading), 0, cos(toRadians(Angle_Heading));

but what if I am looking down the -ve or +ve y-axis, this again will not work as desired and also I try to change the heading then it will automatically affect the roll transformation.

I hope that I am not confusing you guys too much with the my description, please correct me if I am wrong in my assumptions.

So, the bottom line question is, what I need to do make this camera work in every orientation so that it pitchs, rolls, and turns correctly relative to its orientation.

I am not very skilled with mathematics, but I have studied trigonometry and equations of curves and shapes. Please let me know If I need to cover more mathematics in order to implement what I want.

Any input will be appreciated, Thanks.