Sign in to follow this  
BlackJoker

WireFrame using HLSL 5

Recommended Posts

Hello,

 

Could someone write how to make wireframe using HLSL?

Now I have the following code inside vertex shader:

 

cbuffer MatrixBuffer
{
	matrix worldMatrix;
	matrix viewMatrix;
	matrix projectionMatrix;
};


//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
    float4 position : POSITION;
	float2 tex : TEXCOORD0;
	float3 normal : NORMAL;
	
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
	float3 normal : NORMAL;
};


////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType LightVertexShader(VertexInputType input)
{
	const float C = 0.1f;
	float Far = 100000000.0f;
    PixelInputType output;
    
	
	// Change the position vector to be 4 units for proper matrix calculations.
    input.position.w = 1.0f;

	// Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(input.position, worldMatrix); //???????????, ???????? ? ???????? ?????????
    output.position = mul(output.position, viewMatrix); 
    output.position = mul(output.position, projectionMatrix); 
    

	output.position.z = log(C*output.position.z + 1) / log(C*Far + 1) * output.position.w;

	// Store the texture coordinates for the pixel shader.
	output.tex = input.tex;
    
	// Calculate the normal vector against the world matrix only.
    output.normal = mul(input.normal, (float3x3)worldMatrix);
	
    // Normalize the normal vector.
    output.normal = normalize(output.normal);

    return output;
}

 

What I need to add to have a possibility switch wireframe on and off using only shader?

Share this post


Link to post
Share on other sites

Maybe...

 

technique LightVertexShader

{

 

  pass P0

  {

 

     FillMode          = WireFrame;

  }

}

 

 

Works for me. Of course, you probably have to make it a global var in the shader if you want to turn it on and off....but that raises the question, if you are turning it on and off, why not just do it setting the render state? That's how I always do it. Hope this helps.

 

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.renderstate_members%28v=xnagamestudio.31%29.aspx

Share this post


Link to post
Share on other sites
Why not just switch the rasterizer states?
//FillState and WireframeState have already been defined and created...
pImmediateContext->RSSetState(FillState);
//draw complete objects
pImmediateContext->RSSetState(WireframeState);
//draw wire objects

Share this post


Link to post
Share on other sites

Draw it once with the FillState and draw it again with WireframeState?

OK, but if I will do it for a lot amout of objects at the same time is this technique will not influence on performance?

There is an impact on performance for every time you switch a state.  That is why people usually collect objects into groups of similar states and process them all together to minimize state changes.  Doing something inside the shader will most likely be much more expensive than switching the rasterizer state directly though - so if performance is your goal then you should definitely try out using multiple rasterizer states and using them accordingly.

Share this post


Link to post
Share on other sites

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