为什么纹理渲染和采样需要偏移?

时间:2018-03-05 05:07:30

标签: opengl textures shader texel

我有两个相似版本的顶点着色器,它们会产生稍微不同的纹理。

第一个这样的版本如下:

#version 450

layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex
layout(location = 1) in vec3 norm; //a 3D vertex representing the normal to the vertex 
layout(location = 2) in vec2 texture_coordinate; // texture coordinates

layout(std430, binding = 3) buffer instance_buffer
{
    vec4 cubes_info[];//first 3 values are position of object 
};

out vec3 normalized_pos;
out vec3 normal; 
out vec2 texture_coord;

uniform float width = 128;
uniform float depth = 128;
uniform float height = 128;

void main()
{
    texture_coord = texture_coordinate; 
    vec4 pos = (vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));

    pos+=ivec4(1,1,0,0);

    pos.x = (2.f*pos.x-width)/(width);
    pos.y = (2.f*pos.y-depth)/(depth);

    pos.z = 0.9;

    gl_Position = pos;

    normalized_pos = vec3(pos);

    normal = normalize(norm);
}

产生:

enter image description here

查看边框,特别是在底部,注意左边的一些孤立像素。

现在是以下版本:

#version 450

layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex
layout(location = 1) in vec3 norm; //a 3D vertex representing the normal to the vertex 
layout(location = 2) in vec2 texture_coordinate; // texture coordinates

layout(std430, binding = 3) buffer instance_buffer
{
    vec4 cubes_info[];//first 3 values are position of object 
};

out vec3 normalized_pos;
out vec3 normal; 
out vec2 texture_coord;

uniform float width = 128;
uniform float depth = 128;
uniform float height = 128;

void main()
{
    texture_coord = texture_coordinate; 
    vec4 pos = (vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));

   // pos+=ivec4(1,1,0,0); <- Look here, we're not offsetting anymore

    pos.x = (2.f*pos.x-width)/(width);
    pos.y = (2.f*pos.y-depth)/(depth);

    pos.z = 0.9;

    gl_Position = pos;

    normalized_pos = vec3(pos);

    normal = normalize(norm);
}

制作图片: enter image description here

不出所料,当我们不添加该偏移时,图像会移位(底部的像素消失,我们知道顶部和右边有一个空边框)。我的问题是,为什么这种偏移必须以“

”开头

为什么没有映射

    pos.x = (2.f*pos.x-width)/(width);
    pos.y = (2.f*pos.y-depth)/(depth);

直接计算正确的纹理元素?

编辑:

基于评论的一些澄清。

您看到的图像是3D立体纹理图层。虽然它可能会有所不同,但我们可以假设x,y坐标将在[0,width-1]和[0,depth-1]的范围内(在这种情况下,两者都是127)。

0 个答案:

没有答案