OpenGL CubeMapping黑色纹理

时间:2015-10-06 10:02:03

标签: c++ opengl glsl shader

所以我试图让一个立方体贴图在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);
}

Fragment Shader:

#version 400

in vec3 texcoords; 

uniform samplerCube cubeTexture;

out vec4 FragColour;

void main () {
   vec4 cubeMapColour = texture (cubeTexture, texcoords);
   FragColour = cubeMapColour;
}

1 个答案:

答案 0 :(得分:0)

当我看到自己的帖子时,我发现了问题。

我使用了GL_TEXTURE_CUBE_MAP_POSITIVE_Y两次而不是GL_TEXTURE_CUBE_MAP_NEGATIVE_Y现在可以使用。

相关问题