Пример #1
0
        static void Main()
        {
            var renderer = new Renderer();
            var game     = new Game(renderer);

            var vertShaderString = File.ReadAllText("Assets/Shaders/basic.vert");
            var fragShaderString = File.ReadAllText("Assets/Shaders/basic.frag");

            RawModel model;

            using (var reader = new StreamReader("Assets/Models/dragon.obj"))
            {
                model = new RawModel(ObjParser.LoadModel(reader));
            }

            var textureData = new ImageSharpTexture("Assets/Textures/white.png");
            var shaderSet   = new ShaderSet(vertShaderString, fragShaderString);

            renderer.Initialize(true);

            var mesh   = renderer.CreateMesh(model, textureData, shaderSet);
            var entity = new RenderableEntity(new Transform(new Vector3(0, -5, -10), new Vector3(), 1), mesh);

            entity.InitializeMesh(renderer);

            game.AddEntity(entity);
            game.AddEntity(new CameraController());

            game.RunMainLoop();
            renderer.DisposeGraphicsDevices();
        }
        public override bool Run(FeatureContext context)
        {
            TopoShape        box    = GlobalInstance.BrepTools.MakeBox(Vector3.ZERO, Vector3.UNIT_Z, new Vector3(10, 10, 10));
            RenderableEntity entity = GlobalInstance.TopoShapeConvert.ToEntity(box, 0);

            for (int ii = 0; ii < 10; ++ii)
            {
                EntitySceneNode node = new EntitySceneNode();
                node.SetEntity(entity);
                Matrix4 trf = GlobalInstance.MatrixBuilder.MakeTranslate(new Vector3(11 * ii, 0, 0));
                node.SetTransform(trf);

                context.ShowSceneNode(node);
            }
            context.RequestDraw();

            SceneNodeIterator itr = context.RenderView.SceneManager.NewSceneNodeIterator();
            String            msg = "Node Ids: ";

            while (itr.More())
            {
                SceneNode node = itr.Next();
                msg += String.Format(" {0}", node.GetId().AsInt());
            }

            MessageBox.Show(msg);

            return(true);
        }
Пример #3
0
        public static float3 SurfaceNormal(RenderableEntity entity, float3 hitPoint)
        {
            switch (entity.Type)
            {
            case SPHERE:
                return(SphereSurfaceNormal(entity, hitPoint));

            case PLANE:
                return(PlaneSurfaceNormal(entity, hitPoint));

            default:
                return(new float3(0, 0, 0));
            }
        }
Пример #4
0
        public static RayIntersection IntersectWithRay(RenderableEntity entity, Ray ray)
        {
            switch (entity.Type)
            {
            case SPHERE:
                return(IntersectSphereWithRay(entity, ray));

            case PLANE:
                return(IntersectPlaneWithRay(entity, ray));

            default:
                return(new RayIntersection());
            }
        }
Пример #5
0
        private void customToolStripMenuItem_Click(object sender, EventArgs e)
        {
            float[]          vb     = { 0, 0, 0, 100, 0, 0, 100, 100, 0 };
            uint[]           ib     = { 0, 1, 2 };
            float[]          cb     = { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 };
            float[]          nb     = {};
            RenderableEntity entity = GlobalInstance.TopoShapeConvert.CreateColoredFaceEntity(vb, ib, nb, cb, new AABox(Vector3.ZERO, new Vector3(100, 100, 1)));

            EntitySceneNode node = new EntitySceneNode();

            node.SetEntity(entity);

            renderView.SceneManager.AddNode(node);
            renderView.RequestDraw();
        }
Пример #6
0
        private static RayIntersection IntersectSphereWithRay(RenderableEntity entity, Ray ray)
        {
            // Create a line segment between the ray origin and the center of the sphere
            var line = entity.Position - ray.Origin;

            // Use line as a hypotenuse and find the length of the adjacent side
            var adjacent = Hlsl.Dot(line, ray.Direction);

            // Find the length-squared of the opposite side
            var length2 = Hlsl.Dot(line, line) - (adjacent * adjacent);

            // Determine the radius squared
            var radius2 = entity.Radius * entity.Radius;

            // If that length-squared is greater than radius squared, the ray doesn't interact the sphere
            if (length2 > radius2)
            {
                return(new RayIntersection());
            }

            var thc = MathF.Sqrt(radius2 - length2);
            var t0  = adjacent - thc;
            var t1  = adjacent + thc;

            if (t0 < 0.0f && t1 < 0.0f)
            {
                return(new RayIntersection());
            }

            // Determine the intersect distance
            var distance = t0 < t1 ? t0 : t1;

#pragma warning disable IDE0017 // Simplify object initialization
            var rayIntersection = new RayIntersection();
            rayIntersection.Intersecting = true;
            rayIntersection.Distance     = distance;
#pragma warning restore IDE0017 // Simplify object initialization

            return(rayIntersection);
        }
Пример #7
0
        private static RayIntersection IntersectPlaneWithRay(RenderableEntity entity, Ray ray)
        {
            var denom = Hlsl.Dot(entity.Normal, ray.Direction);

            if (denom > 0.000001f)
            {
                var v        = entity.Position - ray.Origin;
                var distance = Hlsl.Dot(v, entity.Normal) / denom;
                if (distance >= 0.0)
                {
#pragma warning disable IDE0017 // Simplify object initialization
                    var rayIntersection = new RayIntersection();
                    rayIntersection.Intersecting = true;
                    rayIntersection.Distance     = distance;
#pragma warning restore IDE0017 // Simplify object initialization

                    return(rayIntersection);
                }
            }

            return(new RayIntersection());
        }
