Sign in to follow this  
AlanMcCormick

[SlimDX] clipping Z axis

Recommended Posts

I have a basic example where I draw 9 points, and attempt to rotate them on the Y axis. Using SlimDX my points disappear completely. Using SharpDX it behaves like I would expect. Is there a way around this? If you have both installed, all you have to do is comment out the #define SLIMDX line to use SharpDX. They are both implemented for comparison

I attached the project as a zip file, but here's the code

[code]
// comment this out and try SharpDX instead
// SharpDX works
#define SLIMDX

using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

#if SLIMDX
using SlimDX;
using SlimDX.Direct3D9;
#else
using SharpDX;
using SharpDX.Direct3D9;
#endif

namespace SlimDXTests
{
public partial class TestDialog : Form
{
// D3D
Direct3D d3d;
Device d3dDevice;
VertexBuffer vertexBuffer;

// for SharpDX since the aren't defined
const int D3DWORLD = 256;
const int D3DVIEW = 2;
const int D3DPROJECTION = 3;

struct CustomVertex
{
public float x, y, z;
public float pointSize;
public uint color;
}

public TestDialog()
{
InitializeComponent();

InitD3D9();
InitGeometry();
}
private void InitD3D9()
{
d3d = new Direct3D();
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,
//FullScreenRefreshRateInHertz = 0,
//Multisample = MultisampleType.NonMaskable,
//MultisampleQuality = 1 // todo query max and set to max-1
});

d3dDevice.SetRenderState(RenderState.Lighting, 0); // Turn off D3D Lighting
d3dDevice.Viewport = new Viewport()
{
X = 0,
Y = 0,
Width = panel1.Width,
Height = panel1.Height,
MinZ = 0,
MaxZ = 1
};
SetupTransform(0);
}

private void SetupTransform(long millis)
{
Matrix world;
Matrix view;
Matrix projection;

//-----------------------------
Matrix rotationY = Matrix.RotationY((float)(millis * 2 * Math.PI / 1000f));
Matrix scale = Matrix.Scaling(0.8f, 0.8f, 0.8f);
world = scale * rotationY;

Vector3 eyeVector = new Vector3(0.0f, 2.0f, -3.0f);
Vector3 lookVector = new Vector3(0.0f, 0.0f, 0.0f);
Vector3 upVector = new Vector3(0.0f, 1.0f, 0.0f);
Matrix.LookAtLH(ref eyeVector, ref lookVector, ref upVector, out view);
Matrix.PerspectiveFovLH((float)(Math.PI / 4), (float)panel1.Width / (float)panel1.Height, 1f, 100f, out projection);

#if SLIMDX
d3dDevice.SetTransform(TransformState.World, world);
d3dDevice.SetTransform((TransformState)TransformState.View, view);
d3dDevice.SetTransform((TransformState)TransformState.Projection, projection);
#else
d3dDevice.SetTransform((TransformState)D3DWORLD, ref world);
d3dDevice.SetTransform((TransformState)D3DVIEW, ref view);
d3dDevice.SetTransform((TransformState)D3DPROJECTION, ref projection);
#endif
}

private void InitGeometry()
{
CustomVertex[] vertices = new CustomVertex[]
{
new CustomVertex(){x = 0.0f, y = 0.0f, z = 0.0f, pointSize = 5f, color = 0xff0094FF},
new CustomVertex(){x = -1.0f, y = 0.0f, z = 0.0f, pointSize = 5f, color = 0xffFF6A00},
new CustomVertex(){x = 1.0f, y = 0.0f, z = 0.0f, pointSize = 5f, color = 0xff007F0E},
new CustomVertex(){x = 0.0f, y = -1.0f, z = 0.0f, pointSize = 5f, color = 0xffD477FF},
new CustomVertex(){x = 0.0f, y = 1.0f, z = 0.0f, pointSize = 5f, color = 0xffFF00DC},
new CustomVertex(){x = -1.0f, y = 1.0f, z = 0.0f, pointSize = 5f, color = 0xffFFE15E},
new CustomVertex(){x = 1.0f, y = 1.0f, z = 0.0f, pointSize = 5f, color = 0xffFFFFFF},
new CustomVertex(){x = -1.0f, y = -1.0f, z = 0.0f, pointSize = 5f, color = 0xff7D00B7},
new CustomVertex(){x = 1.0f, y = -1.0f, z = 0.0f, pointSize = 5f, color = 0xffFFB27F}
};

// create vertex buffer
vertexBuffer = new VertexBuffer(
d3dDevice,
3 * Marshal.SizeOf(typeof(CustomVertex)),
Usage.None,
VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.PointSize,
Pool.Default);

DataStream stream = vertexBuffer.Lock(0, Marshal.SizeOf(typeof(CustomVertex)) * vertices.Length, LockFlags.None);
for (int i = 0; i < vertices.Length; i++)
stream.Write(vertices[i]);
vertexBuffer.Unlock();
}

protected override void OnClosed(EventArgs e)
{
d3d.Dispose();
d3dDevice.Dispose();
vertexBuffer.Dispose();
base.OnClosed(e);
}

private void btnDX9_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
int frames = 0;
sw.Start();
while (sw.ElapsedMilliseconds < 3000)
{
d3dDevice.Clear(ClearFlags.Target, new Color4(255, 0.2f, 0.2f, 0.2f), 1.0f, 0);
SetupTransform(sw.ElapsedMilliseconds);
d3dDevice.BeginScene();
d3dDevice.SetStreamSource(0, vertexBuffer, 0, Marshal.SizeOf(typeof(CustomVertex)));
d3dDevice.VertexFormat = VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.PointSize;
d3dDevice.DrawPrimitives(PrimitiveType.PointList, 0, 9);
d3dDevice.EndScene();
d3dDevice.Present();
frames++;
}
sw.Stop();
}
}
}
[/code]

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