1 Ring, Circle, Cone Chapter 4. 2 Triangle rotating We have a triangle in the following position We...

Post on 28-Dec-2015

214 views 1 download

Transcript of 1 Ring, Circle, Cone Chapter 4. 2 Triangle rotating We have a triangle in the following position We...

1

Ring, Circle, Cone

Chapter 4

2

Triangle rotating We have a triangle in the following position

 

     

 

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).

(3,0) (1,0)

(2,1.73) Y

X

3

Set vertexprivate 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();

}

4

Set Rotationprivate 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

5

Make a Ring We have a ring in the following position with inner radius 0.5 and the outer radius 1.

 

     

Both circles has equation

(3,0)

Y

X(2,0)

(1,0)

2 cos 2 0.5cosand (0 2π)

sin 0.5sin

x x

y y

6

Set Ring vertexprivate 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 circlev_ring[2*k].Color=Color.Red.ToArgb(); // redv_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 circlev_ring[2*k+1].Color=Color.White.ToArgb(); //white

}

7

Multiple Rotationsprivate 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;

}

8

Draw RingVBR = 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();

m_device.SetStreamSource( 0, VBR, 0);SetRotationR();m_device.DrawPrimitives(PrimitiveType.TriangleStrip, 0,

120);

// omit adding event handling code

9

Output

10

Make a CircleIf we have a circle center at (2,0) with radius 1.

 

     

The circles has equation

(3,0)

Y

X(2,0)

(1,0)

2 cos(0 2π)

sin

x

y

11

Set Circle vertexprivate 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 centerv_ circle[0].Color=Color.Red.ToArgb(); // redfloat 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; // circlev_ circle[k].Color=Color.White.ToArgb(); //white

}

12

Draw Circle codem_device.SetStreamSource( 0, VBCircle, 0);SetRotationR();m_device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 60);

13

Direction of a triangleAny triangle has 3 vertices v1, v2, v2. Suppose we draw this triangle with order v1 v2v2.

v1

v2

v3 v1

v2

v3

Then this direction could be either clockwise or anti-clockwise ( counter-clockwise)

clockwise counter-clockwise

14

Set CULLMODE

If we do not want to draw anti-clockwise triangle only then set

m_device.RenderState.CullMode = Cull.Clockwise;

If we do not want to draw clockwise triangle only then set

m_device.RenderState.CullMode = Cull.CounterClockwise;

If we want to draw all direction triangle then set

m_device.RenderState.CullMode = Cull.None;

Note: When drawing Triangle Strip, the first one must be clockwise, the followings could be any directions.

15

Draw ConeCube 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.

16

Set Cone Vertexvoid 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();

}

}

17

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

}

18

Output

19

• Need to add Cylinder two colors