예제 #1
0
        public static BoundingBox createBoundingBox(Mesh mesh)
        {
            BoundingBox mbox = new BoundingBox();

            foreach (Vector3D et in mesh.meshverts)
            {
                if (et.X > mbox.maxX.X)
                {
                    mbox.maxX = et;
                }
                if (et.Y > mbox.maxY.Y)
                {
                    mbox.maxY = et;
                }
                if (et.Z > mbox.maxZ.Z)
                {
                    mbox.maxZ = et;
                }
                if (et.X < mbox.minX.X)
                {
                    mbox.minX = et;
                }
                if (et.Y < mbox.minY.Y)
                {
                    mbox.minY = et;
                }
                if (et.Z < mbox.minZ.Z)
                {
                    mbox.minZ = et;
                }

            }

            return mbox;
        }
예제 #2
0
파일: RealProg.cs 프로젝트: IDWMaster/3DAPI
        public RealProg(Renderer _renderer)
        {
            renderer = _renderer;
            renderer.cameraPosition.Z = -15;

            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);
            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();
            #if PHONE
            renderer.defaultTouchpad.onTouchMoved += new TouchEvent(defaultTouchpad_onTouchMoved);
            renderer.defaultTouchpad.onTouchFound += new TouchEvent(defaultTouchpad_onTouchFound);
            renderer.defaultTouchpad.onTouchLost += new TouchEvent(defaultTouchpad_onTouchLost);
            #endif
            #if !PHONE
            renderer.defaultKeyboard.onKeyDown += new keyboardeventargs(defaultKeyboard_onKeyDown);
            renderer.defaultKeyboard.onKeyUp += new keyboardeventargs(defaultKeyboard_onKeyUp);
            renderer.defaultMouse.onMouseMove += new mouseEvent(defaultMouse_onMouseMove);
            renderer.defaultMouse.onMouseDown += new mouseEvent(defaultMouse_onMouseDown);
            #endif
        }
예제 #3
0
파일: Main.cs 프로젝트: IDWMaster/3DAPI
        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();
        }
예제 #4
0
파일: Class1.cs 프로젝트: IDWMaster/3DAPI
        /// <summary>
        /// Loads a 3D mesh from an obj file
        /// </summary>
        /// <param name="filename">The file name</param>
        /// <param name="fliptexcoords">Whether or not texture coordinates should be flipped when loading the mesh</param>
        /// <returns></returns>
        public static Mesh[] LoadMesh(string filename, bool fliptexcoords)
        {
            ObjMeshLoader.Clear();
            StreamReader[] readers = ObjMeshLoader.LoadMeshes(filename);
            ObjMesh prevmesh = null;
            List<Mesh> meshes = new List<Mesh>();
            foreach (StreamReader et in readers)
            {
                List<Vector3D> meshverts = new List<Vector3D>();
                List<Vector3D> meshnorms = new List<Vector3D>();
                List<Vector2D> meshtex = new List<Vector2D>();
                ObjMesh currentmesh = new ObjMesh();
                string name = et.ReadLine();
                Console.WriteLine("Loading " + name);
                bool success = ObjMeshLoader.Load2(currentmesh, et, prevmesh);
                foreach (ObjTriangle ett in currentmesh.Triangles)
                {
                    meshverts.Add(new Vector3D(currentmesh.Vertices[(int)ett.Index0].Vertex.X, currentmesh.Vertices[(int)ett.Index0].Vertex.Y, currentmesh.Vertices[(int)ett.Index0].Vertex.Z));
                    meshverts.Add(new Vector3D(currentmesh.Vertices[(int)ett.Index1].Vertex.X, currentmesh.Vertices[(int)ett.Index1].Vertex.Y, currentmesh.Vertices[(int)ett.Index1].Vertex.Z));
                    meshverts.Add(new Vector3D(currentmesh.Vertices[(int)ett.Index2].Vertex.X, currentmesh.Vertices[(int)ett.Index2].Vertex.Y, currentmesh.Vertices[(int)ett.Index2].Vertex.Z));

                    //Normals
                    meshnorms.Add(new Vector3D(currentmesh.Vertices[(int)ett.Index0].Normal.X, currentmesh.Vertices[(int)ett.Index0].Normal.Y, currentmesh.Vertices[(int)ett.Index0].Normal.Z));
                    meshnorms.Add(new Vector3D(currentmesh.Vertices[(int)ett.Index1].Normal.X, currentmesh.Vertices[(int)ett.Index1].Normal.Y, currentmesh.Vertices[(int)ett.Index1].Normal.Z));
                    meshnorms.Add(new Vector3D(currentmesh.Vertices[(int)ett.Index2].Normal.X, currentmesh.Vertices[(int)ett.Index2].Normal.Y, currentmesh.Vertices[(int)ett.Index2].Normal.Z));

                    //Texcoords
                    if (fliptexcoords)
                    {
                        meshtex.Add(new Vector2D(currentmesh.Vertices[(int)ett.Index0].TexCoord.X, -currentmesh.Vertices[(int)ett.Index0].TexCoord.Y));
                        meshtex.Add(new Vector2D(currentmesh.Vertices[(int)ett.Index1].TexCoord.X, -currentmesh.Vertices[(int)ett.Index1].TexCoord.Y));
                        meshtex.Add(new Vector2D(currentmesh.Vertices[(int)ett.Index2].TexCoord.X, -currentmesh.Vertices[(int)ett.Index2].TexCoord.Y));
                    }
                    else
                    {
                        meshtex.Add(new Vector2D(currentmesh.Vertices[(int)ett.Index0].TexCoord.X, currentmesh.Vertices[(int)ett.Index0].TexCoord.Y));
                        meshtex.Add(new Vector2D(currentmesh.Vertices[(int)ett.Index1].TexCoord.X, currentmesh.Vertices[(int)ett.Index1].TexCoord.Y));
                        meshtex.Add(new Vector2D(currentmesh.Vertices[(int)ett.Index2].TexCoord.X, currentmesh.Vertices[(int)ett.Index2].TexCoord.Y));
                    }
                }
                Mesh realmesh = new Mesh();
                realmesh.name = name;
                realmesh.meshverts = meshverts.ToArray();
                realmesh.meshnorms = meshnorms.ToArray();
                realmesh.meshtexas = meshtex.ToArray();
                realmesh.bitmap = currentmesh.Material;
                meshes.Add(realmesh);
            }

            return meshes.ToArray();
        }
예제 #5
0
        public RealProg(Renderer renderer)
        {
            Shader myder = renderer.createBasicShader();
            World physicsworld = new World();
            //Uncomment for AABB collisions

            // physicsworld.testtype = CollisionTestType.AABB;
            myder.Draw();
            Texture2D mtex = renderer.createTexture(512, 512);
            mtex.Draw();

            Mesh[] meshes = Primitives.LoadMesh("test.obj");
            collisiontester = new PhysicalObject(meshes[0].meshverts, 5, CollisionType.Dynamic, physicsworld);

            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();
            }
            Mesh cube = Primitives.LoadMesh("playercube.obj")[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);
            //theobject.Velocity = new Vector3D(-.01f, 0, 0);
            Mesh anothercube = Primitives.LoadMesh("playercube.obj")[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
            // collisiontester.Velocity = new Vector3D(.2f, 0, 0);
            theobject.Weight = 1;
            //Uncomment for an inelastic collision
            //collisiontester.mode = CollisionMode.Inelastic;
            collisiontester.Weight = 1;

            collisiontester.Velocity = new Vector3D(.05f, 0, 0);
            theobject.Position = new Vector3D(15, 0, 0);
            //collisiontester.Velocity = new Vector3D(-.2f, 0, 0);
            //End physics properties

            physicsworld.Start();
            List<Vector3D> vectors = new List<Vector3D>();
            List<Vector2D> texcoords = new List<Vector2D>();
            List<Vector3D> normals = new List<Vector3D>();
            for (int i = 0; i < 3; i++)
            {
                normals.Add(new Vector3D(1, 1, 1));
            }
            vectors.Add(new Vector3D(0, 0, 0));
            vectors.Add(new Vector3D(8, 0, 0));
            vectors.Add(new Vector3D(8, 8, 0));
            texcoords.Add(new Vector2D(0, 0));
            texcoords.Add(new Vector2D(1, 0));
            texcoords.Add(new Vector2D(1, 1));
            VertexBuffer tribuffer = renderer.CreateVertexBuffer(vectors.ToArray(), texcoords.ToArray(), normals.ToArray());
            tribuffer.DepthTesting = false;
            tribuffer.IsStatic = true;
            tribuffer.Draw();

            renderer.cameraPosition.Z = -25;
            renderer.cameraPosition.X = -16;
            //Primitives.createRectangle(renderer, 0, 0, 0, 10, 10).Draw();
            System.Threading.Thread mthread = new System.Threading.Thread(thetar);
            mthread.Start();
        }