简体   繁体   中英

Incorrect texture display on vertical plane in OpenGL

Working on AR app and trying to replace plane texture.

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).

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;

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.

There are 3 ways to refer to the components of a vector in GLSL. xyzw (the most common), rgba (more natural for colours), stpq (more natural for texture coordinates).

The line texture_coord = world_pos.sp would be clearer if it were written as 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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