Пример #1
0
        public static Vector3D[] ComputeNormals(Vector3D[] input)
        {
            //http://stackoverflow.com/questions/1777206/3d-surface-normal-extractor
            Vector3D[] normals = new Vector3D[input.Length];
            List<Triangle> faces = new List<Triangle>();
            int count = input.Length / 3;
            for (int i = 0; i < count; i++)
            {
                faces.Add(new Triangle(input[(i*3)], input[(i*3) + 1], input[(i*3) + 2]));
            }
            // loop over all faces to calculate vertex normals
            for (int i = 0; i < faces.Count; i++)
            {
                Vector3D v1 = faces[i].vertex1;
                Vector3D v2 = faces[i].vertex2;
                Vector3D v3 = faces[i].vertex3;

                Vector3D edge1 = v2 - v1;
                Vector3D edge2 = v3 - v1;
                Vector3D normal = edge1.CrossProduct(edge2);  // or edge2.CrossProduct(edge1)
                normal = normal.Normalize();

                normals[i] += normal;
                normals[i+1] += normal;
                normals[i+2] += normal;
            }

            // vertex normals need to be normalised
            for (int i = 0; i < normals.Length; i++)
            {
                normals[i] = normals[i].Normalize();
            }
            return normals;
        }
Пример #2
0
 public static Vector3D ComputeFaceNormal(Vector3D p1, Vector3D p2, Vector3D p3)
 {
     Vector3D a = p3 - p2;
     Vector3D b = p1 - p2;
     //Compute cross product
     Vector3D pn = VectorGetNormal(a, b);
     return VectorNormalize(pn);
 }
Пример #3
0
 static Vector3D VectorGetNormal(Vector3D a, Vector3D b)
 {
     Vector3D output = new Vector3D();
     output.X = a.Y * b.Z - a.Z * b.Y;
     output.Y = a.Z * b.X - a.X * b.Z;
     output.Z = a.X * b.Y - a.Y * b.X;
     return output;
 }
Пример #4
0
 static Vector3D VectorNormalize(Vector3D input)
 {
     float len = (float)Math.Sqrt(sqr(input.X) + sqr(input.Y) + sqr(input.Z));
     Vector3D output = new Vector3D();
     output.X = input.X / len;
     output.Y = input.Y / len;
     output.Z = input.Z / len;
     return output;
 }
