简体   繁体   English

OpenGL在垂直平面上的纹理显示不正确

[英]Incorrect texture display on vertical plane in OpenGL

Working on AR app and trying to replace plane texture. 使用AR应用程序并尝试替换平面纹理。

I'm trying to render texture on vertical and horizontal planes. 我正在尝试在垂直和水平平面上渲染纹理。 It's working fine for horizontal planes, but doesn't work well on vertical. 它在水平面上工作正常,但在垂直面上效果不佳。

在此处输入图片说明

i found that something wrong with texture_coord calculations, but can't figure it out (new to OpenGL). 我发现texture_coord计算出了点问题,但无法弄清楚(OpenGL的新特性)。

Here is my vertex shader 这是我的顶点着色器

void main()
{
    vec4 local_pos = vec4(a_position, 1.0);
    vec4 world_pos = u_model * local_pos;

    texture_coord = world_pos.sp * u_scale;

    gl_Position = u_mvp * local_pos;
}

fragment shader 片段着色器

out vec4 outColor; vec4 outColor;

void main()
{
    vec4 control = texture(u_texture, diffuse_coord);

    float dotScale = 1.0;

    float lineFade = 0.5;

    vec3 newColor = (control.r * dotScale > u_gridControl.x) ? u_dotColor.rgb : control.g > u_gridControl.y ? u_lineColor.rgb * lineFade: u_lineColor.rgb * 0.25 * lineFade;

    outColor = vec4(newColor, 1.0);
}

The important bit is texture_coord = world_pos.sp in your vertex shader. 重要的一点是顶点着色器中的texture_coord = world_pos.sp

There are 3 ways to refer to the components of a vector in GLSL. 有3种方法可以引用GLSL中向量的组成部分。 xyzw (the most common), rgba (more natural for colours), stpq (more natural for texture coordinates). xyzw (最常见), rgba (对于颜色更自然), stpq (对于纹理坐标更自然)。

The line texture_coord = world_pos.sp would be clearer if it were written as texture_coord = world_pos.xz . 该生产线texture_coord = world_pos.sp如果它被写成会更清晰texture_coord = world_pos.xz

Once you realize that you're generating texture coordinates by ignoring the y-component it's obvious why vertical planes are not textured how you would like. 一旦意识到通过忽略y分量生成纹理坐标,就很明显为什么不对垂直平面进行纹理化处理。

Unfortunately there's no simple one line fix. 不幸的是,没有简单的一线解决方案。 Perhaps tri-planar texturing might be an appropriate solution for you - this seems to be a good explanation of the technique. 也许三平面纹理化可能是您合适的解决方案- 似乎是对该技术的很好解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM