lab 4 basic tasks

This commit is contained in:
Eikthyrnir 2023-11-24 02:48:39 +01:00
parent ce33570341
commit 6e48ee3fc2
5 changed files with 73 additions and 19 deletions

View File

@ -33,7 +33,7 @@ void renderScene(GLFWwindow* window)
glUseProgram(program);
glm::mat4 transformation = glm::mat4(1.0f);
/*
/*
//Zad 5
transformation = glm::translate(transformation, glm::vec3(0.0f, (float)glm::sin(time), 0.0f));
transformation = glm::rotate(transformation, time, glm::vec3(0.0, 0.0, 1.0));

View File

@ -71,7 +71,7 @@ glm::mat4 createPerspectiveMatrix(float fov, float aspectRatio){
float f = 100.f;
float s = 1 / (glm::tan(fov * glm::pi<float>() / 360));
//zad 4
/*glm::mat4 perspectiveMatrix = glm::mat4({
s, 0., 0., 0.,

View File

@ -1,9 +1,27 @@
#version 430 core
uniform vec3 color;
uniform vec3 background_color;
out vec4 out_color;
//zad5
float n = 0.05;
float f = 20.;
float linear_z() {
float z_around_0 = gl_FragCoord.z * 2 - 1;
float z = -2 * n * f / (z_around_0 * (n - f) + n + f);
return z / -f;
}
void main()
{
out_color = vec4(1,0,1,1);
//zad4
// out_color = vec4(gl_FragCoord.z, gl_FragCoord.z, gl_FragCoord.z, 1);
//zad5
// out_color = vec4(vec3(linear_z()), 1);
//zad5*
out_color = vec4(mix(color, background_color, linear_z()), 1);
// out_color = vec4(color, 1);
}

View File

@ -25,6 +25,9 @@ Core::RenderContext sphereContext;
glm::vec3 cameraPos = glm::vec3(-4.f, 0, 0);
glm::vec3 cameraDir = glm::vec3(1.f, 0.f, 0.f);
glm::vec3 spaceshipPos = glm::vec3(-4.f, 0, 0);
glm::vec3 spaceshipDir = glm::vec3(1.f, 0.f, 0.f);
GLuint VAO,VBO;
float aspectRatio = 1.f;
@ -69,8 +72,8 @@ glm::mat4 createPerspectiveMatrix()
float a1 = glm::min(aspectRatio, 1.f);
float a2 = glm::min(1 / aspectRatio, 1.f);
perspectiveMatrix = glm::mat4({
1,0.,0.,0.,
0.,1,0.,0.,
a2,0.,0.,0.,
0.,a1,0.,0.,
0.,0.,(f+n) / (n - f),2*f * n / (n - f),
0.,0.,-1.,0.,
});
@ -86,6 +89,8 @@ void drawObjectColor(Core::RenderContext& context, glm::mat4 modelMatrix, glm::v
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(program, "background_color"), 0.0f, 0.3f, 0.3f);
Core::DrawContext(context);
}
void renderScene(GLFWwindow* window)
@ -97,8 +102,37 @@ void renderScene(GLFWwindow* window)
glUseProgram(program);
drawObjectColor(sphereContext, glm::mat4(), glm::vec3(0.3, 0.3, 0.2));
drawObjectColor(sphereContext, glm::translate(glm::vec3(0, 2, 0)), glm::vec3(0.9, 0.9, 0.2));
//planet
transformation = glm::eulerAngleXYZ(time / 3, 0.f, 0.f) * glm::translate(glm::vec3(0, 4, 0))
* glm::scale(glm::vec3(0.4));
drawObjectColor(sphereContext, transformation, glm::vec3(0.3, 0.3, 0.2));
//satelite
transformation = glm::eulerAngleXYZ(time / 3, 0.f, 0.f) * glm::translate(glm::vec3(0, 4, 0))
* glm::eulerAngleX(time / 2) * glm::translate(glm::vec3(0, 2.f, 0))
* glm::scale(glm::vec3(0.2));
drawObjectColor(sphereContext, transformation, glm::vec3(0.3, 0.3, 0.2));
//satelite of satelite
/*transformation = glm::eulerAngleXYZ(time / 3, 0.f, 0.f) * glm::translate(glm::vec3(0, 4, 0))
* glm::eulerAngleX(time / 2) * glm::translate(glm::vec3(0, 2.f, 0))
* glm::eulerAngleX(time) * glm::translate(glm::vec3(0, 1.f, 0))
* glm::scale(glm::vec3(0.1));
drawObjectColor(sphereContext, transformation, glm::vec3(0.3, 0.3, 0.2));*/
//sun
drawObjectColor(sphereContext, glm::mat4(), glm::vec3(0.9, 0.9, 0.2));
//spaceship
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));
glm::mat4 speceshipCameraRotrationMatrix = glm::mat4({
spaceshipSide.x,spaceshipSide.y,spaceshipSide.z,0,
spaceshipUp.x,spaceshipUp.y,spaceshipUp.z ,0,
-spaceshipDir.x,-spaceshipDir.y,-spaceshipDir.z,0,
0.,0.,0.,1.,
});
drawObjectColor(shipContext,
glm::translate(spaceshipPos) * speceshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()) * glm::scale(glm::vec3(0.5f)),
glm::vec3(0)
);
glUseProgram(0);
glfwSwapBuffers(window);
@ -129,7 +163,7 @@ void init(GLFWwindow* window)
program = shaderLoader.CreateProgram("shaders/shader_4_1.vert", "shaders/shader_4_1.frag");
loadModelToContext("./models/sphere.obj",sphereContext);
loadModelToContext("./models/spaceship.obj", shipContext);
}
void shutdown(GLFWwindow* window)
@ -140,26 +174,28 @@ void shutdown(GLFWwindow* window)
//obsluga wejscia
void processInput(GLFWwindow* window)
{
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f,1.f,0.f)));
float angleSpeed = 0.05f;
float moveSpeed = 0.05f;
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
float angleSpeed = 0.005f;
float moveSpeed = 0.005f;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cameraPos += cameraDir * moveSpeed;
spaceshipPos += spaceshipDir * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cameraPos -= cameraDir * moveSpeed;
spaceshipPos -= spaceshipDir * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
cameraPos += cameraSide * moveSpeed;
spaceshipPos += spaceshipSide * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
cameraPos -= cameraSide * moveSpeed;
spaceshipPos -= spaceshipSide * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cameraDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(cameraDir, 0));
spaceshipDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceshipDir, 0));
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(cameraDir, 0));
spaceshipDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceshipDir, 0));
//cameraDir = glm::normalize(-cameraPos);
cameraPos = spaceshipPos - 1.5f * spaceshipDir + glm::vec3(0, 0.5, 0);
cameraDir = spaceshipDir;
}

View File

@ -75,8 +75,8 @@ glm::mat4 createPerspectiveMatrix()
float a1 = glm::min(aspectRatio, 1.f);
float a2 = glm::min(1 / aspectRatio, 1.f);
perspectiveMatrix = glm::mat4({
1,0.,0.,0.,
0.,aspectRatio,0.,0.,
a2,0.,0.,0.,
0.,a1,0.,0.,
0.,0.,(f + n) / (n - f),2 * f * n / (n - f),
0.,0.,-1.,0.,
});