Ejemplo n.º 1
0
        private void AddGeometryToSceneNode(Geometry geometry, float3 position)
        {
            Geometry newGeo = geometry.CloneGeometry();

            newGeo.Triangulate();
            JometriMesh geometryMesh = new JometriMesh(newGeo);

            SceneNode sceneNodeContainer = new SceneNode {
                Components = new List <SceneComponent>()
            };

            Mesh meshComponent = new Mesh
            {
                Vertices  = geometryMesh.Vertices,
                Triangles = geometryMesh.Triangles,
                Normals   = geometryMesh.Normals,
            };
            Transform translationComponent = new Transform
            {
                Rotation    = float3.Zero,
                Scale       = new float3(1, 1, 1),
                Translation = position
            };

            sceneNodeContainer.Components.Add(translationComponent);
            sceneNodeContainer.Components.Add(MakeEffect.FromDiffuseSpecular(_defaultColor, float4.Zero));
            sceneNodeContainer.Components.Add(meshComponent);

            _parentNode.Children.Add(sceneNodeContainer);
            _activeGeometrys.Add(_parentNode.Children.IndexOf(sceneNodeContainer), geometry);
        }
Ejemplo n.º 2
0
        private void AddGeometryToSceneNode(Geometry geometry, float3 position)
        {
            Geometry newGeo = geometry.CloneGeometry();

            newGeo.Triangulate();
            var geometryMesh = new JometriMesh(newGeo);

            var sceneNodeContainer = new SceneNode {
                Components = new List <SceneComponent>()
            };

            var meshComponent = new Mesh
            {
                Vertices  = geometryMesh.Vertices,
                Triangles = geometryMesh.Triangles,
                Normals   = geometryMesh.Normals,
            };
            var translationComponent = new Transform
            {
                Rotation    = float3.Zero,
                Scale       = new float3(1, 1, 1),
                Translation = position
            };
            var shaderEffect = ShaderCodeBuilder.Default;

            shaderEffect.SetEffectParam(UniformNameDeclarations.AlbedoColor, _defaultColor);
            sceneNodeContainer.Components.Add(translationComponent);
            sceneNodeContainer.Components.Add(shaderEffect);
            sceneNodeContainer.Components.Add(meshComponent);

            _parentNode.Children.Add(sceneNodeContainer);
            _activeGeometrys.Add(_parentNode.Children.IndexOf(sceneNodeContainer), geometry);
        }
Ejemplo n.º 3
0
        private static void CreateTopSurface(Geometry geometry, float zOffset, bool exturdeAlongNormal)
        {
            //Clone front face.
            var backface = geometry.CloneGeometry();

            if (!exturdeAlongNormal)
            {
                //Add zOffset to each vertex coordinate.
                UpdateVertexZCoord(backface, zOffset);
            }
            else
            {
                var unbounded = backface.GetFaceVertices(1).ToList();
                var normal    = GeometricOperations.CalculateFaceNormal(unbounded);
                UpdateVertexZCoord(backface, unbounded, normal, zOffset);
            }

            Join2DGeometries(geometry, backface);
        }
