OpenGL 3D纹理“照亮" [英] OpenGL 3D texture "shine through"

查看:105
本文介绍了OpenGL 3D纹理“照亮"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于测试目的,我想显示3D纹理的第一片.但是,似乎还显示了其他切片.

For testing purposes I want to display the first slice of a 3D texture. However it seems that the other slices are also displayed.

我的顶点着色器:

#version 130

attribute vec4 position;

varying vec2 texcoord;

void main()
{
    gl_Position = position;
    texcoord = position.xy * vec2(0.5) + vec2(0.5);
}

片段着色器:

#version 130

uniform sampler3D textures[1];

varying vec2 texcoord;

void main()
{
    gl_FragColor = texture3D(textures[0], vec3(texcoord.x, texcoord.y, 0));
}

我使用C 绑定C ++代码中的纹理

I bind the texture from my C++ code using

     glTexImage3D(GL_TEXTURE_3D,0,GL_LUMINANCE,rows-1,cols-1,dep-  1,0,GL_RED,GL_UNSIGNED_SHORT, vol.data());

这是它的外观;整个头部应该是不可见的,只有它的上部是可见的.

This is how it looks; the whole head should not be visible, only the upper portion of it.

推荐答案

使用线性过滤(GL_TEXTURE_MIN_FILTER和/或GL_TEXTURE_MAG_FILTER纹理参数的值GL_LINEAR)时,样本值通常为由最接近您的第三纹理坐标的两个切片确定.

When you use linear filtering (value GL_LINEAR for the GL_TEXTURE_MIN_FILTER and/or GL_TEXTURE_MAG_FILTER texture parameters), the value of your samples will generally be determined by the two slices closest to your 3rd texture coordinate.

如果第3维中3D纹理的大小为d,则第一个切片在纹理坐标空间中的位置为0.5 / d.如果您在纹理坐标空间中描绘每个具有一定厚度的切片,这将最容易理解.由于您有d个切片,并且纹理坐标范围为[0.0, 1.0],因此每个切片的厚度为1.0 / d.因此,第一个切片从0.0延伸到1.0 / d,其中心在0.5 / d.

If the size of your 3D texture in the 3rd dimension is d, the position of the first slice in texture coordinate space is 0.5 / d. This is easiest to understand if you picture each slice having a certain thickness in texture coordinate space. Since you have d slices, and the texture coordinate range is [0.0, 1.0], each slice has thickness 1.0 / d. Therefore, the first slice extends from 0.0 to 1.0 / d, and its center is at 0.5 / d.

当对第三纹理坐标使用0.0时,它不是第一个切片的中心,因此线性采样开始起作用.由于0.0位于纹理的边缘,因此环绕模式至关重要.默认的环绕模式为GL_REPEAT,这意味着在边缘进行采样就像重复纹理一样.使用此设置,第一个切片的邻居是最后一个切片,纹理坐标0.0恰好在第一个切片和最后一个切片之间的中间.

When you use 0.0 for the 3rd texture coordinate, this is not the center of the first slice, and linear sampling comes into play. Since 0.0 is at the edge of the texture, the wrap modes become critical. The default wrap mode is GL_REPEAT, meaning that sampling at the edges acts as if the texture was repeated. With this setting, the neighbor of the first slice is the last slice, and texture coordinate 0.0 is exactly in the middle between the first slice and the last slice.

其结果是,使用纹理坐标为0.0的线性采样将为您提供第一个切片和最后一个切片的平均值.

The consequence is that linear sampling with texture coordinate 0.0 will give you the average of the first slice and the last slice.

虽然GL_REPEAT是自动换行模式的默认设置,但它几乎不是您想要的,除非您的纹理确实包含重复的模式.在这种情况下,这肯定不是正确的设置.您需要的是:

While GL_REPEAT is the default for the wrap modes, it is rarely what you want, unless your texture really contains a repeating pattern. It's certainly not the right setting in this case. What you need here is:

glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

这篇关于OpenGL 3D纹理“照亮"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