Jump to content
  • Advertisement
Sign in to follow this  
Yura

Shaders implementation

This topic is 2087 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

Hi, please help me resolve next problems:

 

1) how to draw triangle list and line list (like this http://imageshack.us...capture2pu.png/)

with different colours from one vertex buffer? I have 1 buffer which contains points and colors. I have 2 index buffers for it (for line list and triangle list). I want to draw triangles with color from buffer and white (or black or whatever) lines.




2) I've tried to implement shaders to make different front and back triangle colors, but it doesn't work:


//drawing function:

 

private void RedrawPrimitives()
        {

            var effect = Effect.FromFile(d3dDevice, "Shader.fx", ShaderFlags.None);
            var technique = effect.GetTechnique(0);
            var pass = effect.GetPass(technique, 0);
            
            d3dDevice.VertexFormat = myVertexFormat;
            d3dDevice.VertexDeclaration = vertexDecl;

            d3dDevice.Clear(ClearFlags.Target, SharpDX.Color.White, 1.0f, 0);
            d3dDevice.BeginScene();

            effect.Technique = technique;
            effect.Begin();
            effect.BeginPass(0);

            d3dDevice.Indices = triangles;
            d3dDevice.SetStreamSource(0, vertices, 0, Marshal.SizeOf(typeof(Vertex)));
            d3dDevice.DrawIndexedPrimitive(PrimitiveType.TriangleList, 0, 0, totalPointsCount, 0, totalPointsCount / 2);

            
            d3dDevice.Indices = lines;          
            d3dDevice.SetStreamSource(0, vertices, 0, Marshal.SizeOf(typeof(Vertex)));
            d3dDevice.DrawIndexedPrimitive(PrimitiveType.LineList, 0, 0, totalPointsCount, 0, totalPointsCount);

            effect.EndPass();
            effect.End();

            d3dDevice.EndScene();
            d3dDevice.Present();

            effect.Dispose();
            technique.Dispose();
            pass.Dispose();
        }

 

//and shader (which doesn't work)

 

sampler s0;

struct VertexShaderInput
{
    float4 Position : POSITION0;
	float4 Color : COLOR0;
	float4 face : VFACE;

    // TODO: add input channels such as texture
    // coordinates and vertex colors here.
};

struct VertexShaderOutput 
{
    float4 Position : POSITION0;
	float4 Color : COLOR0;

    // TODO: add vertex shader outputs such as colors and textureasdfasd
    // coordinates here. These values will automatically be interpolated
    // over the triangle, and provided as input to your pixel shader.
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
	VertexShaderOutput output;
	output.Position = mul(input.Position, input.Color);
	output.Color=input.Color;
    // TODO: add your vertex shader code here.

    return output;
}

float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
{
    float4 color = tex2D(s0, coords);
	color.rgb = color.r;

    return color;
}

