• 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
XBuilder

DirectX 10 problem with normals in C#

10 posts in this topic

Hello there,

 

I'm trying to learn how to render a simple scene in DirectX 10 with SharpDX's low level wrappers. I have already created a simple

application with a single triangle on the screen and a camera. Now i am trying to add a single directed light to the scene (like in the DX10 Example 6).

 

 It works fine with hard-coded normals in the Effect-File (active line in the PS shader), but if I try to pass normals from my vector buffer (commented out line below) I don't see anything.

 

What i don't understand is why the type of the position vector should be float3 in the element definition and why is it float4 in the shader file (copied from DX10 example). And how maps the shader this two structures?

 

My setup for the test triangle (like in the DX10 Example) and my effect file follows:

 

thanks in advance!

  ShaderBytecode shaderBytes = ShaderBytecode.CompileFromFile(_ShaderFile, "fx_4_0", ShaderFlags.None, EffectFlags.None, null, null);
  _SimpleEffect = new Effect(device, shaderBytes);

  EffectTechnique technique = _SimpleEffect.GetTechniqueByName("Render");
  EffectPass pass = technique.GetPassByIndex(0);
            
  var elements = new[] {
    new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 0),
    new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 12)
  };

  _LightDir = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
  _LightDir.Normalize();
  _LightColor = new Vector4( 0.5f, 0.5f, 0.5f, 1.0f );

  _VertexLayout = new InputLayout(device, pass.Description.Signature, elements);

  _VertexStream = new DataStream(3 * 24, true, true);
  _VertexStream.Write(new Vector3(0.0f, 0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
  _VertexStream.Write(new Vector3(0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
  _VertexStream.Write(new Vector3(-0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
  _VertexStream.Position = 0;

  _Vertices = new SharpDX.Direct3D10.Buffer(device, _VertexStream, new BufferDescription()
  {
    BindFlags = BindFlags.VertexBuffer,
    CpuAccessFlags = CpuAccessFlags.None,
    OptionFlags = ResourceOptionFlags.None,
    SizeInBytes = 3 * 24,
    Usage = ResourceUsage.Default
  });

  _Camera.Position = new Vector3(0.0f, 0.0f, -20.0f);
matrix view;
matrix projection;
float4 vLightDir;
float4 vLightColor;

struct VS_IN
{
	float4 pos : POSITION;
	float3 norm : NORMAL;
};

struct PS_IN
{
	float4 pos : SV_POSITION;
	float3 norm : TEXCOORD0;
};

PS_IN VS( VS_IN input )
{
	PS_IN output = (PS_IN)0;
	
	output.pos = mul(input.pos, view);
	output.pos = mul(output.pos, projection); 
	output.norm = input.norm;
	
	return output;
}

float4 PS( PS_IN input ) : SV_Target
{
	float4 finalColor = 0;
	//finalColor = saturate( dot((float3)vLightDir, input.norm) * vLightColor);
	finalColor = saturate(dot((float3)vLightDir, float3(0,0,1)) * vLightColor);
	finalColor.a = 1;
	return finalColor;
}

float4 PSSolid( PS_IN input) : SV_Target
{
	return float4(1,0,0,1);
}

technique10 Render
{
	pass P0
	{
		SetVertexShader( CompileShader( vs_4_0, VS() ) );
		SetGeometryShader( 0 );
		SetPixelShader( CompileShader( ps_4_0, PS() ) );
	}
}

technique10 RenderLight
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( 0 );
        SetPixelShader( CompileShader( ps_4_0, PSSolid() ) );
    }
}
0

Share this post


Link to post
Share on other sites

In the shader you use a float4 for position for transform reasons - to get the vert into projection space.  Try the following in your VS()

 

current: output.pos = mul(input.pos, view);

proposed: output.pos = mul(float4(input.pos,1), view);

 

EDIT:  I realized this isn't going to affect anything since you are defining your structure in the shader file as a float4 already.  Disregard!! 

 

Do you have a struct in your code to define what a vertex looks like to the CPU?  It looks like you are sending just simple Vector3s to the input assembler.  You should be sending a struct that has two elements, one for position and one for normal.  I apologize because i'm not familiar with that framework.

Edited by LyGuy
0

Share this post


Link to post
Share on other sites

Hello LyGuy,

 

thanks for your answer! I have tryed to write all data at once, but it doesn't changed anything. As i understand, the mapping is done by the constructor of the InputLayout class. It extracts the register information directly from the shader. But it seems that the vertex shader can't access normals from the buffer.

 

I have tried to hard-code the normals as the output of the vertex shader (line 25) and it works, but if i try to access the normal from the input (commented out line 24) it's doesn't render anything more.

PS_IN VS( VS_IN input )
{
	PS_IN output = (PS_IN)0;
	
	output.pos = mul(input.pos, view);
	output.pos = mul(output.pos, projection); 
	//output.norm = input.norm;
	output.norm = float3(0,0,1)

	return output;
}
 
float4 PS( PS_IN input ) : SV_Target
{
	float4 finalColor = 0;
	finalColor = saturate( dot((float3)vLightDir, input.norm) * vLightColor);
	finalColor.a = 1;
	return finalColor;
}
Edited by XBuilder
0

Share this post


Link to post
Share on other sites

_VertexStream = new DataStream(3 * 24, true, true);
_VertexStream.Write(new Vector3(0.0f, 0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
_VertexStream.Write(new Vector3(0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
_VertexStream.Write(new Vector3(-0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
_VertexStream.Position = 0;
 
_Vertices = new SharpDX.Direct3D10.Buffer(device, _VertexStream, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = 3 * 24,
Usage = ResourceUsage.Default
});

 

From this code it looks like your vertex buffer is being filled with vertex elements of type Vector3.  The buffer should be filled with elements that match the signature in your vertex buffer. 

 

struct SimpleVertex

{

       Vector4 Position;

       Vector3 Normal;
};

 

This of course changes your buffer description as well.  Specifically the SizeInBytes field/property will be the size of a SimpleVertex * NumberOfVerts

Edited by LyGuy
0

Share this post


Link to post
Share on other sites

I have tried it already. If I try to change the elements array as following, I get the exception "HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: Invalid Argument." on InputLayout creation. The official DirectX 10 example using the Declaration with Vector3. As i have understood the mapping and the conversion is done by the system.

 

New elements array:

var elements = new[] {
  new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32A32_Float, 0, 0),
  new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 16)
};

D3D10 Example 6:

// Define the input layout
D3D10_INPUT_ELEMENT_DESC layout[] =
{
  { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
  { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};

//...

SimpleVertex vertices[] =
{
  { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) },
   ...
}
0

Share this post


Link to post
Share on other sites


New elements array:
var elements = new[] {
new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 16)
};
D3D10 Example 6:
// Define the input layout
D3D10_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};

//...

SimpleVertex vertices[] =
{
{ D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) },
...
}

 

In the example they are using a SimpleVertex array which uses two 3 component structs for the Position and Normal respectively.  Their input layout DGXI_FORMAT matches this construct in that they are using DXGI_FORMAT_R32G32B32_FLOAT.  According to Microsoft, this format handles 3 components.

 

If you are using a SimpleVertex structure in the same way that they are, change your input layout to match as well by using the DXGI_FORMAT_R32G32B32_FLOAT format.

 

DXGI_FORMAT_R32G32B32_FLOAT

A three-component, 96-bit floating-point format that supports 32 bits per color channel.

 

0

Share this post


Link to post
Share on other sites

It the same situation as at start - nothing to see. I have now added a SimpleVertex struct to my code, but this have no effect too.

 

Code now:



    internal struct SimpleVertex
    {
        Vector3 Position;
        Vector3 Normal;

        public SimpleVertex(Vector3 pos, Vector3 norm)
        {
            Position = pos;
            Normal = norm;
        }
    };
const int VERTEX_SIZE = 24;

var elements = new[] {
  new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 0),
  new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 12)
};

_VertexLayout = new InputLayout(device, pass.Description.Signature, elements);

_VertexStream = new DataStream(3 * VERTEX_SIZE, true, true);
_VertexStream.Write(new SimpleVertex(new Vector3(0.0f, 0.5f, 0.5f),  new Vector3(0.0f, 0.0f, 1.0f)));
_VertexStream.Write(new SimpleVertex(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(0.0f, 0.0f, 1.0f)));
_VertexStream.Write(new SimpleVertex(new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(0.0f, 0.0f, 1.0f)));
_VertexStream.Position = 0;

_Vertices = new SharpDX.Direct3D10.Buffer(device, _VertexStream, new BufferDescription()
{
  BindFlags = BindFlags.VertexBuffer,
  CpuAccessFlags = CpuAccessFlags.None,
  OptionFlags = ResourceOptionFlags.None,
  SizeInBytes = 3 * VERTEX_SIZE,
  Usage = ResourceUsage.Default
});
Edited by XBuilder
0

Share this post


Link to post
Share on other sites

Where do you (or the framework) bind the buffer to the IA?  Does your debugger support graphics debugging?

 

EDIT:  If you are sure that the buffer is being bound properly to the InputAssembler then the data is likely available in the vertex shader.  Perhaps something unexpected is happening with your transforms?

Edited by LyGuy
0

Share this post


Link to post
Share on other sites

Hallo LyGuy,

 

I am using VisualStudio 2012 with SharpDX Wrapper for C# (only low level without toolkit). I compile the effect file at the runtime. I have found a graphic debugger window. I will try it out. 

 

I bind the buffer as follows (setup before rendering):

device.InputAssembler.InputLayout = _VertexLayout;
device.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_Vertices, VERTEX_SIZE, 0));

Render code:

//Draw schene
for (int i = 0; i < technique.Description.PassCount; ++i)
{
  pass.Apply();
  device.Draw(3, 0);
}
0

Share this post


Link to post
Share on other sites

Hello there,

 

I have tried to use graphics debugger, but it is no support for WPF hosted Direct3D in the debugger. As i have read, third party debuggers have no support for this scenario too.

 

So i should continue error searching without debugger sad.png 

 

Any other idea?

0

Share this post


Link to post
Share on other sites

Hello LyGuy,

 

thanks for your help! I have created a new test WinForms-Application and have found the error (swapped parameters is second InputElement 0, 12  should be 12, 0):

var elements = new[] {
  new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 0),
  new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 12, 0)
};

It's works nice now! Thank you for your hints!

 

 

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