Пример #5
0
 //End TODO
 /// <summary>
 /// Checks if a given point is within a bounding box
 /// </summary>
 /// <param name="point">The point to test</param>
 /// <returns></returns>
 public bool PointWithin(Vector3D _point)
 {
     // Vector3D training = new Vector3D(0, 0, 10.9f);
     Vector3D point = _point;// + training;
     if (point.X > minX.X & point.Y > minY.Y & point.Z > minZ.Z & point.X < maxX.X & point.Y < maxY.Y & point.Z < maxX.Z)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
Пример #6
0
        //End TODO
        /// <summary>
        /// Checks if a given point is within a bounding box
        /// </summary>
        /// <param name="point">The point to test</param>
        /// <returns></returns>
        public bool PointWithin(Vector3D _point)
        {
            if (!OptimizationEnabled)
            {
                return true;
            }

               // Vector3D training = new Vector3D(0, 0, 10.9f);
            Vector3D point = _point;// + training;
            if (point.X >= minX.X && point.Y >= minY.Y && point.Z >= minZ.Z && point.X <= maxX.X && point.Y <= maxY.Y && point.Z <= maxZ.Z)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
Пример #7
0
 public Triangle(Vector3D v1, Vector3D v2, Vector3D v3)
 {
     vertex1 = v1;
     vertex2 = v2;
     vertex3 = v3;
 }
Пример #8
0
 public DXVertBuffer(DirectEngine mgine,Vector3D[] vertices,Vector3D[] normals, Vector2D[] texcoords)
 {
     engine = mgine;
     verts = vertices;
     norms = normals;
     texas = texcoords;
 }
Пример #9
0
 /// <summary>
 ///Creates a generic vertex buffer 
 /// </summary>
 /// <returns>
 /// A <see cref="VertexBuffer"/>
 /// </returns>
 public abstract VertexBuffer CreateVertexBuffer(Vector3D[] vertices, Vector2D[] texcoords, Vector3D[] normals);
Пример #10
0
 public abstract void SetRenderTarget(Texture2D texture, Vector3D campos, Vector3D camrot);
Пример #11
0
 /// <summary>
 /// Creates a quad
 /// </summary>
 /// <param name="vertices">Input vertices specific to this quad</param>
 /// <param name="texcoords">Input texcoords specific to this quad</param>
 /// <param name="normals">Input normals specific to this quad</param>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <param name="z"></param>
 /// <param name="cx"></param>
 /// <param name="cy"></param>
 /// <param name="cz"></param>
 /// <param name="inputnormals"></param>
 /// <param name="inputtexcoords"></param>
 public static void CreateQuad(out Vector3D[] vertices, out Vector2D[] texcoords,out Vector3D[] normals, float x, float y, float z, float cx, float cy, float cz, Vector3D[] inputnormals, Vector2D[] inputtexcoords)
 {
     Vector3D[] verts;
     Vector2D[] faketexcoords;
     Vector3D[] norms;
     plotQuad(out verts, out faketexcoords, x, y, z, cx - x, cy - y, cz - z);
     vertices = verts;
     normals = inputnormals;
     //TODO: Calculate the NEW texcoords through subtraction
     List<Vector2D> texc = new List<Vector2D>();
     for(int i = 0;i<vertices.Length;i++)
     {
         texc.Add(new Vector2D(faketexcoords[i].X - inputtexcoords[i].X, faketexcoords[i].Y - inputtexcoords[i].Y));
     }
     texcoords = texc.ToArray();
 }
Пример #12
0
        public static void plotRectangle(out Vector3D[] vertices,out Vector2D[] texcoords, float x, float y, float z, float width, float height)
        {
            List<Vector3D> points = new List<Vector3D>();
            //First triangle
            //0,0,0
            points.Add(new Vector3D(x,y,z));
            //0,1,0
            points.Add(new Vector3D(x,y+height,z));
            //1,1,0
            points.Add(new Vector3D(x+width,y+height,z));
            //Second triangle
            //1,1,0
            points.Add(new Vector3D(x+width,y+height,z));
            //1,0,0
            points.Add(new Vector3D(x+width,y,z));
            //0,0,0
            points.Add(new Vector3D(x,y,z));

            //Create texcoords
            List<Vector2D> t = new List<Vector2D>();
            //0,0
            t.Add(new Vector2D(0,0));
            //0,1
            t.Add(new Vector2D(0,.5f));
            //1,1
            t.Add(new Vector2D(.5f,.5f));

            //1,1
            t.Add(new Vector2D(1,1));
            //1,0
            t.Add(new Vector2D(1,.5f));
            //0,0
            t.Add(new Vector2D(.5f,.5f));
            vertices = points.ToArray();
            texcoords = t.ToArray();
        }
Пример #13
0
 public override _3DAPI.VertexBuffer CreateVertexBuffer(Vector3D[] vertices, Vector2D[] texcoords, Vector3D[] normals)
 {
     return new WPVertexBuffer(this, vertices, texcoords, normals);
 }
Пример #14
0
 public override void SetRenderTarget(Texture2D texture, Vector3D cpos, Vector3D crot)
 {
     spos = cpos;
     srot = crot;
     rendertexture = texture as GLTexture;
     //TODO: Use a RenderTargetOperation instead of this method
     //SetRenderTargetOperation mop = new SetRenderTargetOperation(this,texture as GLTexture,cpos,crot);
 }
Пример #15
0
        public RenderTargetChange(DXTexture target, DirectEngine engine, Vector3D _campos, Vector3D _camrot)
        {
            _engine = engine;
            interntarget = target;
            if (mchange != null)
            {
                hasran = true;
                previous = mchange.previous;
            }
            mchange = this;

            prevpos = _engine.cameraPosition;
            prevrot = _engine.worldRotation;
            campos = _campos;
            camrot = _camrot;
        }
Пример #16
0
 public override void SetRenderTarget(_3DAPI.Texture2D texture, _3DAPI.Vector3D pos, _3DAPI.Vector3D rot)
 {
     tex = texture as WPTexture;
     position = pos;
     rotation = rot;
 }
Пример #17
0
 public SetRenderTargetOperation(GLRenderer mder, GLTexture ite, Vector3D sp, Vector3D sr)
 {
     internrenderer = mder;
     itexture = ite;
     spos = sp;
     srot = sr;
 }
Пример #18
0
 public GlVertexNotSupportedBuffer(Vector3D[] vertices, Vector2D[] texcoords, Vector3D[] normals, GLRenderer nder)
 {
     verts = vertices;
     texc = texcoords;
     norms = normals;
     renderer = nder;
 }
Пример #19
0
 public GlVertexBuffer(Vector3D[] vertices, Vector2D[] _texcoords, Vector3D[] _normals, GLRenderer _rend)
 {
     renderer = _rend;
     verts = vertices;
     texcoords = _texcoords;
     normals = _normals;
     for (int i = 0; i < texcoords.Length; i++)
     {
         texcoords[i].Y *= -1;
     }
 }
Пример #20
0
 public Vertex(Vector3D Position, Vector3D normal, Vector2D Texture)
 {
     position = new Vector3(Position.X,Position.Y,Position.Z);
     Normal = new Vector3(normal.X,normal.Y,normal.Z);
     texcoord = new Vector2(Texture.X,Texture.Y);
 }
Пример #21
0
 public override void SetRenderTarget(Texture2D texture, Vector3D campos, Vector3D camrot)
 {
     RenderTargetChange mchange = new RenderTargetChange(texture as DXTexture, this,campos,camrot);
 }
Пример #22
0
 /// <summary>
 /// Computes a rotational direction
 /// </summary>
 /// <param name="rotation"></param>
 public static Vector3D ComputeRotation(Vector3D rotation)
 {
     Vector3D retval = new Vector3D();
     retval.X += (float)Math.Sin(-rotation.Y * piover180);
     retval.Z += (float)Math.Cos(rotation.Y * piover180);
     retval.Y += (float)Math.Sin(rotation.X * piover180);
     return retval;
 }
Пример #23
0
 public override VertexBuffer CreateVertexBuffer(Vector3D[] vertices, Vector2D[] texcoords, Vector3D[] normals)
 {
     if (BasicShader.supportsShaders)
     {
         Console.WriteLine("SHADER");
         GlVertexBuffer mbuff = new GlVertexBuffer(vertices, texcoords, normals, this);
         return mbuff;
     }
     else
     {
         Console.WriteLine("NSSS");
         GlVertexNotSupportedBuffer mbuff = new GlVertexNotSupportedBuffer(vertices, texcoords, normals, this);
         return mbuff;
     }
 }
Пример #24
0
        public static void Main(string[] args)
        {
            try {

               // throw new Exception();
                renderer = new DirectXLib.DirectEngine();
                flip = true;
            }catch(Exception) {
                renderer = new GLRenderer();
                flip = false;
            }
            renderer.cameraPosition.Z = -5;

            Shader myder = renderer.createBasicShader();

               //Uncomment for AABB collisions

             //physicsworld.testtype = CollisionTestType.AABB;
            myder.Draw();

            Bitmap mmap = new Bitmap("pic.jpg");

            Texture2D mtex = renderer.createTextureFromBitmap(mmap);
            Bitmap newmap = new Bitmap("pic.jpg");
            Graphics tfix = Graphics.FromImage(newmap);
            tfix.DrawString("Hello world!", new Font(FontFamily.GenericMonospace, 16), Brushes.Red, new PointF(0, 0));
            tfix.Dispose();
            mtex.UploadBitmap(newmap);
            mtex.Draw();
            rtex = renderer.createTexture(512, 512);

            rtex.Draw();
            Mesh[] meshes = Primitives.LoadMesh("playercube.obj",flip);

            collisiontester = new PhysicalObject(meshes[0].meshverts, 5, CollisionType.Dynamic,physicsworld);
            collisiontester.Position = new Vector3D(-5, 0, 0);

            mainmesh = meshes[0];
            foreach (Mesh mesh in meshes)
            {
                VertexBuffer tbuff = renderer.CreateVertexBuffer(mesh.meshverts, mesh.meshtexas, mesh.meshnorms);
                rotatingbuffer = tbuff;
                if (mesh.bitmap != null)
                {
                    Console.WriteLine("BITMAP RENDER");
                    Texture2D tt = renderer.createTextureFromBitmap(mesh.bitmap);
                    tt.Draw();
                }
                tbuff.Draw();
            }
            mtex.Draw();
            Mesh cube = Primitives.LoadMesh("playercube.obj",flip)[0];

            theobject = new PhysicalObject(cube.meshverts, 9, CollisionType.Dynamic,physicsworld);

            collisiontester.ownedVBO = rotatingbuffer;
            theobject.ownedVBO = renderer.CreateVertexBuffer(cube.meshverts, cube.meshtexas, cube.meshnorms);

            theobject.ownedVBO.Draw();

            physicsworld.physicalobjects.Add(theobject);
            physicsworld.physicalobjects.Add(collisiontester);

            Mesh anothercube = Primitives.LoadMesh("playercube.obj",flip)[0];
            PhysicalObject mobject = new PhysicalObject(anothercube.meshverts.Clone() as Vector3D[], 1, CollisionType.Dynamic,physicsworld);
            mobject.ownedVBO = renderer.CreateVertexBuffer(anothercube.meshverts, anothercube.meshtexas, anothercube.meshnorms);
            physicsworld.physicalobjects.Add(mobject);
            mobject.ownedVBO.Draw();
            mobject.Position = new Vector3D(30, 0, 0);
            //Set physics properties

            theobject.Weight = 1;

            collisiontester.Weight = 1;

            //collisiontester.Velocity = new Vector3D(.05f, 0, 0);
            theobject.Position = new Vector3D(15, 0, 0);
            theobject.IsCube = true;
            theobject.Weight = 9999999;
            mobject.IsCube = true;

            //End physics properties

            physicsworld.Start();

            physicsworld.physicsUpdateFrame += new System.Threading.ThreadStart(physicsworld_physicsUpdateFrame);

            System.Threading.Thread mthread = new System.Threading.Thread(thetar);
            mthread.Start();
            foreach (Keyboard et in renderer.GetExtensionKeyboards())
            {
                et.onKeyDown += new keyboardeventargs(defaultKeyboard_onKeyDown);
                et.onKeyUp += new keyboardeventargs(defaultKeyboard_onKeyUp);

            }
            renderer.defaultMouse.onMouseMove += new mouseEvent(defaultMouse_onMouseMove);
            renderer.defaultMouse.onMouseDown += new mouseEvent(defaultMouse_onMouseDown);
            //Draw a quad
            List<Vector3D> overts = new List<Vector3D>();
            List<Vector3D> onorms = new List<Vector3D>();
            List<Vector2D> ocords = new List<Vector2D>();
            //Triangle 0

            overts.Add(new Vector3D(-1,-1,0));
            overts.Add(new Vector3D(-1,1,0));
            overts.Add(new Vector3D(1,1,0));
            ocords.Add(new Vector2D(0,0));
            ocords.Add(new Vector2D(0,1));
            ocords.Add(new Vector2D(1,1));
            //Triangle 1
            overts.Add(new Vector3D(1,1,0));
            overts.Add(new Vector3D(1,-1,0));
            overts.Add(new Vector3D(-1,-1,0));
            ocords.Add(new Vector2D(1,1));
            ocords.Add(new Vector2D(1,0));
            ocords.Add(new Vector2D(0,0));
            float zfactor = 900;
            for(int i = 0;i<overts.Count;i++) {
            //Translate by -1

                overts[i] = new Vector3D((overts[i].X)*zfactor,(overts[i].Y)*zfactor,overts[i].Z);
            }
            for(int i = 0;i<overts.Count;i++) {
            onorms.Add(new Vector3D(1,1,1));
            }
            mbuff = renderer.CreateVertexBuffer(overts.ToArray(),ocords.ToArray(),onorms.ToArray());
            mbuff.IsStatic = true;
            mbuff.Position.Z = zfactor;

            //rtex.Draw();
            mbuff.Draw();
            mtex.Draw();
        }
Пример #25
0
 /// <summary>
 /// Gets the center between two vectors
 /// </summary>
 /// <param name="othervect">The other vector to center</param>
 /// <returns></returns>
 public Vector3D Center(Vector3D othervect)
 {
     return new Vector3D((othervect.X + X) / 2, (othervect.Y + Y) / 2, (othervect.Z + Z) / 2);
 }
Пример #26
0
 public WPVertexBuffer(PhoneRenderer mder, Vector3D[] verts, Vector2D[] tex, Vector3D[] norms)
 {
     renderer = mder;
     vertices = verts;
     normals = norms;
     texcoords = tex;
 }
Пример #27
0
 public Vector3D CrossProduct(Vector3D b)
 {
     return new Vector3D(Y * b.Z - Z * b.Y,Z*b.X-X*b.Z,X*b.Y-Y*b.X);
 }
Пример #28
0
        static void thetar()
        {
            renderer.cameraPosition.Z -= 5f;

            while (isrunning)
            {
                //theobject.ownedVBO.rotation.X += .01f;
                Vector3D prevpos = renderer.cameraPosition;
                //rotatingbuffer.rotation.Y = MathHelpers.DegreesToRadians(-90);
                //rotatingbuffer.rotation.Y += .01f;
                Vector3D cameraPosition = new Vector3D(0, 0, 0);

                Vector3D direction = Vector3D.ComputeRotation(new Vector3D(renderer.worldRotation.X, renderer.worldRotation.Y, renderer.worldRotation.Z));
                direction.Y *= -1;
                Vector3D direction2 = Vector3D.ComputeRotation(new Vector3D(renderer.worldRotation.X, renderer.worldRotation.Y - 90, renderer.worldRotation.Z));

                if (left)
                {

                    cameraPosition.X -= .1f * direction2.X;
                    cameraPosition.Z -= .1f * direction2.Z;
                }
                if (right)
                {
                    cameraPosition.X += .1f * direction2.X;
                    cameraPosition.Z += .1f * direction2.Z;
                }
                if (up)
                {
                    cameraPosition.Z += .1f * direction.Z;
                    cameraPosition.X += .1f * direction.X;
                }
                if (down)
                {
                    cameraPosition.Z -= .1f * direction.Z;
                    cameraPosition.X -= .1f * direction.X;
                }

                // collisiontester.Rotation = rotatingbuffer.rotation;
                renderer.cameraPosition += cameraPosition;
                if (collisiontester.Contains(renderer.cameraPosition))
                {
                    renderer.cameraPosition = prevpos;
                }

                System.Threading.Thread.Sleep(10);
            }
        }
Пример #29
0
 public override _3DAPI.VertexBuffer CreateVertexBuffer(Vector3D[] vertices, Vector2D[] texcoords, Vector3D[] normals)
 {
     for (int i = 0; i < vertices.Length; i++)
     {
         vertices[i] = new Vector3D(-vertices[i].X, vertices[i].Y, vertices[i].Z);
     }
     return new DXVertBuffer(this, vertices, normals, texcoords);
 }