Prześlij pliki do 'src'

This commit is contained in:
Michał Ciesiółka 2023-02-12 16:26:20 +01:00
parent 0329c784d1
commit ebee896fa5

View File

@ -15,16 +15,20 @@
#include <assimp/scene.h> #include <assimp/scene.h>
#include <assimp/postprocess.h> #include <assimp/postprocess.h>
#include <string> #include <string>
#include "SOIL/stb_image_aug.h"
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024; const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
int WIDTH = 900, HEIGHT = 900; int WIDTH = 900, HEIGHT = 900;
unsigned int skyboxVAO, skyboxVBO, skyboxEBO;
unsigned int cubemapTexture;
namespace models { namespace models {
Core::RenderContext spaceshipContext; Core::RenderContext spaceshipContext;
Core::RenderContext sphereContext; Core::RenderContext sphereContext;
Core::RenderContext laptop;
Core::RenderContext windowFrame; Core::RenderContext windowFrame;
Core::RenderContext bookShelf; Core::RenderContext bookShelf;
Core::RenderContext window; Core::RenderContext window;
@ -35,19 +39,38 @@ namespace models {
Core::RenderContext ceilingLamp; Core::RenderContext ceilingLamp;
Core::RenderContext desk; Core::RenderContext desk;
Core::RenderContext bed; Core::RenderContext bed;
Core::RenderContext floor;
Core::RenderContext carpet;
Core::RenderContext cabinet;
Core::RenderContext roof;
Core::RenderContext longerWall;
Core::RenderContext longerWall_d;
Core::RenderContext smallerWall;
Core::RenderContext smallerWall_ww;
Core::RenderContext trashcan;
Core::RenderContext cap;
Core::RenderContext aquarium;
} }
namespace textures { namespace textures {
GLuint floorTexture;
GLuint sunTexture; GLuint sunTexture;
GLuint windowFrameTexture; GLuint windowFrameTexture;
GLuint bookShelfTexture; GLuint bookShelfTexture;
GLuint potPlantTexture; GLuint potPlantTexture;
GLuint officeChariTexture; GLuint officeChariTexture;
GLuint lampTexture; GLuint lampTexture;
GLuint laptopTexture;
GLuint doorTexture; GLuint doorTexture;
GLuint deskTexture; GLuint deskTexture;
GLuint ceilingLampTexture; GLuint ceilingLampTexture;
GLuint bedTexture; GLuint bedTexture;
GLuint flashlightTexture; GLuint flashlightTexture;
GLuint carpetTexture;
GLuint roofTexture;
GLuint longerWallTexture;
GLuint smallerWallTexture;
GLuint trashcanTexture;
GLuint capTexture;
} }
GLuint depthMapFBO; GLuint depthMapFBO;
@ -83,19 +106,20 @@ Core::RenderContext sphereContext;
glm::vec3 sunPos = glm::vec3(-4.740971f, 2.149999f, 0.369280f); glm::vec3 sunPos = glm::vec3(-4.740971f, 2.149999f, 0.369280f);
glm::vec3 sunDir = glm::vec3(-0.93633f, 0.351106, 0.003226f); glm::vec3 sunDir = glm::vec3(-0.93633f, 0.351106, 0.003226f);
glm::vec3 sunColor = glm::vec3(0.9f, 0.9f, 0.7f)*5; glm::vec3 sunColor = glm::vec3(0.9f, 0.9f, 0.7f);
glm::vec3 cameraPos = glm::vec3(0.479490f, 1.250000f, -2.124680f); glm::vec3 cameraPos = glm::vec3(0.479490f, 1.250000f, -2.124680f);
glm::vec3 cameraDir = glm::vec3(-0.354510f, 0.000000f, 0.935054f); glm::vec3 cameraDir = glm::vec3(-0.354510f, 0.000000f, 0.935054f);
glm::vec3 spaceshipPos = glm::vec3(0.065808f, 1.250000f, -2.189549f); glm::vec3 spaceshipPos = glm::vec3(0.065808f, 1.250000f, 0.689549f);
glm::vec3 spaceshipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f); glm::vec3 spaceshipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f);
GLuint VAO,VBO; GLuint VAO,VBO;
float aspectRatio = 1.f; float aspectRatio = 1.f;
float exposition = 1.f; float exposition = 4.f;
glm::vec3 pointlightPos = glm::vec3(0, 2, 0); glm::vec3 pointlightPos = glm::vec3(0, 2, 0);
glm::vec3 pointlightColor = glm::vec3(0.9, 0.6, 0.6); glm::vec3 pointlightColor = glm::vec3(0.9, 0.6, 0.6);
@ -105,7 +129,40 @@ glm::vec3 spotlightConeDir = glm::vec3(0, 0, 0);
glm::vec3 spotlightColor = glm::vec3(0.4, 0.4, 0.9)*3; glm::vec3 spotlightColor = glm::vec3(0.4, 0.4, 0.9)*3;
float spotlightPhi = 3.14 / 4; 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 lastTime = -1.f;
float deltaTime = 0.f; float deltaTime = 0.f;
@ -212,47 +269,105 @@ void renderShadowapSun() {
glUseProgram(programDepth); glUseProgram(programDepth);
glm::mat4 lightVP = glm::ortho(-3.f, 2.3f, -1.3f, 3.f, -1.0f, 40.0f) * glm::lookAt(sunPos, sunPos - sunDir, glm::vec3(0, 1, 0)); glm::mat4 lightVP = glm::ortho(-3.f, 2.3f, -1.3f, 3.f, -1.0f, 40.0f) * glm::lookAt(sunPos, sunPos - sunDir, glm::vec3(0, 1, 0));
glm::mat4 model;
drawObjectDepth(sphereContext,
lightVP,
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)));
drawObjectDepth(sphereContext,
lightVP,
glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(time) * glm::translate(glm::vec3(1.f, 0, 0)) * glm::scale(glm::vec3(0.1f)));
//drawObjectDepth(models::bedContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::chairContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::deskContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::doorContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::drawerContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::marbleBustContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::materaceContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::pencilsContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::planeContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::roomContext,
// lightVP,
// glm::mat4());
//drawObjectDepth(models::windowContext,
// lightVP,
// glm::mat4());
drawObjectDepth(models::floor,
lightVP,
glm::mat4());
model = glm::mat4() * glm::translate(glm::vec3(0, 2.6, 0));
drawObjectDepth(models::roof,
lightVP,
model);
model = glm::mat4() * glm::translate(glm::vec3(0, 0, -2));
drawObjectDepth(models::longerWall,
lightVP,
model);
model = glm::mat4() * glm::translate(glm::vec3(0, 0, 2));
model = glm::rotate(model, glm::radians(180.0f), glm::vec3(0, 1, 0));
drawObjectDepth(models::longerWall_d,
lightVP,
model);
model = glm::mat4() * glm::translate(glm::vec3(2.5, 0, 0));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1, 0));
drawObjectDepth(models::smallerWall,
lightVP,
model);
model = glm::mat4() * glm::translate(glm::vec3(-2.5, 0, 0));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1, 0));
drawObjectDepth(models::smallerWall_ww,
lightVP,
model);
model = glm::mat4() * glm::translate(glm::vec3(0.0, 0.01, 0.9));
drawObjectDepth(models::carpet,
lightVP,
model);
model = glm::mat4() * glm::translate(glm::vec3(-2.5, 0.99, -0.75)) * glm::scale(glm::vec3(0.36, 0.34, 0.36));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1, 0));
drawObjectDepth(models::window,
lightVP,
model);
model = glm::mat4();
model = glm::scale(glm::vec3(0.5f)) * translate(glm::vec3(0.8f, 0.0f, -3.0f));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1, 0));
drawObjectDepth(models::cabinet,
lightVP,
model);
model = glm::mat4();
model = glm::scale(glm::vec3(0.15f)) * glm::translate(glm::vec3(2.3f, 5.0f, -10.6f));
drawObjectDepth(models::potPlant,
lightVP,
model);
model = glm::mat4(1.0f);
model = glm::scale(glm::vec3(0.5f)) * glm::translate(glm::vec3(-2.65f, 0.0f, -1.65f));
model = glm::rotate(model, glm::radians(180.0f), glm::vec3(0, 1, 0));
drawObjectDepth(models::officeChair,
lightVP,
model);
model = glm::mat4();
model = glm::translate(glm::vec3(-0.7f, 0.75f, -1.7f)) * glm::scale(glm::vec3(0.001f));
drawObjectDepth(models::lamp,
lightVP,
model);
model = glm::mat4(1.0f) * glm::translate(glm::vec3(1.7f, 1.15f, 2.0f));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0.0, 1.0, 0.0)) * glm::scale(glm::vec3(1.0f, 1.1f, 1.3f));
drawObjectDepth(models::door,
lightVP,
model);
model = glm::mat4(1.0f) * glm::translate(glm::vec3(-2.2f, 0.38f, -1.5f));
drawObjectDepth(models::desk,
lightVP,
model);
model = glm::mat4(1.0f);
model = glm::scale(glm::vec3(0.4f)) * glm::translate(glm::vec3(0.0f, 3.6f, 0.0f));
drawObjectDepth(models::ceilingLamp,
lightVP,
model);
model = glm::mat4(1.0f) * glm::translate(glm::vec3(1.6f, 0.0f, -0.86f)) * glm::scale(glm::vec3(0.8f));
model = glm::rotate(model, glm::radians(270.0f), glm::vec3(0.0f, 1.0f, 0.0f));
drawObjectDepth(models::bed,
lightVP,
model);
model = glm::mat4() * glm::translate(glm::vec3(-1.8f, -0.2f, 2.15f)) * glm::scale(glm::vec3(0.015));
model = glm::rotate(model, glm::radians(180.0f), glm::vec3(0, 1, 0));
drawObjectDepth(models::bookShelf,
lightVP,
model);
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WIDTH, HEIGHT); glViewport(0, 0, WIDTH, HEIGHT);
@ -266,63 +381,148 @@ void renderScene(GLFWwindow* window)
updateDeltaTime(time); updateDeltaTime(time);
renderShadowapSun(); renderShadowapSun();
//space lamp /*space lamp*/
glUseProgram(programSun); glUseProgram(programSun);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix(); glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)); glm::mat4 transformation = viewProjectionMatrix * glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1));
glUniformMatrix4fv(glGetUniformLocation(programSun, "transformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(programSun, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniform3f(glGetUniformLocation(programSun, "color"), sunColor.x / 2, sunColor.y / 2, sunColor.z / 2); glUniform3f(glGetUniformLocation(programSun, "color"), sunColor.x, sunColor.y, sunColor.z);
glUniform1f(glGetUniformLocation(programSun, "exposition"), exposition); glUniform1f(glGetUniformLocation(programSun, "exposition"), exposition);
Core::DrawContext(sphereContext); Core::DrawContext(sphereContext);
glUseProgram(programSkybox);
glUniform1i(glGetUniformLocation(programSkybox, "skybox"), 0);
glUseProgram(program); glUseProgram(program);
drawObjectPBR(models::window, glm::mat4(), drawObjectPBR(models::floor, glm::mat4(),
textures::floorTexture,
0,
0);
drawObjectPBR(models::roof, glm::mat4() * glm::translate(glm::vec3(0, 2.6, 0)),
textures::roofTexture,
0,
0
);
drawObjectPBR(models::longerWall, glm::mat4() * glm::translate(glm::vec3(0,0,-2)),
textures::longerWallTexture,
0,
0);
glm::mat4 model = glm::mat4() * glm::translate(glm::vec3(0, 0, 2));
model = glm::rotate(model, glm::radians(180.0f), glm::vec3(0, 1, 0));
drawObjectPBR(models::longerWall_d, model,
textures::longerWallTexture,
0,
0);
model = glm::mat4() * glm::translate(glm::vec3(2.5, 0, 0));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1, 0));
drawObjectPBR(models::smallerWall, model,
textures::longerWallTexture,
0,
0
);
model = glm::mat4() * glm::translate(glm::vec3(-2.5, 0, 0));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1, 0));
drawObjectPBR(models::smallerWall_ww, model,
textures::longerWallTexture,
0,
0
);
drawObjectPBR(models::carpet, glm::mat4() * glm::translate(glm::vec3(0.0 ,0.01 ,0.9)),
textures::carpetTexture,
0,
0
);
model = glm::mat4() * glm::translate(glm::vec3(-2.5, 0.99, -0.75)) * glm::scale(glm::vec3(0.36,0.34,0.36));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1, 0));
drawObjectPBR(models::window, model,
textures::windowFrameTexture, textures::windowFrameTexture,
0.2, 0.2,
0.1 0.1
); );
drawObjectPBR(models::potPlant, glm::mat4() * glm::scale(glm::vec3(0.2f)) * glm::translate(glm::vec3(-5.0f, 55.0f, -6.0f)), model = glm::mat4();
model = glm::scale(glm::vec3(0.5f)) * translate(glm::vec3(0.8f, 0.0f, -3.0f));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1, 0));
drawObjectPBR(models::cabinet, model,
textures::floorTexture,
0,
0);
model = glm::mat4();
model = glm::scale(glm::vec3(0.15f)) * glm::translate(glm::vec3(2.3f, 5.0f, -10.6f));
drawObjectPBR(models::potPlant, model,
textures::potPlantTexture, textures::potPlantTexture,
1.0f, 1.0f,
0.2f 0.2f
); );
drawObjectPBR(models::officeChair, glm::mat4(), model = glm::mat4(1.0f);
model = glm::scale(glm::vec3(0.5f)) * glm::translate(glm::vec3(-2.65f, 0.0f, -1.65f));
model = glm::rotate(model, glm::radians(180.0f), glm::vec3(0, 1, 0));
drawObjectPBR(models::officeChair, model,
textures::officeChariTexture, textures::officeChariTexture,
0.2f, 0.2f,
0.0f 0.0f
); );
drawObjectPBR(models::lamp, glm::mat4() * glm::scale(glm::vec3(0.001f)), model = glm::mat4();
model = glm::translate(glm::vec3(-0.7f, 0.75f, -1.7f)) * glm::scale(glm::vec3(0.001f));
drawObjectPBR(models::lamp, model,
textures::lampTexture, textures::lampTexture,
0.3f, 0.3f,
0.0f 0.0f
); );
drawObjectPBR(models::door, glm::mat4(), model = glm::mat4(1.0f) * glm::translate(glm::vec3(1.7f, 1.15f, 2.0f));
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0.0, 1.0, 0.0)) * glm::scale(glm::vec3(1.0f, 1.1f, 1.3f));
drawObjectPBR(models::door, model,
textures::doorTexture, textures::doorTexture,
0.4f, 0.4f,
0.0f 0.0f
); );
drawObjectPBR(models::desk, glm::mat4(), model = glm::mat4(1.0f) * glm::translate(glm::vec3(-2.2f, 0.38f, -1.5f));
drawObjectPBR(models::desk, model,
textures::deskTexture, textures::deskTexture,
0.4f, 0.4f,
0.0f 0.0f
); );
drawObjectPBR(models::ceilingLamp, glm::mat4(), model = glm::mat4(1.0f) * glm::translate(glm::vec3(-1.25f, 0.75f, -1.4f)) * glm::scale(glm::vec3(0.01f));
model = glm::rotate(model, glm::radians(-87.0f), glm::vec3(1.0, 0.0, 0.0));
drawObjectPBR(models::laptop, model,
textures::laptopTexture,
0.4f,
0.0f
);
model = glm::mat4(1.0f) * glm::translate(glm::vec3(-0.15f, 0.16f, -1.5f)) * glm::scale(glm::vec3(2.5f));
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0, 0.0, 0.0));
drawObjectPBR(models::trashcan, model,
textures::trashcanTexture,
0.4f,
0.0f
);
model = glm::mat4(1.0f) * glm::translate(glm::vec3(-1.5f, 0.94f, 1.75f)) * glm::scale(glm::vec3(0.01f));
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0, 0.0, 0.0));
model = glm::rotate(model, glm::radians(-33.0f), glm::vec3(0.0, 0.0, 1.0));
drawObjectPBR(models::cap, model,
textures::capTexture,
0.4f,
0.0f
);
model = glm::mat4(1.0f);
model = glm::scale(glm::vec3(0.4f)) * glm::translate(glm::vec3(0.0f, 3.6f, 0.0f));
drawObjectPBR(models::ceilingLamp, model,
textures::ceilingLampTexture, textures::ceilingLampTexture,
0.2f, 0.2f,
0.0f 0.0f
); );
drawObjectPBR(models::bed, glm::mat4(), model = glm::mat4(1.0f) * glm::translate(glm::vec3(1.6f, 0.0f, -0.86f)) * glm::scale(glm::vec3(0.8f));
model = glm::rotate(model, glm::radians(270.0f), glm::vec3(0.0f, 1.0f, 0.0f));
drawObjectPBR(models::bed, model,
textures::bedTexture, textures::bedTexture,
0.3f, 0.3f,
0.0f 0.0f
); );
drawObjectPBR(models::windowFrame, glm::mat4(), model = glm::mat4() * glm::translate(glm::vec3(-1.8f, -0.2f, 2.15f)) * glm::scale(glm::vec3(0.015));
textures::windowFrameTexture, model = glm::rotate(model, glm::radians(180.0f), glm::vec3(0, 1, 0));
0.2f, drawObjectPBR(models::bookShelf, model,
0.0f);
drawObjectPBR(models::bookShelf, glm::mat4() * glm::scale(glm::vec3(0.01)),
textures::bookShelfTexture, textures::bookShelfTexture,
0.4f, 0.4f,
0.1f); 0.1f);
@ -336,7 +536,6 @@ void renderScene(GLFWwindow* window)
0.,0.,0.,1., 0.,0.,0.,1.,
}); });
//drawObjectColor(shipContext, //drawObjectColor(shipContext,
// glm::translate(cameraPos + 1.5 * cameraDir + cameraUp * -0.5f) * inveseCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()), // glm::translate(cameraPos + 1.5 * cameraDir + cameraUp * -0.5f) * inveseCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()),
// glm::vec3(0.3, 0.3, 0.5) // glm::vec3(0.3, 0.3, 0.5)
@ -347,12 +546,47 @@ void renderScene(GLFWwindow* window)
0.0, 0.0,
0.0 0.0
); );
//drawObjectPBR(models::aquarium,
//glm::mat4(),
//textures::flashlightTexture,
//0.0,
//0.0
//);
spotlightPos = spaceshipPos + 0.2 * spaceshipDir; //spotlightPos = spaceshipPos + 0.2 * spaceshipDir;
if (!flashOff) if (!flashOff) {
spotlightConeDir = spaceshipDir; spotlightConeDir = spaceshipDir;
else spotlightPos = spaceshipPos + 0.2 * spaceshipDir;
}
else {
spotlightConeDir = -spaceshipDir; spotlightConeDir = -spaceshipDir;
spotlightPos = glm::vec3(500,500,500);
}
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); glUseProgram(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
@ -376,6 +610,73 @@ void loadModelToContext(std::string path, Core::RenderContext& context)
context.initFromAssimpMesh(scene->mMeshes[0]); 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] =
{
"./front.png",
"./back.png",
"./up.png",
"./down.png",
"./right.png",
"./left.png"
};
//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() { void initDepthMap() {
glGenFramebuffers(1, &depthMapFBO); glGenFramebuffers(1, &depthMapFBO);
@ -400,24 +701,43 @@ void init(GLFWwindow* window)
{ {
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
initDepthMap(); initDepthMap();
initSkybox();
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag"); 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"); programSun = shaderLoader.CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag");
programDepth = shaderLoader.CreateProgram("shaders/shader_depth.vert", "shaders/shader_depth.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/sphere.obj", sphereContext);
loadModelToContext("./models/spaceship.obj", shipContext); loadModelToContext("./models/spaceship.obj", shipContext);
loadModelToContext("./models/spaceship.obj", models::spaceshipContext); loadModelToContext("./models/spaceship.obj", models::spaceshipContext);
loadModelToContext("./models/sphere.obj", models::sphereContext); loadModelToContext("./models/sphere.obj", models::sphereContext);
loadModelToContext("./models2/Frame/Frame.obj", models::windowFrame);
loadModelToContext("./models2/bookshelf/shelf.obj", models::bookShelf); loadModelToContext("./models2/bookshelf/shelf.obj", models::bookShelf);
loadModelToContext("./models2/potplant/Pot.obj", models::potPlant); loadModelToContext("./models2/potplant/Pot.obj", models::potPlant);
loadModelToContext("./models2/lamp/Lamp_LOVMANAD_OBJ.obj", models::lamp); loadModelToContext("./models2/lamp/Lamp_LOVMANAD_OBJ.obj", models::lamp);
loadModelToContext("./models2/door/source/door.obj", models::door); loadModelToContext("./models2/door/source/door.obj", models::door);
loadModelToContext("./models2/bed/untitled.obj", models::bed);
loadModelToContext("./models2/laptop/10119_Laptop_Computer_v2_max2011_SG_it2.obj", models::laptop);
loadModelToContext("./models2/ceiling_lamp/ceilinglamp.obj", models::ceilingLamp);
loadModelToContext("./models2/desk/Desk0.5.obj", models::desk);
loadModelToContext("./models2/chair/chair.obj", models::officeChair);
loadModelToContext("./models2/window/WindowUK.obj", models::window);
loadModelToContext("./models2/carpet/carpet.obj", models::carpet);
loadModelToContext("./models2/room/floor.obj", models::floor);
loadModelToContext("./models2/room/floor.obj", models::roof);
loadModelToContext("./models2/room/longerWall.obj", models::longerWall);
loadModelToContext("./models2/room/smallerWall.obj", models::smallerWall);
loadModelToContext("./models2/room/smallerWall_ww.obj", models::smallerWall_ww);
loadModelToContext("./models2/room/longerWall_d.obj", models::longerWall_d);
loadModelToContext("./models2/cabinet/cabinet.obj", models::cabinet);
loadModelToContext("./models2/trashcan/trashcan.obj", models::trashcan);
loadModelToContext("./models2/cap/Cap.obj", models::cap);
loadModelToContext("./models/aquarium1.obj", models::aquarium);
textures::floorTexture = Core::LoadTexture("./models2/room/wood.png");
textures::flashlightTexture = Core::LoadTexture("./models/FlashlightTexture.png"); textures::flashlightTexture = Core::LoadTexture("./models/FlashlightTexture.png");
textures::sunTexture = Core::LoadTexture("./models/sun.jpeg"); textures::sunTexture = Core::LoadTexture("./models/sun.jpeg");
textures::windowFrameTexture = Core::LoadTexture("./models2/Frame/frame.png"); textures::windowFrameTexture = Core::LoadTexture("./models2/Frame/frame.png");
@ -425,6 +745,18 @@ void init(GLFWwindow* window)
textures::potPlantTexture = Core::LoadTexture("./models2/potplant/b3.bmp"); textures::potPlantTexture = Core::LoadTexture("./models2/potplant/b3.bmp");
textures::lampTexture = Core::LoadTexture("./models2/lamp/_Base_color.png"); textures::lampTexture = Core::LoadTexture("./models2/lamp/_Base_color.png");
textures::doorTexture = Core::LoadTexture("./models2/door/textures/Door_albedo.bmp"); textures::doorTexture = Core::LoadTexture("./models2/door/textures/Door_albedo.bmp");
textures::bedTexture = Core::LoadTexture("./models2/bed/bed.png");
textures::ceilingLampTexture = Core::LoadTexture("./models2/ceiling_lamp/ceilingLamp.png");
textures::deskTexture = Core::LoadTexture("./models2/desk/defsk.png");
textures::officeChariTexture = Core::LoadTexture("./models2/chair/chair.png");
textures::windowFrameTexture = Core::LoadTexture("./models2/window/windowColor.png");
textures::carpetTexture = Core::LoadTexture("./models2/carpet/1251.jpg");
textures::roofTexture = Core::LoadTexture("./models2/room/roof.png");
textures::longerWallTexture = Core::LoadTexture("./models2/room/longerWall.png");
textures::longerWallTexture = Core::LoadTexture("./models2/room/smallerWall.png");
textures::laptopTexture = Core::LoadTexture("./models2/laptop/10119_Laptop_Computer_v1_Diffuse_SG.jpg");
textures::trashcanTexture = Core::LoadTexture("./models2/trashcan/trashcan.jpg");
textures::capTexture = Core::LoadTexture("./models2/cap/Cap.jpg");
} }
void shutdown(GLFWwindow* window) void shutdown(GLFWwindow* window)
@ -443,9 +775,9 @@ void processInput(GLFWwindow* window)
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
} }
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) 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) 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) if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
spaceshipPos += spaceshipSide * moveSpeed; spaceshipPos += spaceshipSide * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
@ -467,20 +799,20 @@ void processInput(GLFWwindow* window)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwGetCursorPos(window, &xpos, &ypos); glfwGetCursorPos(window, &xpos, &ypos);
float rotX = sensitivity * (float)(xpos - (HEIGHT / 2)) / HEIGHT; float rotX = sensitivity * (float)(xpos - (WIDTH / 2)) / HEIGHT;
float rotY = sensitivity * (float)(ypos - (WIDTH / 2)) / WIDTH; float rotY = sensitivity * (float)(ypos - (HEIGHT / 2)) / WIDTH;
glm::vec3 newOrientation = glm::rotate(spaceshipDir, glm::radians(-rotY), glm::normalize(glm::cross(spaceshipDir, Up))); glm::vec3 newOrientation = glm::rotate(spaceshipDir, glm::radians(-rotY), glm::normalize(glm::cross(spaceshipDir, spaceshipUp)));
// Decides whether or not the next vertical Orientation is legal or not // Decides whether or not the next vertical Orientation is legal or not
if (abs(glm::angle(newOrientation, Up) - glm::radians(90.0f)) <= glm::radians(85.0f)) if (abs(glm::angle(newOrientation, spaceshipUp) - glm::radians(90.0f)) <= glm::radians(85.0f))
{ {
spaceshipDir = newOrientation; spaceshipDir = newOrientation;
} }
// Rotates the Orientation left and right // Rotates the Orientation left and right
spaceshipDir = glm::rotate(spaceshipDir, glm::radians(-rotX), Up); spaceshipDir = glm::rotate(spaceshipDir, glm::radians(-rotX), spaceshipUp);
glfwSetCursorPos(window, WIDTH/2, HEIGHT/2); glfwSetCursorPos(window, WIDTH/2, HEIGHT/2);
} }