简体   繁体   English

opengl 中 3d 空间平面上的程序网格

[英]Procedural grid on a plane in 3d space in opengl

I have a simple graphics program written in c++ that uses opengl for rendering.我有一个用 c++ 编写的简单图形程序,它使用 opengl 进行渲染。 I have a plane that I draw at the origin and I want to add a procedural grid texture to it.我有一个在原点绘制的平面,我想向它添加程序网格纹理。 In my current fragment shader I only do some lighting calculations:在我当前的片段着色器中,我只做一些光照计算:

#version 330 core
out vec4 FragColor;

in vec3 Normal;  
in vec2 TexCoord;
in vec3 FragPos;  

uniform vec3 objectColor;
uniform vec3 lightColor;
uniform vec3 lightPos;  


void main()
{

    float ambientStrength = 0.5;
    vec3 ambient = ambientStrength * lightColor;

    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(lightPos - FragPos);  

    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;

    vec3 result = (((ambient + diffuse) * objectColor) * 0.5) ;
    FragColor = vec4(result, 1.0);
}


My question is if modifying the fragment shader is the best way of having a procedural grid on my plane and if so how should I go about writing such a shader?我的问题是修改片段着色器是否是在我的平面上拥有程序网格的最佳方式,如果是这样,我应该如何编写这样的着色器?

The easiest way to do this will be to play around with trig functions.最简单的方法是使用三角函数。 Since you are passing the texture coordinates already.由于您已经在传递纹理坐标。

Let the color intensity be max(cos(u), cos(v)) .让颜色强度为max(cos(u), cos(v)) That will give you a start.那会给你一个开始。 Then you should play around with the cos formula to get it exactly how you want it (ie multiply it by some coefficients and apply powers to it until it looks exactly how you need it).然后你应该使用 cos 公式来得到你想要的结果(即乘以一些系数并对其应用幂,直到它看起来完全符合你的需要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM