Inputhandelierer outgelagert yessir
This commit is contained in:
parent
be78abddcc
commit
a9d658502a
|
@ -0,0 +1,192 @@
|
||||||
|
#include "inputHandler.h"
|
||||||
|
|
||||||
|
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.
|
||||||
|
* */
|
||||||
|
float chalkboardYPosition = 0.0f;
|
||||||
|
void handleInputs(double deltaTime)
|
||||||
|
{
|
||||||
|
assert(window != NULL);
|
||||||
|
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
travelCamera((vec3){0.0f, 1.7f, 2.4f});
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
travelCamera((vec3){3.3f, 3.4f, -11.0f});
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
travelCamera((vec3){-3.0f, 2.9f, -7.5f});
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
travelCamera((vec3){-0.6f, 2.1f, -4.5f});
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
travelCamera((vec3){-10.6f, 22.1f, -4.5f});
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_6) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
travelCamera((vec3){3.574698f, 0.966039f, 3.852249f});
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_7) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
travelCamera((vec3){0.324839f, 1.325346f, 3.798471f});
|
||||||
|
}
|
||||||
|
if (!cameraTraveling)
|
||||||
|
{
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
vec3 delta;
|
||||||
|
|
||||||
|
vec3Subtract(&delta, &origin, &cameraPosition);
|
||||||
|
vec3Normalise(&delta, &delta);
|
||||||
|
vec3Multiply(&delta, &delta, -10);
|
||||||
|
vec3Multiply(&delta, &delta, deltaTime);
|
||||||
|
|
||||||
|
vec3Add(&cameraPosition, &cameraPosition, &delta);
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
vec3 delta;
|
||||||
|
vec3Subtract(&delta, &origin, &cameraPosition);
|
||||||
|
if (vec3Length(&delta) > 0.1)
|
||||||
|
{
|
||||||
|
vec3Normalise(&delta, &delta);
|
||||||
|
vec3Multiply(&delta, &delta, 10);
|
||||||
|
vec3Multiply(&delta, &delta, deltaTime);
|
||||||
|
|
||||||
|
vec3Add(&cameraPosition, &cameraPosition, &delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
vec4 delta;
|
||||||
|
mat4 transform;
|
||||||
|
|
||||||
|
vec3Subtract((vec3 *)&delta, &origin, &cameraPosition);
|
||||||
|
delta.y = 0;
|
||||||
|
vec3Normalise((vec3 *)&delta, (vec3 *)&delta);
|
||||||
|
vec3Multiply((vec3 *)&delta, (vec3 *)&delta, -10);
|
||||||
|
|
||||||
|
rotationY(&transform, pi / 2);
|
||||||
|
multiplyAny((GLfloat *)&delta, (GLfloat *)&transform, (GLfloat *)&delta, 4, 4, 1);
|
||||||
|
|
||||||
|
vec3Multiply((vec3 *)&delta, (vec3 *)&delta, deltaTime);
|
||||||
|
|
||||||
|
vec3Add(&cameraPosition, &cameraPosition, (vec3 *)&delta);
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
vec4 delta;
|
||||||
|
mat4 transform;
|
||||||
|
|
||||||
|
vec3Subtract((vec3 *)&delta, &origin, &cameraPosition);
|
||||||
|
delta.y = 0;
|
||||||
|
vec3Normalise((vec3 *)&delta, (vec3 *)&delta);
|
||||||
|
vec3Multiply((vec3 *)&delta, (vec3 *)&delta, 10);
|
||||||
|
|
||||||
|
rotationY(&transform, pi / 2);
|
||||||
|
multiplyAny((GLfloat *)&delta, (GLfloat *)&transform, (GLfloat *)&delta, 4, 4, 1);
|
||||||
|
|
||||||
|
vec3Multiply((vec3 *)&delta, (vec3 *)&delta, deltaTime);
|
||||||
|
|
||||||
|
vec3Add(&cameraPosition, &cameraPosition, (vec3 *)&delta);
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
cameraPosition.y -= deltaTime * 10;
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
cameraPosition.y += deltaTime * 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SceneNode *chalkboard = findNodeByName("myChalkboard1", rootNode);
|
||||||
|
if (!chalkboard)
|
||||||
|
return;
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
chalkboardYPosition += deltaTime * 1.0f;
|
||||||
|
chalkboardYPosition = fminf(chalkboardYPosition, 1.0f);
|
||||||
|
chalkboard->transformation = (mat4){1.0f, 0.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.0f, chalkboardYPosition, 0.0f, 1.0f};
|
||||||
|
updateSceneNode(chalkboard, &findNodeByName("myChalkboard", rootNode)->worldTransformation);
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
chalkboardYPosition -= deltaTime * 1.0f;
|
||||||
|
chalkboardYPosition = fmaxf(chalkboardYPosition, 0.0f);
|
||||||
|
chalkboard->transformation = (mat4){1.0f, 0.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.0f, chalkboardYPosition, 0.0f, 1.0f};
|
||||||
|
updateSceneNode(chalkboard, &findNodeByName("myChalkboard", rootNode)->worldTransformation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
objectPosition.x += deltaTime * 10;
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
objectPosition.x -= deltaTime * 10;
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_I) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
objectPosition.z += deltaTime * 10;
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_K) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
objectPosition.z -= deltaTime * 10;
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
radius += deltaTime * 10;
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_U) == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
radius -= deltaTime * 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// input handler to quit with ESC
|
||||||
|
void keyboardHandler(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||||
|
{
|
||||||
|
assert(window != NULL);
|
||||||
|
if (action == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
if (key == GLFW_KEY_ESCAPE)
|
||||||
|
{
|
||||||
|
exitRequested = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef INPUTHANDLER_H
|
||||||
|
#define INPUTHANDLER_H
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include "matrixMath.h"
|
||||||
|
#include "sceneGraph.h"
|
||||||
|
|
||||||
|
extern const GLfloat pi;
|
||||||
|
extern bool exitRequested;
|
||||||
|
extern GLFWwindow *window;
|
||||||
|
extern vec3 origin;
|
||||||
|
extern vec3 cameraPosition;
|
||||||
|
extern vec3 cameraTravelPosition;
|
||||||
|
extern vec3 objectPosition;
|
||||||
|
extern GLfloat radius;
|
||||||
|
extern SceneNode *rootNode;
|
||||||
|
extern bool cameraTraveling;
|
||||||
|
|
||||||
|
void handleInputs(double deltaTime);
|
||||||
|
void keyboardHandler(GLFWwindow *window, int key, int scancode, int action, int mods);
|
||||||
|
void handleCameraTravel();
|
||||||
|
void travelCamera(vec3 newPosition);
|
||||||
|
|
||||||
|
#endif
|
171
src/main.c
171
src/main.c
|
@ -1,23 +1,21 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#include "vertexShader.c"
|
#include "vertexShader.c"
|
||||||
#include "fragmentShader.c"
|
#include "fragmentShader.c"
|
||||||
|
|
||||||
#include "objectHandler.h"
|
#include "objectHandler.h"
|
||||||
#include "matrixMath.h"
|
#include "matrixMath.h"
|
||||||
#include "transformation.h"
|
#include "transformation.h"
|
||||||
#include "wavefrontobj.h"
|
#include "wavefrontobj.h"
|
||||||
#include "sceneGraph.h"
|
#include "sceneGraph.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
#include "inputHandler.c"
|
||||||
#include <stdlib.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
GLuint program;
|
GLuint program;
|
||||||
|
|
||||||
|
@ -58,163 +56,6 @@ 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.
|
|
||||||
* */
|
|
||||||
float chalkboardYPosition = 0.0f;
|
|
||||||
void handleInputs(double deltaTime) {
|
|
||||||
assert(window != NULL);
|
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) {
|
|
||||||
travelCamera((vec3){0.0f, 1.7f, 2.4f});
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) {
|
|
||||||
travelCamera((vec3){3.3f, 3.4f, -11.0f});
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) {
|
|
||||||
travelCamera((vec3){-3.0f, 2.9f, -7.5f});
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS) {
|
|
||||||
travelCamera((vec3){-0.6f, 2.1f, -4.5f});
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS) {
|
|
||||||
travelCamera((vec3){-10.6f, 22.1f, -4.5f});
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_6) == GLFW_PRESS) {
|
|
||||||
travelCamera((vec3){3.574698f, 0.966039f, 3.852249f});
|
|
||||||
}
|
|
||||||
if (!cameraTraveling) {
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
|
||||||
vec3 delta;
|
|
||||||
|
|
||||||
vec3Subtract(&delta, &origin, &cameraPosition);
|
|
||||||
vec3Normalise(&delta, &delta);
|
|
||||||
vec3Multiply(&delta, &delta, -10);
|
|
||||||
vec3Multiply(&delta, &delta, deltaTime);
|
|
||||||
|
|
||||||
vec3Add(&cameraPosition, &cameraPosition, &delta);
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
|
||||||
vec3 delta;
|
|
||||||
vec3Subtract(&delta, &origin, &cameraPosition);
|
|
||||||
if (vec3Length(&delta) > 0.1) {
|
|
||||||
vec3Normalise(&delta, &delta);
|
|
||||||
vec3Multiply(&delta, &delta, 10);
|
|
||||||
vec3Multiply(&delta, &delta, deltaTime);
|
|
||||||
|
|
||||||
vec3Add(&cameraPosition, &cameraPosition, &delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
|
||||||
vec4 delta;
|
|
||||||
mat4 transform;
|
|
||||||
|
|
||||||
vec3Subtract((vec3*)&delta, &origin, &cameraPosition);
|
|
||||||
delta.y = 0;
|
|
||||||
vec3Normalise((vec3*)&delta, (vec3*)&delta);
|
|
||||||
vec3Multiply((vec3*)&delta, (vec3*)&delta, -10);
|
|
||||||
|
|
||||||
rotationY(&transform, pi / 2);
|
|
||||||
multiplyAny((GLfloat*)&delta, (GLfloat*)&transform, (GLfloat*)&delta, 4, 4, 1);
|
|
||||||
|
|
||||||
vec3Multiply((vec3*)&delta, (vec3*)&delta, deltaTime);
|
|
||||||
|
|
||||||
vec3Add(&cameraPosition, &cameraPosition, (vec3*)&delta);
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
|
||||||
vec4 delta;
|
|
||||||
mat4 transform;
|
|
||||||
|
|
||||||
vec3Subtract((vec3*)&delta, &origin, &cameraPosition);
|
|
||||||
delta.y = 0;
|
|
||||||
vec3Normalise((vec3*)&delta, (vec3*)&delta);
|
|
||||||
vec3Multiply((vec3*)&delta, (vec3*)&delta, 10);
|
|
||||||
|
|
||||||
rotationY(&transform, pi / 2);
|
|
||||||
multiplyAny((GLfloat*)&delta, (GLfloat*)&transform, (GLfloat*)&delta, 4, 4, 1);
|
|
||||||
|
|
||||||
vec3Multiply((vec3*)&delta, (vec3*)&delta, deltaTime);
|
|
||||||
|
|
||||||
vec3Add(&cameraPosition, &cameraPosition, (vec3*)&delta);
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
|
|
||||||
cameraPosition.y -= deltaTime * 10;
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
|
||||||
cameraPosition.y += deltaTime * 10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SceneNode* chalkboard = findNodeByName("myChalkboard1", rootNode);
|
|
||||||
if (!chalkboard) return;
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS) {
|
|
||||||
chalkboardYPosition += deltaTime * 1.0f;
|
|
||||||
chalkboardYPosition = fminf(chalkboardYPosition, 1.0f);
|
|
||||||
chalkboard->transformation = (mat4){1.0f, 0.0f, 0.0f, 0.0f,
|
|
||||||
0.0f, 1.0f, 0.0f, 0.0f,
|
|
||||||
0.0f, 0.0f, 1.0f, 0.0f,
|
|
||||||
0.0f, chalkboardYPosition, 0.0f, 1.0f};
|
|
||||||
updateSceneNode(chalkboard, &findNodeByName("myChalkboard", rootNode)->worldTransformation);
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) {
|
|
||||||
chalkboardYPosition -= deltaTime * 1.0f;
|
|
||||||
chalkboardYPosition = fmaxf(chalkboardYPosition, 0.0f);
|
|
||||||
chalkboard->transformation = (mat4){1.0f, 0.0f, 0.0f, 0.0f,
|
|
||||||
0.0f, 1.0f, 0.0f, 0.0f,
|
|
||||||
0.0f, 0.0f, 1.0f, 0.0f,
|
|
||||||
0.0f, chalkboardYPosition, 0.0f, 1.0f};
|
|
||||||
updateSceneNode(chalkboard, &findNodeByName("myChalkboard", rootNode)->worldTransformation);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
|
||||||
objectPosition.x += deltaTime * 10;
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS) {
|
|
||||||
objectPosition.x -= deltaTime * 10;
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_I) == GLFW_PRESS) {
|
|
||||||
objectPosition.z += deltaTime * 10;
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_K) == GLFW_PRESS) {
|
|
||||||
objectPosition.z -= deltaTime * 10;
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS) {
|
|
||||||
radius += deltaTime * 10;
|
|
||||||
}
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_U) == GLFW_PRESS) {
|
|
||||||
radius -= deltaTime * 10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// input handler to quit with ESC
|
|
||||||
void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
|
||||||
assert(window != NULL);
|
|
||||||
if (action == GLFW_PRESS) {
|
|
||||||
if (key == GLFW_KEY_ESCAPE) {
|
|
||||||
exitRequested = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void renderNode(SceneNode* node) {
|
void renderNode(SceneNode* node) {
|
||||||
assert(node != NULL);
|
assert(node != NULL);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue