GLSL 代码到具有高度图渲染的 2D 地形

发布于 2024-11-19 01:28:11 字数 210 浏览 2 评论 0原文

我有一个基于二维图块的地图和高度图图像,我想用它来使地图看起来不那么平坦。但我对 SFML 中使用的 GLSL 很陌生。我正在寻找一些 GLSL 着色器,我可以将平铺纹理和部分高度图传递到相同的坐标或创建高度错觉的东西。但是我自己做所有事情,我没有时间仅仅为了我想使用的这个着色器来学习 GLSL,所以感谢您的任何回复(最好是如果有人可以发布一些着色器代码并向傻瓜解释它是怎样的)在 .cpp 文件中使用)

I have a 2d tile based map and image of height map, which I would like to use to make map look less flat. But I am new to GLSL which is used in SFML. I am looking for some GLSL shader to which I can pass tile texture and part of height map on the same coordinates or something to create height illusion. But I am doing everything by myself and I dont have time to learn GLSL just for this one shader I would like to use, so thanks for any reply (best would be if someone could post some shader code and explaint it for dummies how is it used in .cpp file)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一花一树开 2024-11-26 01:28:11

您可能对“视差遮挡贴图”技术感兴趣。可以在此处找到一个好的实现。

You are probably interested in the technique "Parallax Occlusion Mapping". A good implementation can be found here.

初见你 2024-11-26 01:28:11

这会将 Z 踢到屏幕上,从而产生高度图的错觉。
它实际上并没有改变几何形状。
[编辑] 这对于自上而下的视图没有用。我会尝试添加某种基于新 Z 和光线方向的阴影效果。

此行:pos.z += color.x + color.y + color.z; 是推动 Z 的内容。您可以为比例指定正数或负数。

/* Vertex Program
 * Shifts Z by Textures color     
 */
uniform sampler2D color_texture;
uniform float scale;
varying vec4 p_color;
void main()
{
     vec2 texcoords = gl_MultiTexCoord0.st;
     p_color = texture2D(color_texture,texcoords);
     vec4 color = texture2D(color_texture,texcoords);
     vec4 pos = gl_Vertex;
     pos.z +=  color.x + color.y + color.z;
     pos.z*=scale;
   gl_Position = gl_ModelViewProjectionMatrix * pos;
}

这是非常简单的片段程序..

/* Fragment Program
   Copies incoming fragment color without change.
*/
varying vec4 p_color;
 void main()
{
    gl_FragColor = p_color;
}  

This will kick the Z on the screen to give the illusion of a height map.
It does not actually change the geometry.
[edit] This is not useful for top down view.. Id try adding some sort of shadow effect based on the new Z and a light direction.

This line: pos.z += color.x + color.y + color.z; is what pushes the Z. You can give scale a positive or negative number.

/* Vertex Program
 * Shifts Z by Textures color     
 */
uniform sampler2D color_texture;
uniform float scale;
varying vec4 p_color;
void main()
{
     vec2 texcoords = gl_MultiTexCoord0.st;
     p_color = texture2D(color_texture,texcoords);
     vec4 color = texture2D(color_texture,texcoords);
     vec4 pos = gl_Vertex;
     pos.z +=  color.x + color.y + color.z;
     pos.z*=scale;
   gl_Position = gl_ModelViewProjectionMatrix * pos;
}

And here is the VERY simple Fragment program..

/* Fragment Program
   Copies incoming fragment color without change.
*/
varying vec4 p_color;
 void main()
{
    gl_FragColor = p_color;
}  
一绘本一梦想 2024-11-26 01:28:11

这会使地形网格体变形,无论光线在世界空间中的位置,其基础都是气泡/圆顶形状。地形细分得越多,结果就越好。

Const Float PI = 3.1415928;
vec3 vVertex = vec3( gl_Vertex);
float d = distance(lightPos , gl_Vertex);
float d2;
    if (d < 1500.0)
    {
    d2 = d/1500.0;
    d = cos(PI*2.0*d2);
    }
    else
    {
    d=0.0;
    d2=0.0;
    }
vVertex.y += (d*75.0)*(1.0-d2)-50.;

gl_Position = gl_ModelViewProjectionMatrix* vec4(vVertex,gl_Vertex.w);

This deforms the a terrain mesh with a bubble/dome shape base where ever the light is in world space. The more the terrain is tessellated the better the results.

Const Float PI = 3.1415928;
vec3 vVertex = vec3( gl_Vertex);
float d = distance(lightPos , gl_Vertex);
float d2;
    if (d < 1500.0)
    {
    d2 = d/1500.0;
    d = cos(PI*2.0*d2);
    }
    else
    {
    d=0.0;
    d2=0.0;
    }
vVertex.y += (d*75.0)*(1.0-d2)-50.;

gl_Position = gl_ModelViewProjectionMatrix* vec4(vVertex,gl_Vertex.w);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文