[英]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.