add movement

This commit is contained in:
v7eZ3t 2022-02-09 17:29:22 +01:00
parent 7ba6ca9918
commit ae145e1142

View File

@ -61,7 +61,7 @@ glm::vec3 cameraSide; // Wektor "w bok" kamery
glm::vec3 vehiclePos = glm::vec3(0, 0, 0); glm::vec3 vehiclePos = glm::vec3(0, 0, 0);
glm::vec3 vehicleDir; // Wektor "do przodu" kamery glm::vec3 vehicleDir; // Wektor "do przodu" kamery
glm::vec3 vehicleSide; // Wektor "w bok" kamery glm::vec3 vehicleSide; // Wektor "w bok" kamery
float vehicleAngle = 0; float vehicleAngle = 0.0f;
float cameraAngle = 0; float cameraAngle = 0;
@ -98,19 +98,35 @@ float dx = 0;
unsigned int skyboxVAO, skyboxVBO; unsigned int skyboxVAO, skyboxVBO;
unsigned int cubemapTexture; unsigned int cubemapTexture;
char lastClickedButtonFrontal = 'w';
char lastClickedButtonAngle = 's';
float vehicleSpeed = 0.0f;
float lastVehicleAngle = 0.0f;
float vehicleAngleSpeed = 0.0f;
bool directionChanged = false;
void keyboard(unsigned char key, int x, int y) void keyboard(unsigned char key, int x, int y)
{ {
float angleSpeed = 0.1f; float angleSpeed = 0.1f;
float moveSpeed = 0.1f; float moveSpeed = 0.1f;
//lastClickedButton = key;
if (key == 'w' || key == 's')
{
lastClickedButtonFrontal = key;
}
printf("%c", key);
switch (key) switch (key)
{ {
case 'z': vehicleAngle -= angleSpeed; delta_x = vehicleAngle; break; case 'a': vehicleAngleSpeed -= 0.001f; break;
case 'x': vehicleAngle += angleSpeed; cameraAngle = vehicleAngle; break; case 'd': vehicleAngleSpeed += 0.001f; break;
case 'w': vehiclePos += vehicleDir * moveSpeed; break; case 'w': vehicleSpeed += 0.01f; break;
case 's': vehiclePos -= vehicleDir * moveSpeed; break; case 's': vehicleSpeed -= 0.01f; break;
case 'd': vehiclePos += vehicleSide * moveSpeed; break; case 'z': vehiclePos += vehicleSide * moveSpeed; break;
case 'a': vehiclePos -= vehicleSide * moveSpeed; break; case 'x': vehiclePos -= vehicleSide * moveSpeed; break;
//case 'h': rotation_y = glm::quat(1, 0, 0, 0); rotation_x = glm::quat(1, 0, 0, 0); rotationCamera = glm::quat(1, 0, 0, 0); break; //case 'h': rotation_y = glm::quat(1, 0, 0, 0); rotation_x = glm::quat(1, 0, 0, 0); rotationCamera = glm::quat(1, 0, 0, 0); break;
//case 'h': rotation_y = glm::quat(1, 0, 0, 0); rotation_x = glm::quat(1, 0, 0, 0); rotationCamera = glm::quat(1, 0, 0, 0); break; //case 'h': rotation_y = glm::quat(1, 0, 0, 0); rotation_x = glm::quat(1, 0, 0, 0); rotationCamera = glm::quat(1, 0, 0, 0); break;
@ -120,9 +136,39 @@ void keyboard(unsigned char key, int x, int y)
//glutWarpPointer(WIN_WIDTH / 2, WIN_HEIGHT/ 2); //glutWarpPointer(WIN_WIDTH / 2, WIN_HEIGHT/ 2);
} }
//glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
//rot_y = glm::angleAxis(vehicleAngle * 0.01f, glm::vec3(0, 1, 0)); //rot_y = glm::angleAxis(vehicleAngle * 0.01f, glm::vec3(0, 1, 0));
} }
void changeVehicleSpeed() {
if (vehicleAngleSpeed > 0.01f) {
vehicleAngleSpeed = 0.009f;
}
if (vehicleAngleSpeed < -0.01f) {
vehicleAngleSpeed = -0.009f;
}
if (vehicleSpeed > 0.06f) {
vehicleSpeed = 0.05f;
}
if (vehicleSpeed < -0.06f) {
vehicleSpeed = -0.05f;
}
switch (lastClickedButtonFrontal)
{
case 'w': vehiclePos += vehicleDir * vehicleSpeed; break;
case 's': vehiclePos -= vehicleDir * -vehicleSpeed; break;
}
vehicleAngle += vehicleAngleSpeed;
}
void mouse(int x, int y) void mouse(int x, int y)
{ {
if (mouseXPositionLast >= 0) { if (mouseXPositionLast >= 0) {
@ -133,40 +179,47 @@ void mouse(int x, int y)
mouseYPositionLast = y; mouseYPositionLast = y;
} }
bool freecam = false;
glm::mat4 createCameraMatrix() glm::mat4 createCameraMatrix()
{ {
//trzymanie freecama do usuniêcia
freecam = false;
//delta_x = delta_x + vehicleAngle; //delta_x = delta_x + vehicleAngle;
glm::quat rot_x = glm::angleAxis(delta_y * 0.01f, glm::vec3(1, 0, 0)); glm::quat rot_x = glm::angleAxis(delta_y, glm::vec3(1, 0, 0));
glm::quat rot_y = glm::angleAxis(delta_x * 0.01f, glm::vec3(0, 1, 0)); glm::quat rot_y = glm::angleAxis(delta_x, glm::vec3(0, 1, 0));
dy += delta_y; dy += delta_y;
dx += delta_x; dx += delta_x;
delta_x = 0; delta_x = 0;
delta_y = 0; delta_y = 0;
vehicleDir = glm::vec3(cosf(vehicleAngle - glm::radians(90.0f)), 0.0f, sinf(vehicleAngle - glm::radians(90.0f)));
vehicleSide = glm::cross(vehicleDir, glm::vec3(0, 1, 0));
rotation_x = glm::normalize(rot_x * rotation_x); rotation_x = glm::normalize(rot_x * rotation_x);
rotation_y = glm::normalize(rot_y * rotation_y); rotation_y = glm::normalize(rot_y * rotation_y);
rotationCamera = glm::normalize(rotation_y * rotation_x); rotationCamera = glm::normalize(rotation_y * rotation_x);
glm::quat inverse_rot = glm::inverse(rotationCamera);
cameraDir = inverse_rot * glm::vec3(0, 0, -1); vehicleDir = glm::vec3(cosf(vehicleAngle - glm::radians(90.0f)), 0.0f, sinf(vehicleAngle - glm::radians(90.0f)));
vehicleDir = glm::vec3(cosf(vehicleAngle - glm::radians(90.0f)), 0.0f, sinf(vehicleAngle - glm::radians(90.0f)));
glm::vec3 up = glm::vec3(0, 1, 0); glm::vec3 up = glm::vec3(0, 1, 0);
cameraSide = inverse_rot * glm::vec3(1, 0, 0); vehicleSide = glm::cross(vehicleDir, up);
return Core::createViewMatrixQuat(vehiclePos + glm::vec3(0, 2, 2), rotationCamera);
if (freecam) {
return Core::createViewMatrixQuat(vehiclePos, rotationCamera);
}
return Core::createViewMatrix(vehiclePos + glm::vec3(0, 0, 0), vehicleDir, up);
} }
void drawObjectColor(Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 color) void drawObjectColor(Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 color)
{ {
GLuint program = programColor; GLuint program = programColor;
@ -353,12 +406,12 @@ void drawLightLocation()
void drawScene() void drawScene()
{ {
glm::mat4 shipInitialTransformation = glm::translate(glm::vec3(0, 0, 0)); glm::mat4 shipInitialTransformation = glm::translate(glm::vec3(0, -0.2, -0.05));
// Scaling models // Scaling models
glm::vec3 shipScale = glm::vec3(0.01, 0.01, 0.01); glm::vec3 shipScale = glm::vec3(0.01, 0.01, 0.01);
shipInitialTransformation = glm::scale(shipInitialTransformation, shipScale); shipInitialTransformation = glm::scale(shipInitialTransformation, shipScale);
shipInitialTransformation = shipInitialTransformation * glm::translate(glm::vec3(0, 0, 0)) * glm::rotate(glm::radians(180.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.5f)); shipInitialTransformation = shipInitialTransformation * glm::translate(glm::vec3(0, 0, 0)) * glm::rotate(glm::radians(-1.4f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.1f));
glm::mat4 shipModelMatrix = glm::translate(vehiclePos + vehicleDir) * glm::rotate(-vehicleAngle, glm::vec3(0, 1, 0)) * shipInitialTransformation; glm::mat4 shipModelMatrix = glm::translate(vehiclePos + vehicleDir) * glm::rotate(-vehicleAngle, glm::vec3(0, 1, 0)) * shipInitialTransformation;
drawObjectColor(shipContext, shipModelMatrix, glm::vec3(0.6f)); drawObjectColor(shipContext, shipModelMatrix, glm::vec3(0.6f));
@ -439,10 +492,10 @@ void drawSceneLight(int pass)
void renderScene() void renderScene()
{ {
changeVehicleSpeed();
cameraMatrix = createCameraMatrix(); cameraMatrix = createCameraMatrix();
perspectiveMatrix = Core::createPerspectiveMatrix(); perspectiveMatrix = Core::createPerspectiveMatrix();
// changeVehicleSpeed();
int startTime, endTime; int startTime, endTime;
startTime = glutGet(GLUT_ELAPSED_TIME); startTime = glutGet(GLUT_ELAPSED_TIME);