Setting Color and Alpha Channel

Started by
11 comments, last by Medo Mex 10 years, 11 months ago
I'm trying to set the color and alpha channel for volumetric lines.
How do I set the lines diffuse color and alpha channel?
I tried adding DWORD color; to the vertex structure and setting the FVF to:
static const DWORD FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_NORMAL |
D3DFVF_TEX2 | // D3DFVF_TEX2 specifies we have two sets of texture coordinates.
D3DFVF_TEXCOORDSIZE4(0) | // This specifies that the first (0th) tex coord set has size 4 floats.
D3DFVF_TEXCOORDSIZE3(1); // Specifies that second tex coord set has size 2 floats.
But, not working.
Vertex structure:

struct TVertex
{
    D3DXVECTOR3 pos;
    D3DXVECTOR3 otherPos;
    D3DXVECTOR4 texOffset;
    D3DXVECTOR3 thickness;
    static const DWORD FVF = D3DFVF_XYZ | D3DFVF_NORMAL |
    D3DFVF_TEX2 | // D3DFVF_TEX2 specifies we have two sets of texture coordinates.
    D3DFVF_TEXCOORDSIZE4(0) | // This specifies that the first (0th) tex coord set has size 4 floats.
    D3DFVF_TEXCOORDSIZE3(1); // Specifies that second tex coord set has size 2 floats.
};

Effect file:


//////////////////////
// Volume line shader. Based on the CG Volume Lines sample from NVidia.

float4x4 mWVP : WorldViewProjection;
float4x4 mWV : WorldView;

texture lineTexture;

