Sign in to follow this  
BumblingIdiot

Simple Point Cloud Visualization Issue

Recommended Posts

So I'm using SlimDX to help me visualize some data as a 3D point cloud.

I found some sample code to get me going:

[url="http://www.java2s.com/Open-Source/CSharp/Game/slimdxcontrol/Flaxen/SlimDXControlLib/MouseExample/CatalogMouseExample.htm"]http://www.java2s.co...ouseExample.htm[/url]

To test the code I created a very simple array of points (640x480 in a single plane, z coords = 0). I can see that the code is working, but DirectX or the shader is hiding most of the points on me... I need to see all of them at once. From zooming in and out I can see that the geometry is there, but the lighting or something is screwing me up.

I have attached a simple screen shot. What I should see (when I fix this) is complete a rectangle of green points...

This is the sample shader. Please help!

[code]
cbuffer cbPerObject
{
float4x4 gWVP;
}

// VertexShader
void VS(float4 inputPos: POSITION,
float4 inputCol: COLOR,
out float4 outputPos : SV_POSITION,
out float4 outputCol : COLOR)
{
//PS_IN output = (PS_IN)0;

outputPos = mul(inputPos, gWVP);

outputCol = inputCol;
}

// PixelShader
void PS(float4 inputPos: SV_POSITION,
float4 inputCol: COLOR,
out float4 outputCol: SV_TARGET)
{
outputCol = inputCol;
}

technique10 Render
{[attachment=5712:PointsScreenShot.jpg]
pass P0
{
SetGeometryShader( 0 );
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
}
[/code]

Share this post


Link to post
Share on other sites
The way that the lines appear in your output make it look like a matrix issue - are you sure your world/view/projection matrices are properly concatenated and loaded into the constant buffer as you expect? You can check this with PIX by taking a frame capture, then stepping through the draw calls and looking at the geometry you are rendering before and after the transformation (i.e. before and after the vertex shader). This should give you a good idea of where to look deeper into.

Share this post


Link to post
Share on other sites
Thanks for the input Jason!

I will get PIX running soon. The DirectX SDK I have only has PIX for x64 and my Visual Studio 2010 C# express can only build x86, so I've been downloading updated SDKs, etc. for an hour now trying to work around the issue. I underestimated how tricky it could be to draw a bunch of points to a Direct X display... I seem to recall easily blasting through this sort of thing back in the managed Direct X days. This code is just so simple...

Perhaps I should note that the points displayed change drastically depending on how close the camera is to them (from 95% of the points to 5% of the points). With a very minimal tweak to the camera position, I get the two attached results. As I zoom in the pixels come and go seemingly at random. I can't help but think that some fancy lighting or shading is hitting my points from me.

Rendering Function:
[code]public override void Render()
{
m_points = MainWindow.CreateTestPointCloud();
updateGeometry();

Device.OutputMerger.SetTargets(SampleDepthView, SampleRenderView);
Device.Rasterizer.SetViewports(new Viewport(0, 0, WindowWidth, WindowHeight, 0.0f, 1.0f));

Device.ClearDepthStencilView(SampleDepthView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);

Device.ClearRenderTargetView(SampleRenderView, new SlimDX.Color4(1.0f, 0.9f, 0.9f, 0.9f));

Device.InputAssembler.SetInputLayout(m_sampleLayout);
Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.PointList);
Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_sampleVertices, m_pointSize, 0));

EffectTechnique technique = m_sampleEffect.GetTechniqueByIndex(0);
EffectPass pass = technique.GetPassByIndex(0);

// we may have changed our camera or target positions, so rerun the transforms
SetTransforms();

for (int i = 0; i < technique.Description.PassCount; ++i)
{
pass.Apply();
Device.Draw(NumPoints, 0);
}

Device.Flush();
}[/code]


Updating geometry:
[code] public void updateGeometry()
{
if (m_sampleVertices != null)
{
m_sampleVertices.Dispose();
m_sampleVertices = null;

m_sampleStream.Dispose();
m_sampleStream = null;
}

m_sampleStream = new DataStream(NumPoints * m_pointSize, true, true);

m_sampleStream.WriteRange(m_points);
m_sampleStream.Position = 0;

BufferDescription bufferDesc = new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = NumPoints * m_pointSize,
Usage = ResourceUsage.Default
};
m_sampleVertices = new Buffer(Device, m_sampleStream, bufferDesc);
Device.Flush();
}[/code]


Transformations:
[code] private void SetTransforms()
{
// world transfrom: from local coordinates to world coordinates
// in our case, from the range [0..100] to [0..1]
m_worldTransform = Matrix.Scaling(0.01f, 0.01f, 0.01f);

// view transform: from world coordinates to view (camera, eye) coordinates
// the "up" direction is the Y axis
m_worldUp = new Vector3(0, 1, 0);
view = Matrix.LookAtLH(CameraPosition, TargetPosition, m_worldUp);

// projection transform: from view coordinates to perspective space
float znear = 0.0f; // in view space
float zfar = 10.0f; // in view space
if (m_perspective)
{
float fovY = (float)(Math.PI * 0.25); // radians, 45 deg
float aspect = 1.0f;
m_projectionTransform = Matrix.PerspectiveFovLH(fovY, aspect, znear, zfar);
}
else
{
float width = 2.0f;
float height = 2.0f;
m_projectionTransform = Matrix.OrthoLH(width, height, znear, zfar);
}

// compute the summary transform, and push it into the gpu
SlimDX.Matrix wvp = m_worldTransform * view * m_projectionTransform;
EffectMatrixVariable wvpTransform = m_sampleEffect.GetVariableByName("gWVP").AsMatrix();
wvpTransform.SetMatrix(wvp);

return;
}[/code]

Share this post


Link to post
Share on other sites
I assume perspective projection (m_perspective == true) and I don't see you explicitly setting a depth stencil state. In that case depth test is enabled (default). Using a znear of 0 will give you bad results. Try using something > 0 (e.g. 0.1f) or disable depth testing altogether.

Share this post


Link to post
Share on other sites
That did the trick! I am now running with [b][i]znear = 0.1f[/i][/b] and[b][i] zfar at 50.0f[/i] [/b]and there is no trace of the problem. I had played with znear a little bit yesterday, but tried growing it too much such that I couldn't see anything in the view and gave up... Gah.

I had hoped that it was something simple and that some expert would chime in and point it out. You did it. Thanks a ton unbird!

[indent][quote name='unbird' timestamp='1318158886' post='4870749']
I assume perspective projection (m_perspective == true) and I don't see you explicitly setting a depth stencil state. In that case depth test is enabled (default). Using a znear of 0 will give you bad results. Try using something > 0 (e.g. 0.1f) or disable depth testing altogether.
[/quote][/indent]

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