为了账号安全,请及时绑定邮箱和手机立即绑定

Opengl - 极端的纹理质量损失

Opengl - 极端的纹理质量损失

qq_遁去的一_1 2023-06-08 19:41:36
我正在尝试纹理球体。我的顶点着色器:attribute vec3 a_position;attribute vec3 a_normal;attribute vec3 a_texCoord0;uniform mat4 model;uniform mat4 view;uniform mat4 projection;uniform sampler2D u_texture;varying vec3 fragPos;varying vec3 normal;varying vec3 color;void main(){    gl_Position = projection * view * model * vec4(a_position, 1.0);    fragPos = vec3(model * vec4(a_position, 1.0));    normal = a_normal;    if(a_texCoord0.x > 50){        color = vec3(1f, 0.0f, 0.0f);    } else {        color = texture(u_texture, a_texCoord0);    }}我的片段着色器:#ifdef GL_ES    precision mediump float;#endifvarying vec3 normal;varying vec3 color;varying vec3 fragPos;uniform vec3 lightPos;uniform vec3 lightColor;void main(){    // Ambient    float ambientStrength = 0.1;    vec3 ambient = ambientStrength * lightColor;    // Diffuse    vec3 norm = normalize(normal);    vec3 lightDir = normalize(lightPos - fragPos);    float diff = max(dot(norm, lightDir), 0.0);    vec3 diffuse = diff * lightColor;    //vec3 result = (ambient + diffuse) * color;    vec3 result = color;    gl_FragColor = vec4(result, 1.0);}结果,我得到了可怕的纹理质量损失。这是原始纹理和我在球体上得到的(这里只是立方体贴图的一部分,其他面是红色的)。我猜想这可能与构建方法(来自二十面体)和纹理(使用立方体贴图)的差异有关。但它可以解释纹理边缘不均匀,但不能解释如此可怕的质量损失。有人可以向我解释这里发生了什么吗?
查看完整描述

1 回答

?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

发生这种情况是因为您在顶点着色器中对纹理进行采样,这意味着您只能在每个三角形的角上获得三种颜色。其他像素被插值。


为了获得更好的质量,应该将纹理采样移动到片段着色器,并且应该插值 uv 坐标而不是颜色:


顶点着色器:


attribute vec3 a_position;

attribute vec3 a_normal;

attribute vec3 a_texCoord0;

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;


varying vec3 fragPos;

varying vec3 normal;

varying vec2 texcoord0;


void main()

{

    gl_Position = projection * view * model * vec4(a_position, 1.0);

    fragPos = vec3(model * vec4(a_position, 1.0));

    normal = a_normal;

    texcoord0 = a_texCoord0;

}

片段着色器:


varying vec3 normal;

varying vec2 texcoord0;

varying vec3 fragPos;


uniform sampler2D u_texture;

uniform vec3 lightPos;

uniform vec3 lightColor;


void main()

{

    vec3 color = texture(u_texture, texcoord0).rgb;


    // Ambient

    float ambientStrength = 0.1;

    vec3 ambient = ambientStrength * lightColor;


    // Diffuse

    vec3 norm = normalize(normal);

    vec3 lightDir = normalize(lightPos - fragPos);

    float diff = max(dot(norm, lightDir), 0.0);

    vec3 diffuse = diff * lightColor;


    //vec3 result = (ambient + diffuse) * color;

    vec3 result = color;

    gl_FragColor = vec4(result, 1.0);

}


查看完整回答
反对 回复 2023-06-08
  • 1 回答
  • 0 关注
  • 147 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信