Hi! I’m trying to port my opengl code to vulkan. I followed this tutorial and worked with no problem for openg : https://learnopengl.com/Advanced-OpenGL/Cubemaps
But with vulkan my skybox is clipped :
I use 1.0 for near value and 1000 for far value.
I define my skybox vertices like this :
Skybox::Skybox(std::vector<std::string> filepaths, EntityFactory& factory, window::Device& vkDevice) : GameObject(math::Vec3f(0, 0, 0),math::Vec3f(2, 2, 2),math::Vec3f(1,1,1),"E_SKYBOX", factory), vkDevice(vkDevice), skyboxCM(vkDevice) {
skyboxCM.loadCubeMapFromFile(filepaths);
VertexArray va1(sf::Quads, 4, this);
Vertex v1(Vector3f(1, -1, -1));
Vertex v2(Vector3f(1, 1, -1));
Vertex v3(Vector3f(1, 1, 1));
Vertex v4(Vector3f(1, -1, 1));
va1[0] = v1;
va1[1] = v2;
va1[2] = v3;
va1[3] = v4;
Material material1;
material1.addTexture(nullptr, sf::IntRect(0, 0, 0, 0));
Face face1 (va1, material1, getTransform());
//Gauche.
VertexArray va2(sf::Quads, 4, this);
Vertex v5(Vector3f(-1, -1, -1));
Vertex v6(Vector3f(-1, 1, -1));
Vertex v7(Vector3f(-1, 1, 1));
Vertex v8(Vector3f(-1, -1, 1));
va2[0] = v5;
va2[1] = v6;
va2[2] = v7;
va2[3] = v8;
Material material2;
material2.addTexture(nullptr, sf::IntRect(0, 0, 0, 0));
Face face2 (va2, material2, getTransform());
//Dessus
VertexArray va3(sf::Quads, 4, this);
Vertex v9(Vector3f(-1, 1, -1));
Vertex v10(Vector3f(1, 1, -1));
Vertex v11(Vector3f(1, 1, 1));
Vertex v12(Vector3f(-1, 1, 1));
va3[0] = v9;
va3[1] = v10;
va3[2] = v11;
va3[3] = v12;
Material material3;
material3.addTexture(nullptr, sf::IntRect(0, 0, 0, 0));
Face face3 (va3, material3, getTransform());
//Dessous.
VertexArray va4(sf::Quads, 4, this);
Vertex v13(Vector3f(-1, -1, -1));
Vertex v14(Vector3f(1, -1, -1));
Vertex v15(Vector3f(1, -1, 1));
Vertex v16(Vector3f(-1, -1, 1));
va4[0] = v13;
va4[1] = v14;
va4[2] = v15;
va4[3] = v16;
Material material4;
material4.addTexture(nullptr, sf::IntRect(0, 0, 0, 0));
Face face4 (va4, material4, getTransform());
for (unsigned int i = 0; i < face4.getVertexArray().getVertexCount(); i++) {
std::cout<<"vertex position : "<<face4.getVertexArray()[i].position.x<<std::endl;
}
//Devant
VertexArray va5(sf::Quads, 4, this);
Vertex v17(Vector3f(-1, -1, 1));
Vertex v18(Vector3f(1, -1, 1));
Vertex v19(Vector3f(1, 1, 1));
Vertex v20(Vector3f(-1, 1, 1));
va5[0] = v17;
va5[1] = v18;
va5[2] = v19;
va5[3] = v20;
Material material5;
material5.addTexture(nullptr, sf::IntRect(0, 0, 0, 0));
Face face5 (va5, material5, getTransform());
//Derrière.
VertexArray va6(sf::Quads, 4, this);
Vertex v21(Vector3f(-1, -1, -1));
Vertex v22(Vector3f(1, -1, -1));
Vertex v23(Vector3f(1, 1, -1));
Vertex v24(Vector3f(-1, 1, -1));
va6[0] = v21;
va6[1] = v22;
va6[2] = v23;
va6[3] = v24;
Material material6;
material6.addTexture(nullptr, sf::IntRect(0, 0, 0, 0));
Face face6 (va6, material6, getTransform());
addFace(face1);
addFace(face2);
addFace(face3);
addFace(face4);
addFace(face5);
addFace(face6);
}
I remove the vertices transform to my skybox being centered with the camera.
In my shader, I remove the view translation :
const std::string skyboxVertexShader = R"(#version 460
#extension GL_EXT_debug_printf : enable
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec4 color;
layout (location = 2) in vec2 texCoords;
layout (location = 3) in vec3 normals;
layout(location = 0) out vec4 frontColor;
layout(location = 1) out vec3 fTexCoords;
layout(location = 2) out vec3 normal;
layout (push_constant) uniform PushConsts {
mat4 projectionMatrix;
mat4 viewMatrix;
} pushConsts;
void main()
{
gl_PointSize = 2.0f;
frontColor = color;
normal = normals;
fTexCoords = aPos;
gl_Position = pushConsts.projectionMatrix * mat4(mat3(pushConsts.viewMatrix)) * vec4(aPos, 1.0);
//debugPrintfEXT("cubemap vertex shader %v4f", gl_Position);
}
)";
My perspective projection matrix is defined like this :
void ProjMatrix::setGlPerspectiveMatrix (double l, double r, double b, double t, double n, double f) {
matrix4f[0][0] = (2 * n) / (r - l);
matrix4f[0][1] = 0;
matrix4f[0][2] = (r + l) / (r - l);
matrix4f[0][3] = 0;
matrix4f[1][0] = 0;
#ifndef VULKAN
matrix4f[1][1] = (2 * n) / (t - b);
matrix4f[1][2] = (t + b) / (t - b);
#else
matrix4f[1][1] = -(2 * n) / (t - b);
matrix4f[1][2] = -(t + b) / (t - b);
#endif
matrix4f[1][3] = 0;
matrix4f[2][0] = 0;
matrix4f[2][1] = 0;
#ifndef VULKAN
matrix4f[2][2] = ((f + n) / (f - n));
matrix4f[2][3] = (2 * f * n) / (f - n);
#else
matrix4f[2][2] = f / (f - n);
matrix4f[2][3] = -f * n / (f - n);
#endif
matrix4f[3][0] = 0;
matrix4f[3][1] = 0;
#ifndef VULKAN
matrix4f[3][2] = -1;
#else
matrix4f[3][2] = 1;
#endif
matrix4f[3][3] = 0;
this->l = l;
this->r = r;
this->b = b;
this->t = t;
this->n = n;
this->f = f;
invMatrix4f = matrix4f.inverse();
}
I searched for the whole day and I can’t figure out why my skybox is clipped and how to solve this issue. I guess this is vulkan use another coordinates system but if I set a too small near value, it’s clipped even more and if I set a greater value my scene objects are clipped. If I made a skybox of a too small size it’s clipped bug with a bigger size it’s also clipped.
What’s wrong with my code ?
Thanks.
