Finaly managed to draw something on the screen, now its time to get it 3D!!.
But now when i try to make it 3d i dont see the thing i draw.
Here is my code,C#, What am I doing wrong?
Code:
public void FillRectangle(float width, float height, Vector3 pos, Color color)
{
device.VertexShader = null;
device.PixelShader = null;
device.SetTransform(TransformState.World, World);
device.SetTransform(TransformState.View, View);
device.SetTransform(TransformState.Projection, Projection);
device.VertexFormat = VertexFormat.PositionRhw | VertexFormat.Diffuse | VertexFormat.Texture1;
device.SetTexture(0, null);
float startXDown = pos.X - (width / 2);
float startYDown = pos.Y + (height / 2);
float endXDown = startXDown + width;
float endYLeftSide = pos.Y - (height / 2);
Vertex[] data1 = newVertex[3];
data1[0].Color = (uint)color.ToArgb();
data1[0].X = startXDown;
data1[0].Y = startYDown;
data1[0].Z = pos.Z;
data1[0].RHW = 1.0f;
data1[1].Color = (uint)color.ToArgb();
data1[1].X = endXDown;
data1[1].Y = startYDown;
data1[1].Z = pos.Z;
data1[1].RHW = 1.0f;
data1[2].Color = (uint)color.ToArgb();
data1[2].X = endXDown;
data1[2].Y = endYLeftSide;
data1[2].Z = pos.Z;
data1[2].RHW = 1.0f;
Vertex[] data2 = newVertex[3];
data2[0].Color = (uint)color.ToArgb();
data2[0].X = startXDown;
data2[0].Y = startYDown;
data2[0].Z = pos.Z;
data2[0].RHW = 1.0f;
data2[1].Color = (uint)color.ToArgb();
data2[1].X = startXDown;
data2[1].Y = endYLeftSide;
data2[1].Z = pos.Z;
data2[1].RHW = 1.0f;
data2[2].Color = (uint)color.ToArgb();
data2[2].X = endXDown;
data2[2].Y = endYLeftSide;
data2[2].Z = pos.Z;
data2[2].RHW = 1.0f;
device.SetRenderState(RenderState.Ambient, Color.FromArgb(255, 127, 127, 127).ToArgb());
device.SetRenderState(RenderState.Lighting, true);
device.SetRenderState(RenderState.CullMode, 0);
device.DrawUserPrimitives<Vertex>(PrimitiveType.TriangleList, 1, data1);
device.DrawUserPrimitives<Vertex>(PrimitiveType.TriangleList, 1, data2);
}
public bool WorldToScreen(Vector3 position, outVector3 screen)
{
screen = Vector3.Project(position,device.Viewport.X,device.Viewport.Y,device.Viewport.Width,device.Viewport.Height,device.Viewport.MinZ,device.Viewport.MaxZ, (World*View*Projection));
return screen.Z < 1f;
}
public Matrix View
{
get
{
return Matrix.LookAtRH(newVector3(CamX, CamY, CamZ), newVector3(LookX, LookY, LookZ), newVector3(0, 0, 1f));
}
}
public Matrix Projection
{
get
{
return Matrix.PerspectiveFovRH(fieldOfView * 0.6f, aspect, zNearPlane, zFarPlane);
}
}
public Matrix World
{
get
{
return Matrix.Identity * Matrix.Translation(PlrX, PlrY, PlrZ);
}
}
And the code to draw:
Code:
Vector3 vec = newVector3();
device.WorldToScreen(newVector3(PlrX, PlrY, PlrZ), out vec);
device.FillRectangle(500, 500, vec, Color.Red);