-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
476 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "camera.h" | ||
#include "renderer.h" | ||
|
||
|
||
Camera::Camera(float width, float height) : position(4.0f, 5.0f, 10.0f), horizontalAngle(3.14), verticalAngle(-0.4), FoV(45) | ||
{ | ||
computeMatrices(width, height); | ||
} | ||
|
||
|
||
void Camera::computeMatrices(float width, float height) | ||
{ | ||
|
||
glm::vec3 direction( | ||
cos(verticalAngle) * sin(horizontalAngle), | ||
sin(verticalAngle), | ||
cos(verticalAngle) * cos(horizontalAngle) | ||
); | ||
|
||
// Right vector | ||
glm::vec3 right = glm::vec3( | ||
sin(horizontalAngle - 3.14f/2.0f), | ||
0, | ||
cos(horizontalAngle - 3.14f/2.0f) | ||
); | ||
|
||
// Up vector : perpendicular to both direction and right | ||
glm::vec3 up = glm::cross( right, direction ); | ||
|
||
|
||
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units | ||
projectionMatrix = glm::perspective(glm::radians(FoV), width/height, 0.1f, 100.0f); | ||
// Camera matrix | ||
viewMatrix = glm::lookAt( | ||
position, // Camera is here | ||
position+direction, // and looks here : at the same position, plus "direction" | ||
up // Head is up (set to 0,-1,0 to look upside-down) | ||
); | ||
|
||
} | ||
|
||
void Camera::setFoV(float newFoV) | ||
{ | ||
FoV = newFoV; | ||
} | ||
|
||
const glm::mat4 &Camera::getViewMatrix() const | ||
{ | ||
return viewMatrix; | ||
} | ||
|
||
const glm::mat4 &Camera::getProjectionMatrix() const | ||
{ | ||
return projectionMatrix; | ||
} | ||
|
||
void Camera::Bind(Shader *shader) | ||
{ | ||
shader->setUniform3fv("camPosition", position); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef CAMERA_H | ||
#define CAMERA_H | ||
|
||
#include "glm/glm.hpp" | ||
#include "glm/gtx/transform.hpp" | ||
#include "shader.h" | ||
|
||
class Camera | ||
{ | ||
public: | ||
Camera(float width, float height); | ||
void computeMatrices(float width, float height); | ||
|
||
glm::vec3 position; | ||
|
||
float horizontalAngle; | ||
|
||
float verticalAngle; | ||
|
||
void setFoV(float newFoV); | ||
|
||
const glm::mat4 &getViewMatrix() const; | ||
|
||
const glm::mat4 &getProjectionMatrix() const; | ||
|
||
void Bind(Shader *shader); | ||
|
||
private: | ||
float FoV; | ||
glm::mat4 viewMatrix; | ||
glm::mat4 projectionMatrix; | ||
}; | ||
|
||
#endif // CAMERA_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "controls.h" | ||
|
||
Controls::Controls(GLFWwindow* window, Camera *camera):m_Camera(camera),m_Window(window), speed(6), mouseSpeed(0.05) | ||
{ | ||
glfwGetWindowSize(window, &width, &height); | ||
} | ||
|
||
void Controls::setSpeed(float newSpeed) | ||
{ | ||
speed = newSpeed; | ||
} | ||
|
||
void Controls::setMouseSpeed(float newMouseSpeed) | ||
{ | ||
mouseSpeed = newMouseSpeed; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef CONTROLS_H | ||
#define CONTROLS_H | ||
|
||
#include "camera.h" | ||
#include <GLFW/glfw3.h> | ||
#include "shader.h" | ||
|
||
class Controls | ||
{ | ||
public: | ||
Controls(GLFWwindow* window, Camera *camera); | ||
void setSpeed(float newSpeed); | ||
|
||
void setMouseSpeed(float newMouseSpeed); | ||
virtual void update(float deltaTime, Shader *shader)=0; | ||
|
||
protected: | ||
Camera *m_Camera; | ||
float speed; | ||
float mouseSpeed; | ||
int width,height; | ||
|
||
GLFWwindow* m_Window; | ||
}; | ||
|
||
#endif // CONTROLS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "fpscontrols.h" | ||
|
||
FPSControls::FPSControls(GLFWwindow *window, Camera *camera):Controls(window, camera) | ||
{ | ||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); | ||
} | ||
|
||
void FPSControls::update(float deltaTime, Shader *shader) | ||
{ | ||
double xpos, ypos; | ||
glfwGetCursorPos(m_Window, &xpos, &ypos); | ||
|
||
|
||
glfwSetCursorPos(m_Window, width/2, height/2); | ||
|
||
|
||
|
||
|
||
m_Camera->horizontalAngle+= mouseSpeed * deltaTime * float( width/2 - xpos ); | ||
m_Camera->verticalAngle += mouseSpeed * deltaTime * float(height/2 - ypos ); | ||
|
||
glm::vec3 direction( | ||
cos(m_Camera->verticalAngle) * sin(m_Camera->horizontalAngle), | ||
sin(m_Camera->verticalAngle), | ||
cos(m_Camera->verticalAngle) * cos(m_Camera->horizontalAngle) | ||
); | ||
|
||
// Right vector | ||
glm::vec3 right = glm::vec3( | ||
sin(m_Camera->horizontalAngle - 3.14f/2.0f), | ||
0, | ||
cos(m_Camera->horizontalAngle - 3.14f/2.0f) | ||
); | ||
|
||
// Up vector : perpendicular to both direction and right | ||
glm::vec3 up = glm::cross( right, direction ); | ||
|
||
|
||
// Move forward | ||
if (glfwGetKey(m_Window, GLFW_KEY_UP ) == GLFW_PRESS){ | ||
m_Camera->position += direction * deltaTime * speed; | ||
} | ||
// Move backward | ||
if (glfwGetKey(m_Window, GLFW_KEY_DOWN ) == GLFW_PRESS){ | ||
m_Camera->position -= direction * deltaTime * speed; | ||
} | ||
// Strafe right | ||
if (glfwGetKey(m_Window, GLFW_KEY_RIGHT ) == GLFW_PRESS){ | ||
m_Camera->position += right * deltaTime * speed; | ||
} | ||
// Strafe left | ||
if (glfwGetKey(m_Window, GLFW_KEY_LEFT ) == GLFW_PRESS){ | ||
m_Camera->position -= right * deltaTime * speed; | ||
} | ||
// Move forward | ||
if (glfwGetKey(m_Window, GLFW_KEY_W ) == GLFW_PRESS){ | ||
m_Camera->position += direction * deltaTime * speed; | ||
} | ||
// Move backward | ||
if (glfwGetKey(m_Window, GLFW_KEY_S ) == GLFW_PRESS){ | ||
m_Camera->position -= direction * deltaTime * speed; | ||
} | ||
// Strafe right | ||
if (glfwGetKey(m_Window, GLFW_KEY_D ) == GLFW_PRESS){ | ||
m_Camera->position += right * deltaTime * speed; | ||
} | ||
// Strafe left | ||
if (glfwGetKey(m_Window, GLFW_KEY_A ) == GLFW_PRESS){ | ||
m_Camera->position -= right * deltaTime * speed; | ||
} | ||
// go up | ||
if (glfwGetKey(m_Window, GLFW_KEY_SPACE ) == GLFW_PRESS){ | ||
m_Camera->position += up * deltaTime * speed; | ||
} | ||
// go down | ||
if (glfwGetKey(m_Window, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS){ | ||
m_Camera->position -= up * deltaTime * speed; | ||
} | ||
|
||
shader->setUniform3fv("ambiantLight", glm::vec3(0.1,0.1,0.1)); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef FPSCONTROLS_H | ||
#define FPSCONTROLS_H | ||
|
||
#include "controls.h" | ||
|
||
class FPSControls : public Controls | ||
{ | ||
public: | ||
FPSControls(GLFWwindow* window, Camera *camera); | ||
void update(float deltaTime, Shader* shader); | ||
}; | ||
|
||
#endif // FPSCONTROLS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.