[SlimDX] Multitexturing & a cursor on the terrain

Started by
3 comments, last by RyxiaN 13 years ago
Hey, I've got a slight problem when I'm trying to render a cursor on the terrain. I've got a grid of 100*100 vertices and a shader that does multitexturing with 2 different textures that works great. I looked at some XNA code which did exactly what I wanted: displaying a ground cursor on the terrain. It works great at close views, let me show you some pictures.

Here's the cursor I want to display: (Temporarily used from the code I found, for testing & learning)
4ttwli.png

Here's what it looks like at a close view, with and without wireframe mode on:
t5g42a.png
112fzx2.png

And this is what happens when you zoom out:
15x6at4.png
fufjd.png

Basically the the cursor is rendered across the entire terrain but I'm clamping the coordinates, so I'm assuming when I zoom out the texture gets so small that the edges are yellow aswell. I've done it exactly how the sample that I looked in did it, and it works perfect in the sample. So I don't get whats wrong.

Here's the HLSL code:
// Variables
float4x4 View;
float4x4 Projection;
float4x4 World;
float3 LightDirection;
float Ambient;
bool EnableLighting;

// Cursor
float3 GroundCursorPosition;
texture GroundCursorTexture;
int GroundCursorSize;
bool ShowGroundCursor;

// Textures
texture t0;
float t0scale;
texture t1;
float t1scale;

sampler t0sampler = sampler_state {
Texture = (t0);
MagFilter = Linear;
MinFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler t1sampler = sampler_state {
Texture = (t1);
MagFilter = Linear;
MinFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler CursorSampler = sampler_state {
Texture = (GroundCursorTexture);
MagFilter = Linear;
MipFilter = Linear;
MinFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};

struct VS_INPUT {
float4 Position : POSITION0;
float3 Normal : NORMAL;
float2 TextureCoordinate : TEXCOORD0;
float2 Weights : TEXCOORD1;
};

struct VS_OUTPUT {
float4 Position : POSITION0;
float2 TextureCoordinate : TEXCOORD0;
float3 Position3D : TEXCOORD1;
float2 Weights : TEXCOORD2;
float3 Normal : TEXCOORD3;
float Depth : TEXCOORD4;
};

void Transform(in VS_INPUT Input, out VS_OUTPUT Output) {
Output.Position = mul(Input.Position, mul(World, mul(View, Projection)));
Output.Position3D = mul(Input.Position, World);
Output.TextureCoordinate = Input.TextureCoordinate;
Output.Normal = Input.Normal;
Output.Weights = Input.Weights;
Output.Depth = Output.Position.z / Output.Position.w;
}

struct PS_INPUT {
float3 Position : POSITION0;
float2 TextureCoordinate : TEXCOORD0;
float3 Position3D : TEXCOORD1;
float2 Weights : TEXCOORD2;
float3 Normal : TEXCOORD3;
float Depth : TEXCOORD4;
};

struct PS_OUTPUT {
float4 Color : COLOR0;
};

void Texture(in PS_INPUT Input, out PS_OUTPUT Output) {

float blendDistance = 0.96f;
float blendWidth = 0.040f;
float blendFactor = clamp((Input.Depth - blendDistance) / blendWidth, 0, 1);

float4 farColor;
farColor = tex2D(t0sampler, Input.TextureCoordinate * t0scale) * Input.Weights[0];
farColor += tex2D(t1sampler, Input.TextureCoordinate * t1scale) * Input.Weights[1];

float4 nearColor;
float2 nearTexCoord = Input.TextureCoordinate * 3;
nearColor = tex2D(t0sampler, nearTexCoord * t0scale) * Input.Weights[0];
nearColor += tex2D(t1sampler, nearTexCoord * t1scale) * Input.Weights[1];

float4 a = tex2D(t0sampler, Input.TextureCoordinate * t0scale);
float4 b = tex2D(t1sampler, Input.TextureCoordinate * t1scale);

float4 first = a * Input.Weights[0];
float4 second = b * Input.Weights[1];

float4 color = first + second;

Output.Color = color;

if (ShowGroundCursor) {
float cursorScale = 1.0;
float4 cursorColor = tex2D(CursorSampler, (Input.TextureCoordinate * (cursorScale / GroundCursorSize)) - (GroundCursorPosition.xz * (cursorScale / GroundCursorSize)) + 0.5);
Output.Color.rgb += cursorColor.rgb;
}

Output.Color.a = 1;

//Output.Color = lerp(nearColor, farColor, blendFactor);
}

void Wireframe(in VS_INPUT Input, out VS_OUTPUT Output) {
Output.Position = mul(Input.Position, mul(World, mul(View, Projection)));
Output.Position.y += 0.005;
}

float4 White() : COLOR0 {
return 1;
}

technique TransformTexture {
pass P0 {
VertexShader = compile vs_2_0 Transform();
PixelShader = compile ps_2_0 Texture();
}
}

technique TransformTextureWireframe {
pass P0 {
VertexShader = compile vs_2_0 Transform();
PixelShader = compile ps_2_0 Texture();
}
pass P1 {
VertexShader = compile vs_2_0 Wireframe();
PixelShader = compile ps_2_0 White();
FillMode = Wireframe;
}
}


Any help is appreciated! :D
Thanks in advance!
Advertisement
The zoom-out behaviour suggests that when your cursor texture has been downsampled to generate lower MIP levels the yellow has bled to the edge of the image. When sampled close-up the high-res mip is sampled and looks OK, but from a distance the lower res mip is sampled and the yellow value at the edge is clamped indefinitely. You could generate the mip levels manually to ensure there's no bleeding to the edge, or use address mode set to 'border' and set the border colour to black.

Visit http://www.mugsgames.com

Stroids, a retro style mini-game for Windows PC. http://barryskellern.itch.io/stroids

Mugs Games on Twitter: [twitter]MugsGames[/twitter] and Facebook: www.facebook.com/mugsgames

Me on Twitter [twitter]BarrySkellern[/twitter]

Thanks a bunch, border as address mode worked great! Altough, I'm a little interested in manual mipmapping, found nothing but automatic generation of mipmaps on google. How exactly do you manually create mipmaps?
I believe the DirectX SDK download comes with a texture tool that can compile custom mipmap levels into a texture (I haven't used it for a long time, but check it out, it might do what you need.) Otherwise, Google. I just searched for "DirectX texture tool" and got a bunch of useful looking hits.

Visit http://www.mugsgames.com

Stroids, a retro style mini-game for Windows PC. http://barryskellern.itch.io/stroids

Mugs Games on Twitter: [twitter]MugsGames[/twitter] and Facebook: www.facebook.com/mugsgames

Me on Twitter [twitter]BarrySkellern[/twitter]

Aha, I thought it had something to do with coding. But thanks a bunch, helped me tons! ;D

This topic is closed to new replies.

Advertisement