Пример #8
0
        private void faceTriangulateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopoShape circle = GlobalInstance.BrepTools.MakeCircle(Vector3.ZERO, 100, Vector3.UNIT_Z);
            TopoShape face   = GlobalInstance.BrepTools.MakeFace(circle);

            if (face.GetShapeType() == (int)EnShapeType.Topo_FACE)
            {
                MessageBox.Show("This is a face!");
            }

            FaceTriangulation ft = new FaceTriangulation();

            ft.SetTolerance(5);
            ft.Perform(face);
            float[] points     = ft.GetVertexBuffer();
            int     pointCount = points.Length / 3;

            uint[] indexBuffer = ft.GetIndexBuffer();
            int    faceCount   = indexBuffer.Length / 3;

            float[] normals = ft.GetNormalBuffer();

            MessageBox.Show(String.Format("Point Count: {0}\n Face Count: {1}", pointCount, faceCount));

            float[] colorBuffer = new float[pointCount * 4];

            Random num = new Random();

            for (int ii = 0; ii < pointCount; ++ii)
            {
                int idx = ii * 4;
                colorBuffer[idx]     = num.Next(0, 256) / 256.0f;
                colorBuffer[idx + 1] = num.Next(0, 256) / 256.0f;
                colorBuffer[idx + 2] = num.Next(0, 256) / 256.0f;
                colorBuffer[idx + 3] = 1;
            }

            RenderableEntity entity = GlobalInstance.TopoShapeConvert.CreateColoredFaceEntity(points, indexBuffer, normals, colorBuffer, face.GetBBox());

            EntitySceneNode node = new EntitySceneNode();

            node.SetEntity(entity);

            renderView.SceneManager.AddNode(node);
            renderView.RequestDraw();
            //////////////////////////////////////////////////////////////////////////
            // Code to get the mesh

            /*
             * for (int ii = 0; ii < faceCount; ++ii)
             * {
             *  int p0 = (int)indexBuffer[ii * 3];
             *  int p1 = (int)indexBuffer[ii * 3 + 1];
             *  int p2 = (int)indexBuffer[ii * 3 + 2];
             *
             *  Vector3 pt0 = new Vector3(points[p0 * 3], points[p0 * 3 + 1], points[p0 * 3 + 2]);
             *  Vector3 pt1 = new Vector3(points[p1 * 3], points[p1 * 3 + 1], points[p1 * 3 + 2]);
             *  Vector3 pt2 = new Vector3(points[p2 * 3], points[p2 * 3 + 1], points[p2 * 3 + 2]);
             *
             *  // ....
             *  // use the same way to get the normal data for each point.
             * }
             * */
            //////////////////////////////////////////////////////////////////////////
        }
Пример #9
0
 private static float3 PlaneSurfaceNormal(RenderableEntity entity, float3 hitPoint)
 {
     return(-Hlsl.Normalize(entity.Normal));
 }
Пример #10
0
 private static float3 SphereSurfaceNormal(RenderableEntity entity, float3 hitPoint)
 {
     return(Hlsl.Normalize(hitPoint - entity.Position));
 }
        public override bool Run(FeatureContext context)
        {
            TopoShape circle = GlobalInstance.BrepTools.MakeCircle(Vector3.ZERO, 100, Vector3.UNIT_Z);
            TopoShape face   = GlobalInstance.BrepTools.MakeFace(circle);

            FaceTriangulation ft = new FaceTriangulation();

            ft.SetTolerance(5);
            ft.Perform(face);
            float[] points     = ft.GetVertexBuffer();
            int     pointCount = points.Length / 3;

            uint[] indexBuffer = ft.GetIndexBuffer();
            int    faceCount   = indexBuffer.Length / 3;

            float[] normals = ft.GetNormalBuffer();


            float[] colorBuffer = new float[pointCount * 4];

            Random num = new Random();

            for (int ii = 0; ii < pointCount; ++ii)
            {
                int idx = ii * 4;
                colorBuffer[idx]     = num.Next(0, 256) / 256.0f;
                colorBuffer[idx + 1] = num.Next(0, 256) / 256.0f;
                colorBuffer[idx + 2] = num.Next(0, 256) / 256.0f;
                colorBuffer[idx + 3] = 1;
            }

            RenderableEntity entity = GlobalInstance.TopoShapeConvert.CreateColoredFaceEntity(points, indexBuffer, normals, colorBuffer, face.GetBBox());

            EntitySceneNode node = new EntitySceneNode();

            node.SetEntity(entity);

            context.ShowSceneNode(node);

            //////////////////////////////////////////////////////////////////////////
            // Code to get the mesh

            /*
             * for (int ii = 0; ii < faceCount; ++ii)
             * {
             *  int p0 = (int)indexBuffer[ii * 3];
             *  int p1 = (int)indexBuffer[ii * 3 + 1];
             *  int p2 = (int)indexBuffer[ii * 3 + 2];
             *
             *  Vector3 pt0 = new Vector3(points[p0 * 3], points[p0 * 3 + 1], points[p0 * 3 + 2]);
             *  Vector3 pt1 = new Vector3(points[p1 * 3], points[p1 * 3 + 1], points[p1 * 3 + 2]);
             *  Vector3 pt2 = new Vector3(points[p2 * 3], points[p2 * 3 + 1], points[p2 * 3 + 2]);
             *
             *  // ....
             *  // use the same way to get the normal data for each point.
             * }
             * */


            return(true);
        }