Hello,
So I implemented skyboxes in the environment I am building and so far success with the exception of a very small seam appearing on the edges.
How do I get rid of the seams?
Image:
Here are the basics for how I am creating the SkyBox.
Geometry:
Vertices = new GLfloat[72]
{
// Front face
50.0f, 50.0f, 50.0f,
50.0f, -50.0f, 50.0f,
-50.0f, 50.0f, 50.0f,
-50.0f, -50.0f, 50.0f,
// Back face
-50.0f, 50.0f, -50.0f,
-50.0f, -50.0f, -50.0f,
50.0f, 50.0f, -50.0f,
50.0f, -50.0f, -50.0f,
// Left face
-50.0f, 50.0f, 50.0f,
-50.0f, -50.0f, 50.0f,
-50.0f, 50.0f, -50.0f,
-50.0f, -50.0f, -50.0f,
// Right face
50.0f, 50.0f, -50.0f,
50.0f, -50.0f, -50.0f,
50.0f, 50.0f, 50.0f,
50.0f, -50.0f, 50.0f,
// Top face
-50.0f, 50.0f, -50.0f,
50.0f, 50.0f, -50.0f,
-50.0f, 50.0f, 50.0f,
50.0f, 50.0f, 50.0f,
// Bottom face
50.0f, -50.0f, -50.0f,
-50.0f, -50.0f, -50.0f,
50.0f, -50.0f, 50.0f,
-50.0f, -50.0f, 50.0f
};
NumOfVertices = 72;
Normals = new GLfloat[72] {
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
NumOfNormals = 72;
TextureCoordinates = new GLfloat[48] {
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
NumOfTextureCoordinates = 48;
ModelMatrix = glm::mat4(1.0f);
Textures:
GLuint WidthOfTexture = 0;
GLuint HeightOfTexture = 0;
GLuint BytesPerPixel = 0;
FIBITMAP *loadedImage = NULL;
FREE_IMAGE_FORMAT CurrentFormat = FIF_UNKNOWN;
CurrentFormat = FreeImage_GetFileType(PathToTexture->c_str(), 0);
if (CurrentFormat == FIF_UNKNOWN) {
FREE_IMAGE_FORMAT CurrentFormat = FreeImage_GetFIFFromFilename(PathToTexture->c_str());
}
if (CurrentFormat == FIF_UNKNOWN) {
return false;
}
if (FreeImage_FIFSupportsReading(CurrentFormat)) {
loadedImage = FreeImage_Load(CurrentFormat, PathToTexture->c_str());
}
if (!loadedImage) {
return false;
}
BYTE* bytesDataPointer = FreeImage_GetBits(loadedImage);
WidthOfTexture = FreeImage_GetWidth(loadedImage);
HeightOfTexture = FreeImage_GetHeight(loadedImage);
BytesPerPixel = FreeImage_GetBPP(loadedImage);
if (WidthOfTexture == 0 || HeightOfTexture == 0 || BytesPerPixel == 0) {
return false;
}
glGenTextures(1, &TextureObject_ID); // section being edited by CKoeber to pull in material files
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, TextureObject_ID);
GLsizei iFormat = BytesPerPixel == 24 ? GL_BGR : BytesPerPixel == 8 ? GL_LUMINANCE : 0;
GLsizei iInternalFormat = BytesPerPixel == 24 ? GL_RGB : GL_DEPTH_COMPONENT;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
WidthOfTexture, HeightOfTexture, 0, iFormat,
GL_UNSIGNED_BYTE, bytesDataPointer);
glGenerateMipmap(GL_TEXTURE_2D);
FreeImage_Unload(loadedImage);
glGenSamplers(1, &TextureSamplerObject_ID);
/*
We clamp to edge here versus wrapping for SkyBoxes.
*/
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
/*
End clamp.
*/
setFiltering(TEXTURE_FILTER_MAG_BILINEAR, TEXTURE_FILTER_MIN_BILINEAR);
glBindTexture(GL_TEXTURE_2D, NULL);
glActiveTexture(GL_TEXTURE0);
return true;
Loading Geometry into OpenGL:
glGenVertexArrays(1, &VertextArrayObjectID);
glBindVertexArray(VertextArrayObjectID);
if (NumOfTextureCoordinates > 0 && NumOfNormals > 0) {
GLuint TotalBufferData = (sizeof(GLfloat)* NumOfVertices) +
(sizeof(GLfloat)* NumOfNormals) +
(sizeof(GLfloat)* NumOfTextureCoordinates);
glGenBuffers(1, &MainBufferID);
glBindBuffer(GL_ARRAY_BUFFER, MainBufferID);
glBufferData(GL_ARRAY_BUFFER, TotalBufferData, NULL, GL_STATIC_DRAW);
}
else if (NumOfNormals > 0) {
GLuint TotalBufferData = (sizeof(GLfloat)* NumOfVertices) +
(sizeof(GLfloat)* NumOfNormals);
glGenBuffers(1, &MainBufferID);
glBindBuffer(GL_ARRAY_BUFFER, MainBufferID);
glBufferData(GL_ARRAY_BUFFER, TotalBufferData, NULL, GL_STATIC_DRAW);
}
else {
GLuint TotalBufferData = (sizeof(GLfloat)* NumOfVertices);
glGenBuffers(1, &MainBufferID);
glBindBuffer(GL_ARRAY_BUFFER, MainBufferID);
glBufferData(GL_ARRAY_BUFFER, TotalBufferData, NULL, GL_STATIC_DRAW);
}
glBufferSubData(GL_ARRAY_BUFFER, NULL, sizeof(GLfloat)* NumOfVertices, Vertices);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
if (NumOfNormals > 0) {
glBufferSubData(GL_ARRAY_BUFFER,
sizeof(GLfloat)* NumOfVertices,
sizeof(GLfloat)* NumOfNormals,
Normals);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(sizeof(GLfloat)* NumOfVertices));
glEnableVertexAttribArray(1);
}
if (NumOfTextureCoordinates > 0) {
glBufferSubData(GL_ARRAY_BUFFER,
(sizeof(GLfloat)* NumOfVertices) + (sizeof(GLfloat)* NumOfNormals),
sizeof(GLfloat)* NumOfTextureCoordinates,
TextureCoordinates);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0,
(GLvoid*)((sizeof(GLfloat)* NumOfVertices) + (sizeof(GLfloat)* NumOfNormals)));
glEnableVertexAttribArray(2);
}
glBindVertexArray(NULL);
And drawing:
ModelMatrix = glm::mat4(1.0f);
ModelMatrix = glm::translate(ModelMatrix, glm::vec3(glm::inverse((*ViewMatrix))[3]));
MVPMatrix = (*ProjectionMatrix) * (*ViewMatrix) * ModelMatrix;
NormalMatrix = glm::transpose(glm::inverse(glm::mat3(MVPMatrix)));
glBindVertexArray(VertextArrayObjectID);
glUniformMatrix4fv((*AssociatedOpenGLProgram->GetMVPMatrixID()), 1, GL_FALSE, glm::value_ptr(MVPMatrix));
glUniformMatrix3fv((*AssociatedOpenGLProgram->GetNormalMatrixID()), 1, GL_FALSE, glm::value_ptr(NormalMatrix));
AssociatedOpenGLProgram->SetDrawingSkyBox(true);
glDepthMask(NULL);
for (GLuint i = 0; i < 6; ++i)
{
glUniform1i((*AssociatedOpenGLProgram->GetTextureSamplerID()), 1);
SkyBoxMaterials[i]->BindTexture(1);
glDrawArrays(GL_TRIANGLE_STRIP, i * 4, 4);
}
glBindTexture(GL_TEXTURE_2D, NULL);
AssociatedOpenGLProgram->SetDrawingSkyBox(false);
glDepthMask(1);