• Advertisement
Sign in to follow this  

[SlimDX] Can't view Z-dimension

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

edit: I copied this code and replaced it with SharpDX and it works, however I'm not sure SharpDX implements DX9 completely.. it seems to be missing a lot (no transforms?) but vertices with z-values display

I've been experimenting with DirectX9 using SlimDX. I've been able to render graphs and 2d lines, points, triangles...

However, whenever I add a z-value for my vertices or try a rotation around the Y-axis, or attempt to set up the View and Projection transforms, my graphics disappear completely. I'm copying example code verbatim from c++ using directx9, and I see no differences. Any time that I attempt to view anything not straight on, with z = 0.0, I see nothing....

What's wrong?



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using SlimDX;
using SlimDX.Direct2D;
using SlimDX.Direct3D10;
using SlimDX.DirectWrite;
using SlimDX.DXGI;
using System.Runtime.InteropServices;

namespace GraphDrawingTests
{
public partial class GraphDrawingDialog : Form
{
// D3D
SlimDX.Direct3D9.Device d3dDevice;

public GraphDrawingDialog()
{
InitializeComponent();

InitD3D9();
InitGeometry();
}

private void button4_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
int frames = 0;
sw.Start();

// while(sw.ElapsedMilliseconds < 1000)
//{
d3dDevice.Clear(SlimDX.Direct3D9.ClearFlags.Target, new SlimDX.Color4(0, 0, 0, 0), 1.0f, 0);
d3dDevice.BeginScene();
d3dDevice.SetStreamSource(0, vertexBuffer, 0, Marshal.SizeOf(typeof(CustomVertex)));
d3dDevice.VertexFormat = SlimDX.Direct3D9.VertexFormat.Diffuse | SlimDX.Direct3D9.VertexFormat.Position;
d3dDevice.DrawPrimitives(SlimDX.Direct3D9.PrimitiveType.TriangleStrip, 0, 1);
d3dDevice.EndScene();
d3dDevice.Present();
frames++;
// }

sw.Stop();
//MessageBox.Show("D3D9 Frames: " + frames);
}

private void InitD3D9()
{
d3dDevice = new SlimDX.Direct3D9.Device(
new SlimDX.Direct3D9.Direct3D(),
0,
SlimDX.Direct3D9.DeviceType.Hardware,
panel1.Handle,
SlimDX.Direct3D9.CreateFlags.HardwareVertexProcessing,
new SlimDX.Direct3D9.PresentParameters()
{
Windowed = true,
SwapEffect = SlimDX.Direct3D9.SwapEffect.Discard,
BackBufferFormat = SlimDX.Direct3D9.Format.Unknown,
// FullScreenRefreshRateInHertz = 0,
// PresentationInterval = SlimDX.Direct3D9.PresentInterval.Immediate,
// Multisample = SlimDX.Direct3D9.MultisampleType.NonMaskable,
// MultisampleQuality = 1, // todo query max and set to max-1
// BackBufferWidth = panel1.Width,
// BackBufferHeight = panel1.Height
});

d3dDevice.SetRenderState(SlimDX.Direct3D9.RenderState.CullMode, 1); // Show front/back triangles
d3dDevice.SetRenderState(SlimDX.Direct3D9.RenderState.Lighting, false); // Turn off D3D Lighting

SetupTransform();
}

private void SetupTransform()
{
SlimDX.Matrix rotation;

// If any of these transforms are used, nothing is displayed, transforms that do not affect z-axis display fine, projections and view never display anything
uint iTime = 100;
float fAngle = (float)(iTime * (2.0f * Math.PI) / 1000.0f);
SlimDX.Matrix.RotationY(fAngle, out rotation);
//d3dDevice.SetTransform(SlimDX.Direct3D9.TransformState.World, rotation);

SlimDX.Vector3 eyePt = new SlimDX.Vector3(0.0f, -1.0f, -2.615f);
SlimDX.Vector3 vLookatPt = new SlimDX.Vector3(0.0f, -1.0f, 0.0f);
SlimDX.Vector3 vUpVec = new SlimDX.Vector3(0.0f, 1.0f, 0.0f);
SlimDX.Matrix matView;
SlimDX.Matrix.LookAtLH(ref eyePt, ref vLookatPt, ref vUpVec, out matView);
//d3dDevice.SetTransform(SlimDX.Direct3D9.TransformState.View, matView);

SlimDX.Matrix matProj;
SlimDX.Matrix.PerspectiveFovLH((float)(Math.PI / 4),
(float)panel1.Width / (float)panel1.Height,
1f,
100f,
out matProj);
//d3dDevice.SetTransform(SlimDX.Direct3D9.TransformState.Projection, matProj);
}

struct CustomVertex
{
public float x, y, z;
public uint color;
}
// const int NUM_VERTICES = 3;
SlimDX.Direct3D9.VertexBuffer vertexBuffer;

private void InitGeometry()
{
// if any of these have a non-zero z-value, nothing is shown
CustomVertex[] vertices = new CustomVertex[]
{
new CustomVertex(){x = -1.0f, y = -1.0f, z = 0.0f, color = 0xffff0000},
new CustomVertex(){x = 1.0f, y = -1.0f, z = 0.0f, color = 0xff0000ff},
new CustomVertex(){x = 0.0f, y = 1.0f, z = 0.0f, color = 0xffffffff}
};

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

SlimDX.DataStream stream = vertexBuffer.Lock(0, Marshal.SizeOf(typeof(CustomVertex)) * vertices.Length, SlimDX.Direct3D9.LockFlags.None);
for (int i = 0; i < vertices.Length; i++)
{
// the triangle displays properly with all zero z-values.. I've done stream.Write(vertices) as well
stream.Write(vertices.x);
stream.Write(vertices.y);
stream.Write(vertices.z);
stream.Write(vertices.color);
}
vertexBuffer.Unlock();
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
Ok this is as simple as I can make it.. I have 5 points being drawn.... rotating them on Y and Z axis. All I can see are 3 points being rotated around the Z-axis. (spinng counter clockwise)



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

namespace GraphDrawingTests
{
public partial class GraphDrawingDialog : Form
{
// D3D
SlimDX.Direct3D9.Direct3D d3d;
SlimDX.Direct3D9.Device d3dDevice;
SlimDX.Direct3D9.VertexBuffer vertexBuffer;

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

public GraphDrawingDialog()
{
InitializeComponent();

InitD3D9();
InitGeometry();
}
private void InitD3D9()
{

d3d = new SlimDX.Direct3D9.Direct3D();
d3dDevice = new SlimDX.Direct3D9.Device(
d3d,
0,
SlimDX.Direct3D9.DeviceType.Hardware,
panel1.Handle,
SlimDX.Direct3D9.CreateFlags.HardwareVertexProcessing,
new SlimDX.Direct3D9.PresentParameters()
{
Windowed = true,
SwapEffect = SlimDX.Direct3D9.SwapEffect.Discard,
BackBufferFormat = SlimDX.Direct3D9.Format.Unknown,
FullScreenRefreshRateInHertz = 0,
PresentationInterval = SlimDX.Direct3D9.PresentInterval.Immediate,
Multisample = SlimDX.Direct3D9.MultisampleType.NonMaskable,
MultisampleQuality = 1, // todo query max and set to max-1
BackBufferWidth = panel1.Width,
BackBufferHeight = panel1.Height
});

d3dDevice.SetRenderState(SlimDX.Direct3D9.RenderState.CullMode, 1); // Show front/back triangles
d3dDevice.SetRenderState(SlimDX.Direct3D9.RenderState.Lighting, false); // Turn off D3D Lighting

d3dDevice.Viewport = new SlimDX.Direct3D9.Viewport(0, 0, panel1.Width, panel1.Height, 0, 1);
SetupTransform(0);
}

private void SetupTransform(long millis)
{
SlimDX.Matrix rotationZ, rotationY, rotation, scale, final;

//-----------------------------
rotationZ = SlimDX.Matrix.Identity;
SlimDX.Matrix.RotationY((float)(millis * 2 * Math.PI / 1000f), out rotationY);
SlimDX.Matrix.RotationZ((float)(millis * Math.PI / 1000f), out rotationZ);
SlimDX.Matrix.Multiply(ref rotationY, ref rotationZ, out rotation);
SlimDX.Matrix.Scaling(0.8f, -0.8f, 0.8f, out scale);
SlimDX.Matrix.Multiply(ref scale, ref rotation, out final);
d3dDevice.SetTransform(SlimDX.Direct3D9.TransformState.World, final);

SlimDX.Vector3 eyePt = new SlimDX.Vector3(0.0f, 1.0f, -2.615f);
SlimDX.Vector3 vLookatPt = new SlimDX.Vector3(0.0f, 0.0f, 0.0f);
SlimDX.Vector3 vUpVec = new SlimDX.Vector3(0.0f, 1.0f, 0.0f);
SlimDX.Matrix matView;
SlimDX.Matrix.LookAtLH(ref eyePt, ref vLookatPt, ref vUpVec, out matView);
//d3dDevice.SetTransform(SlimDX.Direct3D9.TransformState.View, matView);

SlimDX.Matrix matProj;
SlimDX.Matrix.PerspectiveFovLH((float)(Math.PI / 4),
(float)panel1.Width / (float)panel1.Height,
1f,
100f,
out matProj);
//d3dDevice.SetTransform(SlimDX.Direct3D9.TransformState.Projection, matProj);
}

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 = 0xffFF006E},
};

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

SlimDX.DataStream stream = vertexBuffer.Lock(0, Marshal.SizeOf(typeof(CustomVertex)) * vertices.Length, SlimDX.Direct3D9.LockFlags.None);
for (int i = 0; i < vertices.Length; i++)
{
stream.Write(vertices.x);
stream.Write(vertices.y);
stream.Write(vertices.z);
stream.Write(vertices.pointSize);
stream.Write(vertices.color);
}
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 < 4000)
{
d3dDevice.Clear(SlimDX.Direct3D9.ClearFlags.Target, new SlimDX.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 = SlimDX.Direct3D9.VertexFormat.Diffuse | SlimDX.Direct3D9.VertexFormat.Position | SlimDX.Direct3D9.VertexFormat.PointSize;
d3dDevice.DrawPrimitives(SlimDX.Direct3D9.PrimitiveType.PointList, 0, 5);
d3dDevice.EndScene();
d3dDevice.Present();
frames++;
}

sw.Stop();
//MessageBox.Show("D3D9 Frames: " + frames);
}
}
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement