c++ - 如何在 OpenGL 中设置顶点的不透明度?

标签 c++ opengl transparency opacity

以下代码片段绘制了一个灰色方 block 。

glColor3b(50, 50, 50);

glBegin(GL_QUADS);
glVertex3f(-1.0, +1.0, 0.0); // top left
glVertex3f(-1.0, -1.0, 0.0); // bottom left
glVertex3f(+1.0, -1.0, 0.0); // bottom right
glVertex3f(+1.0, +1.0, 0.0); // top right
glEnd();

在我的应用程序中,这个正方形后面有一个彩色立方体。

我应该使用什么函数使正方形(并且只有这个正方形)不透明?

最佳答案

在 init 函数中,使用这两行:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

并且在您的渲染函数中,确保使用 glColor4f 而不是 glColor3f,并将第四个参数设置为所需的不透明度级别。

glColor4f(1.0, 1.0, 1.0, 0.5);

glBegin(GL_QUADS);
glVertex3f(-1.0, +1.0, 0.0); // top left
glVertex3f(-1.0, -1.0, 0.0); // bottom left
glVertex3f(+1.0, -1.0, 0.0); // bottom right
glVertex3f(+1.0, +1.0, 0.0); // top right
glEnd();

关于c++ - 如何在 OpenGL 中设置顶点的不透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/721705/

相关文章:

java - 忽略透明部分的图像的 2D 碰撞检测

ios - 使我的 UIWebView 的背景透明失败

c++ - 分辨率有区别吗?

c++ - Boost 多索引容器与基于 std::unordered_map( map 的 map )的多级映射容器

c++ - main.cpp :-1: error: undefined reference to `cvLoadImage'

c++ - 在 opengl 中动画由立方体制成的模型

c++ - 使用来自不同线程的 opengl-command

c++ - 与 Visual Studio 2010 中的 MD 相比,无法在 MT 模式下编译简单的 Qt 程序

c - 学习 OpenGL 需要具备扎实的数学基础吗?

inheritance - 重置继承的不透明度/过滤器 :alpha to full transparency