Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Setting Color and Alpha Channel


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Medo3337   Members   -  Reputation: 671

Like
-2Likes
Like

Posted 17 May 2013 - 02:06 PM

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();
}
}

Edited by Medo3337, 21 May 2013 - 01:25 PM.


Sponsor:

#2 Medo3337   Members   -  Reputation: 671

Like
0Likes
Like

Posted 18 May 2013 - 02:35 PM

Up.



#3 Medo3337   Members   -  Reputation: 671

Like
0Likes
Like

Posted 21 May 2013 - 11:49 AM

Up. Any assistance?



#4 mhagain   Crossbones+   -  Reputation: 8134

Like
0Likes
Like

Posted 21 May 2013 - 11:59 AM

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?


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 Medo3337   Members   -  Reputation: 671

Like
0Likes
Like

Posted 21 May 2013 - 01:26 PM

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.



#6 Steve_Segreto   Crossbones+   -  Reputation: 1538

Like
0Likes
Like

Posted 21 May 2013 - 02:39 PM

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.
};
 


#7 Medo3337   Members   -  Reputation: 671

Like
0Likes
Like

Posted 21 May 2013 - 03:21 PM

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();
}
}

 

 



#8 phil_t   Crossbones+   -  Reputation: 3945

Like
1Likes
Like

Posted 21 May 2013 - 04:17 PM

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



#9 Medo3337   Members   -  Reputation: 671

Like
0Likes
Like

Posted 21 May 2013 - 06:17 PM

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?



#10 unbird   Crossbones+   -  Reputation: 5498

Like
0Likes
Like

Posted 21 May 2013 - 06:42 PM

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).



#11 Medo3337   Members   -  Reputation: 671

Like
0Likes
Like

Posted 21 May 2013 - 07:12 PM

I don't have much experience with shaders, so I will need alittle more help here.

 

You said I should multiply IN.color with the texture color in Pixel Shader, how? should I use mul?

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 );
}

 

BTW, I'm not getting assertion failure or any runtime complain.

 

EDIT:

 

I tried the following vertex structure:

struct TVertex
{
D3DXVECTOR3 pos;
D3DXVECTOR3 otherPos;
DWORD color;
D3DXVECTOR4 texOffset;
D3DXVECTOR3 thickness;
static const DWORD FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE |
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.
};

 

Now, I can see the lines, but I don't see the diffuse color applied, maybe I have to change the Pixel Shader code?



#12 phil_t   Crossbones+   -  Reputation: 3945

Like
1Likes
Like

Posted 21 May 2013 - 07:25 PM

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 ) * IN.color;
}

 

 

Regarding a per-vertex tint, you would do it like the above ^^^.

 

But of course you need to add color to your TOutputVertex:

 

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

 

 

 

.. and pass it through in your vertex shader, e.g.

 


OUT.color = IN.color;

 

 

 

 

If you're already having problems getting things to show up though, I wouldn't bother with a tint until you have everything else working.



#13 Medo3337   Members   -  Reputation: 671

Like
0Likes
Like

Posted 21 May 2013 - 07:39 PM

@phil_t: Nice! Now I got it to work with the diffuse color and I see that the color changed, however the alpha channel is not working.

 

// Getting different color but the alpha channel is not getting affected
vrts[0].color = D3DCOLOR_RGBA(255, 0, 255, 1);
vrts[1].color = D3DCOLOR_RGBA(255, 0, 255, 1);
vrts[2].color = D3DCOLOR_RGBA(255, 0, 255, 1);
vrts[3].color = D3DCOLOR_RGBA(255, 0, 255, 1);

 

I also want to say that the alpha channel is not getting affected only when I use D3DBLEND_ONE:
device->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_ONE );
device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
device->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD );





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS