Using Vertex Paint in DirectX and C++

Started by
4 comments, last by Nik02 11 years, 10 months ago
I have made a terrain in 3Ds Max, added "Vertex Paint" modifier and exported the mesh as .x file.

I imported the .x file in DirectX/C++, what I am trying to do now is to set the main texture as stone and and "Vertex Paint" texture as grass.

How can I do that?
Advertisement
If the exporter writes the vertex paint channel as vertex colors (though I doubt it), you can write a pixel shader that gets the interpolated vertex color and does texture mixing with the color data.

In general, Max-specific modifiers are poorly supported in file exporters. This may have changed in the past years, I haven't used Max for a while.

One option, though time-consuming, is to define your own file format and write an exporter that does export the channel generated by the modifier. The Max SDK used to have a comprehensive sample about this.

Niko Suni

Can you give example on the the Shader code that I can use to mix textures?
You'd just use the interpolated vertex color components as weights by which you apply the textures. The vertex shader would pass thru the vertex colors, and the pixel shader would look something like this:


// compile with < SM4
// vertex shader not illustrated here, but it should produce the VS_OUT
struct VS_OUT
{
float4 pos : POSITION; // ordinary position
float4 tc : TEXCOORD0; // ordinary texture coordinate
float4 textureWeights : COLOR; // vertex color represents texture weights; r component = rock weight and g = grass weight
};
Texture2D rockTexture;
Texture2D grassTexture;
sampler rockSampler = sampler_state
{
Texture = <rockTexture>;
// set filters here as needed
};
sampler grassSampler = sampler_state
{
Texture = <grassTexture>;
};

float4 ps(VS_OUT input) : COLOR
{
float4 ret = float4(0,0,0,0); // or whatever your "base" color is
ret = lerp (ret, tex2D (rockSampler, input.tc), input.textureWeights.r); // add the contribution of the rock texture
ret = lerp (ret, tex2D (grassSampler, input.tc), input.textureWeights.g); // add the contribution of the grass texture
// you'd still have blue and alpha free for two more textures
return ret;
}


Of course, the actual PS logic would depend on how you author the textures and vertex colors.

Niko Suni

Texture = <rockTexture>;
Texture = <grassTexture>;

Since the textures will be encrypted image files, is it possible that I use the textures that I assigned using Device->SetTexture(0, ...); and Device->SetTexture(, ...);
If you use shaders in D3D9 without using the Effect framework, your approach is the only one anyway smile.png

You can find the sampler indices of named sampler variables from compiled shader by using the ID3DXConstantTable interface. Note that in D3D9, a given texture is always bound to a given sampler index; in D3D10 and up, textures and sampler states are more separated.

Alternatively, you can force the texture variables to slots of your choice, by declaring them in the shader file as follows:

rockTexture : register(t0)
grassTexture : register(t1)


Then, SetTexture(0, ...) would set the rock texture, and SetTexture(1, ...) would set the grass texture.

Niko Suni

This topic is closed to new replies.

Advertisement