public void RenderBezier(ViewportInfo viewportInfo) { pp = new PresentParameters(); pp.SwapEffect = SwapEffect.Discard; pp.Windowed = true; pp.BackBufferWidth = viewportInfo.resX; pp.BackBufferHeight = viewportInfo.resY; pp.BackBufferFormat = Format.A8R8G8B8; d3dBezier = new Direct3D(); deviceBezier = new Device(d3dBezier, 0, DeviceType.Hardware, handleBezier, CreateFlags.HardwareVertexProcessing, pp); deviceBezier.SetRenderState(RenderState.Lighting, true); deviceBezier.SetLight(0, defLight); deviceBezier.EnableLight(0, true); deviceBezier.SetRenderState(RenderState.FillMode, wireframe ? 2 : 0); deviceBezier.SetRenderState(RenderState.CullMode, 1); deviceBezier.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); deviceBezier.Present(); d3dBezier.Dispose(); deviceBezier.Dispose(); }
ViewportInfo GetViewCoords(int selectedTab) { ViewportInfo viewInfo = new ViewportInfo(); if (selectedTab == 0) { viewInfo.resX = Views.Size.Width; viewInfo.resY = Views.Size.Height; } else { viewInfo.resX = ViewsBezier.Size.Width; viewInfo.resY = ViewsBezier.Size.Height; } int panelX = 0; int panelY = 0; viewInfo.posX = new int[4]; viewInfo.posY = new int[4]; viewInfo.sizeX = new int[4]; viewInfo.sizeY = new int[4]; if(maxViewport == ViewportOrientation.None || selectedTab != 0) { int elemWidth; int elemHeight; if (selectedTab == 0) { elemWidth = (int)(Views.Size.Width / 2) - 1; elemHeight = (int)(Views.Size.Height / 2) - 1; } else { elemWidth = (int)(ViewsBezier.Size.Width / 2) - 1; elemHeight = (int)(ViewsBezier.Size.Height / 2) - 1; } viewInfo.posX[0] = panelX; viewInfo.posY[0] = panelY; viewInfo.sizeX[0] = elemWidth; viewInfo.sizeY[0] = elemHeight; viewInfo.posX[1] = panelX + elemWidth + 2; viewInfo.posY[1] = panelY; viewInfo.sizeX[1] = viewInfo.resX - viewInfo.posX[1]; viewInfo.sizeY[1] = elemHeight; viewInfo.posX[2] = panelX; viewInfo.posY[2] = panelY + elemHeight + 2; viewInfo.sizeX[2] = elemWidth; viewInfo.sizeY[2] = viewInfo.resY - viewInfo.posY[2]; viewInfo.posX[3] = panelX + elemWidth + 2; viewInfo.posY[3] = panelY + elemHeight + 2; viewInfo.sizeX[3] = viewInfo.resX - viewInfo.posX[3]; viewInfo.sizeY[3] = viewInfo.resY - viewInfo.posY[3]; } else { for(int i = 0; i < 4; ++i) { viewInfo.posX[i] = viewInfo.posY[i] = viewInfo.sizeX[i] = viewInfo.sizeY[i] = 0; } if (selectedTab == 0) { viewInfo.sizeX[(int)maxViewport] = Views.Size.Width; viewInfo.sizeY[(int)maxViewport] = Views.Size.Height; } else { viewInfo.sizeX[(int)maxViewport] = ViewsBezier.Size.Width; viewInfo.sizeY[(int)maxViewport] = ViewsBezier.Size.Height; } } return viewInfo; }
public void RenderViews(ViewportInfo viewportInfo, Scene scene) { #if MEASURE_TIMES DateTime ts = DateTime.Now; DateTime t1 = DateTime.Now; #endif if (pp.BackBufferWidth != viewportInfo.resX || pp.BackBufferHeight != viewportInfo.resY) { pp = new PresentParameters(); pp.SwapEffect = SwapEffect.Discard; pp.Windowed = true; pp.BackBufferWidth = viewportInfo.resX; pp.BackBufferHeight = viewportInfo.resY; 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); } #if MEASURE_TIMES DateTime t2 = DateTime.Now; TimeSpan t = t2 - t1; w.WriteLine("Tworzenie device'a " + t.Milliseconds); #endif //selectionTex = Texture.FromFile(device, "..\\..\\selectionTex.png"); #if MEASURE_TIMES t1 = DateTime.Now; device.SetRenderState(RenderState.Lighting, true); #endif int l = 0; if (scene.lights.Count == 0) { device.SetLight(0, defLight); device.EnableLight(0, true); device.EnableLight(1, false); } else { foreach (Light_ light_ in scene.lights) { Light light = new Light(); light.Diffuse = new Color4(light_.colorR, light_.colorG, light_.colorB); light.Position = light_.position; light.Range = 100; light.Attenuation0 = 10.0f / light_.power; if (light_.type == Light_Type.Point) { light.Type = LightType.Point; } else { light.Type = LightType.Spot; } light.Direction = light_.direction; device.SetLight(l, light); device.EnableLight(l, true); l++; } device.EnableLight(l + 1, false); } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Tworzenie świateł " + t.Milliseconds); t1 = DateTime.Now; #endif device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); device.SetRenderState(RenderState.CullMode, Cull.Clockwise); device.SetRenderState(RenderState.ShadeMode, ShadeMode.Flat); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("SetRenderState " + t.Milliseconds); t1 = DateTime.Now; #endif //Vertex[] vertices = new Vertex[scene.points.Count]; //List<int>[] vertexTriangle = new List<int>[scene.points.Count]; //Parallel.For(0, vertexTriangle.Length, index => vertexTriangle[index] = new List<int>()); ///*for(int i = 0; i < vertexTriangle.Length; ++i) //{ // vertexTriangle[i] = new List<int>(); //}*/ //int[] indices = new int[3 * scene.triangles.Count]; //int[] selIndices = new int[3 * scene.triangles.Count]; //uint numIndices = 0; //uint numSelIndices = 0; bool[] selPoints = new bool[scene.points.Count]; Parallel.For(0, selPoints.Length, index => selPoints[index] = false); /*for(int i = 0; i < selPoints.Length; ++i) * { * selPoints[i] = false; * }*/ bool[] selected = scene.GetSelectedTriangles(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("t1 " + t.Milliseconds); t1 = DateTime.Now; #endif for (int i = 0; i < scene.triangles.Count; ++i) { Triangle triangle = scene.triangles[i]; //if (selected[i] == false) //{ // scene.indices[numIndices] = (int)triangle.p1; // scene.indices[numIndices + 1] = (int)triangle.p3; // scene.indices[numIndices + 2] = (int)triangle.p2; // numIndices += 3; //} //else if (selected[i]) { //scene.selIndices[numSelIndices] = (int)triangle.p1; //scene.selIndices[numSelIndices + 1] = (int)triangle.p3; //scene.selIndices[numSelIndices + 2] = (int)triangle.p2; selPoints[triangle.p1] = true; selPoints[triangle.p2] = true; selPoints[triangle.p3] = true; //numSelIndices += 3; } //scene.vertexTriangle[triangle.p1].Add(i); //scene.vertexTriangle[triangle.p2].Add(i); //scene.vertexTriangle[triangle.p3].Add(i); } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("t2 " + t.Milliseconds); t1 = DateTime.Now; #endif for (int i = 0; i < scene.points.Count; ++i) { if (selPoints[i] || scene.normals.Count <= i) { Vector3D normal = new Vector3D(); foreach (int face in scene.vertexTriangle[i]) { normal += Utilities.CalculateNormal(scene.points[(int)scene.triangles[face].p3], scene.points[(int)scene.triangles[face].p2], scene.points[(int)scene.triangles[face].p1]); } normal.Normalize(); if (scene.normals.Count <= i) { scene.normals.Add(normal); } else { scene.normals[i] = normal; } } } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Liczenie normalnych " + t.Milliseconds); t1 = DateTime.Now; #endif Dictionary <string, int> matNames = new Dictionary <string, int>(); for (int i = 0; i < scene.materials.Count; ++i) { matNames.Add(scene.materials[i].name, i); } int[] trPart = new int[scene.triangles.Count]; for (int i = 0; i < scene.parts.Count; ++i) { for (int j = 0; j < scene.parts[i].triangles.Count; ++j) { trPart[scene.parts[i].triangles[j]] = i; } } for (int i = 0; i < scene.vertices.Length; ++i) { int partIndex = trPart[scene.vertexTriangle[i][0]]; String matName = scene.materialAssign[partIndex]; Material_ material = scene.materials[matNames[matName]]; Vector3D point = scene.points[i]; scene.vertices[i].Position = new Vector3(point.x, point.y, point.z); scene.vertices[i].Normal = new Vector3(scene.normals[i].x, scene.normals[i].y, scene.normals[i].z); scene.vertices[i].Color = Color.FromArgb((int)(255 * material.colorR), (int)(255 * material.colorG), (int)(255 * material.colorB)).ToArgb(); scene.vertices[i].tex0 = 0; scene.vertices[i].tex1 = 0; } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Znajdowanie części i przyp. wierz. " + t.Milliseconds); t1 = DateTime.Now; #endif Mesh mesh = scene.numIndices >= 3 ? new Mesh(device, (int)scene.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 MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Tworzenie mesh1 " + t.Milliseconds); t1 = DateTime.Now; #endif if (mesh != null) { vb.Lock(0, 0, LockFlags.None).WriteRange(scene.vertices); vb.Unlock(); ib.Lock(0, 0, LockFlags.None).WriteRange(scene.indices, 0, (int)scene.numIndices); ib.Unlock(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Kopiowanie buforów mesh1 " + t.Milliseconds); #endif } #if MEASURE_TIMES t1 = DateTime.Now; #endif Mesh selMesh = scene.numSelIndices >= 3 ? new Mesh(device, (int)scene.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 MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Tworzenie mesh2 " + t.Milliseconds); t1 = DateTime.Now; #endif if (selMesh != null) { selvb.Lock(0, 0, LockFlags.None).WriteRange(scene.vertices); selvb.Unlock(); selib.Lock(0, 0, LockFlags.None).WriteRange(scene.selIndices, 0, (int)scene.numSelIndices); selib.Unlock(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Kopiowanie buforów mesh2 " + t.Milliseconds); #endif } #if MEASURE_TIMES t1 = DateTime.Now; #endif device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); if (perspective.X != viewportInfo.posX[0] || perspective.Y != viewportInfo.posY[0] || perspective.Width != viewportInfo.sizeX[0] || perspective.Height != viewportInfo.sizeY[0]) { perspective = new Viewport(); perspective.X = viewportInfo.posX[0]; perspective.Y = viewportInfo.posY[0]; perspective.Width = viewportInfo.sizeX[0]; perspective.Height = viewportInfo.sizeY[0]; perspective.MinZ = 0; perspective.MaxZ = 1; } if (top.X != viewportInfo.posX[1] || top.Y != viewportInfo.posY[1] || top.Width != viewportInfo.sizeX[1] || top.Height != viewportInfo.sizeY[1]) { top = new Viewport(); top.X = viewportInfo.posX[1]; top.Y = viewportInfo.posY[1]; top.Width = viewportInfo.sizeX[1]; top.Height = viewportInfo.sizeY[1]; top.MinZ = 0; top.MaxZ = 1; } if (front.X != viewportInfo.posX[2] || front.Y != viewportInfo.posY[2] || front.Width != viewportInfo.sizeX[2] || front.Height != viewportInfo.sizeY[2]) { front = new Viewport(); front.X = viewportInfo.posX[2]; front.Y = viewportInfo.posY[2]; front.Width = viewportInfo.sizeX[2]; front.Height = viewportInfo.sizeY[2]; front.MinZ = 0; front.MaxZ = 1; } if (side.X != viewportInfo.posX[3] || side.Y != viewportInfo.posY[3] || side.Width != viewportInfo.sizeX[3] || side.Height != viewportInfo.sizeY[3]) { side = new Viewport(); side.X = viewportInfo.posX[3]; side.Y = viewportInfo.posY[3]; side.Width = viewportInfo.sizeX[3]; side.Height = viewportInfo.sizeY[3]; side.MinZ = 0; side.MaxZ = 1; } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Modyfikacja viewport'ów " + t.Milliseconds); t1 = DateTime.Now; #endif if (perspective.Width > 0 && perspective.Height > 0) { device.Viewport = perspective; float camRotAngle = scene.cams.ElementAt(scene.activeCamera).rotateAngle; float aspect = (float)perspective.Width / perspective.Height; float angle = 2.0f * (float)Math.Atan(Math.Tan(Utilities.DegToRad(scene.cams.ElementAt(scene.activeCamera).fovAngle) / 2.0f) / aspect); device.SetTransform(TransformState.View, Matrix.LookAtRH( scene.cams[scene.activeCamera].position, scene.cams[scene.activeCamera].lookAt, Utilities.RotatePointAroundVector(new Vector3(0, 1, 0), Vector3.Normalize(scene.cams[scene.activeCamera].lookAt - scene.cams[scene.activeCamera].position), camRotAngle))); device.SetTransform(TransformState.Projection, Matrix.PerspectiveFovRH( angle, aspect, 0.01f, 20000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.EndScene(); } if (top.Width > 0 && top.Height > 0) { device.Viewport = top; device.SetTransform(TransformState.View, Matrix.LookAtRH( orthoPos[0], orthoLookAt[0], new Vector3(0, 1, 0))); device.SetTransform(TransformState.Projection, Matrix.OrthoRH( orthoWidht[0], (float)(top.Height) / top.Width * orthoWidht[0], 0.01f, 20000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.EndScene(); } if (front.Width > 0 && front.Height > 0) { device.Viewport = front; device.SetTransform(TransformState.View, Matrix.LookAtRH( orthoPos[1], orthoLookAt[1], new Vector3(0, 1, 0))); device.SetTransform(TransformState.Projection, Matrix.OrthoRH( orthoWidht[1], (float)(front.Height) / front.Width * orthoWidht[1], 0.01f, 20000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.EndScene(); } if (side.Width > 0 && side.Height > 0) { device.Viewport = side; device.SetTransform(TransformState.View, Matrix.LookAtRH( orthoPos[2], orthoLookAt[2], new Vector3(0, 1, 0))); device.SetTransform(TransformState.Projection, Matrix.OrthoRH( orthoWidht[2], (float)(side.Height) / side.Width * orthoWidht[2], 0.01f, 20000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.EndScene(); } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Renderowanie " + t.Milliseconds); t1 = DateTime.Now; #endif device.Present(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Present " + t.Milliseconds); t1 = DateTime.Now; #endif //selectionTex.Dispose(); if (selMesh != null) { selvb.Dispose(); selib.Dispose(); selMesh.Dispose(); } if (mesh != null) { vb.Dispose(); ib.Dispose(); mesh.Dispose(); } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Czyszczenie " + t.Milliseconds); DateTime te = DateTime.Now; t = te - ts; w.WriteLine("Całość " + t.Milliseconds); w.WriteLine("-----------------------------------------------------------------"); w.WriteLine(); w.WriteLine(); w.WriteLine(); #endif }
public void ScaleOrtho(int orthoViewport, float value, ViewportInfo info, int x, int y) { Vector2 oldSize = new Vector2(orthoWidth[orthoViewport], orthoWidth[orthoViewport] * (info.sizeY[orthoViewport] / (float)info.sizeX[orthoViewport])); orthoWidth[0] *= 1 + 0.05f * -value; orthoWidth[1] *= 1 + 0.05f * -value; orthoWidth[2] *= 1 + 0.05f * -value; if(orthoWidth[orthoViewport] < 0.01f) { orthoWidth[0] = 0.01f; orthoWidth[1] = 0.01f; orthoWidth[2] = 0.01f; } Vector2 newSize = new Vector2(orthoWidth[orthoViewport], orthoWidth[orthoViewport] * (info.sizeY[orthoViewport] / (float)info.sizeX[orthoViewport])); Vector2 mousePos = new Vector2((x - info.posX[orthoViewport]) / (float)info.sizeX[orthoViewport], (y - info.posY[orthoViewport]) / (float)info.sizeY[orthoViewport]); Vector2 shift = new Vector2(); shift.X = (oldSize.X - newSize.X) * (mousePos.X - 0.5f) * info.sizeX[orthoViewport] / orthoWidth[orthoViewport]; shift.Y = (oldSize.Y - newSize.Y) * (mousePos.Y - 0.5f) * info.sizeX[orthoViewport] / orthoWidth[orthoViewport]; MoveOrtho(orthoViewport, shift.X, shift.Y, info.sizeX[orthoViewport], info.sizeY[orthoViewport]); UpdateClipPlanes(); ortoWidhtChange = true; }
public void RenderBezier(ViewportInfo viewportInfo, BezierSurface bezier) { if (bezierPp.BackBufferWidth != viewportInfo.resX || bezierPp.BackBufferHeight != viewportInfo.resY || bezierImageCreated) { bezierPp = new PresentParameters(); bezierPp.SwapEffect = SwapEffect.Discard; bezierPp.Windowed = true; bezierPp.BackBufferWidth = viewportInfo.resX; bezierPp.BackBufferHeight = viewportInfo.resY; bezierPp.BackBufferFormat = Format.A8R8G8B8; if (d3dBezier != null) { d3dBezier.Dispose(); } if (deviceBezier != null) { deviceBezier.Dispose(); } bezierImageCreated = false; d3dBezier = new Direct3D(); deviceBezier = new Device(d3dBezier, 0, DeviceType.Hardware, handleBezier, CreateFlags.HardwareVertexProcessing, bezierPp); if(font2 != null) { font2.Dispose(); } font2 = new SlimDX.Direct3D9.Font(deviceBezier, new System.Drawing.Font(FontFamily.GenericSansSerif, 7)); } if (top.X != viewportInfo.posX[2] || top.Y != viewportInfo.posY[2] || top.Width != viewportInfo.sizeX[2] || top.Height != viewportInfo.sizeY[2]) { top = new Viewport(); top.X = viewportInfo.posX[2]; top.Y = viewportInfo.posY[2]; top.Width = viewportInfo.sizeX[2]; top.Height = viewportInfo.sizeY[2]; top.MinZ = 0; top.MaxZ = 1; } if (front.X != viewportInfo.posX[0] || front.Y != viewportInfo.posY[0] || front.Width != viewportInfo.sizeX[0] || front.Height != viewportInfo.sizeY[0]) { front = new Viewport(); front.X = viewportInfo.posX[0]; front.Y = viewportInfo.posY[0]; front.Width = viewportInfo.sizeX[0]; front.Height = viewportInfo.sizeY[0]; front.MinZ = 0; front.MaxZ = 1; } if (side.X != viewportInfo.posX[1] || side.Y != viewportInfo.posY[1] || side.Width != viewportInfo.sizeX[1] || side.Height != viewportInfo.sizeY[1]) { side = new Viewport(); side.X = viewportInfo.posX[1]; side.Y = viewportInfo.posY[1]; side.Width = viewportInfo.sizeX[1]; side.Height = viewportInfo.sizeY[1]; side.MinZ = 0; side.MaxZ = 1; } if(perspective.X != viewportInfo.posX[3] || perspective.Y != viewportInfo.posY[3] || perspective.Width != viewportInfo.sizeX[3] || perspective.Height != viewportInfo.sizeY[3]) { perspective = new Viewport(); perspective.X = viewportInfo.posX[3]; perspective.Y = viewportInfo.posY[3]; perspective.Width = viewportInfo.sizeX[3]; perspective.Height = viewportInfo.sizeY[3]; perspective.MinZ = 0; perspective.MaxZ = 1; } deviceBezier.SetRenderState(RenderState.Lighting, true); deviceBezier.SetLight(0, defLight); deviceBezier.EnableLight(0, true); deviceBezier.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); deviceBezier.SetRenderState(RenderState.CullMode, Cull.None); deviceBezier.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); Vertex[] gridVertices = new Vertex[bezier.OutputPoints.Length]; Vertex[] controlVertices = new Vertex[bezier.ControlPoints.Length]; List<int>[] vertexTriangle = new List<int>[bezier.OutputPoints.Length]; Parallel.For(0, vertexTriangle.Length, index => vertexTriangle[index] = new List<int>()); /*for(int i = 0; i < vertexTriangle.Length; ++i) { vertexTriangle[i] = new List<int>(); }*/ int[] indices = new int[3 * bezier.triangles.Count]; //int[] selIndices = new int[3 * scene.triangles.Count]; uint numIndices = 0; //uint numSelIndices = 0; //bool[] selPoints = new bool[scene.points.Count]; //Parallel.For(0, selPoints.Length, index => selPoints[index] = false); for (int i = 0; i < bezier.triangles.Count; i++) { indices[numIndices++] = (int)bezier.triangles[i].p1; indices[numIndices++] = (int)bezier.triangles[i].p2; indices[numIndices++] = (int)bezier.triangles[i].p3; vertexTriangle[bezier.triangles[i].p1].Add(i); vertexTriangle[bezier.triangles[i].p2].Add(i); vertexTriangle[bezier.triangles[i].p3].Add(i); } // Liczenie normalnych siatki trojkątów for (int i = 0; i < bezier.OutputPoints.Length; i++) { Vector3 normal = new Vector3(); foreach (int face in vertexTriangle[i]) { normal += Utilities.CalculateNormal(bezier.OutputPoints[(int)bezier.triangles[face].p3], bezier.OutputPoints[(int)bezier.triangles[face].p2], bezier.OutputPoints[(int)bezier.triangles[face].p1]); } normal.Normalize(); gridVertices[i].Position = new Vector3(bezier.OutputPoints[i].x, bezier.OutputPoints[i].y, bezier.OutputPoints[i].z); gridVertices[i].Normal = normal; gridVertices[i].Color = Color.Beige.ToArgb(); } Mesh gridMesh = numIndices > 2 ? new Mesh(deviceBezier, (int)numIndices / 3, bezier.OutputPoints.Length, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems): null; VertexBuffer vb = gridMesh != null ? gridMesh.VertexBuffer : null; IndexBuffer ib = gridMesh != null ? gridMesh.IndexBuffer : null; if (gridMesh != null) { vb.Lock(0, 0, LockFlags.None).WriteRange(gridVertices); vb.Unlock(); ib.Lock(0, 0, LockFlags.None).WriteRange(indices, 0, (int)numIndices); ib.Unlock(); } Mesh controlPointsMesh = Mesh.CreateSphere(deviceBezier, 0.05f, 12, 12); if (perspective.Width > 0 && perspective.Height > 0) { deviceBezier.Viewport = perspective; float aspect = (float)perspective.Width / perspective.Height; deviceBezier.SetTransform(TransformState.View, Matrix.LookAtRH( bezierCam.position, bezierCam.lookAt, Utilities.RotatePointAroundVector(new Vector3(0, 1, 0), Vector3.Normalize(bezierCam.lookAt - bezierCam.position), bezierCam.rotateAngle))); deviceBezier.SetTransform(TransformState.Projection, Matrix.PerspectiveFovRH( bezierCam.fovAngle, aspect, 0.01f, 110000)); deviceBezier.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); deviceBezier.BeginScene(); if (gridMesh != null) { gridMesh.DrawSubset(0); deviceBezier.SetRenderState(RenderState.FillMode, FillMode.Solid); for (int i = 0; i < bezier.ControlPoints.Length; i++) { deviceBezier.SetTransform(TransformState.World, Matrix.Translation( bezier.ControlPoints[i].x, bezier.ControlPoints[i].y, bezier.ControlPoints[i].z)); if (i == bezier.selectedPointIdx) { deviceBezier.Material = selectedControlPointMaterial; deviceBezier.SetRenderState(RenderState.Lighting, false); controlPointsMesh.DrawSubset(0); deviceBezier.Material = controlPointMaterial; deviceBezier.SetRenderState(RenderState.Lighting, true); } else { controlPointsMesh.DrawSubset(0); } } deviceBezier.SetTransform(TransformState.World, Matrix.Identity); deviceBezier.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } deviceBezier.EndScene(); } if (top.Width > 0 && top.Height > 0) { deviceBezier.Viewport = top; deviceBezier.SetTransform(TransformState.View, Matrix.LookAtRH( bezierOrthoPos[2], bezierOrthoLookAt[2], new Vector3(0, 1, 0))); deviceBezier.SetTransform(TransformState.Projection, Matrix.OrthoRH( bezierOrthoWidth[2], (float)(top.Height) / top.Width * bezierOrthoWidth[2], 0.01f, 110000)); deviceBezier.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); deviceBezier.BeginScene(); string text1 = " -z\n/|\\\n |\n"; font2.DrawString(null, text1, viewportInfo.posX[2] + 15, viewportInfo.posY[2], Color.Blue); string text2 = "\n\n\n -----> x"; font2.DrawString(null, text2, viewportInfo.posX[2] + 15, viewportInfo.posY[2], Color.Red); if (gridMesh != null) { gridMesh.DrawSubset(0); deviceBezier.SetRenderState(RenderState.FillMode, FillMode.Solid); for (int i = 0; i < bezier.ControlPoints.Length; i++) { deviceBezier.SetTransform(TransformState.World, Matrix.Translation( bezier.ControlPoints[i].x, bezier.ControlPoints[i].y, bezier.ControlPoints[i].z)); if (i == bezier.selectedPointIdx) { deviceBezier.Material = selectedControlPointMaterial; deviceBezier.SetRenderState(RenderState.Lighting, false); controlPointsMesh.DrawSubset(0); deviceBezier.Material = controlPointMaterial; deviceBezier.SetRenderState(RenderState.Lighting, true); } else { controlPointsMesh.DrawSubset(0); } } deviceBezier.SetTransform(TransformState.World, Matrix.Identity); deviceBezier.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } deviceBezier.EndScene(); } if (front.Width > 0 && front.Height > 0) { deviceBezier.Viewport = front; deviceBezier.SetTransform(TransformState.View, Matrix.LookAtRH( bezierOrthoPos[0], bezierOrthoLookAt[0], new Vector3(0, 1, 0))); deviceBezier.SetTransform(TransformState.Projection, Matrix.OrthoRH( bezierOrthoWidth[0], (float)(front.Height) / front.Width * bezierOrthoWidth[0], 0.01f, 110000)); deviceBezier.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); deviceBezier.BeginScene(); string text1 = " y\n/|\\\n |\n"; font2.DrawString(null, text1, viewportInfo.posX[0] + 15, viewportInfo.posY[0], Color.Green); string text2 = "\n\n\n -----> x"; font2.DrawString(null, text2, viewportInfo.posX[0] + 15, viewportInfo.posY[0], Color.Red); if (gridMesh != null) { gridMesh.DrawSubset(0); deviceBezier.SetRenderState(RenderState.FillMode, FillMode.Solid); for (int i = 0; i < bezier.ControlPoints.Length; i++) { deviceBezier.SetTransform(TransformState.World, Matrix.Translation( bezier.ControlPoints[i].x, bezier.ControlPoints[i].y, bezier.ControlPoints[i].z)); if (i == bezier.selectedPointIdx) { deviceBezier.Material = selectedControlPointMaterial; deviceBezier.SetRenderState(RenderState.Lighting, false); controlPointsMesh.DrawSubset(0); deviceBezier.Material = controlPointMaterial; deviceBezier.SetRenderState(RenderState.Lighting, true); } else { controlPointsMesh.DrawSubset(0); } } deviceBezier.SetTransform(TransformState.World, Matrix.Identity); deviceBezier.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } deviceBezier.EndScene(); } if (side.Width > 0 && side.Height > 0) { deviceBezier.Viewport = side; deviceBezier.SetTransform(TransformState.View, Matrix.LookAtRH( bezierOrthoPos[1], bezierOrthoLookAt[1], new Vector3(0, 1, 0))); deviceBezier.SetTransform(TransformState.Projection, Matrix.OrthoRH( bezierOrthoWidth[1], (float)(side.Height) / side.Width * bezierOrthoWidth[1], 0.01f, 110000)); deviceBezier.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); deviceBezier.BeginScene(); string text1 = " y\n/|\\\n |\n"; font2.DrawString(null, text1, viewportInfo.posX[1] + 15, viewportInfo.posY[1], Color.Green); string text2 = "\n\n\n -----> -z"; font2.DrawString(null, text2, viewportInfo.posX[1] + 15, viewportInfo.posY[1], Color.Blue); if (gridMesh != null) { gridMesh.DrawSubset(0); deviceBezier.SetRenderState(RenderState.FillMode, FillMode.Solid); for (int i = 0; i < bezier.ControlPoints.Length; i++) { deviceBezier.SetTransform(TransformState.World, Matrix.Translation( bezier.ControlPoints[i].x, bezier.ControlPoints[i].y, bezier.ControlPoints[i].z)); if (i == bezier.selectedPointIdx) { deviceBezier.Material = selectedControlPointMaterial; deviceBezier.SetRenderState(RenderState.Lighting, false); controlPointsMesh.DrawSubset(0); deviceBezier.Material = controlPointMaterial; deviceBezier.SetRenderState(RenderState.Lighting, true); } else { controlPointsMesh.DrawSubset(0); } } deviceBezier.SetTransform(TransformState.World, Matrix.Identity); deviceBezier.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } deviceBezier.EndScene(); } deviceBezier.Present(); gridMesh.Dispose(); controlPointsMesh.Dispose(); //d3dBezier.Dispose(); //deviceBezier.Dispose(); }
public void ScaleBezierOrtho(int orthoViewport, float value, ViewportInfo info, int x, int y) { Vector2 oldSize = new Vector2(BezierOrthoWidth[orthoViewport], BezierOrthoWidth[orthoViewport] * (info.sizeY[orthoViewport] / (float)info.sizeX[orthoViewport])); BezierOrthoWidth[0] *= 1 + 0.05f * -value; BezierOrthoWidth[1] *= 1 + 0.05f * -value; BezierOrthoWidth[2] *= 1 + 0.05f * -value; if (BezierOrthoWidth[orthoViewport] < 0.01f) { BezierOrthoWidth[0] = 0.01f; BezierOrthoWidth[1] = 0.01f; BezierOrthoWidth[2] = 0.01f; } Vector2 newSize = new Vector2(bezierOrthoWidth[orthoViewport], bezierOrthoWidth[orthoViewport] * (info.sizeY[orthoViewport] / (float)info.sizeX[orthoViewport])); Vector2 mousePos = new Vector2((x - info.posX[orthoViewport]) / (float)info.sizeX[orthoViewport], (y - info.posY[orthoViewport]) / (float)info.sizeY[orthoViewport]); Vector2 shift = new Vector2(); shift.X = (oldSize.X - newSize.X) * (mousePos.X - 0.5f) * info.sizeX[orthoViewport] / bezierOrthoWidth[orthoViewport]; shift.Y = (oldSize.Y - newSize.Y) * (mousePos.Y - 0.5f) * info.sizeX[orthoViewport] / bezierOrthoWidth[orthoViewport]; MoveBezierOrtho(orthoViewport, shift.X, shift.Y, info.sizeX[orthoViewport], info.sizeY[orthoViewport]); }
public void RenderViews(ViewportInfo viewportInfo, Scene scene) { currScene = scene; #if MEASURE_TIMES DateTime ts = DateTime.Now; DateTime t1 = DateTime.Now; #endif if (pp.BackBufferWidth != viewportInfo.resX || pp.BackBufferHeight != viewportInfo.resY) { pp = new PresentParameters(); pp.SwapEffect = SwapEffect.Discard; pp.Windowed = true; pp.BackBufferWidth = viewportInfo.resX; pp.BackBufferHeight = viewportInfo.resY; 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); if(font != null) { font.Dispose(); } font = new SlimDX.Direct3D9.Font(device, new System.Drawing.Font(FontFamily.GenericSansSerif, 7)); } bool viewportChange = false; if(top.X != viewportInfo.posX[2] || top.Y != viewportInfo.posY[2] || top.Width != viewportInfo.sizeX[2] || top.Height != viewportInfo.sizeY[2]) { top = new Viewport(); top.X = viewportInfo.posX[2]; top.Y = viewportInfo.posY[2]; top.Width = viewportInfo.sizeX[2]; top.Height = viewportInfo.sizeY[2]; top.MinZ = 0; top.MaxZ = 1; viewportChange = true; } if(front.X != viewportInfo.posX[0] || front.Y != viewportInfo.posY[0] || front.Width != viewportInfo.sizeX[0] || front.Height != viewportInfo.sizeY[0]) { front = new Viewport(); front.X = viewportInfo.posX[0]; front.Y = viewportInfo.posY[0]; front.Width = viewportInfo.sizeX[0]; front.Height = viewportInfo.sizeY[0]; front.MinZ = 0; front.MaxZ = 1; viewportChange = true; } if(side.X != viewportInfo.posX[1] || side.Y != viewportInfo.posY[1] || side.Width != viewportInfo.sizeX[1] || side.Height != viewportInfo.sizeY[1]) { side = new Viewport(); side.X = viewportInfo.posX[1]; side.Y = viewportInfo.posY[1]; side.Width = viewportInfo.sizeX[1]; side.Height = viewportInfo.sizeY[1]; side.MinZ = 0; side.MaxZ = 1; viewportChange = true; } if(perspective.X != viewportInfo.posX[3] || perspective.Y != viewportInfo.posY[3] || perspective.Width != viewportInfo.sizeX[3] || perspective.Height != viewportInfo.sizeY[3]) { perspective = new Viewport(); perspective.X = viewportInfo.posX[3]; perspective.Y = viewportInfo.posY[3]; perspective.Width = viewportInfo.sizeX[3]; perspective.Height = viewportInfo.sizeY[3]; perspective.MinZ = 0; perspective.MaxZ = 1; viewportChange = true; } if(perspective.Width > 0 && perspective.Height > 0) { persPos = new System.Drawing.Point(perspective.Width, perspective.Height); } #if MEASURE_TIMES DateTime t2 = DateTime.Now; TimeSpan t = t2 - t1; w.WriteLine("Tworzenie device'a " + t.Milliseconds); #endif //selectionTex = Texture.FromFile(device, "..\\..\\selectionTex.png"); #if MEASURE_TIMES t1 = DateTime.Now; #endif device.SetRenderState(RenderState.Lighting, true); for(int i = scene.lights.Count; i < maxLights; ++i) { device.EnableLight(i, false); } maxLights = scene.lights.Count; int l = 0; foreach (Light_ light_ in scene.lights) { Light light = new Light(); light.Diffuse = new Color4(light_.colorR, light_.colorG, light_.colorB); light.Position = light_.position; light.Range = 100000; light.Attenuation1 = 2.0f / light_.power; if(light_.type == Light_Type.Point || light_.type == Light_Type.Goniometric) { light.Type = LightType.Point; } else { light.Type = LightType.Spot; } if(light_.type == Light_Type.Spot) { light.Direction = light_.direction; light.Theta = Utilities.DegToRad(light_.innerAngle); light.Phi = Utilities.DegToRad(light_.outerAngle); light.Falloff = 1; } device.SetLight(l, light); device.EnableLight(l, true); ++l; } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Tworzenie świateł " + t.Milliseconds); t1 = DateTime.Now; #endif device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); device.SetRenderState(RenderState.CullMode, Cull.None); device.SetRenderState(RenderState.ShadeMode, ShadeMode.Gouraud); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("SetRenderState " + t.Milliseconds); t1 = DateTime.Now; #endif for(int i = 0; i < scene.points.Count; ++i) { if(selPoints[i] == true || scene.normals.Count <= i) { Vector3D normal = new Vector3D(); foreach(int face in vertexTriangle[i]) { normal += Utilities.CalculateNormal(scene.points[(int)scene.triangles[face].p3], scene.points[(int)scene.triangles[face].p2], scene.points[(int)scene.triangles[face].p1]); } normal.Normalize(); if(scene.normals.Count <= i) { scene.normals.Add(normal); } else { scene.normals[i] = normal; } Vector3D point = scene.points[i]; vertices[i].Position = new Vector3(point.x, point.y, point.z); vertices[i].Normal = new Vector3(scene.normals[i].x, scene.normals[i].y, scene.normals[i].z); } } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Liczenie normalnych " + t.Milliseconds); t1 = DateTime.Now; #endif 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 MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Tworzenie mesh1 " + t.Milliseconds); t1 = DateTime.Now; #endif 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(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Kopiowanie buforów mesh1 " + t.Milliseconds); #endif } #if MEASURE_TIMES t1 = DateTime.Now; #endif 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 MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Tworzenie mesh2 " + t.Milliseconds); t1 = DateTime.Now; #endif 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(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Kopiowanie buforów mesh2 " + t.Milliseconds); #endif } while(cameras.Count < scene.cams.Count) { cameras.Add(new RenderCamera()); } while(cameras.Count > scene.cams.Count) { cameras.RemoveAt(cameras.Count - 1); } while(camsLookAtPoints.Count < scene.cams.Count) { camsLookAtPoints.Add(new Vector3()); } while(camsLookAtPoints.Count > scene.cams.Count) { camsLookAtPoints.RemoveAt(camsLookAtPoints.Count - 1); } for(int i = 0; i < cameras.Count; ++i) { if(cameras[i].set == false || viewportChange || cameras[i].position != scene.cams[i].position || cameras[i].lookAt != scene.cams[i].lookAt || cameras[i].fovAngle != scene.cams[i].fovAngle || cameras[i].rotateAngle != scene.cams[i].rotateAngle) { if(cameras[i].set == false) { cameras[i].set = true; } cameras[i].position = scene.cams[i].position; cameras[i].lookAt = scene.cams[i].lookAt; cameras[i].fovAngle = scene.cams[i].fovAngle; cameras[i].rotateAngle = scene.cams[i].rotateAngle; Vector3 upLeft, upRight, loLeft, loRight; SelectingElems.GetViewCorners(cameras[i].position, cameras[i].lookAt, cameras[i].fovAngle, cameras[i].rotateAngle, persPos, out upLeft, out upRight, out loLeft, out loRight); const float camViewLength = 2.0f; Vector3 ld = loLeft - cameras[i].position; ld.Normalize(); Vector3 lu = upLeft - cameras[i].position; lu.Normalize(); Vector3 rd = loRight - cameras[i].position; rd.Normalize(); Vector3 ru = upRight - cameras[i].position; ru.Normalize(); cameras[i].points[0] = cameras[i].position; /*cameras[i].points[1] = cameras[i].position + ((0.5f + (cameras[i].fovAngle / 180)) * camViewLength) * ld; cameras[i].points[2] = cameras[i].position + ((0.5f + (cameras[i].fovAngle / 180)) * camViewLength) * lu; cameras[i].points[3] = cameras[i].position + ((0.5f + (cameras[i].fovAngle / 180)) * camViewLength) * ru; cameras[i].points[4] = cameras[i].position + ((0.5f + (cameras[i].fovAngle / 180)) * camViewLength) * rd;*/ cameras[i].points[1] = loLeft; cameras[i].points[2] = upLeft; cameras[i].points[3] = upRight; cameras[i].points[4] = loRight; Vector3 up = cameras[i].points[2] - cameras[i].points[1]; up.Normalize(); Vector3 left = cameras[i].points[2] - cameras[i].points[3]; left.Normalize(); Vector3 right = -left; float dist = (float)Math.Sqrt((cameras[i].points[2].X - cameras[i].points[3].X) * (cameras[i].points[2].X - cameras[i].points[3].X) + (cameras[i].points[2].Y - cameras[i].points[3].Y) * (cameras[i].points[2].Y - cameras[i].points[3].Y) + (cameras[i].points[2].Z - cameras[i].points[3].Z) * (cameras[i].points[2].Z - cameras[i].points[3].Z)); dist *= 0.8f; float upTriangleFactor = cameras[i].fovAngle / 60; cameras[i].points[5] = (cameras[i].points[2] + cameras[i].points[3]) / 2 + left * upTriangleFactor * 0.3f * dist; cameras[i].points[6] = (cameras[i].points[2] + cameras[i].points[3]) / 2 + up * upTriangleFactor * 0.25f * dist; cameras[i].points[7] = (cameras[i].points[2] + cameras[i].points[3]) / 2 + right * upTriangleFactor * 0.3f * dist; camsLookAtPoints[i] = (cameras[i].points[1] + cameras[i].points[2] + cameras[i].points[3] + cameras[i].points[4]) / 4; } } Vertex[] camVertices = new Vertex[cameras.Count * 8]; int[] camIndices = new int[3 * cameras.Count * RenderCamera.triangles.Length]; for(int i = 0; i < cameras.Count; ++i) { for(int j = 0; j < RenderCamera.triangles.Length; ++j) { camIndices[i * 3 * RenderCamera.triangles.Length + 3 * j + 0] = i * cameras[i].points.Length + (int)RenderCamera.triangles[j].p1; camIndices[i * 3 * RenderCamera.triangles.Length + 3 * j + 1] = i * cameras[i].points.Length + (int)RenderCamera.triangles[j].p2; camIndices[i * 3 * RenderCamera.triangles.Length + 3 * j + 2] = i * cameras[i].points.Length + (int)RenderCamera.triangles[j].p3; } } bool[] selCameras = new bool[cameras.Count]; for(int i = 0; i < selCameras.Length; ++i) { selCameras[i] = false; } for(int i = 0; i < scene.selCams.Count; ++i) { selCameras[scene.selCams[i]] = true; } for(int i = 0; i < cameras.Count; ++i) { for(int j = 0; j < cameras[i].points.Length; ++j) { camVertices[i * 8 + j].Position = cameras[i].points[j]; camVertices[i * 8 + j].Normal = new Vector3(); if(selCameras[i] == false) { if(scene.activeCamera == i) { camVertices[i * 8 + j].Color = Color.FromArgb(249, 203, 44).ToArgb(); } else { camVertices[i * 8 + j].Color = Color.FromArgb(193, 227, 195).ToArgb(); } } else { if(scene.activeCamera == i) { camVertices[i * 8 + j].Color = Color.FromArgb(243, 106, 24).ToArgb(); } else { camVertices[i * 8 + j].Color = Color.FromArgb(255, 255, 150).ToArgb(); } } camVertices[i * 8 + j].tex0 = 0; camVertices[i * 8 + j].tex1 = 0; } } Mesh camMesh = cameras.Count > 0 ? new Mesh(device, (int)camIndices.Length / 3, camVertices.Length, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems) : null; VertexBuffer camVB = camMesh != null ? camMesh.VertexBuffer : null; IndexBuffer camIB = camMesh != null ? camMesh.IndexBuffer : null; if(camMesh != null) { camVB.Lock(0, 0, LockFlags.None).WriteRange(camVertices); camVB.Unlock(); camIB.Lock(0, 0, LockFlags.None).WriteRange(camIndices); camIB.Unlock(); } while(lights.Count < scene.lights.Count) { lights.Add(new RenderLight()); } while(lights.Count > scene.lights.Count) { lights.RemoveAt(lights.Count - 1); } for(int i = 0; i < lights.Count; ++i) { if(ortoWidhtChange == true || lights[i].set == false || lights[i].position != scene.lights[i].position || lights[i].direction != scene.lights[i].direction || lights[i].type != scene.lights[i].type || lights[i].innerAngle != scene.lights[i].innerAngle || lights[i].outerAngle != scene.lights[i].outerAngle) { if(lights[i].set == false) { lights[i].set = true; } lights[i].position = scene.lights[i].position; lights[i].direction = scene.lights[i].direction; lights[i].type = scene.lights[i].type; lights[i].innerAngle = scene.lights[i].innerAngle; lights[i].outerAngle = scene.lights[i].outerAngle; for(int j = 0; j < RenderLight.pointsDef.Length; ++j) { lights[i].points[j] = RenderLight.pointsDef[j]; } for(int j = RenderLight.pointsDef.Length; j < RenderLight.pointsSpotNum; ++j) { lights[i].points[j] = new Vector3(); } if(lights[i].type == Light_Type.Spot || lights[i].type == Light_Type.Goniometric) { Vector3 dir = lights[i].direction; dir.Normalize(); if(dir.Z == 0) { dir.Z = 0.0001f; } Vector3 Vs = new Vector3(1, 1, -(dir.X + dir.Y) / dir.Z); Vector3 V1 = Vector3.Normalize(Vs); Vector3 V2 = Vector3.Cross(dir, V1); Vector2[,] circle = new Vector2[2, 20]; float r1 = spotLightDist * (float)Math.Tan(Utilities.DegToRad(lights[i].innerAngle / 2)); float r2 = spotLightDist * (float)Math.Tan(Utilities.DegToRad(lights[i].outerAngle / 2)); for(int j = 0; j < 2; ++j) { for(int k = 0; k < 20; ++k) { circle[j, k] = new Vector2((j == 0 ? r1 : r2) * (float)Math.Sin(Utilities.DegToRad(18 * k)), (j == 0 ? r1 : r2) * (float)Math.Cos(Utilities.DegToRad(18 * k))); } } Vector3[,] circle3D = new Vector3[2, 20]; for(int j = 0; j < 2; ++j) { for(int k = 0; k < 20; ++k) { float X = V1.X * circle[j, k].X + V2.X * circle[j, k].Y; float Y = V1.Y * circle[j, k].X + V2.Y * circle[j, k].Y; float Z = V1.Z * circle[j, k].X + V2.Z * circle[j, k].Y; Vector3 radius = spotLightDist * dir; circle3D[j, k] = new Vector3(X, Y, Z) + radius; } } for(int j = 0; j < 20; ++j) { lights[i].points[RenderLight.pointsPointNum + 2 * j] = new Vector3(circle3D[0, j].X, circle3D[0, j].Y, circle3D[0, j].Z); lights[i].points[RenderLight.pointsPointNum + 2 * j + 1] = new Vector3(circle3D[0, j].X + 0.000001f, circle3D[0, j].Y, circle3D[0, j].Z); lights[i].points[RenderLight.pointsPointNum + 40 + 2 * j] = new Vector3(circle3D[1, j].X, circle3D[1, j].Y, circle3D[1, j].Z); lights[i].points[RenderLight.pointsPointNum + 40 + 2 * j + 1] = new Vector3(circle3D[1, j].X + 0.000001f, circle3D[1, j].Y, circle3D[1, j].Z); } lights[i].points[120] = new Vector3(0, 0, 0); lights[i].points[121] = new Vector3(0.000001f, 0, 0); } for(int j = 0; j < lights[i].points.Length; ++j) { lights[i].points[j] *= OrthoWidth[0] / 10; lights[i].points[j] += lights[i].position; } } } ortoWidhtChange = false; Vertex[] lightsVertices = new Vertex[lights.Count * RenderLight.pointsSpotNum]; int[] lightsIndices = new int[3 * lights.Count * RenderLight.triangles.Length]; int trIndex = 0; for(int i = 0; i < lights.Count; ++i) { int trNum = lights[i].type == Light_Type.Point ? RenderLight.trianglesPointNum : RenderLight.trianglesSpotNum; for(int j = 0; j < trNum; ++j) { lightsIndices[trIndex++] = i * lights[i].points.Length + (int)RenderLight.triangles[j].p1; lightsIndices[trIndex++] = i * lights[i].points.Length + (int)RenderLight.triangles[j].p2; lightsIndices[trIndex++] = i * lights[i].points.Length + (int)RenderLight.triangles[j].p3; } } bool[] selLights = new bool[lights.Count]; for(int i = 0; i < selLights.Length; ++i) { selLights[i] = false; } for(int i = 0; i < scene.selLights.Count; ++i) { selLights[scene.selLights[i]] = true; } for(int i = 0; i < lights.Count; ++i) { for(int j = 0; j < lights[i].points.Length; ++j) { lightsVertices[i * RenderLight.pointsSpotNum + j].Position = lights[i].points[j]; lightsVertices[i * RenderLight.pointsSpotNum + j].Normal = new Vector3(); if(selLights[i] == false) { if(scene.lights[i].enabled == true) { if(scene.lights[i].type != Light_Type.Goniometric) { lightsVertices[i * RenderLight.pointsSpotNum + j].Color = Color.FromArgb(234, 203, 28).ToArgb(); } else { lightsVertices[i * RenderLight.pointsSpotNum + j].Color = Color.FromArgb(123, 120, 46).ToArgb(); } } else { if(scene.lights[i].type != Light_Type.Goniometric) { lightsVertices[i * RenderLight.pointsSpotNum + j].Color = Color.FromArgb(48, 46, 54).ToArgb(); } else { lightsVertices[i * RenderLight.pointsSpotNum + j].Color = Color.FromArgb(67, 78, 63).ToArgb(); } } } else { if(scene.lights[i].enabled == true) { if(scene.lights[i].type != Light_Type.Goniometric) { lightsVertices[i * RenderLight.pointsSpotNum + j].Color = Color.FromArgb(243, 106, 24).ToArgb(); } else { lightsVertices[i * RenderLight.pointsSpotNum + j].Color = Color.FromArgb(159, 210, 4).ToArgb(); } } else { if(scene.lights[i].type != Light_Type.Goniometric) { lightsVertices[i * RenderLight.pointsSpotNum + j].Color = Color.FromArgb(19, 24, 224).ToArgb(); } else { lightsVertices[i * RenderLight.pointsSpotNum + j].Color = Color.FromArgb(19, 224, 96).ToArgb(); } } } lightsVertices[i * RenderLight.pointsSpotNum + j].tex0 = 0; lightsVertices[i * RenderLight.pointsSpotNum + j].tex1 = 0; } } Mesh lightsMesh = lights.Count > 0 ? new Mesh(device, trIndex / 3, lightsVertices.Length, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems) : null; VertexBuffer lightsVB = lightsMesh != null ? lightsMesh.VertexBuffer : null; IndexBuffer lightsIB = lightsMesh != null ? lightsMesh.IndexBuffer : null; if(lightsMesh != null) { lightsVB.Lock(0, 0, LockFlags.None).WriteRange(lightsVertices); lightsVB.Unlock(); lightsIB.Lock(0, 0, LockFlags.None).WriteRange(lightsIndices, 0, trIndex); lightsIB.Unlock(); } int coneLights = 0; foreach(Light_ light in scene.lights) { if(light.type == Light_Type.Spot || light.type == Light_Type.Goniometric) { ++coneLights; } } Mesh pointsMesh = Mesh.CreateBox(device, pointSize, pointSize, pointSize); Mesh clipMeshX = null; Mesh clipMeshY = null; Mesh clipMeshZ = null; VertexBuffer clipVBX = null; IndexBuffer clipIBX = null; VertexBuffer clipVBY = null; IndexBuffer clipIBY = null; VertexBuffer clipVBZ = null; IndexBuffer clipIBZ = null; if(clipping == true) { clipMeshX = new Mesh(device, clipIndices.Length / 9, clipVertices.Length, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems); clipMeshY = new Mesh(device, clipIndices.Length / 9, clipVertices.Length, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems); clipMeshZ = new Mesh(device, clipIndices.Length / 9, clipVertices.Length, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems); clipVBX = clipMeshX.VertexBuffer; clipIBX = clipMeshX.IndexBuffer; clipVBX.Lock(0, 0, LockFlags.None).WriteRange(clipVertices); clipVBX.Unlock(); clipIBX.Lock(0, 0, LockFlags.None).WriteRange(clipIndices, (clipIndices.Length / 6) * 2, (clipIndices.Length / 6) * 2); clipIBX.Unlock(); clipVBY = clipMeshY.VertexBuffer; clipIBY = clipMeshY.IndexBuffer; clipVBY.Lock(0, 0, LockFlags.None).WriteRange(clipVertices); clipVBY.Unlock(); clipIBY.Lock(0, 0, LockFlags.None).WriteRange(clipIndices, 0, (clipIndices.Length / 6) * 2); clipIBY.Unlock(); clipVBZ = clipMeshZ.VertexBuffer; clipIBZ = clipMeshZ.IndexBuffer; clipVBZ.Lock(0, 0, LockFlags.None).WriteRange(clipVertices); clipVBZ.Unlock(); clipIBZ.Lock(0, 0, LockFlags.None).WriteRange(clipIndices, (clipIndices.Length / 6) * 4, (clipIndices.Length / 6) * 2); clipIBZ.Unlock(); } #if MEASURE_TIMES t1 = DateTime.Now; #endif device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Modyfikacja viewport'ów " + t.Milliseconds); t1 = DateTime.Now; #endif if (perspective.Width > 0 && perspective.Height > 0) { device.Viewport = perspective; float camRotAngle = scene.cams.ElementAt(scene.activeCamera).rotateAngle; float aspect = (float)perspective.Width / perspective.Height; float angle = 2.0f * (float)Math.Atan(Math.Tan(Utilities.DegToRad(scene.cams.ElementAt(scene.activeCamera).fovAngle) / 2.0f) / aspect); device.SetTransform(TransformState.View, Matrix.LookAtRH( scene.cams[scene.activeCamera].position, scene.cams[scene.activeCamera].lookAt, Utilities.RotatePointAroundVector(new Vector3(0, 1, 0), Vector3.Normalize(scene.cams[scene.activeCamera].lookAt - scene.cams[scene.activeCamera].position), camRotAngle))); device.SetTransform(TransformState.Projection, Matrix.PerspectiveFovRH( angle, aspect, 0.01f, 110000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.EndScene(); } SlimDX.Plane clipPlaneX = new SlimDX.Plane(); SlimDX.Plane clipPlaneX2 = new SlimDX.Plane(); SlimDX.Plane clipPlaneY = new SlimDX.Plane(); SlimDX.Plane clipPlaneY2 = new SlimDX.Plane(); SlimDX.Plane clipPlaneZ = new SlimDX.Plane(); SlimDX.Plane clipPlaneZ2 = new SlimDX.Plane(); SlimDX.Plane clipPlaneXPlus = new SlimDX.Plane(); SlimDX.Plane clipPlaneX2Minus = new SlimDX.Plane(); SlimDX.Plane clipPlaneYPlus = new SlimDX.Plane(); SlimDX.Plane clipPlaneY2Minus = new SlimDX.Plane(); SlimDX.Plane clipPlaneZPlus = new SlimDX.Plane(); SlimDX.Plane clipPlaneZ2Minus = new SlimDX.Plane(); clipPlaneYPlus = new SlimDX.Plane(new Vector3(0, 50000, 0), new Vector3(0, -1, 0)); clipPlaneY2Minus = new SlimDX.Plane(new Vector3(0, -50000, 0), new Vector3(0, 1, 0)); clipPlaneXPlus = new SlimDX.Plane(new Vector3(50000, 0, 0), new Vector3(-1, 0, 0)); clipPlaneX2Minus = new SlimDX.Plane(new Vector3(-50000, 0, 0), new Vector3(1, 0, 0)); clipPlaneZPlus = new SlimDX.Plane(new Vector3(0, 0, -50000), new Vector3(0, 0, 1)); clipPlaneZ2Minus = new SlimDX.Plane(new Vector3(0, 0, 50000), new Vector3(0, 0, -1)); if(clipping == true) { clipPlaneY = new SlimDX.Plane(new Vector3(0, clipVertices[0].Position.Y, 0), new Vector3(0, -1, 0)); clipPlaneY2 = new SlimDX.Plane(new Vector3(0, clipVertices[8].Position.Y, 0), new Vector3(0, 1, 0)); clipPlaneX = new SlimDX.Plane(new Vector3(clipVertices[16].Position.X, 0, 0), new Vector3(-1, 0, 0)); clipPlaneX2 = new SlimDX.Plane(new Vector3(clipVertices[24].Position.X, 0, 0), new Vector3(1, 0, 0)); clipPlaneZ = new SlimDX.Plane(new Vector3(0, 0, clipVertices[32].Position.Z), new Vector3(0, 0, 1)); clipPlaneZ2 = new SlimDX.Plane(new Vector3(0, 0, clipVertices[40].Position.Z), new Vector3(0, 0, -1)); device.SetRenderState(RenderState.ClipPlaneEnable, ClipFlags.Bottom | ClipFlags.Front | ClipFlags.Left | ClipFlags.Right | ClipFlags.Back | ClipFlags.Top); } if (top.Width > 0 && top.Height > 0) { device.Viewport = top; device.SetTransform(TransformState.View, Matrix.LookAtRH( orthoPos[2], orthoLookAt[2], new Vector3(0, 1, 0))); device.SetTransform(TransformState.Projection, Matrix.OrthoRH( orthoWidth[2], (float)(top.Height) / top.Width * orthoWidth[2], 0.01f, 110000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); string text1 = " -z\n/|\\\n |\n"; font.DrawString(null, text1, viewportInfo.posX[2] + 15, viewportInfo.posY[2], Color.Blue); string text2 = "\n\n\n -----> x"; font.DrawString(null, text2, viewportInfo.posX[2] + 15, viewportInfo.posY[2], Color.Red); if(clipping == true) { device.SetRenderState(RenderState.Lighting, false); device.SetRenderState(RenderState.FillMode, FillMode.Solid); clipMeshX.DrawSubset(0); clipMeshZ.DrawSubset(0); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); device.SetRenderState(RenderState.Lighting, true); device.SetClipPlane(0, clipPlaneX); device.SetClipPlane(1, clipPlaneX2); device.SetClipPlane(2, clipPlaneY); device.SetClipPlane(3, clipPlaneY2); device.SetClipPlane(4, clipPlaneZ); device.SetClipPlane(5, clipPlaneZ2); } if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.SetClipPlane(0, clipPlaneXPlus); device.SetClipPlane(1, clipPlaneX2Minus); device.SetClipPlane(2, clipPlaneYPlus); device.SetClipPlane(3, clipPlaneY2Minus); device.SetClipPlane(4, clipPlaneZPlus); device.SetClipPlane(5, clipPlaneZ2Minus); device.SetRenderState(RenderState.FillMode, FillMode.Solid); for(int i = 0; i < scene.lights.Count; ++i) { if(scene.lights[i].type == Light_Type.Spot || scene.lights[i].type == Light_Type.Goniometric) { device.SetTransform(TransformState.World, Matrix.Scaling(orthoWidth[2] / 10, orthoWidth[0] / 10, orthoWidth[2] / 10) * Matrix.Translation(scene.lights[i].position + scene.lights[i].direction * spotLightDist * orthoWidth[1] / 10)); pointsMesh.DrawSubset(0); } } for(int i = 0; i < scene.cams.Count; ++i) { device.SetTransform(TransformState.World, Matrix.Scaling(orthoWidth[2] / 10, orthoWidth[2] / 10, orthoWidth[2] / 10) * Matrix.Translation(scene.cams[i].position)); pointsMesh.DrawSubset(0); device.SetTransform(TransformState.World, Matrix.Scaling(orthoWidth[2] / 10, orthoWidth[2] / 10, orthoWidth[2] / 10) * Matrix.Translation(camsLookAtPoints[i])); pointsMesh.DrawSubset(0); } device.SetTransform(TransformState.World, Matrix.Identity); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); if(camMesh != null) { device.SetRenderState(RenderState.FillMode, FillMode.Wireframe); device.SetRenderState(RenderState.Lighting, false); camMesh.DrawSubset(0); device.SetRenderState(RenderState.Lighting, true); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } if(lightsMesh != null) { device.SetRenderState(RenderState.FillMode, FillMode.Wireframe); device.SetRenderState(RenderState.Lighting, false); lightsMesh.DrawSubset(0); device.SetRenderState(RenderState.Lighting, true); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } device.EndScene(); } if (front.Width > 0 && front.Height > 0) { device.Viewport = front; device.SetTransform(TransformState.View, Matrix.LookAtRH( orthoPos[0], orthoLookAt[0], new Vector3(0, 1, 0))); device.SetTransform(TransformState.Projection, Matrix.OrthoRH( orthoWidth[0], (float)(front.Height) / front.Width * orthoWidth[0], 0.01f, 110000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); string text1 = " y\n/|\\\n |\n"; font.DrawString(null, text1, viewportInfo.posX[0] + 15, viewportInfo.posY[0], Color.Green); string text2 = "\n\n\n -----> x"; font.DrawString(null, text2, viewportInfo.posX[0] + 15, viewportInfo.posY[0], Color.Red); if(clipping == true) { device.SetRenderState(RenderState.Lighting, false); device.SetRenderState(RenderState.FillMode, FillMode.Solid); clipMeshX.DrawSubset(0); clipMeshY.DrawSubset(0); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); device.SetRenderState(RenderState.Lighting, true); device.SetClipPlane(0, clipPlaneX); device.SetClipPlane(1, clipPlaneX2); device.SetClipPlane(2, clipPlaneY); device.SetClipPlane(3, clipPlaneY2); device.SetClipPlane(4, clipPlaneZ); device.SetClipPlane(5, clipPlaneZ2); } if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.SetClipPlane(0, clipPlaneXPlus); device.SetClipPlane(1, clipPlaneX2Minus); device.SetClipPlane(2, clipPlaneYPlus); device.SetClipPlane(3, clipPlaneY2Minus); device.SetClipPlane(4, clipPlaneZPlus); device.SetClipPlane(5, clipPlaneZ2Minus); device.SetRenderState(RenderState.FillMode, FillMode.Solid); for(int i = 0; i < scene.lights.Count; ++i) { if(scene.lights[i].type == Light_Type.Spot || scene.lights[i].type == Light_Type.Goniometric) { device.SetTransform(TransformState.World, Matrix.Scaling(orthoWidth[0] / 10, orthoWidth[0] / 10, orthoWidth[0] / 10) * Matrix.Translation(scene.lights[i].position + scene.lights[i].direction * spotLightDist * orthoWidth[0] / 10)); pointsMesh.DrawSubset(0); } } for(int i = 0; i < scene.cams.Count; ++i) { device.SetTransform(TransformState.World, Matrix.Scaling(orthoWidth[0] / 10, orthoWidth[0] / 10, orthoWidth[0] / 10) * Matrix.Translation(scene.cams[i].position)); pointsMesh.DrawSubset(0); device.SetTransform(TransformState.World, Matrix.Scaling(orthoWidth[0] / 10, orthoWidth[0] / 10, orthoWidth[0] / 10) * Matrix.Translation(camsLookAtPoints[i])); pointsMesh.DrawSubset(0); } device.SetTransform(TransformState.World, Matrix.Identity); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); if(camMesh != null) { device.SetRenderState(RenderState.FillMode, FillMode.Wireframe); device.SetRenderState(RenderState.Lighting, false); camMesh.DrawSubset(0); device.SetRenderState(RenderState.Lighting, true); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } if(lightsMesh != null) { device.SetRenderState(RenderState.FillMode, FillMode.Wireframe); device.SetRenderState(RenderState.Lighting, false); lightsMesh.DrawSubset(0); device.SetRenderState(RenderState.Lighting, true); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } device.EndScene(); } if (side.Width > 0 && side.Height > 0) { device.Viewport = side; device.SetTransform(TransformState.View, Matrix.LookAtRH( orthoPos[1], orthoLookAt[1], new Vector3(0, 1, 0))); device.SetTransform(TransformState.Projection, Matrix.OrthoRH( orthoWidth[1], (float)(side.Height) / side.Width * orthoWidth[1], 0.01f, 110000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); string text1 = " y\n/|\\\n |\n"; font.DrawString(null, text1, viewportInfo.posX[1] + 15, viewportInfo.posY[1], Color.Green); string text2 = "\n\n\n -----> -z"; font.DrawString(null, text2, viewportInfo.posX[1] + 15, viewportInfo.posY[1], Color.Blue); if(clipping == true) { device.SetRenderState(RenderState.Lighting, false); device.SetRenderState(RenderState.FillMode, FillMode.Solid); clipMeshY.DrawSubset(0); clipMeshZ.DrawSubset(0); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); device.SetRenderState(RenderState.Lighting, true); device.SetClipPlane(0, clipPlaneX); device.SetClipPlane(1, clipPlaneX2); device.SetClipPlane(2, clipPlaneY); device.SetClipPlane(3, clipPlaneY2); device.SetClipPlane(4, clipPlaneZ); device.SetClipPlane(5, clipPlaneZ2); } if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.SetClipPlane(0, clipPlaneXPlus); device.SetClipPlane(1, clipPlaneX2Minus); device.SetClipPlane(2, clipPlaneYPlus); device.SetClipPlane(3, clipPlaneY2Minus); device.SetClipPlane(4, clipPlaneZPlus); device.SetClipPlane(5, clipPlaneZ2Minus); device.SetRenderState(RenderState.FillMode, FillMode.Solid); for(int i = 0; i < scene.lights.Count; ++i) { if(scene.lights[i].type == Light_Type.Spot || scene.lights[i].type == Light_Type.Goniometric) { device.SetTransform(TransformState.World, Matrix.Scaling(orthoWidth[1] / 10, orthoWidth[0] / 10, orthoWidth[1] / 10) * Matrix.Translation(scene.lights[i].position + scene.lights[i].direction * spotLightDist * orthoWidth[1] / 10)); pointsMesh.DrawSubset(0); } } for(int i = 0; i < scene.cams.Count; ++i) { device.SetTransform(TransformState.World, Matrix.Scaling(orthoWidth[1] / 10, orthoWidth[1] / 10, orthoWidth[1] / 10) * Matrix.Translation(scene.cams[i].position)); pointsMesh.DrawSubset(0); device.SetTransform(TransformState.World, Matrix.Scaling(orthoWidth[1] / 10, orthoWidth[1] / 10, orthoWidth[1] / 10) * Matrix.Translation(camsLookAtPoints[i])); pointsMesh.DrawSubset(0); } device.SetTransform(TransformState.World, Matrix.Identity); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); if(camMesh != null) { device.SetRenderState(RenderState.FillMode, FillMode.Wireframe); device.SetRenderState(RenderState.Lighting, false); camMesh.DrawSubset(0); device.SetRenderState(RenderState.Lighting, true); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } if(lightsMesh != null) { device.SetRenderState(RenderState.FillMode, FillMode.Wireframe); device.SetRenderState(RenderState.Lighting, false); lightsMesh.DrawSubset(0); device.SetRenderState(RenderState.Lighting, true); device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); } device.EndScene(); } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Renderowanie " + t.Milliseconds); t1 = DateTime.Now; #endif device.Present(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Present " + t.Milliseconds); t1 = DateTime.Now; #endif //selectionTex.Dispose(); if (selMesh != null) { selvb.Dispose(); selib.Dispose(); selMesh.Dispose(); } if (mesh != null) { vb.Dispose(); ib.Dispose(); mesh.Dispose(); } if(camMesh != null) { camVB.Dispose(); camIB.Dispose(); camMesh.Dispose(); } if(lightsMesh != null) { lightsVB.Dispose(); lightsIB.Dispose(); lightsMesh.Dispose(); } if(clipping == true) { clipVBX.Dispose(); clipIBX.Dispose(); clipVBY.Dispose(); clipIBY.Dispose(); clipVBZ.Dispose(); clipIBZ.Dispose(); clipMeshX.Dispose(); clipMeshY.Dispose(); clipMeshZ.Dispose(); } pointsMesh.Dispose(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Czyszczenie " + t.Milliseconds); DateTime te = DateTime.Now; t = te - ts; w.WriteLine("Całość " + t.Milliseconds); w.WriteLine("-----------------------------------------------------------------"); w.WriteLine(); w.WriteLine(); w.WriteLine(); #endif }
public void RenderViews(ViewportInfo viewportInfo, Scene scene) { #if MEASURE_TIMES DateTime ts = DateTime.Now; DateTime t1 = DateTime.Now; #endif if (pp.BackBufferWidth != viewportInfo.resX || pp.BackBufferHeight != viewportInfo.resY) { pp = new PresentParameters(); pp.SwapEffect = SwapEffect.Discard; pp.Windowed = true; pp.BackBufferWidth = viewportInfo.resX; pp.BackBufferHeight = viewportInfo.resY; 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); } #if MEASURE_TIMES DateTime t2 = DateTime.Now; TimeSpan t = t2 - t1; w.WriteLine("Tworzenie device'a " + t.Milliseconds); #endif //selectionTex = Texture.FromFile(device, "..\\..\\selectionTex.png"); #if MEASURE_TIMES t1 = DateTime.Now; device.SetRenderState(RenderState.Lighting, true); #endif int l = 0; if (scene.lights.Count == 0) { device.SetLight(0, defLight); device.EnableLight(0, true); device.EnableLight(1, false); } else { foreach (Light_ light_ in scene.lights) { Light light = new Light(); light.Diffuse = new Color4(light_.colorR, light_.colorG, light_.colorB); light.Position = light_.position; light.Range = 100; light.Attenuation0 = 10.0f / light_.power; if (light_.type==Light_Type.Point) light.Type = LightType.Point; else light.Type = LightType.Spot; light.Direction = light_.direction; device.SetLight(l, light); device.EnableLight(l, true); l++; } device.EnableLight(l + 1, false); } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Tworzenie świateł " + t.Milliseconds); t1 = DateTime.Now; #endif device.SetRenderState(RenderState.FillMode, wireframe ? FillMode.Wireframe : FillMode.Solid); device.SetRenderState(RenderState.CullMode, Cull.Clockwise); device.SetRenderState(RenderState.ShadeMode, ShadeMode.Flat); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("SetRenderState " + t.Milliseconds); t1 = DateTime.Now; #endif //Vertex[] vertices = new Vertex[scene.points.Count]; //List<int>[] vertexTriangle = new List<int>[scene.points.Count]; //Parallel.For(0, vertexTriangle.Length, index => vertexTriangle[index] = new List<int>()); ///*for(int i = 0; i < vertexTriangle.Length; ++i) //{ // vertexTriangle[i] = new List<int>(); //}*/ //int[] indices = new int[3 * scene.triangles.Count]; //int[] selIndices = new int[3 * scene.triangles.Count]; //uint numIndices = 0; //uint numSelIndices = 0; bool[] selPoints = new bool[scene.points.Count]; Parallel.For(0, selPoints.Length, index => selPoints[index] = false); /*for(int i = 0; i < selPoints.Length; ++i) { selPoints[i] = false; }*/ bool[] selected = scene.GetSelectedTriangles(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("t1 " + t.Milliseconds); t1 = DateTime.Now; #endif for (int i = 0; i < scene.triangles.Count; ++i) { Triangle triangle = scene.triangles[i]; //if (selected[i] == false) //{ // scene.indices[numIndices] = (int)triangle.p1; // scene.indices[numIndices + 1] = (int)triangle.p3; // scene.indices[numIndices + 2] = (int)triangle.p2; // numIndices += 3; //} //else if (selected[i]) { //scene.selIndices[numSelIndices] = (int)triangle.p1; //scene.selIndices[numSelIndices + 1] = (int)triangle.p3; //scene.selIndices[numSelIndices + 2] = (int)triangle.p2; selPoints[triangle.p1] = true; selPoints[triangle.p2] = true; selPoints[triangle.p3] = true; //numSelIndices += 3; } //scene.vertexTriangle[triangle.p1].Add(i); //scene.vertexTriangle[triangle.p2].Add(i); //scene.vertexTriangle[triangle.p3].Add(i); } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("t2 " + t.Milliseconds); t1 = DateTime.Now; #endif for(int i = 0; i < scene.points.Count; ++i) { if(selPoints[i] || scene.normals.Count <= i) { Vector3D normal = new Vector3D(); foreach(int face in scene.vertexTriangle[i]) { normal += Utilities.CalculateNormal(scene.points[(int)scene.triangles[face].p3], scene.points[(int)scene.triangles[face].p2], scene.points[(int)scene.triangles[face].p1]); } normal.Normalize(); if(scene.normals.Count <= i) { scene.normals.Add(normal); } else { scene.normals[i] = normal; } } } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Liczenie normalnych " + t.Milliseconds); t1 = DateTime.Now; #endif Dictionary<string, int> matNames = new Dictionary<string,int>(); for(int i = 0; i < scene.materials.Count; ++i) { matNames.Add(scene.materials[i].name, i); } int[] trPart = new int[scene.triangles.Count]; for(int i = 0; i < scene.parts.Count; ++i) { for(int j = 0; j < scene.parts[i].triangles.Count; ++j) { trPart[scene.parts[i].triangles[j]] = i; } } for (int i = 0; i < scene.vertices.Length; ++i) { int partIndex = trPart[scene.vertexTriangle[i][0]]; String matName = scene.materialAssign[partIndex]; Material_ material = scene.materials[matNames[matName]]; Vector3D point = scene.points[i]; scene.vertices[i].Position = new Vector3(point.x, point.y, point.z); scene.vertices[i].Normal = new Vector3(scene.normals[i].x, scene.normals[i].y, scene.normals[i].z); scene.vertices[i].Color = Color.FromArgb((int)(255 * material.colorR), (int)(255 * material.colorG), (int)(255 * material.colorB)).ToArgb(); scene.vertices[i].tex0 = 0; scene.vertices[i].tex1 = 0; } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Znajdowanie części i przyp. wierz. " + t.Milliseconds); t1 = DateTime.Now; #endif Mesh mesh = scene.numIndices >= 3 ? new Mesh(device, (int)scene.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 MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Tworzenie mesh1 " + t.Milliseconds); t1 = DateTime.Now; #endif if (mesh != null) { vb.Lock(0, 0, LockFlags.None).WriteRange(scene.vertices); vb.Unlock(); ib.Lock(0, 0, LockFlags.None).WriteRange(scene.indices, 0, (int)scene.numIndices); ib.Unlock(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Kopiowanie buforów mesh1 " + t.Milliseconds); #endif } #if MEASURE_TIMES t1 = DateTime.Now; #endif Mesh selMesh = scene.numSelIndices >= 3 ? new Mesh(device, (int)scene.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 MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Tworzenie mesh2 " + t.Milliseconds); t1 = DateTime.Now; #endif if (selMesh != null) { selvb.Lock(0, 0, LockFlags.None).WriteRange(scene.vertices); selvb.Unlock(); selib.Lock(0, 0, LockFlags.None).WriteRange(scene.selIndices, 0, (int)scene.numSelIndices); selib.Unlock(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Kopiowanie buforów mesh2 " + t.Milliseconds); #endif } #if MEASURE_TIMES t1 = DateTime.Now; #endif device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); if (perspective.X != viewportInfo.posX[0] || perspective.Y != viewportInfo.posY[0] || perspective.Width != viewportInfo.sizeX[0] || perspective.Height != viewportInfo.sizeY[0]) { perspective = new Viewport(); perspective.X = viewportInfo.posX[0]; perspective.Y = viewportInfo.posY[0]; perspective.Width = viewportInfo.sizeX[0]; perspective.Height = viewportInfo.sizeY[0]; perspective.MinZ = 0; perspective.MaxZ = 1; } if (top.X != viewportInfo.posX[1] || top.Y != viewportInfo.posY[1] || top.Width != viewportInfo.sizeX[1] || top.Height != viewportInfo.sizeY[1]) { top = new Viewport(); top.X = viewportInfo.posX[1]; top.Y = viewportInfo.posY[1]; top.Width = viewportInfo.sizeX[1]; top.Height = viewportInfo.sizeY[1]; top.MinZ = 0; top.MaxZ = 1; } if (front.X != viewportInfo.posX[2] || front.Y != viewportInfo.posY[2] || front.Width != viewportInfo.sizeX[2] || front.Height != viewportInfo.sizeY[2]) { front = new Viewport(); front.X = viewportInfo.posX[2]; front.Y = viewportInfo.posY[2]; front.Width = viewportInfo.sizeX[2]; front.Height = viewportInfo.sizeY[2]; front.MinZ = 0; front.MaxZ = 1; } if (side.X != viewportInfo.posX[3] || side.Y != viewportInfo.posY[3] || side.Width != viewportInfo.sizeX[3] || side.Height != viewportInfo.sizeY[3]) { side = new Viewport(); side.X = viewportInfo.posX[3]; side.Y = viewportInfo.posY[3]; side.Width = viewportInfo.sizeX[3]; side.Height = viewportInfo.sizeY[3]; side.MinZ = 0; side.MaxZ = 1; } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Modyfikacja viewport'ów " + t.Milliseconds); t1 = DateTime.Now; #endif if (perspective.Width > 0 && perspective.Height > 0) { device.Viewport = perspective; float camRotAngle = scene.cams.ElementAt(scene.activeCamera).rotateAngle; float aspect = (float)perspective.Width / perspective.Height; float angle = 2.0f * (float)Math.Atan(Math.Tan(Utilities.DegToRad(scene.cams.ElementAt(scene.activeCamera).fovAngle) / 2.0f) / aspect); device.SetTransform(TransformState.View, Matrix.LookAtRH( scene.cams[scene.activeCamera].position, scene.cams[scene.activeCamera].lookAt, Utilities.RotatePointAroundVector(new Vector3(0, 1, 0), Vector3.Normalize(scene.cams[scene.activeCamera].lookAt - scene.cams[scene.activeCamera].position), camRotAngle))); device.SetTransform(TransformState.Projection, Matrix.PerspectiveFovRH( angle, aspect, 0.01f, 20000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.EndScene(); } if (top.Width > 0 && top.Height > 0) { device.Viewport = top; device.SetTransform(TransformState.View, Matrix.LookAtRH( orthoPos[0], orthoLookAt[0], new Vector3(0, 1, 0))); device.SetTransform(TransformState.Projection, Matrix.OrthoRH( orthoWidht[0], (float)(top.Height) / top.Width * orthoWidht[0], 0.01f, 20000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.EndScene(); } if (front.Width > 0 && front.Height > 0) { device.Viewport = front; device.SetTransform(TransformState.View, Matrix.LookAtRH( orthoPos[1], orthoLookAt[1], new Vector3(0, 1, 0))); device.SetTransform(TransformState.Projection, Matrix.OrthoRH( orthoWidht[1], (float)(front.Height) / front.Width * orthoWidht[1], 0.01f, 20000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.EndScene(); } if (side.Width > 0 && side.Height > 0) { device.Viewport = side; device.SetTransform(TransformState.View, Matrix.LookAtRH( orthoPos[2], orthoLookAt[2], new Vector3(0, 1, 0))); device.SetTransform(TransformState.Projection, Matrix.OrthoRH( orthoWidht[2], (float)(side.Height) / side.Width * orthoWidht[2], 0.01f, 20000)); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(230, 230, 230), 1.0f, 0); device.BeginScene(); if (mesh != null) { mesh.DrawSubset(0); } //device.SetTexture(0, selectionTex); device.SetRenderState(RenderState.Lighting, false); if (selMesh != null) { selMesh.DrawSubset(0); } device.SetRenderState(RenderState.Lighting, true); //device.SetTexture(0, null); device.EndScene(); } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Renderowanie " + t.Milliseconds); t1 = DateTime.Now; #endif device.Present(); #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Present " + t.Milliseconds); t1 = DateTime.Now; #endif //selectionTex.Dispose(); if (selMesh != null) { selvb.Dispose(); selib.Dispose(); selMesh.Dispose(); } if (mesh != null) { vb.Dispose(); ib.Dispose(); mesh.Dispose(); } #if MEASURE_TIMES t2 = DateTime.Now; t = t2 - t1; w.WriteLine("Czyszczenie " + t.Milliseconds); DateTime te = DateTime.Now; t = te - ts; w.WriteLine("Całość " + t.Milliseconds); w.WriteLine("-----------------------------------------------------------------"); w.WriteLine(); w.WriteLine(); w.WriteLine(); #endif }
ViewportInfo GetViewCoords() { ViewportInfo viewInfo = new ViewportInfo(); viewInfo.resX = Views.Size.Width; viewInfo.resY = Views.Size.Height; int panelX = 0; int panelY = 0; viewInfo.posX = new int[4]; viewInfo.posY = new int[4]; viewInfo.sizeX = new int[4]; viewInfo.sizeY = new int[4]; if (maxViewport == -1) { int elemWidth = (int)(Views.Size.Width / 2) - 1; int elemHeight = (int)(Views.Size.Height / 2) - 1; viewInfo.posX[0] = panelX; viewInfo.posY[0] = panelY; viewInfo.sizeX[0] = elemWidth; viewInfo.sizeY[0] = elemHeight; viewInfo.posX[1] = panelX + elemWidth + 2; viewInfo.posY[1] = panelY; viewInfo.sizeX[1] = viewInfo.resX - viewInfo.posX[1]; viewInfo.sizeY[1] = elemHeight; viewInfo.posX[2] = panelX; viewInfo.posY[2] = panelY + elemHeight + 2; viewInfo.sizeX[2] = elemWidth; viewInfo.sizeY[2] = viewInfo.resY - viewInfo.posY[2]; viewInfo.posX[3] = panelX + elemWidth + 2; viewInfo.posY[3] = panelY + elemHeight + 2; viewInfo.sizeX[3] = viewInfo.resX - viewInfo.posX[3]; viewInfo.sizeY[3] = viewInfo.resY - viewInfo.posY[3]; } else { for (int i = 0; i < 4; ++i) { viewInfo.posX[i] = viewInfo.posY[i] = viewInfo.sizeX[i] = viewInfo.sizeY[i] = 0; } viewInfo.sizeX[maxViewport] = Views.Size.Width; viewInfo.sizeY[maxViewport] = Views.Size.Height; } return viewInfo; }