Merge branch 'Texture' of https://git.wmi.amu.edu.pl/s459315/GRK-2023 into Texture

This commit is contained in:
Aleksander Burkowski 2023-01-31 14:39:11 +01:00
commit 0794ab0449
5 changed files with 164 additions and 3 deletions

BIN
right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 KiB

11
shaders/skybox.frag Normal file
View File

@ -0,0 +1,11 @@
#version 330 core
out vec4 FragColor;
in vec3 texCoords;
uniform samplerCube skybox;
void main()
{
FragColor = texture(skybox, texCoords);
}

17
shaders/skybox.vert Normal file
View File

@ -0,0 +1,17 @@
#version 330 core
layout (location = 0) in vec3 aPos;
out vec3 texCoords;
uniform mat4 projection;
uniform mat4 view;
void main()
{
vec4 pos = projection * view * vec4(aPos, 1.0f);
// Having z equal w will always result in a depth of 1.0f
gl_Position = vec4(pos.x, pos.y, pos.w, pos.w);
// We want to flip the z axis due to the different coordinate systems (left hand vs right hand)
texCoords = vec3(aPos.x, aPos.y, -aPos.z);
}

View File

@ -15,12 +15,16 @@
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <string>
#include "SOIL/stb_image_aug.h"
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
int WIDTH = 900, HEIGHT = 900;
unsigned int skyboxVAO, skyboxVBO, skyboxEBO;
unsigned int cubemapTexture;
namespace models {
Core::RenderContext spaceshipContext;
Core::RenderContext sphereContext;
@ -117,7 +121,40 @@ glm::vec3 spotlightConeDir = glm::vec3(0, 0, 0);
glm::vec3 spotlightColor = glm::vec3(0.4, 0.4, 0.9)*3;
float spotlightPhi = 3.14 / 4;
float skyboxVertices[] =
{
// Coordinates
-1.0f, -1.0f, 1.0f,// 7--------6
1.0f, -1.0f, 1.0f,// /| /|
1.0f, -1.0f, -1.0f,// 4--------5 |
-1.0f, -1.0f, -1.0f,// | | | |
-1.0f, 1.0f, 1.0f,// | 3------|-2
1.0f, 1.0f, 1.0f,// |/ |/
1.0f, 1.0f, -1.0f,// 0--------1
-1.0f, 1.0f, -1.0f
};
unsigned int skyboxIndices[] =
{
// Right
1, 2, 6,
6, 5, 1,
// Left
0, 4, 7,
7, 3, 0,
// Top
4, 5, 6,
6, 7, 4,
// Bottom
0, 3, 2,
2, 1, 0,
// Back
0, 1, 5,
5, 4, 0,
// Front
3, 7, 6,
6, 2, 3
};
float lastTime = -1.f;
float deltaTime = 0.f;
@ -287,6 +324,9 @@ void renderScene(GLFWwindow* window)
glUniform1f(glGetUniformLocation(programSun, "exposition"), exposition);
Core::DrawContext(sphereContext);
glUseProgram(programSkybox);
glUniform1i(glGetUniformLocation(programSkybox, "skybox"), 0);
glUseProgram(program);
drawObjectPBR(models::floor, glm::mat4(),
@ -402,6 +442,30 @@ void renderScene(GLFWwindow* window)
spotlightConeDir = spaceshipDir;
else
spotlightConeDir = -spaceshipDir;
glDepthFunc(GL_LEQUAL);
glUseProgram(programSkybox);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
// We make the mat4 into a mat3 and then a mat4 again in order to get rid of the last row and column
// The last row and column affect the translation of the skybox (which we don't want to affect)
view = glm::mat4(glm::mat3(glm::lookAt(cameraPos, cameraPos + cameraDir, Up)));
projection = glm::perspective(glm::radians(45.0f), (float)WIDTH / HEIGHT, 0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(programSkybox, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(programSkybox, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
// Draws the cubemap as the last object so we can save a bit of performance by discarding all fragments
// where an object is present (a depth of 1.0f will always fail against any object's depth value)
glBindVertexArray(skyboxVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
// Switch back to the normal depth function
glDepthFunc(GL_LESS);
glUseProgram(0);
glfwSwapBuffers(window);
}
@ -425,6 +489,73 @@ void loadModelToContext(std::string path, Core::RenderContext& context)
context.initFromAssimpMesh(scene->mMeshes[0]);
}
void initSkybox() {
glUseProgram(programSkybox);
//unsigned int skyboxVAO, skyboxVBO, skyboxEBO;
glGenVertexArrays(1, &skyboxVAO);
glGenBuffers(1, &skyboxVBO);
glGenBuffers(1, &skyboxEBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, skyboxEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(skyboxIndices), &skyboxIndices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
std::string facesCubemap[6] =
{
"./top.jpg",
"./top.jpg",
"./top.jpg",
"./top.jpg",
"./top.jpg",
"./top.jpg"
};
//unsigned int cubemapTexture;
glGenTextures(1, &cubemapTexture);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// These are very important to prevent seams
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
for (unsigned int i = 0; i < 6; i++)
{
int width, height, nrChannels;
unsigned char* data = stbi_load(facesCubemap[i].c_str(), &width, &height, &nrChannels, 0);
if (data)
{
//stbi_set_flip_vertically_on_load(false);
glTexImage2D
(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0,
GL_RGB,
width,
height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
data
);
stbi_image_free(data);
}
else
{
std::cout << "Failed to load texture: " << facesCubemap[i] << std::endl;
stbi_image_free(data);
}
}
}
void initDepthMap() {
glGenFramebuffers(1, &depthMapFBO);
@ -449,11 +580,13 @@ void init(GLFWwindow* window)
{
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
initDepthMap();
initSkybox();
glEnable(GL_DEPTH_TEST);
program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag");
programSun = shaderLoader.CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag");
programDepth = shaderLoader.CreateProgram("shaders/shader_depth.vert", "shaders/shader_depth.frag");
programSkybox = shaderLoader.CreateProgram("shaders/skybox.vert", "shaders/skybox.frag");
loadModelToContext("./models/sphere.obj", sphereContext);
loadModelToContext("./models/spaceship.obj", shipContext);
@ -513,9 +646,9 @@ void processInput(GLFWwindow* window)
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
spaceshipPos += spaceshipDir * moveSpeed;
spaceshipPos += glm::vec3(cameraDir.x, 0.0f, cameraDir.z) * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
spaceshipPos -= spaceshipDir * moveSpeed;
spaceshipPos -= glm::vec3(cameraDir.x, 0.0f, cameraDir.z) * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
spaceshipPos += spaceshipSide * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)

BIN
top.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB