• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Setting Color and Alpha Channel

12 posts in this topic

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
-2

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

 

 

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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 [url="http://www.gamedev.net/topic/641583-where-can-i-find-the-d3dfvf-format/"]here[/url], 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).

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


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

1

Share this post


Link to post
Share on other sites

@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 );
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0