Ejemplo n.º 1
0
 public Geom3DLine(Geom3DVector pt, Geom3DVector v, bool isDir)
 {
     point = new Geom3DVector(pt);
     if (isDir)
         dir = new Geom3DVector(v);
     else
         dir = v.sub(pt);
 }
Ejemplo n.º 2
0
 public Geom3DLine(Geom3DVector pt, Geom3DVector v, bool isDir)
 {
     point = new Geom3DVector(pt);
     if (isDir)
     {
         dir = new Geom3DVector(v);
     }
     else
     {
         dir = v.sub(pt);
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Inersection of plane with line
 /// </summary>
 /// <param name="line"></param>
 /// <param name="inter"></param>
 /// <returns>true if intersection exists</returns>
 public bool intersectLine(Geom3DLine line, Geom3DVector inter)
 {
     float q = normal.x * (origin.x - line.point.x) + normal.y * (origin.y - line.point.y) + normal.z * (origin.z - line.point.z);
     float d = normal.x * line.dir.x + normal.y * line.dir.y + normal.z * line.dir.z;
     if (d == 0)
     {
         inter.x = inter.y = inter.z = 0;
         return false;
     }
     float r = q / d;
     inter.x = line.point.x + r * line.dir.x;
     inter.y = line.point.y + r * line.dir.y;
     inter.z = line.point.z + r * line.dir.z;
     return true;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Inersection of plane with line
        /// </summary>
        /// <param name="line"></param>
        /// <param name="inter"></param>
        /// <returns>true if intersection exists</returns>
        public bool intersectLine(Geom3DLine line, Geom3DVector inter)
        {
            float q = normal.x * (origin.x - line.point.x) + normal.y * (origin.y - line.point.y) + normal.z * (origin.z - line.point.z);
            float d = normal.x * line.dir.x + normal.y * line.dir.y + normal.z * line.dir.z;

            if (d == 0)
            {
                inter.x = inter.y = inter.z = 0;
                return(false);
            }
            float r = q / d;

            inter.x = line.point.x + r * line.dir.x;
            inter.y = line.point.y + r * line.dir.y;
            inter.z = line.point.z + r * line.dir.z;
            return(true);
        }
Ejemplo n.º 5
0
 private void gl_MouseDown(object sender, MouseEventArgs e)
 {
     lastX = xDown = e.X;
     lastY = yDown = e.Y;
     startRotX = view.rotX;
     startRotZ = view.rotZ;
     startViewCenter = view.viewCenter;
     startUserPosition = view.userPosition;
     movePlane = new Geom3DPlane(new Geom3DVector(0,0,0), new Geom3DVector(0, 0, 1));
     moveStart = moveLast = new Geom3DVector(0,0,0);
     UpdatePickLine(e.X,e.Y);
     movePlane.intersectLine(pickLine, moveStart);
     if (e.Button == MouseButtons.Right)
     {
         ThreeDModel sel = Picktest(e.X, e.Y);
         if (sel != null)
         {
             movePlane = new Geom3DPlane(pickPoint, new Geom3DVector(0, 0, 1));
             moveStart = moveLast = new Geom3DVector(pickPoint);
         }
         if(sel!=null && view.eventObjectMoved != null)
             view.eventObjectSelected(sel);
         //computeRay();
     }
 }
Ejemplo n.º 6
0
 public Geom3DPlane(Geom3DVector o, Geom3DVector norm)
 {
     origin = new Geom3DVector(o);
     normal = new Geom3DVector(norm);
 }
Ejemplo n.º 7
0
 public Geom3DVector sub(Geom3DVector v)
 {
     return new Geom3DVector(x - v.x, y - v.y, z - v.z);
 }
Ejemplo n.º 8
0
 public Geom3DVector add(Geom3DVector v)
 {
     return new Geom3DVector(x + v.x, y + v.y, z + v.z);
 }
Ejemplo n.º 9
0
        void Application_Idle(object sender, EventArgs e)
        {
            if (!loaded || !Main.ApplicationIsActivated())
                return;
            // no guard needed -- we hooked into the event in Load handler

            float d = Math.Min(gl.Width, gl.Height) / 3;

            // rotate
            if (mode == 0 && ((Control.ModifierKeys != Keys.Shift && Control.MouseButtons == MouseButtons.Left) || Control.ModifierKeys == Keys.Control))
            {
                rotateX = ((lastXpos - xPos) / d) * (1 - filter) + filter * lastRotateX;
                rotateY = ((lastYpos - yPos) / d) * (1 - filter) + filter * lastRotateY;
                Rotate();
            }
            else if (rotateX != 0 || rotateY != 0)
            {
                rotateX = rotateX * (1 - filter) + filter * lastRotateX;
                rotateY = rotateY * (1 - filter) + filter * lastRotateY;
                Rotate();
            }
            else if (checkMovements(lastRotateX) || checkMovements(lastRotateY))
            {
                rotateX = filter * lastRotateX;
                rotateY = filter * lastRotateY;
                Rotate();
            }

            //zoom
            if (mode == 3 && Control.MouseButtons == MouseButtons.Left)
                zoomSpeed = 120 * (yPos - lastYpos) / d;
            if (zoomSpeed != 0 || checkMovements(lastZoomSpeed))
            {
                zoomSpeed = zoomSpeed * (1 - filter) + filter * lastZoomSpeed;
                lastZoomSpeed = zoomSpeed;
                cam.Zoom(zoomSpeed * (cam.distance - cam.minDistance) / (cam.defaultDistance - cam.minDistance));
                zoomSpeed = 0;
                render = true;
            }

            // MoveViewpoint
            if (mode == 2 || Control.ModifierKeys == Keys.Shift || Control.MouseButtons == MouseButtons.Middle)
            {
                float moveViewpointX = ((xPos - lastXpos) / (float)gl.Width) * (1 - filter) + filter * lastMoveViewpointX;
                float moveViewpointY = ((yPos - lastYpos) / (float)gl.Height) * (1 - filter) + filter * lastMoveViewpointY;
                MoveViewpoint(moveViewpointX, moveViewpointY);
            }
            else if (checkMovements(lastMoveViewpointX) || checkMovements(lastMoveViewpointY))
            {
                float moveViewpointX = filter * lastMoveViewpointX;
                float moveViewpointY = filter * lastMoveViewpointY;
                MoveViewpoint(moveViewpointX, moveViewpointY);
            }

            // MoveObject
            if (view.eventObjectMoved != null && (mode == 4 || Control.ModifierKeys == Keys.Alt || Control.MouseButtons == MouseButtons.Right))
            {
                Geom3DLine pickLine = GetPickLine(xPos, yPos);
                movePos = new Geom3DVector(0, 0, 0);
                movePlane.intersectLine(pickLine, movePos);
                Geom3DVector diff = movePos.sub(moveLast);
                float moveBodyX = diff.x * (1 - moveFilter) + moveFilter * lastMoveBodyX;
                float moveBodyY = diff.y * (1 - moveFilter) + moveFilter * lastMoveBodyY;
                MoveObject(moveBodyX, moveBodyY);
            }
            else if (checkMovements(lastMoveBodyX) || checkMovements(lastMoveBodyY))
            {
                float moveBodyX = moveFilter * lastMoveBodyX;
                float moveBodyY = moveFilter * lastMoveBodyY;
                MoveObject(moveBodyX, moveBodyY);
            }

            if (render)
            {
                gl.Invalidate();
                lastXpos = xPos;
                lastYpos = yPos;
                render = false;
            }
        }
Ejemplo n.º 10
0
 private void gl_MouseDown(object sender, MouseEventArgs e)
 {
     cam.PreparePanZoomRot();
     lastX = xDown = e.X;
     lastY = yDown = e.Y;
     movePlane = new Geom3DPlane(new Geom3DVector(0, 0, 0), new Geom3DVector(0, 0, 1));
     moveStart = moveLast = new Geom3DVector(0, 0, 0);
     UpdatePickLine(e.X, e.Y);
     movePlane.intersectLine(pickLine, moveStart);
     if (e.Button == MouseButtons.Right)
     {
         ThreeDModel sel = Picktest(e.X, e.Y);
         if (sel != null)
         {
             movePlane = new Geom3DPlane(pickPoint, new Geom3DVector(0, 0, 1));
             moveStart = moveLast = new Geom3DVector(pickPoint);
         }
         if (sel != null && view.eventObjectMoved != null)
             view.eventObjectSelected(sel);
         //computeRay();
     }
     gl.Invalidate();
 }
Ejemplo n.º 11
0
 public Geom3DPlane(Geom3DVector o, Geom3DVector norm)
 {
     origin = new Geom3DVector(o);
     normal = new Geom3DVector(norm);
 }
Ejemplo n.º 12
0
 public Geom3DVector sub(Geom3DVector v)
 {
     return(new Geom3DVector(x - v.x, y - v.y, z - v.z));
 }
Ejemplo n.º 13
0
 public Geom3DVector add(Geom3DVector v)
 {
     return(new Geom3DVector(x + v.x, y + v.y, z + v.z));
 }
Ejemplo n.º 14
0
 public Geom3DVector(Geom3DVector v)
 {
     x = v.x;
     y = v.y;
     z = v.z;
 }
Ejemplo n.º 15
0
 private void gl_MouseMove(object sender, MouseEventArgs e)
 {
     double window_y = (gl.Height - e.Y) - gl.Height/2;
     normY = window_y*2.0/(double)(gl.Height);
     double window_x = e.X - gl.Width/2;
     normX = window_x*2.0/(double)(gl.Width);
     if (e.Button == MouseButtons.None)
     {
         speedX = speedY = 0;
         return;
     }
     xPos = e.X;
     yPos = e.Y;
     UpdatePickLine(e.X, e.Y);
     movePos = new Geom3DVector(0, 0, 0);
     movePlane.intersectLine(pickLine, movePos);
     float d = Math.Min(gl.Width, gl.Height) / 3;
     speedX = Math.Max(-1, Math.Min(1, (xPos - xDown) / d));
     speedY = Math.Max(-1, Math.Min(1, (yPos - yDown) / d));
 }
Ejemplo n.º 16
0
 public Geom3DVector(Geom3DVector v)
 {
     x = v.x;
     y = v.y;
     z = v.z;
 }
Ejemplo n.º 17
0
        private ThreeDModel Picktest(int x,int y)
        {
            if (view == null) return null;
            // int x = Mouse.X;
               // int y = Mouse.Y;
               // Console.WriteLine("X:" + x + " Y:" + y);
            gl.MakeCurrent();
            uint[] selectBuffer = new uint[128];
            GL.SelectBuffer(128, selectBuffer);
            GL.RenderMode(RenderingMode.Select);
            SetupViewport();

            GL.MatrixMode(MatrixMode.Projection);
            GL.PushMatrix();
            GL.LoadIdentity();

            int[] viewport = new int[4];
            GL.GetInteger(GetPName.Viewport, viewport);

            Matrix4 m = GluPickMatrix(x, viewport[3] - y, 1, 1, viewport);
            GL.MultMatrix(ref m);

            //GluPerspective(45, 32 / 24, 0.1f, 100.0f);
            //Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, 1, 0.1f, 100.0f);
            GL.MultMatrix(ref view.persp);

            GL.MatrixMode(MatrixMode.Modelview);
            GL.ClearColor(Main.threeDSettings.background.BackColor);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.Enable(EnableCap.DepthTest);
            view.lookAt = Matrix4.LookAt(view.userPosition.X, view.userPosition.Y, view.userPosition.Z, view.viewCenter.X, view.viewCenter.Y, view.viewCenter.Z, 0, 0, 1.0f);

            // Intersection on bottom plane

            int window_y = (viewport[3] - y) - viewport[3]/2;
            double norm_y = (double)window_y/(double)(viewport[3]/2);
            int window_x = x - viewport[2]/2;
            double norm_x = (double)window_x/(double)(viewport[2]/2);
            float fpy = (float)(view.nearHeight * 0.5 * norm_y) * (toolParallelProjection.Checked ? 4f : 1f);
            float fpx = (float)(view.nearHeight * 0.5 * view.aspectRatio * norm_x) * (toolParallelProjection.Checked ? 4f : 1f);

            Vector4 frontPointN = (toolParallelProjection.Checked ? new Vector4(fpx, fpy, 0, 1) : new Vector4(0, 0, 0, 1));
            Vector4 dirN = (toolParallelProjection.Checked ? new Vector4(0, 0, -view.nearDist, 0) : new Vector4(fpx, fpy, -view.nearDist, 0));
            Matrix4 rotx = Matrix4.CreateFromAxisAngle(new Vector3(1, 0, 0), (float)(view.rotX*Math.PI/180.0));
            Matrix4 rotz = Matrix4.CreateFromAxisAngle(new Vector3(0, 0, 1), (float)(view.rotZ * Math.PI / 180.0));
            Matrix4 trans = Matrix4.CreateTranslation(-ps.BedLeft-ps.PrintAreaWidth * 0.5f,-ps.BedFront -ps.PrintAreaDepth * 0.5f, -0.5f * ps.PrintAreaHeight);
            Matrix4 ntrans = view.lookAt;
            ntrans = Matrix4.Mult(rotx,ntrans);
            ntrans = Matrix4.Mult(rotz,ntrans);
            ntrans = Matrix4.Mult(trans,ntrans );
            ntrans = Matrix4.Invert(ntrans);
            Vector4 frontPoint = (toolParallelProjection.Checked ? Vector4.Transform(frontPointN,ntrans) : ntrans.Row3);
            Vector4 dirVec = Vector4.Transform(dirN, ntrans);
            pickLine = new Geom3DLine(new Geom3DVector(frontPoint.X / frontPoint.W, frontPoint.Y / frontPoint.W, frontPoint.Z / frontPoint.W),
                new Geom3DVector(dirVec.X , dirVec.Y , dirVec.Z ), true);
            dirN = new Vector4(0, 0, -view.nearDist, 0);
            dirVec = Vector4.Transform(dirN, ntrans);
            viewLine = new Geom3DLine(new Geom3DVector(frontPoint.X / frontPoint.W, frontPoint.Y / frontPoint.W, frontPoint.Z / frontPoint.W),
                new Geom3DVector(dirVec.X, dirVec.Y, dirVec.Z), true);
            viewLine.dir.normalize();
            pickLine.dir.normalize();
               /* Geom3DPlane plane = new Geom3DPlane(new Geom3DVector(0, 0, 0), new Geom3DVector(0, 0, 1));
            Geom3DVector cross = new Geom3DVector(0, 0, 0);
            plane.intersectLine(pickLine, cross);
            Main.conn.log("Linie: " + pickLine, false, 3);
            Main.conn.log("Schnittpunkt: " + cross, false, 3);
            */
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref view.lookAt);
            GL.Rotate(view.rotX, 1, 0, 0);
            GL.Rotate(view.rotZ, 0, 0, 1);
            GL.Translate(-ps.BedLeft-ps.PrintAreaWidth * 0.5f, -ps.BedFront-ps.PrintAreaDepth * 0.5f, -0.5f * ps.PrintAreaHeight);

            GL.InitNames();
            int pos = 0;
            foreach (ThreeDModel model in view.models)
            {
                GL.PushName(pos++);
                GL.PushMatrix();
                model.AnimationBefore();
                GL.Translate(model.Position.x, model.Position.y, model.Position.z);
                GL.Rotate(model.Rotation.z, Vector3.UnitZ);
                GL.Rotate(model.Rotation.y, Vector3.UnitY);
                GL.Rotate(model.Rotation.x, Vector3.UnitX);
                GL.Scale(model.Scale.x, model.Scale.y, model.Scale.z);
                model.Paint();
                model.AnimationAfter();
                GL.PopMatrix();
                GL.PopName();
            }
            GL.MatrixMode(MatrixMode.Projection);
            GL.PopMatrix();
            GL.MatrixMode(MatrixMode.Modelview);

            int hits = GL.RenderMode(RenderingMode.Render);
            ThreeDModel selected = null;
            if (hits > 0)
            {
                selected = view.models.ElementAt((int)selectBuffer[3]);
                lastDepth = selectBuffer[1];
                for (int i = 1; i < hits; i++)
                {
                    if (selectBuffer[4 * i + 1] < lastDepth)
                    {
                        lastDepth = selectBuffer[i * 4 + 1];
                        selected = view.models.ElementAt((int)selectBuffer[i * 4 + 3]);
                    }
                }
                double dfac = (double)lastDepth/uint.MaxValue;
                dfac = -(view.farDist * view.nearDist) / (dfac * (view.farDist - view.nearDist) - view.farDist);
                Geom3DVector crossPlanePoint = new Geom3DVector(viewLine.dir).scale((float)dfac).add(viewLine.point);
                Geom3DPlane objplane = new Geom3DPlane(crossPlanePoint, viewLine.dir);
                objplane.intersectLine(pickLine, pickPoint);
                //Main.conn.log("Objekttreffer: " + pickPoint, false, 3);

            }
            //PrinterConnection.logInfo("Hits: " + hits);
            return selected;
        }
Ejemplo n.º 18
0
 private void gl_MouseDown(object sender, MouseEventArgs e)
 {
     lastXpos = xPos = e.X;
     lastYpos = yPos = e.Y;
     lastRotateX = lastRotateY = 0;
     movePlane = new Geom3DPlane(new Geom3DVector(0, 0, 0), new Geom3DVector(0, 0, 1));
     moveStart = moveLast = new Geom3DVector(0, 0, 0);
     Geom3DLine pickLine = GetPickLine(e.X, e.Y);
     movePlane.intersectLine(pickLine, moveStart);
     if (e.Button == MouseButtons.Right)
     {
         ThreeDModel sel = Picktest(pickLine, e.X, e.Y);
         if (sel != null)
         {
             movePlane = new Geom3DPlane(pickPoint, new Geom3DVector(0, 0, 1));
             moveStart = moveLast = new Geom3DVector(pickPoint);
         }
         if (sel != null && view.eventObjectMoved != null)
             view.eventObjectSelected(sel);
         //computeRay();
     }
 }