在 OpenGL 中将四边形堆叠在一起
Layering quads on top of each other in OpenGL
我正在使用 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 有两种常见的格式:
-
预乘阿尔法
-
直阿尔法
更多信息: 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); 适用于直接阿尔法。
相关文章:
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 将图形属性与 std::unique_ptr 捆绑在一起
- 你能在 c++ 中将不同的数字类型加在一起吗?
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 为什么push_back和emplace_back结合在一起时,会有不同的行为
- 在浮点精度成为一个问题之前,可以将多少个浮点值加在一起
- 为什么在template函数广播中把两个extensor表达式加在一起不正确
- 实现具有浮点键的类似哈希表的数据结构,其中公差内的值被合并在一起
- 如何在Qt TableView中将列的宽度调整为数据并将最后一部分拉伸在一起
- 编译器如何将链表中的地址字符串在一起?
- 在 c++ 中,两个日志行与 log4Cxx 混合在一起
- 如何将两个 jlong 数据类型转换为 jstring,然后将两个字符串连接在一起以便从 JNI 将字符串返回给 jav
- 将许多向量排序在一起
- C++ std:: 并包括它们如何组合在一起?
- 通过字符串迭代并将每个循环中的先前字符组合在一起
- CMake 项目结构:如何正确地将库合并在一起并将它们包含在多个可执行文件中
- C 操作员超载将3个向量加在一起
- 有没有一种干净(更)的方法将 CRTP 与可变参数继承混合在一起?
- 在 OpenGL 中将四边形堆叠在一起