0
所以我試圖讓一個立方體貼圖在openGL中工作,但不斷提出黑色紋理。我希望其他的一些眼睛能夠發現我出錯的地方。 該模型確實出現,我可以在片段着色器中更改它的顏色。但是當我嘗試使用紋理時,它看起來像黑色一樣沒有約束力。OpenGL CubeMapping黑色紋理
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;
}