sampler textureSampler = sampler_state
{
Texture = (lineTexture);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

struct TInputVertex
{
float4 pos : POSITION; // Position of this vertex
float4 otherPos : NORMAL; // Position of the other vertex at the other end of the line.
float4 texOffset : TEXCOORD0; // Tex coord offset.
float3 thickness : TEXCOORD1; // Thickness info.
};

struct TOutputVertex
{
float4 hPos : POSITION;
float blend : COLOR0;
float4 tex0 : TEXCOORD0;
float4 tex1 : TEXCOORD1;
};

TOutputVertex VolumeLineVS( TInputVertex IN )
{
TOutputVertex OUT = (TOutputVertex)0;

// World*View transformation.
float4 posStart = mul( IN.pos, mWV );
float4 posEnd = mul( IN.otherPos, mWV );

// Unit vector between eye and center of the line.
float3 middlePoint = normalize( (posStart.xyz + posEnd.xyz) / 2.0 );

// Unit vector of the line direction.
float3 lineOffset = posEnd.xyz - posStart.xyz;
float3 lineDir = normalize( lineOffset );
float lineLenSq = dot( lineOffset, lineOffset );

// Dot product to compute texture coef
float texCoef = abs( dot( lineDir, middlePoint ) );

// Change texture coef depending on line length: y=(Sz/(l^2))(x-1)+1
texCoef = max( ( (texCoef - 1) * (lineLenSq / IN.thickness.z ) ) + 1, 0 );

posStart = mul( IN.pos, mWVP );
posEnd = mul( IN.otherPos, mWVP );

// Project these points in screen space.
float2 startPos2D = posStart.xy / posStart.w;
float2 endPos2D = posEnd.xy / posEnd.w;

// Calculate 2D line direction.
float2 lineDir2D = normalize( startPos2D - endPos2D );

// Shift vertices along 2D projected line
posStart.xy += ((texCoef * IN.texOffset.x) * lineDir2D.xy);

// Shift vertex for thickness perpendicular to line direction.
lineDir2D *= IN.thickness.x;
posStart.x += lineDir2D.y;
posStart.y -= lineDir2D.x;

OUT.hPos = posStart;

// Compute tex coords depending on texCoef.
float4 tex;
tex.zw = float2(0,1);
tex.y = min(15.0/16.f, texCoef);
tex.x = modf(tex.y * 4.0, tex.y);
OUT.blend = modf(tex.x * 4.0, tex.x);
tex.xy = (tex.xy / 4.0) + (IN.texOffset).zw;
tex.y = 1-tex.y;
OUT.tex0 = tex;


// Now get the second texture coord : increment.
tex.y = min(texCoef + (1.0/16.f), 15.0/16.0 );
tex.x = modf(tex.y * 4.0, tex.y);
tex.x = floor(tex.x * 4.0);
tex.xy = (tex.xy / 4) + (IN.texOffset).zw;
tex.y = 1-tex.y;
OUT.tex1 = tex;

return OUT;
}


float4 VolumeLinePS( TOutputVertex IN ) : COLOR
{
float4 blendFactor = IN.blend;
float4 c0 = tex2D( textureSampler, IN.tex0 );
float4 c1 = tex2D( textureSampler, IN.tex1 );
return lerp( c0, c1, blendFactor );
}


technique VolumeLine
{
pass p0
{
CullMode = none;
AlphaBlendEnable = true;
SrcBlend = one;
DestBlend = one;
alpharef = 0.9;
alphafunc = GreaterEqual;
ZEnable = false;
VertexShader = compile vs_2_0 VolumeLineVS();
PixelShader = compile ps_2_0 VolumeLinePS();
}
}
Advertisement

Up.

Up. Any assistance?

Sorry, this doesn't make any sense.

The code you have supplied is pre-change; can you post the code that's not working instead?

Also, you really need to define what you mean by "not working". What did you expect to happen? What actually happened?

Your question really reads as though you just want someone to give you working code for this. Can you put more effort into showing that you've actually tried something yourself, please?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

How do I set the vertex color from FFP while I still use the effect file?

I want to add DWORD color; to the vertex structure (FVF) while I still use the effect file.

You can mix FVF for vertex structure in D3D9 C++ and also use an effect file that provides a vertex shader. The only thing you must take care to do is match the FVF declaration to the vertex shader's input data structure exactly.

Therefore if you want to use D3DFVF_DIFFUSE in your FVF, then you must adapt TInputVertex:

New version:

 
struct TInputVertex
{
    float4 pos : POSITION; // Position of this vertex
    float4 otherPos : NORMAL; // Position of the other vertex at the other end of the line.
    float4 texOffset : TEXCOORD0; // Tex coord offset.
    float3 thickness : TEXCOORD1; // Thickness info.
    float4 color: COLOR0;
}; 
 
struct TVertex
{
    D3DXVECTOR3 pos;
    D3DXVECTOR3 otherPos;
    D3DXVECTOR4 texOffset;
    D3DXVECTOR3 thickness;
    DWORD Color;
    static const DWORD FVF = D3DFVF_XYZ | D3DFVF_NORMAL |
     D3DFVF_TEX2 | // D3DFVF_TEX2 specifies we have two sets of texture coordinates.
     D3DFVF_TEXCOORDSIZE4(0) | // This specifies that the first (0th) tex coord set has size 4 floats. 
    D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_DIFFUSE; // Specifies that second tex coord set has size 2 floats.
};
 

I tried that, now I don't see the volumetric lines anymore.

Here is my new effect file:


//////////////////////
// Volume line shader. Based on the CG Volume Lines sample from NVidia.


float4x4 mWVP : WorldViewProjection;
float4x4 mWV : WorldView;

texture lineTexture;

sampler textureSampler = sampler_state
{
Texture = (lineTexture);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

struct TInputVertex
{
        float4 pos : POSITION; // Position of this vertex
        float4 otherPos : NORMAL; // Position of the other vertex at the other end of the line.
        float4 texOffset : TEXCOORD0; // Tex coord offset.
        float3 thickness : TEXCOORD1; // Thickness info.
        float4 color     : COLOR0;
};




struct TOutputVertex
{
float4 hPos : POSITION;
float blend : COLOR0;
float4 tex0 : TEXCOORD0;
float4 tex1 : TEXCOORD1;
};




TOutputVertex VolumeLineVS( TInputVertex IN )
{
TOutputVertex OUT = (TOutputVertex)0;


// World*View transformation.
float4 posStart = mul( IN.pos, mWV );
float4 posEnd = mul( IN.otherPos, mWV );


// Unit vector between eye and center of the line.
float3 middlePoint = normalize( (posStart.xyz + posEnd.xyz) / 2.0 );


// Unit vector of the line direction.
float3 lineOffset = posEnd.xyz - posStart.xyz;
float3 lineDir = normalize( lineOffset );
float lineLenSq = dot( lineOffset, lineOffset );


// Dot product to compute texture coef
float texCoef = abs( dot( lineDir, middlePoint ) );


// Change texture coef depending on line length: y=(Sz/(l^2))(x-1)+1
texCoef = max( ( (texCoef - 1) * (lineLenSq / IN.thickness.z ) ) + 1, 0 );


posStart = mul( IN.pos, mWVP );
posEnd = mul( IN.otherPos, mWVP );


// Project these points in screen space.
float2 startPos2D = posStart.xy / posStart.w;
float2 endPos2D = posEnd.xy / posEnd.w;


// Calculate 2D line direction.
float2 lineDir2D = normalize( startPos2D - endPos2D );


// Shift vertices along 2D projected line
posStart.xy += ((texCoef * IN.texOffset.x) * lineDir2D.xy);


// Shift vertex for thickness perpendicular to line direction.
lineDir2D *= IN.thickness.x;
posStart.x += lineDir2D.y;
posStart.y -= lineDir2D.x;


OUT.hPos = posStart;


// Compute tex coords depending on texCoef.
float4 tex;
tex.zw = float2(0,1);
tex.y = min(15.0/16.f, texCoef);
tex.x = modf(tex.y * 4.0, tex.y);
OUT.blend = modf(tex.x * 4.0, tex.x);
tex.xy = (tex.xy / 4.0) + (IN.texOffset).zw;
tex.y = 1-tex.y;
OUT.tex0 = tex;


// Now get the second texture coord : increment.


tex.y = min(texCoef + (1.0/16.f), 15.0/16.0 );
tex.x = modf(tex.y * 4.0, tex.y);
tex.x = floor(tex.x * 4.0);
tex.xy = (tex.xy / 4) + (IN.texOffset).zw;
tex.y = 1-tex.y;
OUT.tex1 = tex;


return OUT;
}




float4 VolumeLinePS( TOutputVertex IN ) : COLOR
{
float4 blendFactor = IN.blend;
float4 c0 = tex2D( textureSampler, IN.tex0 );
float4 c1 = tex2D( textureSampler, IN.tex1 );
return lerp( c0, c1, blendFactor );
}




technique VolumeLine
{
pass p0
{
CullMode = none;
AlphaBlendEnable = true;
SrcBlend = one;
DestBlend = one;
alpharef = 0.9;
alphafunc = GreaterEqual;
ZEnable = false;
VertexShader = compile vs_2_0 VolumeLineVS();
PixelShader = compile ps_2_0 VolumeLinePS();
}
}

Have you debugged the shader in PIX (or VS 2012)?

I'm not having any problem with the shader, it's working, however I don't see the volumetric lines, I only see the lines when I remove D3DFVF_DIFFUSE from the vertex structure.

Maybe the shader code need to be updated?

Yes. When you change the FVF you have to change both your vertex struct and your shader(s). You have a mismatch which indeed is best diagnosed with PIX, usually the debug runtimes will also complain.

FVF are flags, the order the vertex elements have to take are predefined: Read here, especially NightCreature83's link to the image and my last entry.

For a so-called tint you pass along the color from vertex to pixel shader (float4 tint: COLOR) and finally simply multiply with the color you received from the texture sampling.

Alternatively, if you want to tint globally (all lines) you could as well use a shader uniform (like you do with WorldViewProjection).

This topic is closed to new replies.

Advertisement