簡體   English   中英

如何使用頂點位置坐標進行紋理處理? openGL,c ++

[英]How can I texture with vertex position coordinates? openGL,c++

我想在沒有預定紋理坐標的情況下對地形進行紋理處理。 我想使用頂點位置坐標來確定頂點或fragmant着色器中的坐標。 我現在使用位置“ xz”坐標(up =(0,1,0)),但是如果我有一個與地面成90度的牆,則紋理將像這樣:

在此處輸入圖片說明

如何將這些坐標轉換為合適的位置?

這是我的頂點着色器:

#version 430

in layout(location=0) vec3 position;
in layout(location=1) vec2 textCoord;
in layout(location=2) vec3 normal;

out vec3 pos;
out vec2 text;
out vec3 norm;


uniform mat4 transformation;

void main()
{   
    gl_Position = transformation * vec4(position, 1.0);
    norm = normal;
    pos = position;
    text = position.xz;

}

這是我的fragmant着色器:

#version 430

in vec3 pos;
in vec2 text;
in vec3 norm;

//uniform sampler2D textures[3];

layout(binding=3) uniform sampler2D texture_1;
layout(binding=4) uniform sampler2D texture_2;
layout(binding=5) uniform sampler2D texture_3;

vec3 lightPosition = vec3(-200, 700, 50);
vec3 lightAmbient = vec3(0,0,0);
vec3 lightDiffuse = vec3(1,1,1);
vec3 lightSpecular = vec3(1,1,1);

out vec4 fragColor;
vec4 theColor;



void main()
{
    vec3 unNormPos = pos;
    vec3 lightVector = normalize(lightPosition) - normalize(pos);
    //lightVector = normalize(lightVector);

    float cosTheta = clamp(dot(normalize(lightVector), normalize(norm)), 0.5, 1.0);

    if(pos.y <= 120){
        fragColor = texture2D(texture_2, text*0.05) * cosTheta;
    }
    if(pos.y > 120 && pos.y  <  150){
        fragColor = (texture2D(texture_2, text*0.05) * (1 - (pos.y-120)/29) +  texture2D(texture_3, text*0.05) * ((pos.y-120)/29))*cosTheta;
    }
    if(pos.y >= 150)
    {
        fragColor = texture2D(texture_3, text*0.05) * cosTheta;
    }   
}

編輯:(字體)

text = 0.05 * (position.xz + vec2(0,position.y)); 

在此處輸入圖片說明

text = 0.05 * (position.xz + vec2(position.y,position.y)); 

現在牆壁可以工作了,但是地形卻沒有。 在此處輸入圖片說明

這個問題實際上是一個非常困難的問題,因為您不能為僅使用xyz坐標的紋理坐標設計公式來正確顯示垂直牆。

為了形象化,想象一下一塊平坦土地旁邊的小山。 由於穿過山丘的路徑比穿過平坦土地的路徑更長,因此紋理應在平坦土地上的山丘上纏繞更多次。 在下圖中,紋理在山坡上包裹了5次,在平坦的物件上包裹了4次。

希爾可視化

如果紋理坐標在左邊(0,0) ,那么它們應該在右邊(4,0)還是(5,0) 由於兩個答案都是有效的,因此證明沒有函數可以完全基於xyz坐標來計算正確的紋理坐標。 :(

但是,可以使用不同的方法解決您的問題:

  • 可以通過獨立於地形生成牆並為牆分配正確的紋理坐標來校正牆。 實際上,不將這些合並到您的地形中更有意義。
  • 您可以使用法線貼圖,更高分辨率的紋理或不同紋理的組合在陡峭山坡的側面添加更多細節。 可能有一個我不知道的更好的解決方案。

編輯:三 路映射將解決您的問題!

嘗試:

text = position.xz + vec2(0,y);

另外,我建議在頂點着色器而不是片段着色器中設置*0.05比例因子。 最終代碼為:

text = 0.05 * (position.xz + vec2(0,y));

暫無
暫無

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

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