亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號