OpenGL CubeMapping黑色纹理

OpenGL CubeMapping black texture

本文关键字:纹理 黑色 CubeMapping OpenGL      更新时间:2023-10-16

所以我试图得到一个立方体地图在openGL工作,但不断提出黑色纹理。我希望其他人能指出我的错误。模型确实出现了,我可以在片段着色器中改变它的颜色。但是当我尝试使用纹理时,它看起来是黑色的,好像它没有绑定。

bool result = true;
result &= shader.loadFromFile("Assets\Shader\skybox.vert", "Assets\Shader\skyBox.Frag");
result &= shader.compileShader();
result &= shader.linkShader();
result &= shader.validate();
if (!result){
    printf("ShaderFail");
}
shader.use();
glm::mat4 model = glm::mat4(1.0);
glm::mat4 rotationMatrix = glm::mat4_cast(glm::quat(glm::vec3(0, 0, 0)));
glm::mat4 translationMatrix = glm::translate(glm::vec3(10, 10, 0));
glm::mat4 scaleMatrix = glm::scale(glm::vec3(1, 1, 1));
model = rotationMatrix * translationMatrix * scaleMatrix;
shader.setUniform("M", model);
cube2 = new VBOCube();
Bitmap bmp1 = Bitmap::bitmapFromFile("Assets\Images\TowerHousepano_r.jpg");
Bitmap bmp2 = Bitmap::bitmapFromFile("Assets\Images\TowerHousepano_l.jpg");
Bitmap bmp3 = Bitmap::bitmapFromFile("Assets\Images\TowerHousepano_u.jpg");
Bitmap bmp4 = Bitmap::bitmapFromFile("Assets\Images\TowerHousepano_d.jpg");
Bitmap bmp5 = Bitmap::bitmapFromFile("Assets\Images\TowerHousepano_f.jpg");
Bitmap bmp6 = Bitmap::bitmapFromFile("Assets\Images\TowerHousepano_b.jpg");


glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
/// Applies the images to each side of the map.
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, bmp1.width(), bmp1.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, bmp1.pixelBuffer());
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, bmp2.width(), bmp2.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, bmp2.pixelBuffer());
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, bmp3.width(), bmp3.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, bmp3.pixelBuffer());
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, bmp4.width(), bmp4.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, bmp4.pixelBuffer());
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, bmp5.width(), bmp5.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, bmp5.pixelBuffer());
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, bmp6.width(), bmp6.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, bmp6.pixelBuffer());
/// Applies CLAMPING and sets bilinear texture filtering.
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 Skybox::render(Camera& camera){

    glEnable(GL_DEPTH_TEST);
    shader.use();
    glBindTexture(GL_TEXTURE_CUBE_MAP, texID);
    shader.setUniform("V", camera.view());
    shader.setUniform("P", camera.projection());
    cube2->render();
    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
    glDisable(GL_DEPTH_TEST);
}

顶点着色器:

#version 400
layout (location = 0) in vec3 vp; 
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
out vec3 texcoords;

void main () {
  texcoords = vp;
  gl_Position = P * V* M *  vec4 (vp, 1.0);
}

片段着色器:

#version 400
in vec3 texcoords; 
uniform samplerCube cubeTexture;
out vec4 FragColour;
void main () {
   vec4 cubeMapColour = texture (cubeTexture, texcoords);
   FragColour = cubeMapColour;
}

当我正在查看我自己的帖子时,我发现了这个问题。

我使用GL_TEXTURE_CUBE_MAP_POSITIVE_Y两次,而不是GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,它现在工作。