在 OpenGL 中将四边形堆叠在一起

Layering quads on top of each other in OpenGL

本文关键字:在一起 四边形 OpenGL      更新时间:2023-10-16

我正在使用 OpenGL 使用 C++ 创建一个基于 2D 瓷砖的战术 RPG,我在分层我的瓷砖/四边形时遇到了困难。我想能够说一个树纹理四边形,图像是树的周围有一个透明的阿尔法层,在一个不透明的草纹理四边形之上。我需要让树出现在草的顶部,草通过树图像的 alpha 层显示。

到目前为止,我一直在搞砸glDisable(GL_DEPTH_TEST),glEnable(GL_BLEND),和glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA),但到目前为止没有运气。我最终得到了一棵黑色背景上的树,而不是草地瓷砖。有人可以指出我正确的方向吗?

下面是我的渲染函数和初始化函数,这可能是最相关的。

void View::initialize() {
  updateProjection(window);
  glDisable(GL_DEPTH_TEST);
  glClearColor(0.0, 0.0, 0.0, 1.0);
  camera = new Camera(Vector3(-1, -3, 25), Vector3(-1, -3, 0), Vector3(0, 1, 0));
  loadImages();
  initShaders();
  //needed for transparency?
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void View::render(World* worldTemp)
{
  Matrix4 mvpMatrix, viewMatrix, modelMatrix;
  Matrix4 XAxisRotationMatrix, YAxisRotationMatrix, ZAxisRotationMatrix;
  input->handleInput(camera, worldTemp->getPlayer());
  worldTemp->timerTick();
  worldTemp->clearFog();
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);            // Clear Screen and Depth Buffer
  XAxisRotationMatrix = Matrix4::IDENTITY;
  YAxisRotationMatrix = Matrix4::IDENTITY;
  ZAxisRotationMatrix = Matrix4::IDENTITY;
  XAxisRotationMatrix.rotate(Vector3(1.0, 0.0, 0.0), XAxisRotationAngle);
  YAxisRotationMatrix.rotate(Vector3(0.0, 1.0, 0.0), YAxisRotationAngle);
  ZAxisRotationMatrix.rotate(Vector3(0.0, 0.0, 1.0), ZAxisRotationAngle);
  viewMatrix = camera->getViewMatrix();
  modelMatrix = translationMatrix(Vector3(-4, 2, 0));
  mvpMatrix = projectionMatrix * viewMatrix * modelMatrix;
  //Spit out the map
  for (int i = 0; i < 100; i++){
      for (int j = 0; j < 100; j++){
          for (int t = 0; t < 5; t++){
              if (worldTemp->getTile(i, j)->isOccupied() == true) {
                  if (worldTemp->getTile(i, j)->getOccupyingEntityIndexed(t)->getFog()){
                    worldTemp->getTile(i, j)->getOccupyingEntityIndexed(t)->getEntityQuad()->render_self(mvpMatrix, true);
                  }
                  else{
                    worldTemp->getTile(i, j)->getOccupyingEntityIndexed(t)->getEntityQuad()->render_self(mvpMatrix);
                  }
              }
          }
      }
  }
  //Place the player
  worldTemp->getPlayer()->getEntityQuad()->render_self(mvpMatrix);
  renderEnemies();
  glutSwapBuffers(); //works with GL_DOUBLE. use glFlush(); instead, if using GL_SINGLE
}

基本上,在 2D 游戏中,分层是通过渲染调用的排序完成的。如果要在 B 层之上添加 A,则应先渲染 B 层,然后再渲染 A 层。

您使用的混合功能应取决于图像的纹理格式。alpha 有两种常见的格式:

  1. 预乘阿尔法

  2. 直阿尔法

    更多信息: https://developer.nvidia.com/content/alpha-blending-pre-or-not-pre

OpenGL ES2 预乘与直接 alpha + 混合

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

用于预乘的 alpha,如果您按原样使用颜色,它应该可以很好地工作。 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 适用于直接阿尔法。