Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Using Vertex Paint in DirectX and C++

This topic is 2261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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(, ...);

Share this post


Link to post
Share on other sites
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. Edited by Nik02

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!