So I'm trying to get rendering to work in my bot with SlimDx.
Camera class:
Code:
public struct CameraInfo
{
uint unk0;
uint unk1;
public Vector3 Position;
public Matrix3 Matrix;
public float FieldOfView;
float unk2;
int unk3;
public float NearZ;
public float FarZ;
public float Aspect;
}
public static unsafe class Camera
{
public static void Initialize()
{
_GetFov = Helper.Magic.RegisterDelegate<GetFovDelegate>(Helper.Magic.GetObjectVtableFunction(Pointer, 0));
_Forward = Helper.Magic.RegisterDelegate<ForwardDelegate>(Helper.Magic.GetObjectVtableFunction(Pointer, 1));
_Right = Helper.Magic.RegisterDelegate<RightDelegate>(Helper.Magic.GetObjectVtableFunction(Pointer, 2));
_Up = Helper.Magic.RegisterDelegate<UpDelegate>(Helper.Magic.GetObjectVtableFunction(Pointer, 3));
}
private static IntPtr Pointer
{
get { return new IntPtr(Offsets.ActiveCamera); }
}
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
public delegate float GetFovDelegate(IntPtr ptr);
private static GetFovDelegate _GetFov;
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
public delegate Vector3* ForwardDelegate(IntPtr ptr, Vector3* vecOut);
private static ForwardDelegate _Forward;
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
public delegate Vector3* RightDelegate(IntPtr ptr, Vector3* vecOut);
private static RightDelegate _Right;
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
public delegate Vector3* UpDelegate(IntPtr ptr, Vector3* vecOut);
private static UpDelegate _Up;
public static float FieldOfView
{
get
{
return _GetFov(Pointer);
}
}
public static Vector3 Forward
{
get
{
var res = new Vector3();
_Forward(Pointer, &res);
return res;
}
}
public static Vector3 Right
{
get
{
var res = new Vector3();
_Right(Pointer, &res);
return res;
}
}
public static Vector3 Up
{
get
{
var res = new Vector3();
_Up(Pointer, &res);
return res;
}
}
public static Matrix Projection
{
get
{
var cam = GetCamera();
return Matrix.PerspectiveFovRH(FieldOfView * 0.6f, cam.Aspect, cam.NearZ, cam.FarZ);
}
}
public static Matrix View
{
get
{
var cam = GetCamera();
var eye = cam.Position;
var at = eye + Camera.Forward;
return Matrix.LookAtRH(eye, at, new Vector3(0, 0, 1));
}
}
public static CameraInfo GetCamera()
{
return Helper.Magic.ReadStruct<CameraInfo>(new IntPtr(Offsets.ActiveCamera));
}
}
Rendering class:
Code:
[StructLayout(LayoutKind.Sequential)]
public struct PositionColored
{
public static readonly VertexFormat FVF = VertexFormat.Position | VertexFormat.Diffuse;
public static readonly int Stride = Vector3.SizeInBytes + sizeof(int);
public Vector3 Position;
public int Color;
public PositionColored(Vector3 pos, int col)
{
Position = pos;
Color = col;
}
}
public static class Rendering
{
private static readonly List<IResource> _resources = new List<IResource>();
private static IntPtr _usedDevicePointer = IntPtr.Zero;
public static Device Device { get; private set; }
public static void Initialize(IntPtr devicePointer)
{
if (_usedDevicePointer != devicePointer)
{
Debug.WriteLine("Rendering: Device initialized on " + devicePointer);
Device = Device.FromPointer(devicePointer);
_usedDevicePointer = devicePointer;
}
Camera.Initialize();
}
public static void RegisterResource(IResource source)
{
_resources.Add(source);
}
private static void InternalRender(Location target, PositionColored[] vertices)
{
var camera = Camera.GetCamera();
var eye = camera.Position;
var lookat = camera.Position + Camera.Forward;
var up = new Vector3(0, 0, 1);
var matWorld = Matrix.Translation(target.ToVector3());
var matView = Matrix.LookAtRH(eye, lookat, up);
var matProj = Matrix.PerspectiveFovRH(camera.FieldOfView * 0.6f, 1.335f, camera.NearZ, camera.FarZ);
Device.VertexShader = null;
Device.PixelShader = null;
Device.SetRenderState(RenderState.AlphaBlendEnable, true);
Device.SetRenderState(RenderState.BlendOperation, BlendOperation.Add);
Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
Device.SetRenderState(RenderState.Lighting, 0);
Device.SetTexture(0, null);
Device.SetRenderState(RenderState.CullMode, Cull.None);
Result res = Device.DrawUserPrimitives(PrimitiveType.LineList, (vertices.Length / 2), vertices);
}
private static PositionColored[] LineVertices(Location start, Location end, Color color)
{
var vertices = new PositionColored[2];
vertices[0] = new PositionColored(new Vector3(0f, 0f, 0f), System.Drawing.Color.Yellow.ToArgb());
vertices[1] = new PositionColored(new Vector3((start.X - end.X), (start.Y - end.Y), (start.Z - end.Z)), System.Drawing.Color.Yellow.ToArgb());
return vertices;
}
public static void DrawLine(Location from, Location to, Color color)
{
InternalRender(from, LineVertices(to, from, color));
}
But when I call Rendering.DrawLine I get this error:
Code:
16:39:53] [Drawing] Error: SlimDX.Direct3D9.Direct3D9Exception: D3DERR_INVALIDCALL: Invalid call (-2005530516)
at SlimDX.Result.Throw[T](Object dataKey, Object dataValue)
at SlimDX.Result.Record[T](Int32 hr, Boolean failed, Object dataKey, Object dataValue)
at SlimDX.Direct3D9.Device.DrawUserPrimitives[T](PrimitiveType primitiveType, Int32 startIndex, Int32 primitiveCount, T[] data)
at SlimDX.Direct3D9.Device.DrawUserPrimitives[T](PrimitiveType primitiveType, Int32 primitiveCount, T[] data)
at cleanCore.D3D.Rendering.InternalRender(Location target, PositionColored[] vertices) in C:\Users\Michael\Desktop\Projects\Developement\World of Warcraft\cleanCore\cleanCore\D3D\Rendering.cs:line 74
at cleanCore.D3D.Rendering.DrawLine(Location from, Location to, Color color) in C:\Users\Michael\Desktop\Projects\Developement\World of Warcraft\cleanCore\cleanCore\D3D\Rendering.cs:line 87
at cleanLayer.Scripting.Scripts.DrawingScript.OnTick() in C:\Users\Michael\Desktop\Projects\Developement\World of Warcraft\cleanCore\cleanLayer\Scripting\Scripts\DrawingScript.cs:line 38
at cleanLayer.Scripting.ScriptThread.Tick() in C:\Users\Michael\Desktop\Projects\Developement\World of Warcraft\cleanCore\cleanLayer\Scripting\ScriptThread.cs:line 48
at cleanLayer.Scripting.Script.Tick() in C:\Users\Michael\Desktop\Projects\Developement\World of Warcraft\cleanCore\cleanLayer\Scripting\Script.cs:line 99
I talked to Thongs about this, and he sent me his code, and for him it's working just fine yet for me it's not. I'm however using this approach to get the device pointer (from caytchen's cleanCore):
Code:
var window = new Form();
IntPtr direct3D = Direct3DAPI.Direct3DCreate9(Direct3DAPI.SDKVersion);
if (direct3D == IntPtr.Zero)
throw new Exception("Direct3DCreate9 failed (SDK Version: " + Direct3DAPI.SDKVersion + ")");
var pp = new Direct3DAPI.PresentParameters { Windowed = true, SwapEffect = 1, BackBufferFormat = 0 };
var createDevice = Helper.Magic.RegisterDelegate<Direct3DAPI.Direct3D9CreateDevice>(Helper.Magic.GetObjectVtableFunction(direct3D, 16));
IntPtr device;
if (createDevice(direct3D, 0, 1, window.Handle, 0x20, ref pp, out device) < 0)
throw new Exception("Failed to create device");
EndScenePointer = Helper.Magic.GetObjectVtableFunction(device, Direct3DAPI.EndSceneOffset);
ResetPointer = Helper.Magic.GetObjectVtableFunction(device, Direct3DAPI.ResetOffset);
ResetExPointer = Helper.Magic.GetObjectVtableFunction(device, Direct3DAPI.ResetExOffset);
var deviceRelease = Helper.Magic.RegisterDelegate<Direct3DAPI.D3DRelease>(Helper.Magic.GetObjectVtableFunction(device, 2));
var release = Helper.Magic.RegisterDelegate<Direct3DAPI.D3DRelease>(Helper.Magic.GetObjectVtableFunction(direct3D, 2));
Rendering.Initialize(device);
While he is just using an offset. Do any of you have any idea what might be causing this? I'm out of ideas.