diff --git a/data/shaders/sky.frag b/data/shaders/sky.frag index 89cb639d5..eb339c8a2 100644 --- a/data/shaders/sky.frag +++ b/data/shaders/sky.frag @@ -4,7 +4,7 @@ out vec4 FragColor; void main(void) { - vec3 eyedir = gl_FragCoord.xyz / vec3(screen, 1.); + vec3 eyedir = vec3(gl_FragCoord.xy / screen, 1.); eyedir = 2.0 * eyedir - 1.0; vec4 tmp = (InverseProjectionMatrix * vec4(eyedir, 1.)); tmp /= tmp.w; diff --git a/data/shaders/sky.vert b/data/shaders/sky.vert new file mode 100644 index 000000000..eb254d521 --- /dev/null +++ b/data/shaders/sky.vert @@ -0,0 +1,10 @@ +#if __VERSION__ >= 330 +layout(location = 0) in vec3 Position; +#else +in vec3 Position; +#endif + +void main() +{ + gl_Position = vec4(Position, 1.); +} diff --git a/src/graphics/render_skybox.cpp b/src/graphics/render_skybox.cpp index 59bcc6524..967e756b8 100644 --- a/src/graphics/render_skybox.cpp +++ b/src/graphics/render_skybox.cpp @@ -380,27 +380,18 @@ void IrrDriver::renderSkybox(const scene::ICameraSceneNode *camera) { if (SkyboxTextures.empty()) return; - glBindVertexArray(MeshShader::SkyboxShader::getInstance()->cubevao); glEnable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); assert(SkyboxTextures.size() == 6); - core::matrix4 translate; - translate.setTranslation(camera->getAbsolutePosition()); - - // Draw the sky box between the near and far clip plane - const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f; - core::matrix4 scale; - scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance)); - core::matrix4 transform = translate * scale; - core::matrix4 invtransform; - transform.getInverse(invtransform); glDisable(GL_BLEND); glUseProgram(MeshShader::SkyboxShader::getInstance()->Program); - MeshShader::SkyboxShader::getInstance()->setUniforms(transform); + glBindVertexArray(MeshShader::SkyboxShader::getInstance()->vao); + MeshShader::SkyboxShader::getInstance()->setUniforms(); + MeshShader::SkyboxShader::getInstance()->SetTextureUnits(SkyboxCubeMap); - glDrawElements(GL_TRIANGLES, 6 * 6, GL_UNSIGNED_INT, 0); + glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); } diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 18c931c2f..6cec2fb5b 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -336,66 +336,19 @@ static void initBillboardVBO() glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), quad, GL_STATIC_DRAW); } -GLuint SharedObject::cubevbo = 0; -GLuint SharedObject::cubeindexes = 0; +GLuint SharedObject::skytrivbo = 0; -static void initCubeVBO() +static void initSkyTriVBO() { - // From CSkyBoxSceneNode - float corners[] = - { - // top side - 1., 1., -1., - 1., 1., 1., - -1., 1., 1., - -1., 1., -1., + const float tri_vertex[] = { + -1., -1., 1., + -1., 3., 1., + 3., -1., 1., + }; - // Bottom side - 1., -1., 1., - 1., -1., -1., - -1., -1., -1., - -1., -1., 1., - - // right side - 1., -1, -1, - 1., -1, 1, - 1., 1., 1., - 1., 1., -1., - - // left side - -1., -1., 1., - -1., -1., -1., - -1., 1., -1., - -1., 1., 1., - - // back side - -1., -1., -1., - 1., -1, -1., - 1, 1, -1., - -1, 1, -1., - - // front side - 1., -1., 1., - -1., -1., 1., - -1, 1., 1., - 1., 1., 1., - }; - int indices[] = { - 0, 1, 2, 2, 3, 0, - 4, 5, 6, 6, 7, 4, - 8, 9, 10, 10, 11, 8, - 12, 13, 14, 14, 15, 12, - 16, 17, 18, 18, 19, 16, - 20, 21, 22, 22, 23, 20 - }; - - glGenBuffers(1, &SharedObject::cubevbo); - glBindBuffer(GL_ARRAY_BUFFER, SharedObject::cubevbo); - glBufferData(GL_ARRAY_BUFFER, 6 * 4 * 3 * sizeof(float), corners, GL_STATIC_DRAW); - - glGenBuffers(1, &SharedObject::cubeindexes); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedObject::cubeindexes); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * 6 * sizeof(int), indices, GL_STATIC_DRAW); + glGenBuffers(1, &SharedObject::skytrivbo); + glBindBuffer(GL_ARRAY_BUFFER, SharedObject::skytrivbo); + glBufferData(GL_ARRAY_BUFFER, 3 * 3 * sizeof(float), tri_vertex, GL_STATIC_DRAW); } GLuint SharedObject::frustrumvbo = 0; @@ -535,7 +488,7 @@ void Shaders::loadShaders() initQuadVBO(); initQuadBuffer(); initBillboardVBO(); - initCubeVBO(); + initSkyTriVBO(); initFrustrumVBO(); initShadowVPMUBO(); initLightingDataUBO(); @@ -1417,17 +1370,16 @@ namespace MeshShader SkyboxShader::SkyboxShader() { Program = LoadProgram(OBJECT, - GL_VERTEX_SHADER, file_manager->getAsset("shaders/object_pass.vert").c_str(), + GL_VERTEX_SHADER, file_manager->getAsset("shaders/sky.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/sky.frag").c_str()); - AssignUniforms("ModelMatrix"); + AssignUniforms(); AssignSamplerNames(Program, 0, "tex"); - glGenVertexArrays(1, &cubevao); - glBindVertexArray(cubevao); - glBindBuffer(GL_ARRAY_BUFFER, SharedObject::cubevbo); + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, SharedObject::skytrivbo); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedObject::cubeindexes); glBindVertexArray(0); } diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index a040e4e76..cda475f79 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -30,7 +30,7 @@ class SharedObject { public: static GLuint billboardvbo; - static GLuint cubevbo, cubeindexes, frustrumvbo, frustrumindexes, ParticleQuadVBO; + static GLuint skytrivbo, frustrumvbo, frustrumindexes, ParticleQuadVBO; static GLuint ViewProjectionMatrixesUBO, LightingDataUBO; static GLuint FullScreenQuadVAO; static GLuint UIVAO; @@ -281,11 +281,11 @@ public: DisplaceShader(); }; -class SkyboxShader : public ShaderHelperSingleton, public TextureRead +class SkyboxShader : public ShaderHelperSingleton, public TextureRead { public: SkyboxShader(); - GLuint cubevao; + GLuint vao; }; class NormalVisualizer : public ShaderHelperSingleton