diff --git a/Data/Shaders/SkyBox_Fragment.glsl b/Data/Shaders/SkyBox_Fragment.glsl new file mode 100644 index 0000000..69e225b --- /dev/null +++ b/Data/Shaders/SkyBox_Fragment.glsl @@ -0,0 +1,11 @@ +#version 330 core +out vec4 outColour; + +in vec3 passTexCoords; + +uniform samplerCube skybox; + +void main() +{ + outColour = vec4(1, 1, 0, 1);//texture(skybox, passTexCoords); +} diff --git a/Data/Shaders/SkyBox_Vertex.glsl b/Data/Shaders/SkyBox_Vertex.glsl new file mode 100644 index 0000000..68b7528 --- /dev/null +++ b/Data/Shaders/SkyBox_Vertex.glsl @@ -0,0 +1,14 @@ +#version 330 core + +layout (location = 0) in vec3 inVertexCoords; + +out vec3 passTexCoords; + +uniform mat4 projection; +uniform mat4 view; + +void main() +{ + passTexCoords = inVertexCoords; + gl_Position = projection * view * vec4(inVertexCoords, 1.0); +} diff --git a/Data/Textures/Skybox/Test/README.TXT b/Data/Textures/Skybox/Test/README.TXT new file mode 100644 index 0000000..4fd1677 --- /dev/null +++ b/Data/Textures/Skybox/Test/README.TXT @@ -0,0 +1,14 @@ +UPDATED THE 28TH, REMOVED A FUNKY PLANET + +INTERSTELLAR +high res 1024^2 environment map +ships as TGA. + + +By Jockum Skoglund aka hipshot +hipshot@zfight.com +www.zfight.com +Stockholm, 2005 08 25 + + +Modify however you like, just cred me for my work, maybe link to my page. diff --git a/Data/Textures/Skybox/Test/back.png b/Data/Textures/Skybox/Test/back.png new file mode 100644 index 0000000..35bd5c9 Binary files /dev/null and b/Data/Textures/Skybox/Test/back.png differ diff --git a/Data/Textures/Skybox/Test/bottom.png b/Data/Textures/Skybox/Test/bottom.png new file mode 100644 index 0000000..77dff76 Binary files /dev/null and b/Data/Textures/Skybox/Test/bottom.png differ diff --git a/Data/Textures/Skybox/Test/front.png b/Data/Textures/Skybox/Test/front.png new file mode 100644 index 0000000..945a96e Binary files /dev/null and b/Data/Textures/Skybox/Test/front.png differ diff --git a/Data/Textures/Skybox/Test/left.png b/Data/Textures/Skybox/Test/left.png new file mode 100644 index 0000000..057688e Binary files /dev/null and b/Data/Textures/Skybox/Test/left.png differ diff --git a/Data/Textures/Skybox/Test/right.png b/Data/Textures/Skybox/Test/right.png new file mode 100644 index 0000000..1f5a660 Binary files /dev/null and b/Data/Textures/Skybox/Test/right.png differ diff --git a/Data/Textures/Skybox/Test/top.png b/Data/Textures/Skybox/Test/top.png new file mode 100644 index 0000000..bf4fbc4 Binary files /dev/null and b/Data/Textures/Skybox/Test/top.png differ diff --git a/Source/Application.cpp b/Source/Application.cpp index 8d518cc..8a58cb2 100755 --- a/Source/Application.cpp +++ b/Source/Application.cpp @@ -33,7 +33,7 @@ void Application::runMainGameLoop() auto currentTime = timer.getElapsedTime(); auto elapsed = currentTime - lastTime; lastTime = currentTime; - tickLag = elapsed; + tickLag += elapsed; handleEvents(); if (m_states.empty()) break; diff --git a/Source/Camera.cpp b/Source/Camera.cpp index bfb08a7..00b7969 100755 --- a/Source/Camera.cpp +++ b/Source/Camera.cpp @@ -55,3 +55,14 @@ const Matrix4& Camera::getProjectionViewMatrix() const noexcept { return m_projectionViewMatrix; } + +const Matrix4& Camera::getViewMatrix() const noexcept +{ + return m_viewMatrix; +} + +const Matrix4& Camera::getProjMatrix() const noexcept +{ + return m_projectionMatrix; +} + diff --git a/Source/Camera.h b/Source/Camera.h index 0a5ed34..2c605ef 100755 --- a/Source/Camera.h +++ b/Source/Camera.h @@ -19,7 +19,9 @@ class Camera : public Entity const Frustum& getFrustum() const noexcept; - const Matrix4& getProjectionViewMatrix() const noexcept; + const Matrix4& getProjectionViewMatrix () const noexcept; + const Matrix4& getViewMatrix () const noexcept; + const Matrix4& getProjMatrix () const noexcept; private: const Entity* m_pEntity = nullptr; diff --git a/Source/Main.cpp b/Source/Main.cpp index c17e0ae..2c8317c 100755 --- a/Source/Main.cpp +++ b/Source/Main.cpp @@ -28,6 +28,7 @@ namespace MessageBox(nullptr, message.c_str(), "Error", MB_OK); std::cerr << message << std::endl; #elif __linux__ || __APPLE__ + ///@TODO Test const std::string command = "zenity --error --text \"" + message + "\""; system(command.c_str()); #else diff --git a/Source/Model.cpp b/Source/Model.cpp index 5a6328b..e2f3ac6 100755 --- a/Source/Model.cpp +++ b/Source/Model.cpp @@ -16,6 +16,17 @@ Model::Model(const std::vector& vertexPositions, addEBO(indices); } +Model::Model(const std::vector& vertexPositions, + const std::vector& indices) +: m_indicesCount (indices.size()) +{ + glGenVertexArrays(1, &m_vao); + bind(); + + addVBO(3, vertexPositions); + addEBO(indices); +} + Model::Model(Model&& other) : m_vao (other.m_vao) , m_vboCount (other.m_vboCount) diff --git a/Source/Model.h b/Source/Model.h index fc474e4..a4270e9 100755 --- a/Source/Model.h +++ b/Source/Model.h @@ -13,6 +13,9 @@ class Model const std::vector& textureCoordinates, const std::vector& indices); + Model(const std::vector& vertexPositions, + const std::vector& indices); + ~Model(); Model(const Model& other) = delete; Model& operator =(const Model& other) = delete; diff --git a/Source/ModelCoords.cpp b/Source/ModelCoords.cpp index 8ff1ef0..7696cc5 100755 --- a/Source/ModelCoords.cpp +++ b/Source/ModelCoords.cpp @@ -1,44 +1,44 @@ #include "ModelCoords.h" -std::vector getCubeVerticies(float scale) +std::vector getCubeVerticies(float posScale, float negScale) { return { //Back - 1 * scale, 0, 0, - 0, 0, 0, - 0, 1 * scale, 0, - 1 * scale, 1 * scale, 0, + 1 * posScale, -1 * negScale, -1 * negScale, + -1 * negScale, -1 * negScale, -1 * negScale, + -1 * negScale, 1 * posScale, -1 * negScale, + 1 * posScale, 1 * posScale, -1 * negScale, //Right-Side - 1 * scale, 0, 1 * scale, - 1 * scale, 0, 0, - 1 * scale, 1 * scale, 0, - 1 * scale, 1 * scale, 1 * scale, + 1 * posScale, -1 * negScale, 1 * posScale, + 1 * posScale, -1 * negScale, -1 * negScale, + 1 * posScale, 1 * posScale, -1 * negScale, + 1 * posScale, 1 * posScale, 1 * posScale, //Front - 0, 0, 1 * scale, - 1 * scale, 0, 1 * scale, - 1 * scale, 1 * scale, 1 * scale, - 0, 1 * scale, 1 * scale, + -1 * negScale, -1 * negScale, 1 * posScale, + 1 * posScale, -1 * negScale, 1 * posScale, + 1 * posScale, 1 * posScale, 1 * posScale, + -1 * negScale, 1 * posScale, 1 * posScale, //Left - 0, 0, 0, - 0, 0, 1 * scale, - 0, 1 * scale, 1 * scale, - 0, 1 * scale, 0, + -1 * negScale, -1 * negScale, -1 * negScale, + -1 * negScale, -1 * negScale, 1 * posScale, + -1 * negScale, 1 * posScale, 1 * posScale, + -1 * negScale, 1 * posScale, -1 * negScale, //Top - 0, 1 * scale, 1 * scale, - 1 * scale, 1 * scale, 1 * scale, - 1 * scale, 1 * scale, 0, - 0, 1 * scale, 0, + -1 * negScale, 1 * posScale, 1 * posScale, + 1 * posScale, 1 * posScale, 1 * posScale, + 1 * posScale, 1 * posScale, -1 * negScale, + -1 * negScale, 1 * posScale, -1 * negScale, //Bottom - 0, 0, 0, - 1 * scale, 0, 0, - 1 * scale, 0, 1 * scale, - 0, 0, 1 * scale + -1 * negScale, -1 * negScale, -1 * negScale, + 1 * posScale, -1 * negScale, -1 * negScale, + 1 * posScale, -1 * negScale, 1 * posScale, + -1 * negScale, -1 * negScale, 1 * posScale }; } diff --git a/Source/ModelCoords.h b/Source/ModelCoords.h index 58ca93d..982f0c9 100755 --- a/Source/ModelCoords.h +++ b/Source/ModelCoords.h @@ -11,7 +11,7 @@ struct ModelData std::vector indices; }; -std::vector getCubeVerticies (float scale = 1.0f); +std::vector getCubeVerticies (float posScale = 1.0f, float negScale = 0.0f); std::vector getCubeIndices (); #endif // MODEL_COORDS_H_INCLUDED diff --git a/Source/Renderer/RMaster.cpp b/Source/Renderer/RMaster.cpp index a3229f5..5d1db35 100755 --- a/Source/Renderer/RMaster.cpp +++ b/Source/Renderer/RMaster.cpp @@ -16,12 +16,11 @@ namespace Renderer void Master::update(const Camera& camera) { - ///@TODO This is starting to feel a bit bad - ///Another way to do this? Block::Database::get().getTextureAtlas().bind(); glEnable(GL_DEPTH_TEST); m_simpleRenderer.update(camera); + m_skyBoxRenderer.update(camera); glDisable(GL_DEPTH_TEST); m_sfmlRenderer.update (); @@ -38,4 +37,10 @@ namespace Renderer { m_simpleRenderer.draw(cube); } + + void Master::draw(const Texture::CubeTexture& tex) + { + m_skyBoxRenderer.draw(tex); + } + } diff --git a/Source/Renderer/RMaster.h b/Source/Renderer/RMaster.h index 209e1ec..fa94f0c 100755 --- a/Source/Renderer/RMaster.h +++ b/Source/Renderer/RMaster.h @@ -3,6 +3,7 @@ #include "RSFML.h" #include "RSimple.h" +#include "RSkyBox.h" struct Camera; @@ -19,9 +20,12 @@ namespace Renderer void draw(const sf::Drawable& drawable); void draw(const Cube& cube); + void draw(const Texture::CubeTexture& tex); + private: RSFML m_sfmlRenderer; RSimple m_simpleRenderer; + SkyBox m_skyBoxRenderer; }; } diff --git a/Source/Renderer/RSFML.cpp b/Source/Renderer/RSFML.cpp index fa77e2f..635b123 100755 --- a/Source/Renderer/RSFML.cpp +++ b/Source/Renderer/RSFML.cpp @@ -17,9 +17,13 @@ namespace Renderer glDisable(GL_DEPTH_TEST); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D, 0); + glBindTexture(GL_TEXTURE_CUBE_MAP, 0); + glUseProgram(0); + Display::get().getRaw().pushGLStates(); Display::get().getRaw().resetGLStates(); diff --git a/Source/Renderer/RSkyBox.cpp b/Source/Renderer/RSkyBox.cpp new file mode 100644 index 0000000..130e67b --- /dev/null +++ b/Source/Renderer/RSkyBox.cpp @@ -0,0 +1,115 @@ +#include "RSkyBox.h" + +#include "../Camera.h" +#include "../Texture/CubeTexture.h" + +#include "../ModelCoords.h" + +#include "../Maths/Matrix.h" + +#include + +float skyboxVertices[] = { + // positions + -1.0f, 1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + + 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + + -1.0f, -1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + + -1.0f, 1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, 1.0f +}; + +GLuint vbo; + +namespace Renderer +{ + SkyBox::SkyBox() + : m_shader("SkyBox", "SkyBox") + //, m_cube (getCubeVerticies(10.0f, 10.0f), getCubeIndices()) + { + glBindVertexArray(0); + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, + sizeof(skyboxVertices), + skyboxVertices, + GL_STATIC_DRAW); + + glVertexAttribPointer(0, + 3, + GL_FLOAT, + GL_FALSE, + 0, + (GLvoid*) 0); + + } + + void SkyBox::draw(const Texture::CubeTexture& cubeTex) + { + m_pCubeMap = &cubeTex; + } + + void SkyBox::update(const Camera& camera) + { + if(!m_pCubeMap) return; + + glDepthMask(GL_FALSE); + m_shader .bind(); //shader + //m_cube .bind(); //VAO + m_pCubeMap ->bind(); //texture + + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + + const auto& view = camera.getViewMatrix(); + const auto& proj = camera.getProjMatrix(); + + auto sView = Matrix4(glm::mat3(view)); + + m_shader.setProjViewMatrix (proj * sView); + + glDrawArrays(GL_TRIANGLES, 0, 36); + + //glDisable(GL_CULL_FACE); + //glDrawElements(GL_TRIANGLES, m_cube.getIndicesCount(), GL_UNSIGNED_INT, nullptr); + + glDepthMask(GL_TRUE); + m_pCubeMap = nullptr; + } +} diff --git a/Source/Renderer/RSkyBox.h b/Source/Renderer/RSkyBox.h new file mode 100644 index 0000000..303a86a --- /dev/null +++ b/Source/Renderer/RSkyBox.h @@ -0,0 +1,34 @@ +#ifndef RSKYBOX_H_INCLUDED +#define RSKYBOX_H_INCLUDED + +#include "../Shaders/SimpleShader.h" +#include "../Model.h" + +namespace Texture +{ + class CubeTexture; +} + +class Camera; + +namespace Renderer +{ + class SkyBox + { + public: + SkyBox(); + + void draw(const Texture::CubeTexture& cubeTex); + + void update(const Camera& camera); + + private: + Shader::Simple_Shader m_shader; + + Model m_cube; + + const Texture::CubeTexture* m_pCubeMap = nullptr; + }; +} + +#endif // RSKYBOX_H_INCLUDED diff --git a/Source/Shaders/SimpleShader.h b/Source/Shaders/SimpleShader.h index 6cdb393..404b311 100755 --- a/Source/Shaders/SimpleShader.h +++ b/Source/Shaders/SimpleShader.h @@ -9,14 +9,13 @@ namespace Shader { public: Simple_Shader(); - ~Simple_Shader() = default; + Simple_Shader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile); + virtual ~Simple_Shader() = default; void setProjViewMatrix (const Matrix4& matrix); void setModelMatrix (const Matrix4& matrix); protected: - Simple_Shader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile); - virtual void getUniformLocations() override; GLuint m_locationProjViewMatrix = 0; diff --git a/Source/Shaders/SkyBoxShader.cpp b/Source/Shaders/SkyBoxShader.cpp new file mode 100644 index 0000000..13c0b06 --- /dev/null +++ b/Source/Shaders/SkyBoxShader.cpp @@ -0,0 +1 @@ +#include "SkyBoxShader.h" diff --git a/Source/Shaders/SkyBoxShader.h b/Source/Shaders/SkyBoxShader.h new file mode 100644 index 0000000..1b4870d --- /dev/null +++ b/Source/Shaders/SkyBoxShader.h @@ -0,0 +1,4 @@ +#ifndef SKYBOXSHADER_H_INCLUDED +#define SKYBOXSHADER_H_INCLUDED + +#endif // SKYBOXSHADER_H_INCLUDED diff --git a/Source/States/Playing.cpp b/Source/States/Playing.cpp index fe111d9..a98fca5 100755 --- a/Source/States/Playing.cpp +++ b/Source/States/Playing.cpp @@ -15,6 +15,10 @@ #include "../GUI/BasicButton.h" #include "../GUI/Image.h" +#include "../Texture/CubeTexture.h" + +Texture::CubeTexture t; + namespace State { Playing::Playing(Application& application) @@ -25,6 +29,16 @@ namespace State , m_frameRate ("Frame", "FPS") , m_pauseMenu (GUI::Layout::Center) { + t.loadFromFile( + { + "Data/Textures/Skybox/Test/right.png", + "Data/Textures/Skybox/Test/left.png", + "Data/Textures/Skybox/Test/top.png", + "Data/Textures/Skybox/Test/bottom.png", + "Data/Textures/Skybox/Test/back.png", + "Data/Textures/Skybox/Test/front.png" + }); + application.getCamera().hookEntity(m_player); initHUD(); initPause(); @@ -101,6 +115,8 @@ namespace State m_hud.draw(renderer); + //renderer.draw(t); + static Toggle drawDebugHUD(sf::Keyboard::Key::F3, sf::seconds(0.5)); if (drawDebugHUD) { diff --git a/Source/Texture/Cube_Map.cpp b/Source/Texture/CubeTexture.cpp old mode 100755 new mode 100644 similarity index 51% rename from Source/Texture/Cube_Map.cpp rename to Source/Texture/CubeTexture.cpp index 1ca12d7..c107d42 --- a/Source/Texture/Cube_Map.cpp +++ b/Source/Texture/CubeTexture.cpp @@ -1,35 +1,55 @@ -#include "Cube_Map.h" +#include "CubeTexture.h" #include namespace Texture { - void Cube_Map::load(const std::array& textureFiles) + /** + ORDER: + right + left + top + bottom + back + front + **/ + void CubeTexture::loadFromFile(std::array&& fileNames) { - glGenTextures(1, &m_textureID); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureID); + glGenTextures (1, &m_textureID); + glBindTexture (GL_TEXTURE_CUBE_MAP, m_textureID); + glActiveTexture (GL_TEXTURE0); + + auto val = GL_TEXTURE_CUBE_MAP_POSITIVE_X; for (int i = 0; i < 6; i++) { sf::Image image; - image.loadFromFile(textureFiles[i]); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, + image.loadFromFile(fileNames[i]); + + auto current = val + i; + auto size = image.getSize(); + + glTexImage2D(current, 0, GL_RGB, - image.getSize().x, - image.getSize().y, + size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, image.getPixelsPtr()); } + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 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); - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); } + void CubeTexture::bind() const + { + glBindTexture (GL_TEXTURE_CUBE_MAP, m_textureID); + } } + + diff --git a/Source/Texture/CubeTexture.h b/Source/Texture/CubeTexture.h new file mode 100644 index 0000000..7f8f028 --- /dev/null +++ b/Source/Texture/CubeTexture.h @@ -0,0 +1,22 @@ +#ifndef CUBETEXTURE_H_INCLUDED +#define CUBETEXTURE_H_INCLUDED + +#include +#include +#include + +namespace Texture +{ + class CubeTexture + { + public: + void loadFromFile(std::array&& fileNames); + + void bind() const; + + private: + GLuint m_textureID; + }; +} + +#endif // CUBETEXTURE_H_INCLUDED diff --git a/Source/Texture/Cube_Map.h b/Source/Texture/Cube_Map.h deleted file mode 100755 index ab5e2f5..0000000 --- a/Source/Texture/Cube_Map.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef CUBE_MAP_H_INCLUDED -#define CUBE_MAP_H_INCLUDED - -#include -#include -#include - -#include "../Maths/GLM.h" - -namespace Texture -{ - - class Cube_Map - { - public: - void load(const std::array& textureFiles); - - private: - GLuint m_textureID; - }; - -} - -#endif // CUBE_MAP_H_INCLUDED diff --git a/Source/Util/FileUtil.cpp b/Source/Util/FileUtil.cpp index 5ea745c..3074e26 100755 --- a/Source/Util/FileUtil.cpp +++ b/Source/Util/FileUtil.cpp @@ -5,8 +5,6 @@ #include -#include "Native.h" - /** Because this uses experimental file-system lib (At this time, I can't seem to get GCC 7 to download), you must add compiler option: @@ -34,10 +32,9 @@ std::vector getFilePathsFromFolder(const std::string& folderName) for (auto& entry : DirItr(folderName)) { - if (std::experimental::filesystem::is_regular_file(entry)) + if (FS::is_regular_file(entry)) { fileNames.push_back(Path(entry)); - std::cout << Path(entry); } }