smooth camera travel
This commit is contained in:
parent
f71b33bb47
commit
b428efe1a3
42
src/main.c
42
src/main.c
|
@ -45,14 +45,36 @@ int framesSinceUpdate = 0;
|
||||||
GLfloat step = 0.0f;
|
GLfloat step = 0.0f;
|
||||||
const GLfloat pi = 3.14159f;
|
const GLfloat pi = 3.14159f;
|
||||||
|
|
||||||
vec3 cameraPosition = {0.0f, 3.0f, 5.5f};
|
vec3 cameraPosition = {0.0f, 2.0f, 1.0f};
|
||||||
vec3 objectPosition = {0.0f, 0.0f, 0.0f};
|
vec3 objectPosition = {0.0f, 0.0f, 0.0f};
|
||||||
|
|
||||||
|
vec3 cameraTravelPosition = {0.0f, 3.0f, 5.5f};
|
||||||
|
bool cameraTraveling = true;
|
||||||
|
|
||||||
GLfloat radius = 1.0f;
|
GLfloat radius = 1.0f;
|
||||||
mat4 viewingTransformation;
|
mat4 viewingTransformation;
|
||||||
|
|
||||||
// Define a global scene graph root node
|
// Define a global scene graph root node
|
||||||
SceneNode* rootNode;
|
SceneNode* rootNode;
|
||||||
|
|
||||||
|
void handleCameraTravel() {
|
||||||
|
if (cameraTraveling) {
|
||||||
|
vec3 delta;
|
||||||
|
|
||||||
|
vec3Subtract(&delta, &cameraTravelPosition, &cameraPosition);
|
||||||
|
|
||||||
|
if (vec3Length(&delta) < 0.0001) cameraTraveling = false;
|
||||||
|
|
||||||
|
vec3Multiply(&delta, &delta, 0.1);
|
||||||
|
vec3Add(&cameraPosition, &cameraPosition, &delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void travelCamera(vec3 newPosition) {
|
||||||
|
cameraTravelPosition = newPosition;
|
||||||
|
cameraTraveling = true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Input handler for camera movement.
|
* Input handler for camera movement.
|
||||||
* */
|
* */
|
||||||
|
@ -60,23 +82,24 @@ void handleInputs(double deltaTime) {
|
||||||
assert(window != NULL);
|
assert(window != NULL);
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) {
|
||||||
cameraPosition = (vec3){0.0f, 1.7f, 2.4f};
|
travelCamera((vec3){0.0f, 1.7f, 2.4f});
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) {
|
||||||
cameraPosition = (vec3){3.3f, 3.4f, -11.0f};
|
travelCamera((vec3){3.3f, 3.4f, -11.0f});
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) {
|
||||||
cameraPosition = (vec3){-3.0f, 2.9f, -7.5f};
|
travelCamera((vec3){-3.0f, 2.9f, -7.5f});
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS) {
|
||||||
cameraPosition = (vec3){-0.6f, 2.1f, -4.5f};
|
travelCamera((vec3){-0.6f, 2.1f, -4.5f});
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS) {
|
||||||
cameraPosition = (vec3){-10.6f, 22.1f, -4.5f};
|
travelCamera((vec3){-10.6f, 22.1f, -4.5f});
|
||||||
}
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_6) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_6) == GLFW_PRESS) {
|
||||||
cameraPosition = (vec3){3.574698f, 0.966039f, 3.852249f};
|
travelCamera((vec3){3.574698f, 0.966039f, 3.852249f});
|
||||||
}
|
}
|
||||||
|
if (!cameraTraveling) {
|
||||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||||
cameraPosition.z += deltaTime * 10;
|
cameraPosition.z += deltaTime * 10;
|
||||||
}
|
}
|
||||||
|
@ -95,6 +118,7 @@ void handleInputs(double deltaTime) {
|
||||||
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
|
||||||
cameraPosition.y -= deltaTime * 10;
|
cameraPosition.y -= deltaTime * 10;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
||||||
objectPosition.x += deltaTime * 10;
|
objectPosition.x += deltaTime * 10;
|
||||||
|
@ -251,7 +275,7 @@ void init(void) {
|
||||||
|
|
||||||
void updateStats() {
|
void updateStats() {
|
||||||
printf("\rFPS: %.1f", framesSinceUpdate / timeSinceUpdate);
|
printf("\rFPS: %.1f", framesSinceUpdate / timeSinceUpdate);
|
||||||
printf(" - Camera Position: [%f, %f, %f]", cameraPosition.x, cameraPosition.y, cameraPosition.z);
|
printf(" - Camera Position: [%f, %f, %f] - Traveling: %s ", cameraPosition.x, cameraPosition.y, cameraPosition.z, cameraTraveling ? "true" : "false");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,6 +284,8 @@ void updateStats() {
|
||||||
*/
|
*/
|
||||||
void draw(void) {
|
void draw(void) {
|
||||||
|
|
||||||
|
handleCameraTravel();
|
||||||
|
|
||||||
// FPS Counter
|
// FPS Counter
|
||||||
framesSinceUpdate++;
|
framesSinceUpdate++;
|
||||||
double deltaTime = glfwGetTime();
|
double deltaTime = glfwGetTime();
|
||||||
|
|
Loading…
Reference in New Issue