立方体纹理(TextureCube)

立方体纹理是放置在立方体各个表面上的六个单独的方形纹理的集合。大多数情况下,它们用于在物体上显示无限远的反射,类似于天空盒在背景中显示远处的风景。一个展开的立方体纹理可能是这样的:

立方体纹理(TextureCube)  - 图1

在 Cocos2d-x 中,这样创建立方体纹理:

  1. // create a textureCube object with six texture assets
  2. auto textureCube = TextureCube::create("skybox/left.jpg", "skybox/right.jpg", "skybox/top.jpg", "skybox/bottom.jpg", "skybox/front.jpg", "skybox/back.jpg");
  3. // set cube map texture parameters
  4. Texture2D::TexParams tRepeatParams;
  5. tRepeatParams.magFilter = backend::SamplerFilter::LINEAR;;
  6. tRepeatParams.minFilter = backend::SamplerFilter::LINEAR;;
  7. tRepeatParams.sAddressMode = backend::SamplerAddressMode::MIRROR_REPEAT;
  8. tRepeatParams.tAddressMode = backend::SamplerAddressMode::MIRROR_REPEAT;
  9. textureCube->setTexParameters(tRepeatParams);
  10. // create and set our custom shader
  11. auto vertShader = FileUtils::getStringFromFile("cube_map.vert");
  12. auto fragShader = FileUtils::getStringFromFile("cube_map.frag");
  13. auto programState = new backend::ProgramState(vertShader.c_str(), fragShader.c_str());
  14. // bind cube map texture to uniform
  15. auto cubTexLoc = programState->getUniformLocation("u_cubeTex");
  16. programState->setTexture(cubTexLoc ,0, textureCube->getBackendTexture());