added skybox

This commit is contained in:
koziej97 2022-02-05 14:09:38 +01:00
parent 5b2349e37b
commit ad9083330b
21 changed files with 7954 additions and 13 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,7 @@
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(513,5): warning MSB8028: Katalog pośredni (Debug\) zawiera pliki udostępnione z innego projektu (grk-cw6.vcxproj). Może to spowodować niepoprawne zachowanie podczas oczyszczania i ponownej kompilacji.
main.cpp
C:\Users\lukas\Desktop\Projekt - Grafika komputerowa\Projekt\cw 6\src\main.cpp(72,23): warning C4244: "=": konwersja z "int" do "float", możliwa utrata danych
C:\Users\lukas\Desktop\Projekt - Grafika komputerowa\Projekt\cw 6\src\main.cpp(73,23): warning C4244: "=": konwersja z "int" do "float", możliwa utrata danych
C:\Users\lukas\Desktop\Projekt - Grafika komputerowa\Projekt\cw 6\src\main.cpp(181,12): warning C4244: "argument": konwersja z "time_t" do "unsigned int", możliwa utrata danych
C:\Users\lukas\Desktop\Projekt - Grafika komputerowa\Projekt\cw 6\src\main.cpp(120,23): warning C4244: "=": konwersja z "int" do "float", możliwa utrata danych
C:\Users\lukas\Desktop\Projekt - Grafika komputerowa\Projekt\cw 6\src\main.cpp(121,23): warning C4244: "=": konwersja z "int" do "float", możliwa utrata danych
C:\Users\lukas\Desktop\Projekt - Grafika komputerowa\Projekt\cw 6\src\main.cpp(260,12): warning C4244: "argument": konwersja z "time_t" do "unsigned int", możliwa utrata danych
Camera.obj : warning LNK4075: zignorowano opcję „/EDITANDCONTINUE” z powodu określenia opcji „/INCREMENTAL:NO”
grk-cw6.vcxproj -> C:\Users\lukas\Desktop\Projekt - Grafika komputerowa\Projekt\Debug\Projekt.exe

Binary file not shown.

Binary file not shown.

Binary file not shown.

11
cw 6/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);
}

16
cw 6/shaders/skybox.vert Normal file
View File

@ -0,0 +1,16 @@
#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

@ -10,14 +10,21 @@
#include "Render_Utils.h"
#include "Camera.h"
#include "Texture.h"
#include "SOIL/SOIL.h"
float width = 600;
float height = 600;
GLuint programColor;
GLuint programTexture;
GLuint programSkybox;
Core::Shader_Loader shaderLoader;
Core::RenderContext shipContext;
Core::RenderContext sphereContext;
Core::RenderContext fishContext;
Core::RenderContext skyboxContext;
glm::vec3 cameraPos = glm::vec3(0, 0, 5);
glm::vec3 cameraDir; // Wektor "do przodu" kamery
@ -30,9 +37,9 @@ glm::vec3 lightDir = glm::normalize(glm::vec3(1.0f, -0.9f, 1.0f));
glm::quat rotation = glm::quat(1, 0, 0, 0);
GLuint textureAsteroid;
GLuint textureFish;
glm::vec3 planetLocation[10];
glm::vec3 fishLocation[10];
float mouseXPosition;
float mouseYPosition;
@ -46,6 +53,47 @@ glm::quat rotation_x;
float dy = 0;
float dx = 0;
// SKYBOX
unsigned int skyboxVAO, skyboxVBO, skyboxEBO;
unsigned int cubemapTexture;
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
};
void keyboard(unsigned char key, int x, int y)
{
@ -80,7 +128,7 @@ glm::mat4 createCameraMatrix()
dy += delta_y;
dx += delta_x;
printf("%f", delta_y);
//printf("%f", delta_y);
delta_x = 0;
delta_y = 0;
@ -134,6 +182,36 @@ void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuin
glUseProgram(0);
}
void drawObjectSkybox(Core::RenderContext context)
{
glUseProgram(programSkybox);
glUniform1i(glGetUniformLocation(programSkybox, "skybox"), 0);
glDepthFunc(GL_LEQUAL);
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, glm::vec3(0, 1, 0))));
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);
}
void renderScene()
{
// Aktualizacja macierzy widoku i rzutowania
@ -149,15 +227,16 @@ void renderScene()
for (int i = 0; i < 10; i++)
{
glm::mat4 fishInitialTransformation = glm::translate(planetLocation[i]) * glm::rotate(glm::radians(180.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.01f));
drawObjectTexture(sphereContext, fishInitialTransformation, textureAsteroid);
glm::mat4 fishInitialTransformation = glm::translate(fishLocation[i]) * glm::rotate(glm::radians(180.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.005f));
drawObjectTexture(fishContext, fishInitialTransformation, textureFish);
}
drawObjectSkybox(skyboxContext);
glutSwapBuffers();
}
void loadModelToContext(std::string path, Core::RenderContext& context)
{
Assimp::Importer import;
@ -175,22 +254,94 @@ void init()
{
for (int i = 0; i < 10; i++)
{
planetLocation[i] = glm::ballRand(20.0);
fishLocation[i] = glm::ballRand(20.0);
}
srand(time(0));
glEnable(GL_DEPTH_TEST);
programColor = shaderLoader.CreateProgram("shaders/shader_color.vert", "shaders/shader_color.frag");
programTexture = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag");
programSkybox = shaderLoader.CreateProgram("shaders/skybox.vert", "shaders/skybox.frag");
loadModelToContext("models/spaceship.obj", shipContext);
loadModelToContext("models/TropicalFish01.obj", sphereContext);
textureAsteroid = Core::LoadTexture("textures/TropicalFish01.jpg");
loadModelToContext("models/TropicalFish01.obj", fishContext);
textureFish = Core::LoadTexture("textures/TropicalFish01.jpg");
// SKYBOX
// Create VAO, VBO, and EBO for the skybox
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);
// All the faces of the cubemap (make sure they are in this exact order)
std::string facesCubemap[6] =
{
"textures/skybox/uw_rt.jpg",
"textures/skybox/uw_lf.jpg",
"textures/skybox/uw_up.jpg",
"textures/skybox/uw_dn.jpg",
"textures/skybox/uw_bk.jpg",
"textures/skybox/uw_ft.jpg"
};
// Creates the cubemap texture object
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);
// This might help with seams on some systems
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
// Cycles through all the textures and attaches them to the cubemap object
for (unsigned int i = 0; i < 6; i++)
{
int width, height, nrChannels;
unsigned char* data = SOIL_load_image(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
);
SOIL_free_image_data(data);
}
else
{
std::cout << "Failed to load texture: " << facesCubemap[i] << std::endl;
SOIL_free_image_data(data);
}
}
}
void shutdown()
{
shaderLoader.DeleteProgram(programColor);
shaderLoader.DeleteProgram(programTexture);
shaderLoader.DeleteProgram(programSkybox);
}
void idle()
@ -214,6 +365,7 @@ int main(int argc, char** argv)
glutDisplayFunc(renderScene);
glutIdleFunc(idle);
glutMainLoop();
shutdown();

Binary file not shown.

After

Width:  |  Height:  |  Size: 445 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 433 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 390 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 366 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 KiB

7762
dependencies/stb/stb_image.h vendored Normal file

File diff suppressed because it is too large Load Diff