190 likes | 321 Views
Ring, Circle, Cone. Chapter 4. Triangle rotating . We have a triangle in the following position. Y. ( - 2,1.73). X. ( - 1,0). ( - 3,0).
E N D
Ring, Circle, Cone Chapter 4
Triangle rotating We have a trianglein the following position Y (-2,1.73) X (-1,0) (-3,0) We want to rotate it around line y = -2. For this purpose, we have make a translation T (2,0,0), following a rotation about Y axis, then make the reverse translation T (-2,0,0).
Set vertex private void SetVertex() { verts = new CustomVertex.PositionColored[3]; verts[0].X=-3.0f;verts[0].Y=0.0f;verts[0].Z=0.0f; verts[0].Color = Color.Yellow.ToArgb(); verts[1].X=-1.0f;verts[1].Y=0.0f;verts[1].Z=0.0f; verts[1].Color = Color.FromArgb(0,0,255).ToArgb() ; verts[2].X=-2.0f; verts[2].Y=1.73f;verts[2].Z=0.0f; verts[2].Color = Color.Red.ToArgb(); }
Set Rotation private void SetRotation() { float a = (float)Environment.TickCount /1000.0f; float theta = a*2.0f*(float)Math.PI; Matrix mR = Matrix.RotationY(theta) ; Matrix T1 = Matrix.Translation(2.0f, 0.0f, 0.0f); Matrix T2 = Matrix.Translation(-2.0f, 0.0f, 0.0f); Matrix Mat = T1*mR*T2; m_device.Transform.World=Mat; } Note: In the combination of matrix, the left matrix takes the Transformation action first. So in the above, T1 first, mR second, T2 last
Make a Ring We have a ringin the following position with inner radius 0.5 and the outer radius 1. Y (1,0) (3,0) X (2,0) Both circles has equation
Set Ring vertex private void SetRingVertex() { v_ring = new CustomVertex.PositionColored[122]; float theta = 2.0f*(float)Math.PI/60.0f; // 6º for(int k=0; k<61; k++) { v_ring[2*k].X=2+0.5f*(float)Math.Cos(theta*k); v_ring[2*k].Y=0.5f*(float)Math.Sin(theta*k); v_ring[2*k].Z=0.0f; // inner circle v_ring[2*k].Color=Color.Red.ToArgb(); // red v_ring[2*k+1].X=2+(float)Math.Cos(theta*k); v_ring[2*k+1].Y=(float)Math.Sin(theta*k); v_ring[2*k+1].Z=0.0f; // outer circle v_ring[2*k+1].Color=Color.White.ToArgb(); //white }
Multiple Rotations private void SetRotation() { float a1 = (float)Environment.TickCount /1000.0f; float theta1 = a1*2.0f*(float)Math.PI; float a2 = (float)Environment.TickCount /1300.0f; float theta2 = a2*2.0f*(float)Math.PI; float a3 = (float)Environment.TickCount /1700.0f; float theta3 = a2*2.0f*(float)Math.PI; Matrix RX = Matrix.RotationY(theta1) ; Matrix RY = Matrix.RotationX(theta2) ; Matrix RZ = Matrix.RotationZ(theta3) ; Matrix T1 = Matrix.Translation(2.0f, 0.0f, 0.0f); Matrix T2 = Matrix.Translation(-2.0f, 0.0f, 0.0f); m_device.Transform.World=T2*RX*RY*RZ*T1; }
Draw Ring VBR = new VertexBuffer( typeof(CustomVertex.PositionColored), 122, m_device, 0, CustomVertex.PositionColored.Format, Pool.Default); GraphicsStream stream = VBR.Lock(0, 0, 0); stream.Write(this.v_ring); VBR.Unlock(); // omit adding event handling code m_device.SetStreamSource( 0, VBR, 0); SetRotationR(); m_device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 120);
Make a Circle If we have a circle center at (2,0)with radius 1. Y (1,0) (3,0) X (2,0) The circles has equation
Set Circle vertex private void SetCircleVertex() { v_circle[]= new CustomVertex.PositionColored[62]; v_circle[0].X=2.0f; v_ circle[0].Y=0.0f; v_ circle[0].Z=0.0f; // circle center v_ circle[0].Color=Color.Red.ToArgb(); // red float theta = 2.0f*(float)Math.PI/60.0f; // 6º for(int k=1; k<62; k++) { v_circle[k].X=2+(float)Math.Cos(theta*k); v_ circle[k].Y=(float)Math.Sin(theta*k); v_ circle[k].Z=0.0f; // circle v_ circle[k].Color=Color.White.ToArgb(); //white }
Draw Circle code m_device.SetStreamSource( 0, VBCircle, 0); SetRotationR(); m_device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 60);
Direction of a triangle Any triangle has 3 vertices v1, v2, v2. Suppose we draw this triangle with order v1 v2v2. v2 v3 v2 v1 v3 v1 clockwise counter-clockwise Then this direction could be either clockwise or anti-clockwise ( counter-clockwise)
SetCULLMODE If we do not want to draw clockwise triangle only then set If we do not want to draw anti-clockwise triangle only then set m_device.RenderState.CullMode = Cull.Clockwise; Note: When drawing Triangle Strip, the first one must be clockwise, the followings could be any directions. m_device.RenderState.CullMode = Cull.CounterClockwise; If we want to draw all direction triangle then set m_device.RenderState.CullMode = Cull.None;
Draw Cone Cube has one lateral surface and one base circle. So we will pick 62 points: one at (0, 2, 0), one is the base circle center (0, 0, 0). Also 61 points at the circle. Radius=1.2 The lateral surface will be draw from method: PrimitiveType.TriangleFan the direction is counter-clockwise. The base circle is also use PrimitiveType.TriangleFan, but the direction is clockwise. Also we need to change the vertex buffer value, during the drawing.
Set Cone Vertex • void SetVertex() • { v_cone = new CustomVertex.PositionColored[62]; • v_cone[0].X = 0.0f; v_cone[0].Y = 2.0f; v_cone[0].Z = 0.0f; • v_cone[0].Color =Color.Yellow.ToArgb(); • float theta = 2.0f*(float)Math.PI/60.0f; • for(int k=1; k<62; k++) { • v_cone[k].X = 1.2f*(float)Math.Cos(k*theta); • v_cone[k].Y = 0.0f; v_cone[k].Z = 1.2f*(float)Math.Sin(k*theta); • v_cone[k].Color =Color.Blue.ToArgb(); • } • }
void Render() { • . . . . . . . . . . . .. • m_device.Clear(ClearFlags.Target, Color.Black.ToArgb(),1.0f,0); • m_device.BeginScene(); • m_device.SetStreamSource( 0, VB, 0); • SetRotation(); • m_device.RenderState.CullMode = Cull.Clockwise; • m_device.DrawPrimitives(PrimitiveType.TriangleFan, 0,60); • m_device.RenderState.CullMode = Cull.CounterClockwise; • m_device.DrawPrimitives(PrimitiveType.TriangleFan, 1,59); • m_device.EndScene(); • m_device.Present(); • }