/// <summary> /// Calculates the transformation matrix corresponding to the given transform components</summary> /// <param name="translation">Translation</param> /// <param name="rotation">Rotation</param> /// <param name="scale">Scale</param> /// <param name="scalePivot">Translation to origin of scaling</param> /// <param name="scalePivotTranslate">Translation after scaling</param> /// <param name="rotatePivot">Translation to origin of rotation</param> /// <param name="rotatePivotTranslate">Translation after rotation</param> /// <returns>Transformation matrix corresponding to the given transform components</returns> public static Matrix4F CalcTransform( Vec3F translation, Vec3F rotation, Vec3F scale, Vec3F scalePivot, Vec3F scalePivotTranslate, Vec3F rotatePivot, Vec3F rotatePivotTranslate) { Matrix4F M = new Matrix4F(); Matrix4F temp = new Matrix4F(); M.Set(-scalePivot); temp.Scale(scale); M.Mul(M, temp); temp.Set(scalePivot + scalePivotTranslate - rotatePivot); M.Mul(M, temp); if (rotation.X != 0) { temp.RotX(rotation.X); M.Mul(M, temp); } if (rotation.Y != 0) { temp.RotY(rotation.Y); M.Mul(M, temp); } if (rotation.Z != 0) { temp.RotZ(rotation.Z); M.Mul(M, temp); } temp.Set(rotatePivot + rotatePivotTranslate + translation); M.Mul(M, temp); return(M); }
/// <summary> /// Handles mouse-move events</summary> /// <param name="sender">Control that raised original event</param> /// <param name="e">Event args</param> /// <returns>true, if controller handled the event</returns> public override bool MouseMove(object sender, MouseEventArgs e) { if (m_dragging && InputScheme.ActiveControlScheme.IsControllingCamera(Control.ModifierKeys, e)) { float dx = (float)(e.X - m_lastMousePoint.X) / 150.0f; float dy = (float)(e.Y - m_lastMousePoint.Y) / 150.0f; if (InputScheme.ActiveControlScheme.IsElevating(Control.ModifierKeys, e)) { // move camera up/down Vec3F p = Camera.Eye; p.Y += (dy < 0) ? m_scale : -m_scale; Camera.Set(p); } else if (InputScheme.ActiveControlScheme.IsTurning(Control.ModifierKeys, e)) { // pitch and yaw camera Matrix4F mat = Matrix4F.RotAxisRH(Camera.Right, -dy); // pitch along camera right Matrix4F yaw = new Matrix4F(); yaw.RotY(-dx); mat.Mul(yaw, mat); Vec3F lookAt = Camera.LookAt; Vec3F up = Camera.Up; mat.Transform(ref lookAt); mat.Transform(ref up); Vec3F position = Camera.Eye; float d = Camera.DistanceFromLookAt; Camera.Set(position, position + lookAt * d, up); } m_lastMousePoint = e.Location; return(true); } return(base.MouseMove(sender, e)); }
public override void Render(ViewControl vc) { Matrix4F normWorld = GetManipulatorMatrix(); if (normWorld == null) { return; } Util3D.RenderFlag = BasicRendererFlags.WireFrame | BasicRendererFlags.DisableDepthTest; Color xcolor = (m_hitRegion == HitRegion.XAxis) ? Color.LightSalmon : Color.Red; Color ycolor = (m_hitRegion == HitRegion.YAxis) ? Color.LightGreen : Color.Green; Color Zcolor = (m_hitRegion == HitRegion.ZAxis) ? Color.LightBlue : Color.Blue; float s; Util.CalcAxisLengths(vc.Camera, normWorld.Translation, out s); Vec3F axScale = new Vec3F(s, s, s); Matrix4F scale = new Matrix4F(); scale.Scale(axScale); Matrix4F xform = scale * normWorld; Util3D.DrawCircle(xform, Zcolor); Matrix4F rot = new Matrix4F(); rot.RotY(MathHelper.PiOver2); xform = scale * rot * normWorld; Util3D.DrawCircle(xform, xcolor); rot.RotX(MathHelper.PiOver2); xform = scale * rot * normWorld; Util3D.DrawCircle(xform, ycolor); }
/// <summary> /// Handles mouse-move events</summary> /// <param name="sender">Control that raised original event</param> /// <param name="e">Event args</param> /// <returns>true, if controller handled the event</returns> public override bool MouseMove(object sender, MouseEventArgs e) { if (m_dragging && InputScheme.ActiveControlScheme.IsControllingCamera(Control.ModifierKeys, e)) { float dx = (float)(e.X - m_lastMousePoint.X) / 150.0f; float dy = (float)(e.Y - m_lastMousePoint.Y) / 150.0f; if (InputScheme.ActiveControlScheme.IsElevating(Control.ModifierKeys, e)) { // move camera up/down Vec3F p = Camera.Eye; p.Y += (dy < 0) ? m_scale : -m_scale; Camera.Set(p); } else if (InputScheme.ActiveControlScheme.IsTurning(Control.ModifierKeys, e)) { // pitch and yaw camera Matrix4F mat = Matrix4F.RotAxisRH(Camera.Right, -dy); // pitch along camera right Matrix4F yaw = new Matrix4F(); yaw.RotY(-dx); mat.Mul(yaw, mat); Vec3F lookAt = Camera.LookAt; Vec3F up = Camera.Up; mat.Transform(ref lookAt); mat.Transform(ref up); Vec3F position = Camera.Eye; float d = Camera.DistanceFromLookAt; Camera.Set(position, position + lookAt * d, up); } m_lastMousePoint = e.Location; return true; } return base.MouseMove(sender, e); }
public override void Render(ViewControl vc) { Matrix4F normWorld = GetManipulatorMatrix(); if (normWorld == null) return; Util3D.RenderFlag = BasicRendererFlags.WireFrame | BasicRendererFlags.DisableDepthTest; Color xcolor = (m_hitRegion == HitRegion.XAxis ) ? Color.LightSalmon : Color.Red; Color ycolor = (m_hitRegion == HitRegion.YAxis ) ? Color.LightGreen : Color.Green; Color Zcolor = (m_hitRegion == HitRegion.ZAxis ) ? Color.LightBlue : Color.Blue; float s; Util.CalcAxisLengths(vc.Camera, normWorld.Translation, out s); Vec3F axScale = new Vec3F(s, s, s); Matrix4F scale = new Matrix4F(); scale.Scale(axScale); Matrix4F xform = scale*normWorld; Util3D.DrawCircle(xform, Zcolor); Matrix4F rot = new Matrix4F(); rot.RotY(MathHelper.PiOver2); xform = scale * rot * normWorld; Util3D.DrawCircle(xform, xcolor); rot.RotX(MathHelper.PiOver2); xform = scale * rot * normWorld; Util3D.DrawCircle(xform,ycolor); }
/// <summary> /// Calculates the transformation matrix corresponding to the given transform components /// </summary> /// <param name="translation">Translation</param> /// <param name="rotation">Rotation</param> /// <param name="scale">Scale</param> /// <param name="scalePivot">Translation to origin of scaling</param> /// <param name="scalePivotTranslate">Translation after scaling</param> /// <param name="rotatePivot">Translation to origin of rotation</param> /// <param name="rotatePivotTranslate">Translation after rotation</param> /// <returns>transformation matrix corresponding to the given transform components</returns> public static Matrix4F CalcTransform( Vec3F translation, Vec3F rotation, Vec3F scale, Vec3F pivot) { Matrix4F M = new Matrix4F(); Matrix4F temp = new Matrix4F(); M.Set(-pivot); temp.Scale(scale); M.Mul(M, temp); if (rotation.X != 0) { temp.RotX(rotation.X); M.Mul(M, temp); } if (rotation.Y != 0) { temp.RotY(rotation.Y); M.Mul(M, temp); } if (rotation.Z != 0) { temp.RotZ(rotation.Z); M.Mul(M, temp); } temp.Set(pivot + translation); M.Mul(M, temp); return M; }
/// <summary> /// Performs initialization when the adapter's node is set. /// This method is called each time the adapter is connected to its underlying node. /// Typically overridden by creators of DOM adapters.</summary> protected override void OnNodeSet() { base.OnNodeSet(); // get trans, scale, and rot. foreach (DomNode domNode in this.DomNode.GetChildList(Schema.node.scaleChild)) { m_scale = Tools.GetVector3(domNode, Schema.TargetableFloat3.Attribute); break; } foreach (DomNode domNode in this.DomNode.GetChildList(Schema.node.translateChild)) { m_translation = Tools.GetVector3(domNode, Schema.TargetableFloat3.Attribute); break; } const float PiOver180 = (float)(Math.PI / 180.0f); foreach (DomNode node in DomNode.GetChildList(Schema.node.rotateChild)) { double[] arr = (double[])node.GetAttribute(Schema.rotate.Attribute); float angle = (float)arr[3] * PiOver180; string sid = node.GetAttribute(Schema.rotate.sidAttribute) as string; if (string.IsNullOrEmpty(sid)) continue; if (sid == "rotateX") m_rotation.X = angle; else if (sid == "rotateY") m_rotation.Y = angle; else if (sid == "rotateZ") m_rotation.Z = angle; } Matrix4F M = new Matrix4F(); Matrix4F temp = new Matrix4F(); temp.Scale(Scale); M.Mul(M, temp); if (m_rotation.X != 0) { temp.RotX(m_rotation.X); M.Mul(M, temp); } if (m_rotation.Y != 0) { temp.RotY(m_rotation.Y); M.Mul(M, temp); } if (m_rotation.Z != 0) { temp.RotZ(m_rotation.Z); M.Mul(M, temp); } temp.Set(Translation); M.Mul(M, temp); Transform = M; m_boundingBox = new Cached<Box>(CalculateBoundingBox); Visible = true; }
public static void CreateTorus(float innerRadius, float outerRadius, uint rings, uint sides, List <Vec3F> pos, List <Vec3F> nor, List <Vec2F> tex, List <uint> indices) { uint ringStride = rings + 1; uint sideStride = sides + 1; // radiusC: distance to center of the ring float radiusC = (innerRadius + outerRadius) * 0.5f; //radiusR: the radius of the ring float radiusR = (outerRadius - radiusC); for (uint i = 0; i <= rings; i++) { float u = (float)i / rings; float outerAngle = i * MathHelper.TwoPi / rings; // xform from ring space to torus space. Matrix4F trans = new Matrix4F(); trans.Translation = new Vec3F(radiusC, 0, 0); Matrix4F roty = new Matrix4F(); roty.RotY(outerAngle); Matrix4F transform = trans * roty; // create vertices for each ring. for (uint j = 0; j <= sides; j++) { float v = (float)j / sides; float innerAngle = j * MathHelper.TwoPi / sides + MathHelper.Pi; float dx = (float)Math.Cos(innerAngle); float dy = (float)Math.Sin(innerAngle); // normal, position ,and texture coordinates Vec3F n = new Vec3F(dx, dy, 0); Vec3F p = n * radiusR; if (tex != null) { Vec2F t = new Vec2F(u, v); tex.Add(t); } transform.Transform(ref p); transform.TransformVector(n, out n); pos.Add(p); nor.Add(n); // And create indices for two triangles. uint nextI = (i + 1) % ringStride; uint nextJ = (j + 1) % sideStride; indices.Add(nextI * sideStride + j); indices.Add(i * sideStride + nextJ); indices.Add(i * sideStride + j); indices.Add(nextI * sideStride + j); indices.Add(nextI * sideStride + nextJ); indices.Add(i * sideStride + nextJ); } } }
public static void CreateTorus(float innerRadius, float outerRadius, uint rings, uint sides, List<Vec3F> pos, List<Vec3F> nor, List<Vec2F> tex, List<uint> indices) { uint ringStride = rings + 1; uint sideStride = sides + 1; // radiusC: distance to center of the ring float radiusC = (innerRadius + outerRadius) * 0.5f; //radiusR: the radius of the ring float radiusR = (outerRadius - radiusC); for (uint i = 0; i <= rings; i++) { float u = (float)i / rings; float outerAngle = i * MathHelper.TwoPi / rings; // xform from ring space to torus space. Matrix4F trans = new Matrix4F(); trans.Translation = new Vec3F(radiusC, 0, 0); Matrix4F roty = new Matrix4F(); roty.RotY(outerAngle); Matrix4F transform = trans * roty; // create vertices for each ring. for (uint j = 0; j <= sides; j++) { float v = (float)j / sides; float innerAngle = j * MathHelper.TwoPi / sides + MathHelper.Pi; float dx = (float)Math.Cos(innerAngle); float dy = (float)Math.Sin(innerAngle); // normal, position ,and texture coordinates Vec3F n = new Vec3F(dx, dy, 0); Vec3F p = n * radiusR; if (tex != null) { Vec2F t = new Vec2F(u, v); tex.Add(t); } transform.Transform(ref p); transform.TransformVector(n, out n); pos.Add(p); nor.Add(n); // And create indices for two triangles. uint nextI = (i + 1) % ringStride; uint nextJ = (j + 1) % sideStride; indices.Add(nextI * sideStride + j); indices.Add(i * sideStride + nextJ); indices.Add(i * sideStride + j); indices.Add(nextI * sideStride + j); indices.Add(nextI * sideStride + nextJ); indices.Add(i * sideStride + nextJ); } } }
/// <summary> /// Performs initialization when the adapter's node is set. /// This method is called each time the adapter is connected to its underlying node. /// Typically overridden by creators of DOM adapters.</summary> protected override void OnNodeSet() { base.OnNodeSet(); // get trans, scale, and rot. foreach (DomNode domNode in this.DomNode.GetChildList(Schema.node.scaleChild)) { m_scale = Tools.GetVector3(domNode, Schema.TargetableFloat3.Attribute); break; } foreach (DomNode domNode in this.DomNode.GetChildList(Schema.node.translateChild)) { m_translation = Tools.GetVector3(domNode, Schema.TargetableFloat3.Attribute); break; } const float PiOver180 = (float)(Math.PI / 180.0f); foreach (DomNode node in DomNode.GetChildList(Schema.node.rotateChild)) { double[] arr = (double[])node.GetAttribute(Schema.rotate.Attribute); float angle = (float)arr[3] * PiOver180; string sid = node.GetAttribute(Schema.rotate.sidAttribute) as string; if (string.IsNullOrEmpty(sid)) { continue; } if (sid == "rotateX") { m_rotation.X = angle; } else if (sid == "rotateY") { m_rotation.Y = angle; } else if (sid == "rotateZ") { m_rotation.Z = angle; } } Matrix4F M = new Matrix4F(); Matrix4F temp = new Matrix4F(); temp.Scale(Scale); M.Mul(M, temp); if (m_rotation.X != 0) { temp.RotX(m_rotation.X); M.Mul(M, temp); } if (m_rotation.Y != 0) { temp.RotY(m_rotation.Y); M.Mul(M, temp); } if (m_rotation.Z != 0) { temp.RotZ(m_rotation.Z); M.Mul(M, temp); } temp.Set(Translation); M.Mul(M, temp); Transform = M; m_boundingBox = new Cached <Box>(CalculateBoundingBox); Visible = true; }