technique Technique1
{
    pass Pass1
    {
		VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

 

 

Share this post


Link to post
Share on other sites
Advertisement
1. What have you tried?

2. Define "doesn't work". What happens?
Nothing happens! I can see only blank screen (white, to be exect). There are no compile exceptions. Without shaders everything works fine, I can build triangles and lines.
1. What I've tried and trying to do is written above. ?ertainly not as a book says, but I think it is clear. If you need more information, just say what exactly you need and i'll post it here. Edited by Yura

Share this post


Link to post
Share on other sites

I see that your PixelShader function is supposed to get a VertexShaderOutput as a parameter, but instead you're passing it this :

"float2 coords: TEXCOORD0". It should rather be "VertexShaderOutput coords".

 

Other than this, I would suggest to go through some HLSL examples from DirectX SDK and more from other good online references. That will definitely help.

Share this post


Link to post
Share on other sites
To explain NewDisplayName's post a little more: you're returning the COLOR0 semantic from your vertex shader, but you're not using COLOR0 as an input to your pixel shader. Instead you're using TEXCOORD0 (which wasn't even passed out from your vertex shader, so it will not contain valid values) and sampling from some texture.

Presumably you want to have COLOR0 as an input to your pixel shader (and perhaps just return that value as the output? Not sure, because I don't know what you're trying to do with the texture).

Share this post


Link to post
Share on other sites
Sory for troubling guys, but as you can see, I'm very new in shaders. I wrote a program on direct3d9 which is drawing some surface from triangle list primitives. I have made rotation functions, which allows me to rotate this surface. All of it is made without using any shaders and it works. It works nice, fast, but with one defect. This surface is half transparent. I've tried to blend alfa component, tried to set color in RGB mode but all without success. They are still half transparent. So, now I'm trying to fix it with help of shaders and to add new features - draw this primitives with different colors on front and back sides.

I've made some changes to my shader, now it works, but color is still half transparent. Somebody knows how to fix it? Edited by Yura

Share this post


Link to post
Share on other sites
Well disabling blending would be device.SetRenderState(RenderState.AlphaBlendEnable, false); then. I misunderstood that in your previous thread. Not sure if this actually solves your problem. Can you provide a screenshot and what other render states you used ? And since one can change render states with the effect framework, the shader file is relevant, too. (Please, don't delete code in your posts, rather update).

Share this post


Link to post
Share on other sites
Well disabling blending would be device.SetRenderState(RenderState.AlphaBlendEnable, false); then. I misunderstood that in your previous thread. Not sure if this actually solves your problem. Can you provide a screenshot and what other render states you used ? And since one can change render states with the effect framework, the shader file is relevant, too. (Please, don't delete code in your posts, rather update).


My image contains many colors, but it's ok, each point has it's own color and they might be different. The problems are:
1) As you can see, image is half transparent. Enabling/disabling alpha blend gives nothing.
2) Over the triangles I want to draw Lines from the same vertex buffer but with different color (white, for example). Drawing lines is not a problem, but how can I ignore their color, declarated in vertex buffer?

Render states, which I currently use:

 

d3dDevice.SetRenderState(RenderState.Lighting, 0); // Turn off D3D Lighting
d3dDevice.SetRenderState(RenderState.CullMode, Cull.None);
d3dDevice.SetRenderState(RenderState.FillMode, FillMode.Solid);
d3dDevice.SetRenderState(RenderState.AlphaBlendEnable, false);

 

 

and shader

 

struct VertexShaderInput
{
float4 Position : POSITION0;
float4 Color : COLOR0;
};

struct PixelShaderInput
{
float4 Color : COLOR0;
float face : VFACE;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 Color : COLOR0;
};

float4x4 worldViewProj;

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;

output.Position = mul(input.Position, worldViewProj);
output.Color = input.Color;
output.Color.a = 0.0f;

return output;
}

float4 PixelShaderFunction(PixelShaderInput input) : COLOR0
{
float4 back = float4(200.0f, 0.0f, 0.0f, 0.0f);
//input.Color.a = 0.0f;

if(input.face>0)
{
return input.Color;
}
else
{
return input.Color;
}

}

technique Technique1
{
pass Pass1
{
VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 PixelShaderFunction();
}
}

 

 

Also I've tried to enable depth buffer (ZBuffer) but id didn't help. Picture was drawn same as without it.

 

d3dDevice = new Device(
                         d3d,
                         0,
                         DeviceType.Hardware,
                         panel1.Handle,
                         CreateFlags.HardwareVertexProcessing,                       
                         new PresentParameters()
                         {
                             Windowed = true,
                             SwapEffect = SwapEffect.Discard,
                             BackBufferFormat = Format.Unknown,
                             PresentationInterval = PresentInterval.Immediate,
                             BackBufferWidth = panel1.Width,
                             BackBufferHeight = panel1.Height,
                             EnableAutoDepthStencil = true,
                             AutoDepthStencilFormat = Format.D16
                         });

 

 

 

 

If you need more information, please let me know and I will post it

Edited by Yura

Share this post


Link to post
Share on other sites

Yura,

 

Just check this method in your shader and see if you think this is how it should be (because I definitely don't think so).

 

float4 PixelShaderFunction(PixelShaderInput input) : COLOR0  // Use VertexShaderOutput
{
float4 back = float4(200.0f, 0.0f, 0.0f, 0.0f); // why?
//input.Color.a = 0.0f;
 
if(input.face>0)
{
return input.Color; // nothing
}
else
{
return input.Color; // nothing
}
 
}

Share this post


Link to post
Share on other sites

Yura,

 

Just check this method in your shader and see if you think this is how it should be (because I definitely don't think so).

 

float4 PixelShaderFunction(PixelShaderInput input) : COLOR0  // Use VertexShaderOutput
{
float4 back = float4(200.0f, 0.0f, 0.0f, 0.0f); // why?
//input.Color.a = 0.0f;
 
if(input.face>0)
{
return input.Color; // nothing
}
else
{
return input.Color; // nothing
}
 
}

I've implemented VFACE semantic. If I'm looking to a front of the primitive (face>0), I'm returning true vertex color, else, I'm returning red color (back). It works fine, I've just disabled it for a time, so, independent of VFACE I'm returning true color of vertex.

 

Thanks for suggestion!

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!