Koos
Koos

Reputation: 91

Procedural grid on a plane in 3d space in opengl

I have a simple graphics program written in c++ that uses opengl for rendering. 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?

Upvotes: 2

Views: 584

Answers (1)

Makogan
Makogan

Reputation: 9536

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)). That will give you a start. Then you should play around with the cos formula to get it exactly how you want it (i.e. multiply it by some coefficients and apply powers to it until it looks exactly how you need it).

Upvotes: 2

Related Questions