move camera to spaceship

This commit is contained in:
Pawel Felcyn 2024-01-03 20:59:48 +01:00
parent 5c8faba1f8
commit 034df6be24
2 changed files with 32 additions and 24 deletions

View File

@ -13,6 +13,9 @@ public:
glm::vec3 spotlightColor = glm::vec3(0.4, 0.4, 0.9) * 3;
float spotlightPhi = 3.14 / 4;
glm::vec3 cameraPos = glm::vec3(0.479490f, 1.250000f, -2.124680f);
glm::vec3 cameraDir = glm::vec3(-0.354510f, 0.000000f, 0.935054f);
glm::mat4 calculateModelMatrix() {
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir));
@ -52,7 +55,26 @@ public:
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
spaceshipDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceshipDir, 0));
cameraPos = spaceshipPos - 0.5 * spaceshipDir + glm::vec3(0, 1, 0) * 0.2f;
cameraDir = spaceshipDir;
spotlightPos = spaceshipPos + 0.2 * spaceshipDir;
spotlightConeDir = spaceshipDir;
}
glm::mat4 createCameraMatrix()
{
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, cameraDir));
glm::mat4 cameraRotrationMatrix = glm::mat4({
cameraSide.x,cameraSide.y,cameraSide.z,0,
cameraUp.x,cameraUp.y,cameraUp.z ,0,
-cameraDir.x,-cameraDir.y,-cameraDir.z,0,
0.,0.,0.,1.,
});
cameraRotrationMatrix = glm::transpose(cameraRotrationMatrix);
glm::mat4 cameraMatrix = cameraRotrationMatrix * glm::translate(-cameraPos);
return cameraMatrix;
}
};

View File

@ -53,9 +53,6 @@ Core::RenderContext sphereContext;
Sun sun;
glm::vec3 cameraPos = glm::vec3(0.479490f, 1.250000f, -2.124680f);
glm::vec3 cameraDir = glm::vec3(-0.354510f, 0.000000f, 0.935054f);
Spaceship spaceship;
GLuint VAO,VBO;
@ -79,21 +76,6 @@ void updateDeltaTime(float time) {
if (deltaTime > 0.1) deltaTime = 0.1;
lastTime = time;
}
glm::mat4 createCameraMatrix()
{
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f)));
glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide,cameraDir));
glm::mat4 cameraRotrationMatrix = glm::mat4({
cameraSide.x,cameraSide.y,cameraSide.z,0,
cameraUp.x,cameraUp.y,cameraUp.z ,0,
-cameraDir.x,-cameraDir.y,-cameraDir.z,0,
0.,0.,0.,1.,
});
cameraRotrationMatrix = glm::transpose(cameraRotrationMatrix);
glm::mat4 cameraMatrix = cameraRotrationMatrix * glm::translate(-cameraPos);
return cameraMatrix;
}
glm::mat4 createPerspectiveMatrix()
{
@ -118,7 +100,7 @@ glm::mat4 createPerspectiveMatrix()
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic) {
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * spaceship.createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
@ -130,7 +112,7 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec
glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), spaceship.cameraPos.x, spaceship.cameraPos.y, spaceship.cameraPos.z);
glUniform3f(glGetUniformLocation(program, "sunDir"), sun.sunDir.x, sun.sunDir.y, sun.sunDir.z);
glUniform3f(glGetUniformLocation(program, "sunColor"), sun.sunColor.x, sun.sunColor.y, sun.sunColor.z);
@ -170,13 +152,20 @@ void renderScene(GLFWwindow* window)
//space lamp
glUseProgram(programSun);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * spaceship.createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1));
glUniformMatrix4fv(glGetUniformLocation(programSun, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniform3f(glGetUniformLocation(programSun, "color"), sun.sunColor.x / 2, sun.sunColor.y / 2, sun.sunColor.z / 2);
glUniform1f(glGetUniformLocation(programSun, "exposition"), exposition);
Core::DrawContext(sphereContext);
glm::mat4 viewProjectionMatrix2 = createPerspectiveMatrix() * spaceship.createCameraMatrix();
glm::mat4 transformation2 = viewProjectionMatrix2 * glm::translate(pointlightPos + glm::vec3(1, 1, 1)) * glm::scale(glm::vec3(0.1));
glUniformMatrix4fv(glGetUniformLocation(programSun, "transformation"), 1, GL_FALSE, (float*)&transformation2);
glUniform3f(glGetUniformLocation(programSun, "color"), sun.sunColor.x / 2, sun.sunColor.y / 2, sun.sunColor.z / 2);
glUniform1f(glGetUniformLocation(programSun, "exposition"), exposition);
Core::DrawContext(sphereContext);
glUseProgram(program);
drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0);
@ -278,9 +267,6 @@ void processInput(GLFWwindow* window)
spaceship.processInput(window, deltaTime);
cameraPos = spaceship.spaceshipPos - 0.5 * spaceship.spaceshipDir + glm::vec3(0, 1, 0) * 0.2f;
cameraDir = spaceship.spaceshipDir;
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
exposition -= 0.05;
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)