简体   繁体   中英

OpenGL CubeMapping black texture

So I am trying to get a cube map to work in openGL, but keep coming up with black textures. I hope some other eyes can spot where I am going wrong. The model does appear and I can change its colour in the fragment shader. But when I try and use the texture it appears black as if it isn't binding.

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);

}

Vertex Shader:

#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;
}

As I was just looking over my own post I spotted the problem.

I used GL_TEXTURE_CUBE_MAP_POSITIVE_Y twice and not GL_TEXTURE_CUBE_MAP_NEGATIVE_Y it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM