Skip to content

Commit

Permalink
start fps camera
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed May 25, 2024
1 parent 6bad640 commit b85232a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
10 changes: 10 additions & 0 deletions engine/include/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "Camera.hpp"
#include "utils.hpp"

enum CameraType { ORBITAL, FPS };

class Camera {
public:
glm::vec3 position;
Expand All @@ -16,13 +18,21 @@ class Camera {
int fov;
float near;
float far;
CameraType type;

Camera();
Camera(glm::vec3 position, glm::vec3 lookAt, glm::vec3 up, int fov, float near,
float far);

Camera(const Camera& other);

void leftMovement();
void rightMovement();
void upMovement();
void downMovement();
void forwardMovement();
void backwardMovement();

// std::string toString();
};

Expand Down
44 changes: 44 additions & 0 deletions engine/src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,47 @@ Camera::Camera(const Camera& other) {
this->near = other.near;
this->far = other.far;
}

void Camera::backwardMovement() {
glm::vec3 direction = glm::normalize(lookAt - position);
position += direction;
lookAt += direction;
}

void Camera::forwardMovement() {
glm::vec3 direction = glm::normalize(lookAt - position);
position -= direction;
lookAt -= direction;
}

void Camera::leftMovement() {
glm::vec3 direction = glm::normalize(lookAt - position);
glm::vec3 right = glm::normalize(glm::cross(up, direction));
position -= right;
lookAt -= right;
}

void Camera::rightMovement() {
glm::vec3 direction = glm::normalize(lookAt - position);
glm::vec3 right = glm::normalize(glm::cross(up, direction));
position += right;
lookAt += right;
}

void Camera::upMovement() {
glm::vec3 direction = glm::normalize(lookAt - position);
glm::vec3 right = glm::normalize(glm::cross(up, direction));
glm::vec3 up = glm::normalize(glm::cross(direction, right));
position += up;
lookAt += up;
}

void Camera::downMovement() {
glm::vec3 direction = glm::normalize(lookAt - position);
glm::vec3 right = glm::normalize(glm::cross(up, direction));
glm::vec3 up = glm::normalize(glm::cross(direction, right));
position -= up;
lookAt -= up;
}


24 changes: 12 additions & 12 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int timebase;
float frames;

Configuration c;
Camera camera;

void reshape(int w, int h) {
float aspect_ratio = (float)w / (float)h;
Expand Down Expand Up @@ -99,16 +100,12 @@ void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set camera
glLoadIdentity();
gluLookAt(c.camera.position.x, c.camera.position.y, c.camera.position.z,
c.camera.lookAt.x, c.camera.lookAt.y, c.camera.lookAt.z,
c.camera.up.x, c.camera.up.y, c.camera.up.z);

glRotatef(cameraAngleY, 0.0f, 1.0f, 0.0f);
glRotatef(cameraAngle, 1.0f, 0.0f, 1.0f);
glScalef(zoom, zoom, zoom);
gluLookAt(camera.position.x, camera.position.y, camera.position.z,
camera.lookAt.x, camera.lookAt.y, camera.lookAt.z,
camera.up.x, camera.up.y, camera.up.z);

Window currentW = Window(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
Frustsum frustsum = Frustsum(c.camera, currentW);
Frustsum frustsum = Frustsum(camera, currentW);

drawAxis();

Expand Down Expand Up @@ -155,16 +152,16 @@ void motion(int x, int y) {
void processSpecialKeys(int key, int xx, int yy) {
switch (key) {
case GLUT_KEY_LEFT:
cameraAngle -= 1.0f;
camera.leftMovement();
break;
case GLUT_KEY_RIGHT:
cameraAngle += 1.0f;
camera.rightMovement();
break;
case GLUT_KEY_UP:
cameraAngleY += 1.0f;
camera.forwardMovement();
break;
case GLUT_KEY_DOWN:
cameraAngleY -= 1.0f;
camera.backwardMovement();
break;
default:
break;
Expand Down Expand Up @@ -215,6 +212,9 @@ void setupConfig(char* arg) {
std::cout << "Invalid file format\n";
exit(1);
}

camera = c.camera;

}

void setupModels(Group& group) {
Expand Down
2 changes: 1 addition & 1 deletion scenes/test/test_4_1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<position x="1.757135" y="1.799201" z="3.416921" />
<lookAt x="0" y="0" z="0" />
<up x="0" y="1" z="0" />
<projection fov="60" near="1" far="1000" />
<projection fov="60" near="1" far="10" />
</camera>

<lights>
Expand Down

0 comments on commit b85232a

Please sign in to comment.