C++ 视差映射-GLSL-OpenGL

C++ 视差映射-GLSL-OpenGL,c++,opengl,3d,glsl,parallax,C++,Opengl,3d,Glsl,Parallax,在过去的几天里,我一直在尝试在我的引擎中实现视差映射,但它似乎不起作用,我已经看到了至少15个示例,但我仍然无法让它起作用 这是一张图片: 正如您所看到的,您所能看到的只是基础颜色,而高度贴图不在那里 以下是我的着色器: Fragment Shader #version 330 core uniform sampler2D DiffuseTextureSampler; uniform sampler2D HeightTextureSampler; vec2 scaleBias = vec2(0

在过去的几天里,我一直在尝试在我的引擎中实现视差映射,但它似乎不起作用,我已经看到了至少15个示例,但我仍然无法让它起作用

这是一张图片:

正如您所看到的,您所能看到的只是基础颜色,而高度贴图不在那里

以下是我的着色器:

Fragment Shader

#version 330 core
uniform sampler2D DiffuseTextureSampler;
uniform sampler2D HeightTextureSampler;
vec2 scaleBias = vec2(0.5,0.5); 
in vec3 EyeDirection_tangentspace;
in vec2 UV;
void main() 
{ 
 float height = texture2D(HeightTextureSampler, vec2 (UV.x, -UV.y)).r; 
 //Our heightmap only has one color channel.
 float v = height * scaleBias.r - scaleBias.g; 
 vec3 eye = EyeDirection_tangentspace; 
 vec2 newCoords = UV + (eye.xy * v); 

 vec3 rgb = texture2D(DiffuseTextureSampler,  vec2 (newCoords.x, -newCoords.y)).rgb; 
 gl_FragColor = vec4(rgb, 1.0); 
}






Vertex Shader

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
layout(location = 2) in vec3 vertexNormal_modelspace;
layout(location = 3) in vec3 vertexTangent_modelspace;
layout(location = 4) in vec3 vertexBitangent_modelspace;

// Output data ; will be interpolated for each fragment.
out vec2 UV;
out vec3 Position_worldspace;
out vec3 EyeDirection_cameraspace;
out vec3 LightDirection_cameraspace;

out vec3 LightDirection_tangentspace;
out vec3 EyeDirection_tangentspace;

// Values that stay constant for the whole mesh.
uniform mat4 MVP;
uniform mat4 V;
uniform mat4 M;
uniform mat3 MV3x3;
uniform vec3 LightPosition_worldspace;

void main() 
{ 
 gl_Position = MVP * vec4(vertexPosition_modelspace,1);

Position_worldspace = (M * vec4(vertexPosition_modelspace,1)).xyz;

    // Vector that goes from the vertex to the camera, in camera space.
    // In camera space, the camera is at the origin (0,0,0).
    vec3 vertexPosition_cameraspace = ( V * M * vec4(vertexPosition_modelspace,1)).xyz;
    EyeDirection_cameraspace = vec3(0,0,0) - vertexPosition_cameraspace;


 UV = vertexUV;
 vec3 vertexTangent_cameraspace = MV3x3 * vertexTangent_modelspace;
    vec3 vertexBitangent_cameraspace = MV3x3 * vertexBitangent_modelspace;
    vec3 vertexNormal_cameraspace = MV3x3 * vertexNormal_modelspace;

 mat3 TBNMatrix = transpose(mat3(vertexTangent_cameraspace, vertexBitangent_cameraspace, vertexNormal_cameraspace)); 
 EyeDirection_tangentspace = Position_worldspace - vertexPosition_modelspace.xyz; 
 EyeDirection_tangentspace *= TBNMatrix; 
}
两件事

  • 将比例设置为1。如果你根本看不到的话,就没有必要把你的高分减半

  • (您当前的问题)您正在使用-UV获取纹理坐标。y Opengl没有负纹理坐标。如果设置为负数,则不会从纹理中提取任何内容,或者更糟糕的是,如果已启用平铺,则会提取镜像纹理

  • (您的下一个问题)在计算片段中的新坐标之前,将眼向量规格化。如果不进行规格化,向量的XY坐标将非常大,因此新纹理坐标是大量偏移

  • 试试这些着色器。它们很简单,也很实用。在视差正常工作后,必须添加照明

    顶点着色器

    attribute vec3 tangent;
    attribute vec3 binormal;
    uniform vec3 CAMERA_POSITION;
    varying vec3 eyeVec;
    void main()
    {
        gl_Position = ftransform();
        gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
    
        mat3 TBNMatrix = mat3(tangent, binormal, gl_Normal);
        eyeVec = CAMERA_POSITION - gl_Vertex.xyz;
        eyeVec *= TBNMatrix;
    }
    
    uniform sampler2D basetex;
    uniform sampler2D heightMap;
    uniform vec2 scaleBias;
    varying vec3 eyeVec;
    void main()
    {
        float height = texture2D(heightMap, gl_TexCoord[0].st).r;
    
        float v = height * scaleBias.r - scaleBias.g;
        vec3 eye = normalize(eyeVec);
        vec2 newCoords = texCoord + (eye.xy * v);
    
        vec3 rgb = texture2D(basetex, newCoords).rgb;
        gl_FragColor = vec4(rgb, 1.0);
    }
    
    片段着色器

    attribute vec3 tangent;
    attribute vec3 binormal;
    uniform vec3 CAMERA_POSITION;
    varying vec3 eyeVec;
    void main()
    {
        gl_Position = ftransform();
        gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
    
        mat3 TBNMatrix = mat3(tangent, binormal, gl_Normal);
        eyeVec = CAMERA_POSITION - gl_Vertex.xyz;
        eyeVec *= TBNMatrix;
    }
    
    uniform sampler2D basetex;
    uniform sampler2D heightMap;
    uniform vec2 scaleBias;
    varying vec3 eyeVec;
    void main()
    {
        float height = texture2D(heightMap, gl_TexCoord[0].st).r;
    
        float v = height * scaleBias.r - scaleBias.g;
        vec3 eye = normalize(eyeVec);
        vec2 newCoords = texCoord + (eye.xy * v);
    
        vec3 rgb = texture2D(basetex, newCoords).rgb;
        gl_FragColor = vec4(rgb, 1.0);
    }
    

    将您的
    height
    作为
    gl\u FragColor.rgb
    查看是否正确获取高度贴图。然后对
    newCoords
    执行相同操作,查看屏幕上的线性UV是否有任何波动。这样你就可以找到问题了。嗨,谢谢你的回复。我试过了,高度贴图纹理看起来是白色和黑色的。我对
    newCoords
    也做了同样的操作,但仍然是一样的。