GLSL multi-texturing- blending textures

Aidan Haddon-Wright

I'm trying to implement multi-texturing in OpenGL. The texture used is based on the surface normal of a given vertex- The more vertical it is, the more of the second texture is visible.

Here is what I have so far.

I want to blend the edges together now rather than having that hard edge. Is it possible to blend the textures in this way? if so how do I do that?

This is my fragment shader code:

#version 150

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCamera;
in vec3 playerPosition;
in vec4 vertexPosition;

in float blendPosition;
in float visibility;

out vec4 out_Color;

uniform sampler2D texture0;
uniform sampler2D texture1;

uniform vec3 skyColour;
uniform vec3 light_colour;
uniform float shineDamper;
uniform float reflectivity;

void main(void){
  vec3 unitNormal = normalize(surfaceNormal);
  vec3 unitLightVector = normalize(toLightVector);

  float nDot1 = dot(unitNormal,unitLightVector);
  float brightness = max(nDot1,0.2);
  vec3 diffuse = brightness * light_colour;

  vec3 unitToCamera = normalize(toCamera);
  vec3 lightDirection = -unitLightVector;
  vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);

  float specular = dot(reflectedLightDirection, unitToCamera);
  specular = max(specular,0.0);
  float damped = pow(specular,shineDamper);
  vec3 finalSpecular = damped * reflectivity * light_colour;

  out_Color = (vec4(diffuse,1.0) * texture(texture0,pass_textureCoords)) + vec4(-20,-20,0.0,0.0);
  out_Color = (vec4(diffuse,1.0) * texture(texture0,pass_textureCoords));
  out_Color = mix(vec4(skyColour,1.0),out_Color,visibility);

  if(vertexPosition.y < -6.1 || surfaceNormal.y < 0.6){
    out_Color = (vec4(diffuse,1.0) * texture(texture1,pass_textureCoords)) + vec4(-20,-20,0.0,0.0);
    out_Color = (vec4(diffuse,1.0) * texture(texture1,pass_textureCoords));
    out_Color = mix(vec4(diffuse,1.0) * texture(texture0,pass_textureCoords),out_Color,1);
    out_Color = mix(vec4(skyColour,1.0),out_Color,visibility);
  }
  if(playerPosition.y < -6.1){
    out_Color = mix(vec4(0.0,0.3,0.5,1.0),out_Color,0.1);
  }
}

EDIT:

This is the new fragment shader code for anyone interested

Updated fragment shader code:

#version 150

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCamera;
in vec3 playerPosition;
in vec4 vertexPosition;

in float blendPosition;
in float visibility;

out vec4 out_Color;

uniform sampler2D texture0;
uniform sampler2D texture1;

uniform vec3 skyColour;
uniform vec3 light_colour;
uniform float shineDamper;
uniform float reflectivity;

void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);

float nDot1 = dot(unitNormal,unitLightVector);
float brightness = max(nDot1,0.2);
vec3 diffuse = brightness * light_colour;

vec3 unitToCamera = normalize(toCamera);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection,unitNormal);

float specular = dot(reflectedLightDirection, unitToCamera);
specular = max(specular,0.0);
float damped = pow(specular,shineDamper);
vec3 finalSpecular = damped * reflectivity * light_colour;

out_Color.a = 1;

vec4 fog = vec4(skyColour,1.0);
vec4 diffusion = vec4(diffuse,1.0);

float a = clamp((unitNormal.y - .6)*5 + .5, 0, 0.7);
vec3 texture0_colour = (mix(fog,diffusion * texture(texture0,pass_textureCoords),visibility)).rgb;
vec3 texture1_colour = (mix(fog,diffusion * texture(texture1,pass_textureCoords),visibility)).rgb;

out_Colour.rgb = mix(texture1_colour,texture0_colour,a);

}
Yakov Galka

To mix two texture based on a value a you do:

float a = ...;
vec3 color0 = texture(texture0, pass_textureCoords).rgb;
vec3 color1 = texture(texture1, pass_textureCoords).rgb;
out_Color.rgb = mix(color0, color1, a);

Assuming that your unitNormal = (0,1,0) is the upwards direction, as it appears from the code, then the value of

float a = clamp(unitNormal.y, 0, 1);

will result in a smooth transition between the two textures. However, you probably want a sharper transition, in which case you shift and scale the unitNormal.y value to adjust where the transition starts and ends:

float a = clamp((unitNormal.y - .6)*5 + .5, 0, 1);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Texturing multiple objects with different textures

From Dev

textures with transparent background by using alpha blending opengl

From Dev

Multi-texturing using QOpenGLTexture and QOpenGLFrameBufferObject

From Dev

DAE file not loading multi texturing in threejs

From Dev

Soft, transparent paint stroke textures not blending like I expect

From Dev

Blend textures of different size/coordinates in GLSL

From Dev

GLSL 150 GL 3.2 black textures

From Dev

GLSL Shader - Shadow between 2 textures on a plane

From Dev

c++/OpenGL/GLSL, textures with "random" artifacts

From Dev

GLSL shader: Interpolate between more than two textures

From Dev

Advanced moiré a pattern reduction in HLSL / GLSL procedural textures shader - antialiasing

From Dev

GLSL rendering more textures than texture units available

From Dev

GLSL: Sending variable number of textures to sampler2D

From Dev

webgl 2d blending two transparent textures on top of each other

From Dev

Texturing triangles

From Dev

OpenGL texturing, black square

From Dev

OpenGL Texturing, no error but grey

From Dev

Artefacts in texturing in PyOpenGL

From Dev

OpenGL ES - texturing sphere

From Dev

Unity: Texturing a Shoreline in a Hexgrid

From Dev

OpenGL Texturing with SOIL

From Dev

Model texturing error with libgdx

From Dev

Opengl: earth texturing

From Dev

OpenGl texturing ........ ppm background

From Dev

OpenGL Texturing, no error but grey

From Dev

Trouble Texturing VBO

From Dev

OpenGL Texturing with Android Context

From Dev

Texturing a pyramid in WebGL

From Dev

Multi lights shadow mapping does not work correctly using GLSL