簡體   English   中英

視差映射-GLSL- OpenGL

[英]Parallax Mapping - GLSL- OpenGL

在過去的幾天里,我一直在嘗試在引擎中實現視差映射,但是它似乎不起作用,我至少看到了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. 將比例尺設置為1。如果根本看不到,則沒有必要將高端比例減半。

  2. (您的當前問題)您正在使用-UV.y獲取紋理坐標。y Opengl沒有負的紋理坐標。 變為負值將不會從紋理中拉出任何東西,如果貼上瓷磚,則更糟的是鏡面紋理。

  3. (您的下一個問題)在計算片段中的新坐標之前將您的眼睛向量歸一化。 如果不進行標准化,則矢量的XY坐標將很大,因此新的紋理坐標為MASSIVE偏移。

嘗試這些着色器。 它們非常簡單且有效。 視差正常工作后,您必須添加照明

頂點着色器

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM