体积渲染alpha混合
Volume Rendering alpha blending
我找到了这个教程http://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering关于体渲染,并试图实现我自己的渲染器。在alpha测试中,我可以一次画出所有的切片,这或多或少给了我类似教程中的结果。现在,我想向前移动一步并应用alpha混合,但之后没有任何变化(即使有,它看起来也不像教程中那么漂亮)。
下面是我的渲染代码:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
for (int i = 0; i < z; ++i)
{
glBindTexture(GL_TEXTURE_2D, texturesObjects[i]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0, -1.0, 2 * i / z - 1);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0, -1.0, 2 * i / z - 1);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0, 1.0, 2 * i / z - 1);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0, 1.0, 2 * i / z - 1);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
}
glutSwapBuffers();
,其中z为切片数。纹理生成代码如下:
GLuint* loadXZslices(const VR::DataAccessor& accessor, int& x, int& y, int& z)
{
x = accessor.getX();
y = accessor.getY();
z = accessor.getZ();
GLuint* texturesObjects = new GLuint[accessor.getZ()];
for (size_t i = 0; i < accessor.getZ(); ++i)
{
char* luminanceBuffer = accessor.getXYslice(i);
char* rgbaBuffer = new char[accessor.getX() * accessor.getY() * 4];
for (size_t j = 0; j < accessor.getX() * accessor.getY(); ++j)
{
rgbaBuffer[j * 4] = luminanceBuffer[j];
rgbaBuffer[j * 4 + 1] = luminanceBuffer[j];
rgbaBuffer[j * 4 + 2] = luminanceBuffer[j];
rgbaBuffer[j * 4 + 3] = 255;
if (luminanceBuffer[j] < 20)
{
rgbaBuffer[j * 4 + 3] = 0;
}
}
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texturesObjects[i]);
glBindTexture(GL_TEXTURE_2D, texturesObjects[i]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, accessor.getX(), accessor.getY(), 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)rgbaBuffer);
delete[] luminanceBuffer;
delete[] rgbaBuffer;
}
return texturesObjects;
}
DataAccessor当然是允许访问原始纹理数据的类。
我忘记在我的窗口设置东西还是我的代码有问题?
您的代码中有几个问题:
-
你没有设置你的纹理有部分透明度。在你构建纹理的地方,你将alpha组件设置为255,如果原始亮度小于20,则设置为0。为了得到实际的混合值,你需要使用alpha分量的亮度值:
rgbaBuffer[j * 4 + 3] = luminanceBuffer[j];
要微调结果,您可能必须使用从原始强度到alpha的其他映射。但它可能仍然是某种斜坡,而不是阶跃函数。
-
虽然代码没有显示
z
的声明,但这段代码看起来很可疑:glVertex3f(-1.0, -1.0, 2 * i / z - 1);
从上下文中看,
z
可能是一个整数。如果这是真的,这将是一个整数除法,当2 * i
小于z
时,它将产生0,剩余的循环值为1。 必须将至少一个值转换为浮点数才能使其成为浮点除法。
相关文章:
- 混合组合和继承的C++问题
- 在混合代码库中将C转换为C++时出现许多包含错误
- D3D11-将混合权重和索引传递到顶点着色器
- C++分数混合比较运算符错误
- 是否可以混合使用SFINAE和模板专业化?
- 如何在 Python C++ 混合库中调试非确定性分段错误?
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- 关于 Direct3D9 中的 alpha 混合的问题
- 由于alpha值的原因,将一个图像混合到另一个图像会产生不正确的图像
- Direct3d 11深度模板 / Alpha混合问题
- 通过使用alpha混合opengl实现具有透明背景的纹理
- SDL中的Alpha混合在调整窗口大小后重置
- OpenGL到DirectX的转换-alpha混合
- C++DirectX 9.0精灵Alpha混合未显示
- OpenGL alpha混合突然停止
- 体积渲染alpha混合
- c++:抗锯齿点重叠(alpha混合)
- 在WinXP安装中,带有alpha混合图标的工具栏不能正确显示
- OpenGL通过Alpha混合进行遮罩
- 优化Alpha混合