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