Ejemplo n.º 4
0
        private void AddGeometryToSceneNode(Geometry geometry, float3 position)
        {
            Geometry newGeo = geometry.CloneGeometry();

            newGeo.Triangulate();
            var geometryMesh = new JometriMesh(newGeo);

            var sceneNodeContainer = new SceneNodeContainer {
                Components = new List <SceneComponentContainer>()
            };

            var meshComponent = new Mesh
            {
                Vertices  = geometryMesh.Vertices,
                Triangles = geometryMesh.Triangles,
                Normals   = geometryMesh.Normals,
            };
            var translationComponent = new TransformComponent
            {
                Rotation    = float3.Zero,
                Scale       = new float3(1, 1, 1),
                Translation = position
            };
            var materialComponent = new MaterialComponent
            {
                Diffuse  = new MatChannelContainer(),
                Specular = new SpecularChannelContainer(),
            };

            materialComponent.Diffuse.Color = _defaultColor;
            sceneNodeContainer.Components.Add(translationComponent);
            sceneNodeContainer.Components.Add(materialComponent);
            sceneNodeContainer.Components.Add(meshComponent);

            _parentNode.Children.Add(sceneNodeContainer);
            _activeGeometrys.Add(_parentNode.Children.IndexOf(sceneNodeContainer), geometry);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Performs a Catmull-Clark Subdivision-Surface algorithm on a given geometry which is stored as DCEL.
        /// </summary>
        /// <param name="geometry">The geometry to perform the SD on.</param>
        /// <returns>A smoother geometry with </returns>
        public static Geometry CatmullClarkSubdivision(Geometry geometry)
        {
            //initializing
            var newGeometry = geometry.CloneGeometry();

            //computes all Face Vertices and all Edge Vertices
            var allFaceVertices = GetFaceVertices(geometry);
            var allEdgeVertices = GetEdgeVertices(geometry, allFaceVertices);

            //Calculates the new position of existing Vertices
            var allVertices = newGeometry.GetAllVertices().ToList();

            foreach (var vertex in allVertices)
            {
                var outgoingEdges = newGeometry.GetVertexStartingHalfEdges(vertex.Handle).ToList();

                //Get average of all face Points and average of all edge Points
                var faceVertices = new List <Vertex>();
                var edgeVertices = new List <Vertex>();
                foreach (var edge in outgoingEdges)
                {
                    var twin = geometry.GetHalfEdgeByHandle(edge.TwinHalfEdge);

                    if (allFaceVertices.ContainsKey(edge.IncidentFace))
                    {
                        faceVertices.Add(allFaceVertices[edge.IncidentFace]);
                    }
                    else
                    {
                        faceVertices.Add(allFaceVertices[twin.IncidentFace]);
                    }

                    if (allEdgeVertices.ContainsKey(edge.Handle))
                    {
                        edgeVertices.Add(allEdgeVertices[edge.Handle]);
                    }
                    else
                    {
                        edgeVertices.Add(allEdgeVertices[twin.Handle]);
                    }
                }

                var meanEdgeVertexPos = GeometricOperations.GetVerticesMeanPos(edgeVertices);
                var meanFaceVertexPos = GeometricOperations.GetVerticesMeanPos(faceVertices);

                float edgeCount = outgoingEdges.Count;

                var newVertexPos = (meanFaceVertexPos + 2 * meanEdgeVertexPos + (edgeCount - 3) * vertex.VertData.Pos) / edgeCount;
                var newVertex    = new Vertex(vertex.Handle, newVertexPos)
                {
                    IncidentHalfEdge = vertex.IncidentHalfEdge
                };

                newGeometry.ReplaceVertex(newVertex);
            }

            //adds newly calculated Edge Vertices
            var allEdges = geometry.GetAllHalfEdges();
            var doneHe   = new int[allEdges.Count() + 1];

            foreach (var edge in allEdges)
            {
                if (doneHe[edge.Handle] == edge.TwinHalfEdge)
                {
                    continue;
                }
                var vertexOld1 = edge.OriginVertex;

                var twinEdge = geometry.GetHalfEdgeByHandle(edge.TwinHalfEdge);

                var vertexOld2 = twinEdge.OriginVertex;

                //find correct Edge Vertex
                Vertex edgeVertex;
                if (allEdgeVertices.ContainsKey(edge.Handle))
                {
                    edgeVertex = allEdgeVertices[edge.Handle];
                }
                else
                {
                    edgeVertex = allEdgeVertices[twinEdge.Handle];
                }

                newGeometry.InsertVertex(vertexOld1, vertexOld2, edgeVertex.VertData.Pos);
                doneHe[edge.TwinHalfEdge] = edge.Handle;
            }

            newGeometry.SetHighestHandles();
            geometry = newGeometry.CloneGeometry();

            //creates the new quad faces and connects everything
            AddFaceVerticesAndNewFaces(geometry, newGeometry, allFaceVertices);

            return(newGeometry);
        }
Ejemplo n.º 6
0
        private void InteractionHandler()
        {
            //Add new Geometry
            if (Keyboard.GetKey(KeyCodes.D1) && _keyTimeout < 0)
            {
                _keyTimeout = 1;
                Geometry geometry = CreatePrimitiveGeometry.CreateCuboidGeometry(1, 1, 1);
                AddGeometryToSceneNode(geometry, new float3(0, 0, 0));
            }
            if (Keyboard.GetKey(KeyCodes.D2) && _keyTimeout < 0)
            {
                _keyTimeout = 1;
                Geometry geometry = CreatePrimitiveGeometry.CreatePyramidGeometry(1, 1, 1);
                AddGeometryToSceneNode(geometry, new float3(0, 0, 0));
            }
            if (Keyboard.GetKey(KeyCodes.D3) && _keyTimeout < 0)
            {
                _keyTimeout = 1;
                Geometry geometry = CreatePrimitiveGeometry.CreateConeGeometry(1, 1, 15);
                AddGeometryToSceneNode(geometry, new float3(0, 0, 0));
            }
            if (Keyboard.GetKey(KeyCodes.D4) && _keyTimeout < 0)
            {
                _keyTimeout = 1;
                Geometry geometry = CreatePrimitiveGeometry.CreateSpehreGeometry(1, 30, 15);
                AddGeometryToSceneNode(geometry, new float3(0, 0, 0));
            }
            _keyTimeout -= DeltaTime;

            //following actions are only allowed if something is selected
            if (_selectedNode == null)
            {
                return;
            }

            //Translate Geometry
            if (Keyboard.GetKey(KeyCodes.G))
            {
                _isTranslating = true;
            }
            if (_isTranslating)
            {
                float3 worldPos = new float3(Mouse.Velocity.x * .0001f, Mouse.Velocity.y * -.0001f, Mouse.WheelVel * .001f);
                _selectedNode.GetTransform().Translation += worldPos.xyz;

                if (Mouse.LeftButton)
                {
                    _isTranslating = false;
                }
            }

            //Scaling Geometry
            if (Keyboard.GetKey(KeyCodes.S))
            {
                _isScaling = true;
            }
            if (_isScaling)
            {
                _selectedNode.GetTransform().Scale += new float3(Mouse.Velocity.y, Mouse.Velocity.y, Mouse.Velocity.y) * .0001f;
                if (Mouse.LeftButton)
                {
                    _isScaling = false;
                }
            }

            //DeleteGeom
            if (Keyboard.GetKey(KeyCodes.Delete) && _keyTimeout < 0)
            {
                _keyTimeout = 1;
                int currentGeometryIndex = _parentNode.Children.IndexOf(_selectedNode);
                _activeGeometrys.Remove(currentGeometryIndex);

                Dictionary <int, Geometry> zwerg = new Dictionary <int, Geometry>();
                foreach (int key in _activeGeometrys.Keys)
                {
                    if (key > currentGeometryIndex)
                    {
                        Geometry test = _activeGeometrys[key];
                        zwerg.Add(key - 1, test);
                    }
                    else
                    {
                        zwerg.Add(key, _activeGeometrys[key]);
                    }
                }

                _activeGeometrys.Clear();
                foreach (KeyValuePair <int, Geometry> item in zwerg)
                {
                    _activeGeometrys.Add(item.Key, item.Value);
                }

                _parentNode.Children.RemoveAt(currentGeometryIndex);
                _selectedNode = null;
                _currentPick  = null;
            }

            //Insert
            if (Keyboard.GetKey(KeyCodes.I) && _keyTimeout < 0)
            {
                _keyTimeout = .25f;
                int       currentGeometryIndex    = _parentNode.Children.IndexOf(_selectedNode);
                SceneNode currentSelection        = _parentNode.Children[currentGeometryIndex];
                Geometry  currentSelectedGeometry = _activeGeometrys[currentGeometryIndex];

                currentSelectedGeometry.InsetFace(rng.Next(4, currentSelectedGeometry.GetAllFaces().Count()), .5f);
                Geometry copy = currentSelectedGeometry.CloneGeometry();
                _activeGeometrys[currentGeometryIndex] = copy;
                currentSelectedGeometry.Triangulate();

                JometriMesh geometryMesh  = new JometriMesh(currentSelectedGeometry);
                Mesh        meshComponent = new Mesh
                {
                    Vertices  = geometryMesh.Vertices,
                    Triangles = geometryMesh.Triangles,
                    Normals   = geometryMesh.Normals,
                };
                currentSelection.Components[2] = meshComponent;
            }

            //Extrude
            if (Keyboard.GetKey(KeyCodes.E) && _keyTimeout < 0)
            {
                _keyTimeout = .25f;
                int       currentGeometryIndex    = _parentNode.Children.IndexOf(_selectedNode);
                SceneNode currentSelection        = _parentNode.Children[currentGeometryIndex];
                Geometry  currentSelectedGeometry = _activeGeometrys[currentGeometryIndex];

                currentSelectedGeometry.ExtrudeFace(rng.Next(4, currentSelectedGeometry.GetAllFaces().Count()), 1);
                Geometry copy = currentSelectedGeometry.CloneGeometry();
                _activeGeometrys[currentGeometryIndex] = copy;
                currentSelectedGeometry.Triangulate();

                JometriMesh geometryMesh  = new JometriMesh(currentSelectedGeometry);
                Mesh        meshComponent = new Mesh
                {
                    Vertices  = geometryMesh.Vertices,
                    Triangles = geometryMesh.Triangles,
                    Normals   = geometryMesh.Normals,
                };
                currentSelection.Components[2] = meshComponent;
            }

            //Add Catmull-Clark
            if (Keyboard.GetKey(KeyCodes.C) && _keyTimeout < 0)
            {
                _keyTimeout = .25f;
                int       currentGeometryIndex    = _parentNode.Children.IndexOf(_selectedNode);
                SceneNode currentSelection        = _parentNode.Children[currentGeometryIndex];
                Geometry  currentSelectedGeometry = _activeGeometrys[currentGeometryIndex];

                currentSelectedGeometry = SubdivisionSurface.CatmullClarkSubdivision(currentSelectedGeometry);
                Geometry copy = currentSelectedGeometry.CloneGeometry();
                _activeGeometrys[currentGeometryIndex] = copy;
                currentSelectedGeometry.Triangulate();

                JometriMesh geometryMesh  = new JometriMesh(currentSelectedGeometry);
                Mesh        meshComponent = new Mesh
                {
                    Vertices  = geometryMesh.Vertices,
                    Triangles = geometryMesh.Triangles,
                    Normals   = geometryMesh.Normals,
                };
                currentSelection.Components[2] = meshComponent;
            }
        }