Skip to main content
gamedev.net
🔒 Locked 🎯 Unreal

Weird artifacts

Started by starvinmarvin 4 replies 750+ views
Original Post
starvinmarvin
starvinmarvin
Hi! I'm rendering a terrain using GLSL for texture splatting and PPL, but depending on the camera position/angle I get some ugly "artifacts"/results: Shaders

Fragment Shader
uniform sampler2D Grass;
uniform sampler2D Stone;
uniform sampler2D Rock;
uniform sampler2D Alpha;

varying vec4 diffuse,ambientGlobal, ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;
	
vec4 cursorColor = vec4(1,0,0,1);
uniform float cursorSize;
uniform vec2 cursorPosition;

void main(void)
{
	
	//Texture Splatting

	vec2 texUV = gl_TexCoord[0].xy;
 
	vec4 alpha   = texture2D( Alpha, texUV.xy );

  	vec4 tex0    = texture2D( Grass, texUV.xy * 64.0 ); // Tile
  	vec4 tex1    = texture2D( Rock,  texUV.xy * 64.0 ); // Tile
   	vec4 tex2    = texture2D( Stone, texUV.xy * 64.0 ); // Tile

   	tex0 *= alpha.r; // Red channel
  	tex1 = mix( tex0, tex1, alpha.g ); // Green channel
  	vec4 outColor = mix( tex1, tex2, alpha.b ); // Blue channel
   
	
	//Drawing a Editing-Brush for the Terrain
	float distRing = distance(texUV.xy, cursorPosition);
	if(distRing < cursorSize)
	{
	float strength = max(0.0, mix(-1.5, 0.5, distRing / cursorSize));
	outColor += cursorColor * strength;
	}
		
	//gl_FragColor = outColor;


	//Per Pixel Lighting
	float startAlpha = gl_Color.w;

	vec3 n,halfV,viewV,ldir;
	float NdotL,NdotHV;
	vec4 color = ambientGlobal;
	float att;
		
	n = normalize(normal);
	
	NdotL = max(dot(n,normalize(lightDir)),0.0);


	if (NdotL > 0.0) 
	{
		att = 1.0 / (gl_LightSource[0].constantAttenuation + 

gl_LightSource[0].linearAttenuation * dist + gl_LightSource[0].quadraticAttenuation * dist * dist);
		color += att * (diffuse * NdotL + ambient);
		
			
		halfV = normalize(halfVector);
		NdotHV = max(dot(n,halfV),0.0);
		color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular 		* 

pow(NdotHV,gl_FrontMaterial.shininess);
	}


	color.w = startAlpha;

	gl_FragColor = color*outColor;

}


Vertex Shader
varying vec4 diffuse,ambientGlobal,ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;

void main()
{
	gl_TexCoord[0] = gl_MultiTexCoord0; 
	

	//Per Pixel Lighting
	vec4 ecPos;
	vec3 aux;
		
	normal = normalize(gl_NormalMatrix * gl_Normal);
	
	ecPos = gl_ModelViewMatrix * gl_Vertex;
	aux = vec3(gl_LightSource[0].position-ecPos);
	lightDir = normalize(aux);
	dist = length(aux);
	
	halfVector = normalize(gl_LightSource[0].halfVector.xyz);
		
	
	diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;

	ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;

	ambientGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient;
		

	gl_Position = ftransform();

}


One thing left to say: I don't get this errors, when I disable texture mipmapping, but the results without mipmapping (lot of noise) are even less useable... Oh and, how can I use more than 3-4 textures for splatting? Thanks!
V-man
V-man
Looks like some of the lower mipmaps have problems. Are you using gluBuild2DMipmaps?
Is it RGB or RGBA texture?
starvinmarvin
starvinmarvin
Quote:
Original post by V-man
Looks like some of the lower mipmaps have problems. Are you using gluBuild2DMipmaps?
Is it RGB or RGBA texture?


Yes I'm using it. They are in RGB format.

Calling following functions in right order:
glBindTexture();
glGetTexImage();
gluBuild2DMipmaps();
glTexSubImage2D();

I'm doing this to allow painting on the terrain. Reading the alpha texture data in memory, modify it, build mipmaps, and then call gltexsubimage2d()
V-man
V-man
If this was year 2000, you would call gluBuild2DMipmaps but nowadays have the GPU do it.
http://www.opengl.org/wiki/Texture_Mapping#How_to_create_a_texture

and also use a simple shader like
gl_FragColor = texture2D( Grass, texUV.xy * 64.0 );

to see if the problem is the mipmaps or just your shader.
dpadam450
dpadam450
Quote:
Oh and, how can I use more than 3-4 textures for splatting?


3D textures, or texture arrays (DX10, not sure how they work in GL).

Quote:
If this was year 2000, you would call gluBuild2DMipmaps but nowadays have the GPU do it.
http://www.opengl.org/wiki/Texture_Mapping#How_to_create_a_texture


He said hes "painting" it, so at run time the texture is changing. So my question would be, if you bind a texture and call glTexImage2D every frame, will it still mip-map that texture every frame? I would actually think so, but I've never messed with it.

Also, the mip-map algorithm works based on triangle size on screen. So when I mip-mapped the giant splat texture, everything far away looked like crap because it wasnt using the original splat texture for that triangle, but it was using a very small mip-map (4x4) and obviously that doesnt work correctly because yea.

Try not mip-mapping the splat index texture and see what happens.
NBA2K, Madden, Maneater, Killing Floor, Sims 
V-man
V-man
Quote:
Original post by dpadam450
He said hes "painting" it, so at run time the texture is changing. So my question would be, if you bind a texture and call glTexImage2D every frame, will it still mip-map that texture every frame? I would actually think so, but I've never messed with it.


yes it will. Also, you should call glTexSubImage2D to update a texture, not glTexImage2D.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.