Lwjgl GLSL Random BlendMapping

Hello, i was doing a tutorial on youtube about lwjgl and GLSL shaders.One of there newer videos cover blendMaps which work fine and good, but I would like it to be randomly generated blending as in some soil here and some flower texture there.Here’s my code for the terrainFragmentShader:
#version 400 core

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCameraVector;
in float visibility;
out vec3 position;
out vec4 out_Colour;

uniform sampler2D backgroundTexture;
uniform sampler2D rTexture;
uniform sampler2D gTexture;
uniform sampler2D bTexture;
uniform sampler2D blendMap;

uniform vec3 lightColor;
uniform float shineDamper;
uniform float reflectivity;
uniform vec3 skyColor;

void main(void) {

vec4 blendMapColor = texture(blendMap,pass_textureCoords);	
float backTextureAmount = 1 - (blendMapColor.r + blendMapColor.g + blendMapColor.b);
vec2 tiledCoords = pass_textureCoords * 40.0;

vec4 backgroundTextureColor = texture(backgroundTexture,tiledCoords) * backTextureAmount;
vec4 rTextureColor = texture(rTexture,tiledCoords) * blendMapColor.r;
vec4 gTextureColor = texture(gTexture,tiledCoords) * blendMapColor.g;
vec4 bTextureColor = texture(bTexture,tiledCoords) * blendMapColor.b;

vec4 totalColor = backgroundTextureColor + rTextureColor + gTextureColor + bTextureColor;

vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);

float nDot1 = dot(unitNormal,unitLightVector);
float brightness = max(nDot1,0.4);
vec3 diffuse = brightness * lightColor;

vec3 unitVectorToCamera = normalize(toCameraVector);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);

float specularFactor = dot(reflectedLightDirection,unitVectorToCamera);
specularFactor = max(specularFactor,0.0);
float dampedFactor = pow(specularFactor,shineDamper);
vec3 finalSpecular = dampedFactor * reflectivity * lightColor;
out_Colour = vec4(diffuse,1.0) * totalColor + vec4(finalSpecular,1.0);
out_Colour = mix(vec4(skyColor,1.0),out_Colour,visibility);

}

Any help appreciated!

I don’t know if it’s a good idea to do this in a shader. Personally I would precompute the values once on the cpu write them to the image and then send it over to the gpu, but…

http://www.geeks3d.com/20100831/shader-library-noise-and-pseudo-random-number-generator-in-glsl/

Those where useful posts but like it a bit more specific if you don’t mind. Really just a way to replace blendMapColor.r/g/b with an r/g/b value between 0 and 1.

Here’s another article that’s more specific to what you want, I think…

The reason I’m not going into super detail here is because random number generation is a big topic and very difficult to implement a good one, and I’m not an expert so I don’t want to lead you astray. However, any number you create using a random number generator can be divided by the largest value a data type can hold, eg. Float.MAX, to create a number between zero and one. :point: that might help too.

Anyway the link above should be more in line with what you’re looking for.

from: http://www.ozone3d.net/blogs/lab/20110427/glsl-random-generator/

varying vec3 v;
float rand(vec2 n)
{
  return 0.5 + 0.5 * 
     fract(sin(dot(n.xy, vec2(12.9898, 78.233)))* 43758.5453);
}
void main(void)
{
  float x = rand(v.xz);
  gl_FragColor = vec4(x, x, x, 1.0);
}

Thanks for that, but it only adds all textures to the whole terrain at a fixed brightness with this:
blendMapColor.r/g/b = rand(v);

So how could i get it more normal and the texture spread out equally and in different places?

By using an offset:

blendMapColor.r = rand(v);
v.xy += 1.0;
blendMapColor.g = rand(v);
v.xy += 1.0;
blendMapColor.b = rand(v);
v.xy += 1.0;
blendMapColor.a = rand(v);

Now this will make everything super noisy so I’d take another look at the geeks3d link and you could take a look at this thread here:
http://www.java-gaming.org/topics/like-simplex-noise-but-don-t-like-the-patent-introducing-opensimplex-noise/34365/view.html

Now, really what you’re looking for is not “static”. So I’d look into random 3d terrain generation, the basic principles are exactly the same. https://www.youtube.com/watch?v=03vn1Nv9hyk

I’ve never done any of this on the gpu so I’m not really sure how much more help I can be… :clue:

What I would probably do is code up a perlin noise generator on the cpu, much easier to debug imo, and then once I had a good grasp of that I’d move it over to the gpu.