Texture Splatting [OpenGL]

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
nespa
Posts: 167
Joined: Wed Feb 24, 2010 12:02 pm

Post by nespa »

freetimecoder
Posts: 226
Joined: Fri Aug 22, 2008 8:50 pm
Contact:

Post by freetimecoder »

Looks good, it is easier to handle now.
nespa
Posts: 167
Joined: Wed Feb 24, 2010 12:02 pm

Post by nespa »

wowww.....great Your site!


:)
Buck1000
Posts: 93
Joined: Sun Dec 14, 2008 8:02 pm
Location: Seattle, WA

Post by Buck1000 »

First off, great job, this is extremely useful =]

Is there any way to scale the splat map? I'm using this with an IAnimatedMeshSceneNode terrain, and it comes out looking like this ->
Image

My splatter map is 512x512...
Image
Virror
Posts: 191
Joined: Mon May 02, 2011 3:15 pm

Post by Virror »

Would be nice to have this working in Directx : p
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Virror wrote:Would be nice to have this working in Directx : p
Just convert glsl to hlsl and you're good to go.
Working on game: Marrbles (Currently stopped).
nespa
Posts: 167
Joined: Wed Feb 24, 2010 12:02 pm

Post by nespa »

HLSL shader:

Code: Select all

float4x4 matViewProjection : ViewProjection;
sampler2D splatMap = sampler_state
{ 
   ADDRESSU = WRAP; 
   ADDRESSV = WRAP; 
   ADDRESSW = WRAP; 
}; 
sampler2D layer_red  = sampler_state
{ 
   MipFilter = LINEAR; 
   MinFilter = LINEAR; 
   MagFilter = LINEAR; 
   ADDRESSU = WRAP; 
   ADDRESSV = WRAP; 
   ADDRESSW = WRAP; 
}; 

sampler2D layer_green = sampler_state
{ 
   MipFilter = LINEAR; 
   MinFilter = LINEAR; 
   MagFilter = LINEAR; 
   ADDRESSU = WRAP; 
   ADDRESSV = WRAP; 
   ADDRESSW = WRAP; 
}; 

sampler2D layer_blue = sampler_state
{ 
   MipFilter = LINEAR; 
   MinFilter = LINEAR; 
   MagFilter = LINEAR; 
   ADDRESSU = WRAP; 
   ADDRESSV = WRAP; 
   ADDRESSW = WRAP; 
}; 

struct VS_INPUT 
{ 
   float4 Position : POSITION0; 
   float2 alphamap : TEXCOORD0; 
   float2 tex : TEXCOORD1;
   }; 

struct VS_OUTPUT 
{ 
   float4 Position : POSITION0; 
   float2 alphamap : TEXCOORD0; 
   float2 tex : TEXCOORD1;
   }; 

struct PS_OUTPUT 
{ 
   float4 diffuse : COLOR0; 
}; 

VS_OUTPUT vs_main(in VS_INPUT Input) 
{ 
   VS_OUTPUT Output; 
   Output.Position = mul( Input.Position, matViewProjection );
   Output.alphamap = Input.alphamap; 
   Output.tex = Input.tex;
   return( Output ); 
} 

PS_OUTPUT ps_main(in VS_OUTPUT input) 
{ 
   float texScale = 10.0f; 	
   PS_OUTPUT output = (PS_OUTPUT)0; 

   float4 a = tex2D(splatMap, input.alphamap); 
   float4 i = tex2D(layer_red, input.tex*texScale);
   float4 j = tex2D(layer_green, input.tex*texScale); 
   float4 k = tex2D(layer_blue, input.tex*texScale); 

  
 
 
  output.diffuse  = float4(a.r*i+a.g*j+a.b*k)*float4(1,1,1,a.a);

  
   return output;

  



 
} 

