Jump to content
  • Advertisement
Sign in to follow this  
AlanMcCormick

[SlimDX] clipping Z axis

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

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


// 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);
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();
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
Turns out I needed to clear the ZBuffer in SlimDX but not SharpDX...

d3dDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(255, 0.2f, 0.2f, 0.2f), 1.0f, 0);

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!