public Camera(Camera copy) { this.name = copy.name; this.resolutionX = copy.resolutionX; this.resolutionY = copy.resolutionY; this.position = copy.position; this.lookAt = copy.lookAt; this.fovAngle = copy.fovAngle; this.rotateAngle = copy.rotateAngle; }
/// <summary> /// Działa tylko dla widoków ortogonalnych. /// </summary> /// <param name="bezierSurface"></param> /// <param name="bezierCam"></param> /// <param name="viewportType"></param> /// <param name="pos"></param> /// <param name="size"></param> /// <param name="orthoSize"></param> /// <param name="orthoPos"></param> /// <param name="orthoLookAt"></param> public static void SelectBezierControlPoint(BezierSurface bezierSurface, Camera bezierCam, ViewportType viewportType, Point pos, Point size, Vector2 orthoSize, Vector3 orthoPos, Vector3 orthoLookAt) { Vector3 outCamPos = new Vector3(), outSurfPos = new Vector3(); switch (viewportType) { case ViewportType.Perspective: CalcPerspCoords(pos, size, bezierCam.fovAngle, bezierCam.rotateAngle, bezierCam.position, bezierCam.lookAt, out outCamPos, out outSurfPos); break; case ViewportType.Orto: CalcOrthoCoords(pos, size, orthoSize, orthoPos, orthoLookAt, out outCamPos, out outSurfPos); break; } Vector3 rayDir = Vector3.Normalize(outSurfPos - outCamPos); SlimDX.Ray ray = new SlimDX.Ray(outCamPos + 0.01f * rayDir, rayDir); float dist = float.PositiveInfinity; float tmpDist = -1; BoundingBox bb; for (int i = 0; i < bezierSurface.ControlPoints.Length; i++) { bb = new BoundingBox(new Vector3(bezierSurface.ControlPoints[i].x - 0.05f, bezierSurface.ControlPoints[i].y - 0.05f, bezierSurface.ControlPoints[i].z - 0.05f), new Vector3(bezierSurface.ControlPoints[i].x + 0.05f, bezierSurface.ControlPoints[i].y + 0.05f, bezierSurface.ControlPoints[i].z + 0.05f)); if (SlimDX.Ray.Intersects(ray, bb, out tmpDist)) { if (tmpDist < dist) { dist = tmpDist; bezierSurface.selectedPointIdx = i; } } } if (dist == float.PositiveInfinity) { bezierSurface.selectedPointIdx = -1; } }
public Image GetSceneImage(Scene scene) { RecalculateData(scene); pp = new PresentParameters(); pp.SwapEffect = SwapEffect.Discard; pp.Windowed = true; pp.BackBufferWidth = 512; pp.BackBufferHeight = 512; pp.BackBufferFormat = Format.A8R8G8B8; if(d3d != null) { d3d.Dispose(); } if(device != null) { device.Dispose(); } d3d = new Direct3D(); device = new Device(d3d, 0, DeviceType.Hardware, handle, CreateFlags.HardwareVertexProcessing, pp); device.SetRenderState(RenderState.Lighting, true); for(int i = scene.lights.Count; i < maxLights; ++i) { device.EnableLight(i, false); } maxLights = scene.lights.Count; Modeler.Transformations.BoundingBox bb = new Modeler.Transformations.BoundingBox(scene); Camera cam = new Camera(); Vector3 dir = Vector3.Normalize(bb.minBB - bb.maxBB); cam.position = bb.maxBB - 1 * (float)Math.Sqrt((bb.minBB.x - bb.maxBB.x) * (bb.minBB.x - bb.maxBB.x) + (bb.minBB.y - bb.maxBB.y) * (bb.minBB.y - bb.maxBB.y) + (bb.minBB.z - bb.maxBB.z) * (bb.minBB.z - bb.maxBB.z)) * dir; cam.lookAt = bb.maxBB; cam.fovAngle = 40; cam.rotateAngle = 0; Vector3 oldDir = defLight.Direction; Vector3 dirLight = new Vector3(); dirLight.X = dir.X * (float)Math.Cos(-Math.PI / 4) + dir.Z * (float)Math.Sin(-Math.PI / 4); dirLight.Y = dir.Y; dirLight.Z = dir.Z * (float)Math.Cos(-Math.PI / 4) - dir.X * (float)Math.Sin(-Math.PI / 4); dir.Normalize(); defLight.Direction = dirLight; device.SetLight(0, defLight); device.EnableLight(0, true); device.SetRenderState(RenderState.FillMode, FillMode.Solid); device.SetRenderState(RenderState.CullMode, Cull.None); device.SetRenderState(RenderState.ShadeMode, ShadeMode.Gouraud); Mesh mesh = numIndices >= 3 ? new Mesh(device, (int)numIndices / 3, scene.points.Count, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems) : null; VertexBuffer vb = mesh != null ? mesh.VertexBuffer : null; IndexBuffer ib = mesh != null ? mesh.IndexBuffer : null; if(mesh != null) { vb.Lock(0, 0, LockFlags.None).WriteRange(vertices); vb.Unlock(); ib.Lock(0, 0, LockFlags.None).WriteRange(indices, 0, (int)numIndices); ib.Unlock(); } Mesh selMesh = numSelIndices >= 3 ? new Mesh(device, (int)numSelIndices / 3, scene.points.Count, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems) : null; VertexBuffer selvb = selMesh != null ? selMesh.VertexBuffer : null; IndexBuffer selib = selMesh != null ? selMesh.IndexBuffer : null; if(selMesh != null) { selvb.Lock(0, 0, LockFlags.None).WriteRange(vertices); selvb.Unlock(); selib.Lock(0, 0, LockFlags.None).WriteRange(selIndices, 0, (int)numSelIndices); selib.Unlock(); } Viewport viewport = new Viewport(0, 0, 512, 512, 0, 1); Texture texture = new Texture(device, 64, 64, 0, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default); device.SetRenderTarget(0, texture.GetSurfaceLevel(0)); float camRotAngle = cam.rotateAngle; float aspect = 1; float camAngle = 2.0f * (float)Math.Atan(Math.Tan(Utilities.DegToRad(cam.fovAngle) / 2.0f) / aspect); device.SetTransform(TransformState.View, Matrix.LookAtRH( cam.position, cam.lookAt, Utilities.RotatePointAroundVector(new Vector3(0, 1, 0), Vector3.Normalize(cam.lookAt - cam.position), camRotAngle))); device.SetTransform(TransformState.Projection, Matrix.PerspectiveFovRH( camAngle, aspect, 0.01f, 110000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0); device.BeginScene(); if(mesh != null) { mesh.DrawSubset(0); } if(selMesh != null) { selMesh.DrawSubset(0); } device.EndScene(); Image image = Bitmap.FromStream(Texture.ToStream(texture, ImageFileFormat.Png)); texture.Dispose(); RecalculateData(currScene); defLight.Direction = oldDir; return image; }
public Renderer(IntPtr handle, IntPtr handleBezier) { this.handle = handle; this.handleBezier = handleBezier; currScene = null; font = null; font2 = null; cameras = new List<RenderCamera>(); lights = new List<RenderLight>(); orthoWidth = new float[] { 10, 10, 10 }; orthoPos = new Vector3[] { new Vector3(0, 0, 50001), new Vector3(50001, 0, 0), new Vector3(0, 50001, 0) }; orthoLookAt = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(0, 0, -0.01f) }; ortoWidhtChange = false; bezierOrthoWidth = new float[] { 5, 5, 5 }; bezierOrthoPos = new Vector3[] { new Vector3(0, 0, 50000), new Vector3(50000, 0, 0), new Vector3(0, 50000, 0) }; bezierOrthoLookAt = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(0, 0, -0.01f) }; bezierCam = new Camera(null, 0, 0, new Vector3(-10, 10, -10), new Vector3(0, 0, 0), Utilities.DegToRad(15), 0); selectedControlPointMaterial = new Material() { Diffuse = new Color4(Color.Red) }; controlPointMaterial = new Material() { Diffuse = new Color4(Color.Black) }; camsLookAtPoints = new List<Vector3>(); clipping = false; clipVertices = new Vertex[48]; for(int i = 0; i < clipVertices.Length; ++i) { clipVertices[i] = new Vertex(); clipVertices[i].Color = Color.DarkViolet.ToArgb(); } for(int i = 0; i < 8; ++i) { clipVertices[i + 0].Position = new Vector3(i == 0 || i == 3 || i == 4 || i == 7 ? -50000 : 50000, i == 0 || i == 1 || i == 2 || i == 3 ? 2 : 2 + clipPlaneWidth * orthoWidth[0], i == 0 || i == 1 || i == 4 || i == 5 ? 50000 : -50000); } for(int i = 0; i < 8; ++i) { clipVertices[i + 8].Position = new Vector3(i == 0 || i == 3 || i == 4 || i == 7 ? 50000 : -50000, i == 0 || i == 1 || i == 2 || i == 3 ? -2 : -2 - clipPlaneWidth * orthoWidth[0], i == 0 || i == 1 || i == 4 || i == 5 ? 50000 : -50000); } for(int i = 0; i < 8; ++i) { clipVertices[i + 16].Position = new Vector3(i == 0 || i == 1 || i == 2 || i == 3 ? 2 : 2 + clipPlaneWidth * orthoWidth[0], i == 0 || i == 3 || i == 4 || i == 7 ? 50000 : -50000, i == 0 || i == 1 || i == 4 || i == 5 ? 50000 : -50000); } for(int i = 0; i < 8; ++i) { clipVertices[i + 24].Position = new Vector3(i == 0 || i == 1 || i == 2 || i == 3 ? -2 : -2 - clipPlaneWidth * orthoWidth[0], i == 0 || i == 3 || i == 4 || i == 7 ? -50000 : 50000, i == 0 || i == 1 || i == 4 || i == 5 ? 50000 : -50000); } for(int i = 0; i < 8; ++i) { clipVertices[i + 32].Position = new Vector3(i == 0 || i == 3 || i == 4 || i == 7 ? -50000 : 50000, i == 0 || i == 1 || i == 4 || i == 5 ? -50000 : 50000, i == 0 || i == 1 || i == 2 || i == 3 ? -2 : -2 - clipPlaneWidth * orthoWidth[0]); } for(int i = 0; i < 8; ++i) { clipVertices[i + 40].Position = new Vector3(i == 0 || i == 3 || i == 4 || i == 7 ? 50000 : -50000, i == 0 || i == 1 || i == 4 || i == 5 ? -50000 : 50000, i == 0 || i == 1 || i == 2 || i == 3 ? 2 : 2 + clipPlaneWidth * orthoWidth[0]); } clipIndices = new int[216]; for(int i = 0; i < 6; ++i) { clipIndices[36 * i + 0] = 8 * i + 0; clipIndices[36 * i + 1] = 8 * i + 1; clipIndices[36 * i + 2] = 8 * i + 5; clipIndices[36 * i + 3] = 8 * i + 0; clipIndices[36 * i + 4] = 8 * i + 5; clipIndices[36 * i + 5] = 8 * i + 4; clipIndices[36 * i + 6] = 8 * i + 1; clipIndices[36 * i + 7] = 8 * i + 2; clipIndices[36 * i + 8] = 8 * i + 6; clipIndices[36 * i + 9] = 8 * i + 1; clipIndices[36 * i + 10] = 8 * i + 6; clipIndices[36 * i + 11] = 8 * i + 5; clipIndices[36 * i + 12] = 8 * i + 2; clipIndices[36 * i + 13] = 8 * i + 3; clipIndices[36 * i + 14] = 8 * i + 7; clipIndices[36 * i + 15] = 8 * i + 2; clipIndices[36 * i + 16] = 8 * i + 7; clipIndices[36 * i + 17] = 8 * i + 6; clipIndices[36 * i + 18] = 8 * i + 3; clipIndices[36 * i + 19] = 8 * i + 0; clipIndices[36 * i + 20] = 8 * i + 5; clipIndices[36 * i + 21] = 8 * i + 3; clipIndices[36 * i + 22] = 8 * i + 5; clipIndices[36 * i + 23] = 8 * i + 7; clipIndices[36 * i + 24] = 8 * i + 0; clipIndices[36 * i + 25] = 8 * i + 1; clipIndices[36 * i + 26] = 8 * i + 2; clipIndices[36 * i + 27] = 8 * i + 0; clipIndices[36 * i + 28] = 8 * i + 2; clipIndices[36 * i + 29] = 8 * i + 3; clipIndices[36 * i + 30] = 8 * i + 4; clipIndices[36 * i + 31] = 8 * i + 5; clipIndices[36 * i + 32] = 8 * i + 6; clipIndices[36 * i + 33] = 8 * i + 4; clipIndices[36 * i + 34] = 8 * i + 6; clipIndices[36 * i + 35] = 8 * i + 7; } perspective = new Viewport(); perspective.X = 0; perspective.Y = 0; perspective.Width = 1; perspective.Height = 1; perspective.MinZ = -1000; perspective.MaxZ = 1000; top = new Viewport(); top.X = 0; top.Y = 0; top.Width = 1; top.Height = 1; top.MinZ = -1000; top.MaxZ = 1000; front = new Viewport(); front.X = 0; front.Y = 0; front.Width = 1; front.Height = 1; front.MinZ = -1000; front.MaxZ = 1000; side = new Viewport(); side.X = 0; side.Y = 0; side.Width = 1; side.Height = 1; side.MinZ = -1000; side.MaxZ = 1000; defLight = new Light(); defLight.Type = LightType.Directional; defLight.Diffuse = Color.White; defLight.Direction = new Vector3(1, -1, -2); maxLights = 0; vertexElems = new[] { new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0), new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0), new VertexElement(0, 24, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0), new VertexElement(0, 28, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0), VertexElement.VertexDeclarationEnd }; wireframe = true; pp = new PresentParameters(); pp.SwapEffect = SwapEffect.Discard; pp.Windowed = true; pp.BackBufferFormat = Format.A8R8G8B8; bezierPp = new PresentParameters(); bezierPp.SwapEffect = SwapEffect.Discard; bezierPp.Windowed = true; bezierPp.BackBufferFormat = Format.A8R8G8B8; }
//chwilowo używane do obrotu kamery, po wprowadzniu nowej metody interaktywnej manipulacji kamery - usunąć public void NormalizeCamera(Camera camera) { float tmpLength = (float) Math.Sqrt(Math.Pow(camera.lookAt.X - camera.position.X, 2) + Math.Pow(camera.lookAt.Y - camera.position.Y, 2) + Math.Pow(camera.lookAt.Z - camera.position.Z, 2)); camera.lookAt.X = camera.position.X + (camera.lookAt.X - camera.position.X) / tmpLength; camera.lookAt.Y = camera.position.Y + (camera.lookAt.Y - camera.position.Y) / tmpLength; camera.lookAt.Z = camera.position.Z + (camera.lookAt.Z - camera.position.Z) / tmpLength; }
public void AddCamera(Camera camera, Vector3 translation) { modified = true; camera.name = "Kamera " + cams.Count.ToString(); camera.position += translation; cams.Add(camera); activeCamera = cams.Count - 1; }