technique Default_DirectX_Effect 
{ 
   pass Pass_0 
   { 
      VertexShader = compile vs_2_0 vs_main(); 
      PixelShader = compile ps_2_0 ps_main(); 
   } 

Virror
Posts: 191
Joined: Mon May 02, 2011 3:15 pm

Post by Virror »

Woot! Thanx alot : D
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Post by christianclavet »

Buck1000, since it's a mesh and not the terrain node, check your model inside a 3D editor, and look at the UV coordinates.

Looking at this, it's seem that your model UV is bigger than the U=1,V=1...
freetimecoder
Posts: 226
Joined: Fri Aug 22, 2008 8:50 pm
Contact:

Post by freetimecoder »

Hi,

Buck1000: The shader takes the first uv layer(0) for the splatting map and the second(1) for the other textures. Simply add two uv layers to your model and scale the first one so that the splatmap fits correctly and the second so that the other materials are tiled.

If you only want one uv layer, you can adjust the shader. Remove the "gl_TexCoord[1] = gl_MultiTexCoord1;" in the vertex shader and replace all gl_TexCoord[1] with gl_TexCoord[0] in the fragment shader. The shader then only takes the first uv layer for all textures.

Additionally you could scale the texturecoordinates within the fragment shader "gl_TexCoord[0].xy*20" or something like that to tile the color textures.

greetings
Buck1000
Posts: 93
Joined: Sun Dec 14, 2008 8:02 pm
Location: Seattle, WA

Post by Buck1000 »

Thanks guys, I edited the base UV layer of my model in Deled, and the problem went away! Now I'm trying to figure out how to add a second UV layer using Deled, since the actual textures themselves are scaled up too high) I'll do some googling..

EDIT: I got it working! And it looks AMAZING =D
freetimecoder
Posts: 226
Joined: Fri Aug 22, 2008 8:50 pm
Contact:

Post by freetimecoder »

Hi,

if you can't find a way, you can use this modfied shader:

Code: Select all

    const c8 *vertShader = "void main()\
                            {\
                                gl_TexCoord[0] = gl_MultiTexCoord0;\
                                gl_Position = ftransform();\
                            }\
                            ";

    //Fragment Shader
    const c8 *fragShader = "uniform sampler2D splatMap;\
                            uniform sampler2D layer_red;\
                            uniform sampler2D layer_green;\
                            uniform sampler2D layer_blue;\
                            \
                            void main(){\
                                float factor = 20;\
                                vec4 SplatCol=texture2D(splatMap,gl_TexCoord[0].xy);\
                                vec4 RedCol=texture2D(layer_red,gl_TexCoord[0].xy*factor);\
                                vec4 GreenCol=texture2D(layer_green,gl_TexCoord[0].xy*factor);\
                                vec4 BlueCol=texture2D(layer_blue,gl_TexCoord[0].xy*factor);\
                                gl_FragColor=(vec4(SplatCol.r*RedCol+SplatCol.g*GreenCol+SplatCol.b*BlueCol))*vec4(1,1,1,SplatCol.a);\
                            }\
                            ";
To set a different scale for the color textures change the "float factor" to any value you want. However, that would change the value globally for all nodes with this shader.

greetings
reason
Posts: 5
Joined: Tue Oct 25, 2011 10:06 am

Re: Texture Splatting [OpenGL]

Post by reason »

Hey :) i just compiled this on a MacBook Pro 13' with NVIDIA GeForce 320M. Pretty easy, just add CMultiTexturingManager.h, CMultiTexturingManager.cpp (rename it to CMultiTexturingManager.mm) and main.cpp (rename to main.mm) and compile!

It works really well, i added a 513 terrain, the fps is unstable but it is around 34 to 156, as for the standard terrain it is about 300.

Only problem is:
by roelor » Tue Jun 08, 2010 12:05 am

this doesnt work well on my intel gma 4500. framerate is low, (thats to be expected) but the terrain is transparant in the distance. (you can look through the terrain).

by freetimecoder » Wed Jun 09, 2010 9:03 am

Hi,

Is it transparent only in the distance or everywhere? Only in the distance. Does it look like on the screenshot, ecxept the transparency? Looks like on the screenshots. What OpenGL/GLSL version do you have? OpenGL 2.1 Does it give any errors in the console? Dont think so.

greetings
Thank you :)
roelor
Posts: 240
Joined: Wed Aug 13, 2008 8:06 am

Re: Texture Splatting [OpenGL]

Post by roelor »

Totally neglected my post :/ Glad you could answer, I can always pick the laptop up again to retest if neccesairy.
reason
Posts: 5
Joined: Tue Oct 25, 2011 10:06 am

Re: Texture Splatting [OpenGL]

Post by reason »

Hi, again. :D

I just got my new iMac 27'
RAM: 12GB
Processor: 3.4 i7
SSD
2gb display card

Error is still there, even on such hardware...

Seems like there is some error with mac osx :P
Post Reply