Fix Camera movement

This commit is contained in:
Mateusz Drewniak 2022-01-31 17:18:09 +01:00
parent 8ee1167265
commit 034cb87981

View File

@ -35,8 +35,15 @@ int secondFishPositions[15][3];
int thirdFishPositions[15][3];
float firstFishSpreadFactor = 15.0;
glm::vec2 previousMousePosition = glm::vec2(0, 0);
int resolution[2] = { 600, 600 };
glm::vec2 centerOfTheWindow = { resolution[0] / 2, resolution[1] / 2 };
glm::vec2 mousePositionDifference = glm::vec2(0, 0);
glm::vec2 cameraDelta = glm::vec2(0, 0);
glm::quat rotationY = glm::normalize(glm::angleAxis(209 * 0.03f, glm::vec3(1, 0, 0)));
glm::quat rotationX = glm::normalize(glm::angleAxis(307 * 0.03f, glm::vec3(0, 1, 0)));
glm::vec3 cameraPos = glm::vec3(0, 0, 5);
glm::vec3 cameraDir; // Wektor "do przodu" kamery
glm::vec3 cameraSide; // Wektor "w bok" kamery
@ -49,7 +56,7 @@ glm::vec3 skyColor = glm::vec3(0.0705882353f, 0.4862745098f, 0.7568627451f); //
float fogDensity = 0.03;
float fogGradient = 0.5;
glm::quat rotation = glm::quat(1, 0, 0, 0);
glm::quat cameraRotation = glm::quat(1, 0, 0, 0);
GLuint textureSkybox;
GLuint textureFirstFish;
@ -79,11 +86,8 @@ void keyboard(unsigned char key, int x, int y)
void mouse(int x, int y)
{
mousePositionDifference[0] = x - previousMousePosition[0];
mousePositionDifference[1] = y - previousMousePosition[1];
previousMousePosition[0] = x;
previousMousePosition[1] = y;
mousePositionDifference = { x - centerOfTheWindow[0], y - centerOfTheWindow[0] };
glutWarpPointer(centerOfTheWindow[0], centerOfTheWindow[1]);
}
glm::mat4 createCameraMatrix()
@ -91,15 +95,23 @@ glm::mat4 createCameraMatrix()
glm::quat xMouseRotation = glm::angleAxis(mousePositionDifference[0] / 120, glm::vec3(0, 1, 0));
glm::quat yMouseRotation = glm::angleAxis(mousePositionDifference[1] / 120, glm::vec3(1, 0, 0));
glm::quat mouseRotation = glm::normalize(xMouseRotation * yMouseRotation);
cameraDelta[0] += mousePositionDifference[0];
cameraDelta[1] += mousePositionDifference[1];
mousePositionDifference = {0, 0};
glm::quat invRotation = glm::inverse(mouseRotation);
rotationX = glm::normalize(xMouseRotation * rotationX);
rotationY = glm::normalize(yMouseRotation * rotationY);
cameraRotation = glm::normalize(rotationY * rotationX);
glm::quat invRotation = glm::inverse(cameraRotation);
cameraDir = invRotation * glm::vec3(0, 0, -1);
cameraSide = invRotation * glm::vec3(1, 0, 0);
rotation = glm::normalize(mouseRotation * rotation);
return Core::createViewMatrixQuat(cameraPos, rotation);
glm::mat4 cameraTranslation;
cameraTranslation[3] = glm::vec4(-cameraPos, 1.0f);
return glm::mat4_cast(cameraRotation) * cameraTranslation;
}
void drawObjectColor(Core::RenderContext context, glm::mat4 modelMatrix, glm::vec3 color)
@ -168,7 +180,7 @@ void renderScene()
float time = glutGet(GLUT_ELAPSED_TIME) / 1000.f;
glm::mat4 shipInitialTransformation = glm::translate(glm::vec3(0, -5, -15)) * glm::rotate(glm::radians(180.0f), glm::vec3(0,1,0)) * glm::scale(glm::vec3(0.035f));
glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.5f) * glm::mat4_cast(glm::inverse(rotation)) * shipInitialTransformation;
glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.5f) * glm::mat4_cast(glm::inverse(cameraRotation)) * shipInitialTransformation;
drawObjectColor(shipContext, shipModelMatrix, glm::vec3(0.6f));
drawObjectTexture(seafloorContext, glm::translate(glm::vec3(0,-15,0)) * glm::scale(glm::vec3(5.0f, 1.0f, 5.0f)), textureSand);
@ -267,8 +279,8 @@ int main(int argc, char ** argv)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200, 200);
glutInitWindowSize(600, 600);
glutCreateWindow("OpenGL Pierwszy Program");
glutInitWindowSize(resolution[0], resolution[1]);
glutCreateWindow("OpenGL Grafika UAM rybki 21-22");
glewInit();
init();