Esempio n. 1
0
        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager c, Microsoft.Xna.Framework.Graphics.GraphicsDevice g)
        {
            base.LoadContent(c, g);

            primBatch = new PrimitiveBatch(Device);
            cam = new FirstPersonCamera(0.5f, 10);
            cam.Pos = new Vector3(3, 3, 13);

            balls = new List<Ball>();

            for (int i = 0; i < 50; i++)
            {
                balls.Add(new Ball(new Vector3(i * 2, 2, 2), new Vector3(i + 1, -i, i % 2) * 0.1f, 0.5f));
            }

            planes = new List<Plane>();

            planes.Add(new Plane(Vector3.Right, 0));
            planes.Add(new Plane(Vector3.Up, 0));
            planes.Add(new Plane(new Vector3(0, 0, 1), 0));
            planes.Add(new Plane(Vector3.Left, -10));
            planes.Add(new Plane(Vector3.Down, -10));
            planes.Add(new Plane(new Vector3(0, 0, -1), -10));

            MeshBuilder mb = new MeshBuilder(g);
            sphere = mb.CreateSphere(1f, 10, 10);
        }
Esempio n. 2
0
        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            base.LoadContent(content);

            BlockData.Initialize(Device, content);

            cam = new FirstPersonCamera(0.5f, 10);
            cam.Pos = new Vector3(3, 3, 13);


            map = new Map(Device);

            partition = new MapPartition(map);
            engine = new PhysicsEngine3D(partition);

            MeshBuilder mb = new MeshBuilder(Device);
            sphere = mb.CreateSphere(0.1f, 10, 10);
            marker = new MeshNode(sphere);

            placeType = 1;

            primBatch = new PrimitiveBatch(Device);


        }
Esempio n. 3
0
        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            base.LoadContent(content);

            map = new Map(Device, "startPosTest.txt");
            //        map = new Map();
            BlockData.Initialize(Device, content);
            primBatch = new PrimitiveBatch(Device);

            MeshBuilder mb = new MeshBuilder(Device);
            mb.Begin();

            //mb.AddCylinder(1, 1, 50);
            mb.AddSphere(1, 20, 20);
            testMesh = mb.End();

            mNode = new MeshNode(testMesh);
            mNode.SetPivot(new Vector3(0, -1f, 0));
            mNode.SetScl(new Vector3(1, 5, 1));
            MeshNode child = new MeshNode(testMesh);
            child.SetPos(new Vector3(0, 2, 0));
            MeshNode another = new MeshNode(testMesh);
            another.SetPos(new Vector3(0, 2, 0));

            //Mesh sphere = mb.CreateSphere(0.1f, 10, 10);
            //startMarker = new MeshNode(sphere);
            //startMarker.SetPos(map.StartPos);
            //child.AddChild(another);
            //mNode.AddChild(child);
        }
Esempio n. 4
0
        public override void LoadContent(ContentManager content, GraphicsDevice g)
        {
            base.LoadContent(content, g);

            primBatch = new PrimitiveBatch(Device);
            cam = new FirstPersonCamera(0.5f, 10);
            cam.Pos = new Vector3(3, 3, 13);

            MeshBuilder mb = new MeshBuilder(g);
            sphere = mb.CreateSphere(1f, 10, 10);

            setupEngine();
        }
Esempio n. 5
0
        public override void LoadContent(ContentManager c, GraphicsDevice g)
        {
            base.LoadContent(c, g);

            currentMesh = createCube(c);
            effect = new BasicEffect(g);
            effect.PreferPerPixelLighting = true;
            effect.TextureEnabled = true;
            effect.Texture = currentMesh.Texture;
            effect.EnableDefaultLighting();

            cam = new FirstPersonCamera(0.5f, 5f);
            cam.Pos = new Vector3(0, 2, 4);
        }
Esempio n. 6
0
        public TerrainNode(GraphicsDevice device, Texture2D heightMap)
        {
            LoadHeightMapData(heightMap);
            SetUpVertices();
            SetUpIndices();
            CalculateNormals();

            IndexBuffer iBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly);
            VertexBuffer vBuffer = new VertexBuffer(device, VertexPositionColorNormal.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);

            iBuffer.SetData<Int32>(indices);
            vBuffer.SetData<VertexPositionColorNormal>(vertices);

            Mesh = new Mesh();
            Mesh.Indices = iBuffer;
            Mesh.Vertices = vBuffer;
        }
Esempio n. 7
0
        public List<Triangle> GetTriangles(Mesh m)
        {
            List<Triangle> tris = new List<Triangle>();

            VertexPositionNormalTexture[] verts = new VertexPositionNormalTexture[m.Vertices.VertexCount];
            m.Vertices.GetData<VertexPositionNormalTexture>(verts);

            short[] indices = new short[m.Indices.IndexCount];
            m.Indices.GetData<short>(indices);

            for (int i = 0; i < indices.Length; i += 3)
            {
                Vector3 v1 = verts[indices[i]].Position;
                Vector3 v2 = verts[indices[i+1]].Position;
                Vector3 v3 = verts[indices[i+2]].Position;
                Triangle t = new Triangle();
                t.v1 = v1;
                t.v2 = v2;
                t.v3 = v3;
                tris.Add(t);
            }

            return tris;
        }
Esempio n. 8
0
        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            base.LoadContent(content);

            BlockData.Initialize(Device, content);

            //map = new Map(Device, "Content/Levels/ramps.txt"); //Load map
            map = new Map(Device, "Content/Levels/Level1-1.txt");

            MeshBuilder mb = new MeshBuilder(Device);

            buildWalls(mb, content);

            setupNormalMapEffect(content);

            if (singleplayer)
            {
                objectiveLocation = map.getNextObjective(new Vector3(-1, -1, -1)); //get first objective
            }

            partition = new MapPartition(map);

            player = new Player(new Vector3(map.Width/2,20,map.Depth - 2)); //spawn player

            initializePhysicsEngine();

            //Misc initialization stuff
            sphere = mb.CreateSphere(1f, 10, 10);
            destination = mb.CreateSphere(0.5f, 12, 12); //sphere to draw at objective
            destination.Texture = content.Load<Texture2D>("Textures/BlockTextures/Destination");

            sBatch = new SpriteBatch(Device);
            sf = content.Load<SpriteFont>("Fonts/Helvetica");
            primBatch = new PrimitiveBatch(Device);

            playerColor = new Vector3((int.Parse(localPlayerName) % 5) / 5.0f, (int.Parse(localPlayerName) % 3) / 3.0f, (int.Parse(localPlayerName) % 2) / 2.0f);
        }
Esempio n. 9
0
 private void buildWalls(MeshBuilder mb, ContentManager content)
 {
     mb.Begin();
     //add back wall
     mb.AddQuad(new Vector3(1, map.Height - 1, 1), new Vector3(map.Width - 1, map.Height - 1, 1), new Vector3(map.Width - 1, 1, 1), new Vector3(1, 1, 1), false, Vector2.Zero, new Vector2(map.Width, map.Height));
     //add front wall
     mb.AddQuad(new Vector3(map.Width - 1, map.Height - 1, map.Depth - 1), new Vector3(1, map.Height - 1, map.Depth - 1), new Vector3(1, 1, map.Depth - 1), new Vector3(map.Width - 1, 1, map.Depth - 1), false, Vector2.Zero, new Vector2(map.Width, map.Height));
     //add left wall
     mb.AddQuad(new Vector3(1, map.Height - 1, map.Depth - 1), new Vector3(1, map.Height - 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, map.Depth - 1), false, Vector2.Zero, new Vector2(map.Depth, map.Height));
     //add right wall
     mb.AddQuad(new Vector3(map.Width - 1, map.Height - 1, 1), new Vector3(map.Width - 1, map.Height - 1, map.Depth - 1), new Vector3(map.Width - 1, 1, map.Depth - 1), new Vector3(map.Width - 1, 1, 1), false, Vector2.Zero, new Vector2(map.Depth, map.Height));
     //add floor
     mb.AddQuad(new Vector3(1, 1, 1), new Vector3(map.Width - 1, 1, 1), new Vector3(map.Width - 1, 1, map.Depth - 1), new Vector3(1, 1, map.Depth - 1), false, Vector2.Zero, new Vector2(map.Width, map.Depth));
     walls = mb.End();
     walls.Texture = content.Load<Texture2D>("Textures/BlockTextures/Walls");
     walls.NormalMap = content.Load<Texture2D>("Textures/BlockTextures/WallsN");
 }
Esempio n. 10
0
        public void DrawMesh(Mesh mesh, Matrix world, Camera cam)
        {
            Matrix previousWorld = effect.World;

            device.SetVertexBuffer(mesh.Vertices);
            device.Indices = mesh.Indices;
            effect.View = cam.View;
            effect.Projection = cam.Projection;
            effect.World = world;
            //effect.VertexColorEnabled = false;
            //effect.TextureEnabled = true;
            effect.PreferPerPixelLighting = true;
            effect.EnableDefaultLighting();

            effect.CurrentTechnique.Passes[0].Apply();
            int passes = mesh.Vertices.VertexCount / maxVertsPerDraw;
            int remainder = mesh.Vertices.VertexCount % maxVertsPerDraw;
            int offset = 0;
            for (int i = 0; i < passes; i++)
            {
                device.DrawPrimitives(PrimitiveType.TriangleList, offset, maxVertsPerDraw / numVertsPerPrimitive(PrimitiveType.TriangleList));
                offset += maxVertsPerDraw;
            }

            //device.DrawPrimitives(PrimitiveType.TriangleList, offset, remainder / numVertsPerPrimitive(PrimitiveType.TriangleList));
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mesh.Vertices.VertexCount, 0, mesh.Indices.IndexCount / 3);

            effect.VertexColorEnabled = true;
            effect.LightingEnabled = false;
            effect.World = previousWorld;
            //effect.TextureEnabled = false;
            //effect.PreferPerPixelLighting = false;
        }
Esempio n. 11
0
        public void DrawMesh(Mesh mesh, Matrix world, Camera cam, Vector3 color)
        {
            Matrix previousWorld = effect.World;

            device.SetVertexBuffer(mesh.Vertices);
            device.Indices = mesh.Indices;

            device.BlendState = BlendState.AlphaBlend;
            effect.View = cam.View;
            effect.Projection = cam.Projection;
            effect.World = world;
            effect.VertexColorEnabled = false;
            effect.TextureEnabled = false;

            //Custom Effect:
            effect.SpecularColor = new Vector3(.025f, .05f, .025f);
            effect.EmissiveColor = new Vector3(0.02f, 0.02f, 0.02f);
            //effect.EmissiveColor = color;
            Vector3 lastDiffuse = effect.DiffuseColor;
            effect.DiffuseColor = color;
            effect.FogEnabled = true;
            effect.FogEnd = 35f;
            effect.FogColor = new Vector3(.025f, .05f, .025f);
            //End custom effect

            if (mesh.Texture != null)
            {
                effect.Texture = mesh.Texture;
                effect.TextureEnabled = true;
            }
            effect.PreferPerPixelLighting = true;
            effect.EnableDefaultLighting();
            effect.CurrentTechnique.Passes[0].Apply();
            int passes = mesh.Vertices.VertexCount / maxVertsPerDraw;
            int remainder = mesh.Vertices.VertexCount % maxVertsPerDraw;
            int offset = 0;
            for (int i = 0; i < passes; i++)
            {
                device.DrawPrimitives(PrimitiveType.TriangleList, offset, maxVertsPerDraw / numVertsPerPrimitive(PrimitiveType.TriangleList));
                offset += maxVertsPerDraw;
            }

            //device.DrawPrimitives(PrimitiveType.TriangleList, offset, remainder / numVertsPerPrimitive(PrimitiveType.TriangleList));
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mesh.Vertices.VertexCount, 0, mesh.Indices.IndexCount / 3);

            effect.VertexColorEnabled = true;
            effect.LightingEnabled = false;
            effect.World = previousWorld;
            effect.TextureEnabled = false;
            effect.Alpha = 1;
            //device.BlendState = BlendState.Opaque;
            //effect.PreferPerPixelLighting = false;
            effect.DiffuseColor = lastDiffuse;
        }
Esempio n. 12
0
        public void DrawMesh(Mesh mesh, Matrix world, Camera cam, Effect customEffect)
        {
            device.SetVertexBuffer(mesh.Vertices);
            device.Indices = mesh.Indices;

            device.BlendState = BlendState.AlphaBlend;

            customEffect.Parameters["View"].SetValue(cam.View);
            customEffect.Parameters["Projection"].SetValue(cam.Projection);
            customEffect.Parameters["World"].SetValue(world);

            if (mesh.Texture != null)
            {
                customEffect.Parameters["Texture"].SetValue(mesh.Texture);
                customEffect.Parameters["NormalMap"].SetValue(mesh.NormalMap);
            }

            customEffect.CurrentTechnique.Passes[0].Apply();
            int passes = mesh.Vertices.VertexCount / maxVertsPerDraw;
            int remainder = mesh.Vertices.VertexCount % maxVertsPerDraw;
            int offset = 0;
            for (int i = 0; i < passes; i++)
            {
                device.DrawPrimitives(PrimitiveType.TriangleList, offset, maxVertsPerDraw / numVertsPerPrimitive(PrimitiveType.TriangleList));
                offset += maxVertsPerDraw;
            }

            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mesh.Vertices.VertexCount, 0, mesh.Indices.IndexCount / 3);
        }
Esempio n. 13
0
        private Mesh createCube(ContentManager c)
        {
            Mesh m = new Mesh();
            VertexPositionNormalTexture[] verts = new VertexPositionNormalTexture[24];
            short[] indices = new short[36];

            //Set up vertices for the front face
            VertexPositionNormalTexture[] originalVerts = new VertexPositionNormalTexture[4];
            VertexPositionNormalTexture first = new VertexPositionNormalTexture();
            first.Position = new Vector3(-0.5f, 0.5f, 0.5f);
            first.Normal = new Vector3(0, 0, 1);
            first.TextureCoordinate = new Vector2(0, 0);

            VertexPositionNormalTexture second = new VertexPositionNormalTexture();
            second.Position = new Vector3(0.5f, 0.5f, 0.5f);
            second.Normal = new Vector3(0, 0, 1);
            second.TextureCoordinate = new Vector2(1, 0);

            VertexPositionNormalTexture third = new VertexPositionNormalTexture();
            third.Position = new Vector3(0.5f, -0.5f, 0.5f);
            third.Normal = new Vector3(0, 0, 1);
            third.TextureCoordinate = new Vector2(1, 1);

            VertexPositionNormalTexture fourth = new VertexPositionNormalTexture();
            fourth.Position = new Vector3(-0.5f, -0.5f, 0.5f);
            fourth.Normal = new Vector3(0, 0, 1);
            fourth.TextureCoordinate = new Vector2(0, 1);

            originalVerts[0] = first;
            originalVerts[1] = second;
            originalVerts[2] = third;
            originalVerts[3] = fourth;

            Matrix[] rotations = {Matrix.CreateRotationY(0),
                                  Matrix.CreateRotationY(MathHelper.PiOver2),
                                  Matrix.CreateRotationY(MathHelper.Pi),
                                  Matrix.CreateRotationY(-MathHelper.PiOver2),
                                  Matrix.CreateRotationX(MathHelper.PiOver2),
                                  Matrix.CreateRotationX(-MathHelper.PiOver2)};

            short indexOffset = 0;
            short vertexOffset = 0;

            for (int i = 0; i < rotations.Length; i++)
            {
                VertexPositionNormalTexture v1 = transformVertex(originalVerts[0], rotations[i]);
                VertexPositionNormalTexture v2 = transformVertex(originalVerts[1], rotations[i]);
                VertexPositionNormalTexture v3 = transformVertex(originalVerts[2], rotations[i]);
                VertexPositionNormalTexture v4 = transformVertex(originalVerts[3], rotations[i]);

                verts[vertexOffset] = v1;
                verts[vertexOffset+1] = v2;
                verts[vertexOffset+2] = v3;
                verts[vertexOffset+3] = v4;

                indices[indexOffset] = vertexOffset;
                indices[indexOffset+1] = (short)(vertexOffset+1);
                indices[indexOffset+2] = (short)(vertexOffset + 2);

                indices[indexOffset+3] = vertexOffset;
                indices[indexOffset+4] = (short)(vertexOffset + 2);
                indices[indexOffset+5] = (short)(vertexOffset + 3);

                vertexOffset += 4;
                indexOffset += 6;
            }

            IndexBuffer iBuffer = new IndexBuffer(Device, IndexElementSize.SixteenBits, indices.Length, BufferUsage.None);
            iBuffer.SetData<short>(indices);
            VertexBuffer vBuffer = new VertexBuffer(Device, typeof(VertexPositionNormalTexture), verts.Length, BufferUsage.None);
            vBuffer.SetData<VertexPositionNormalTexture>(verts);

            m.Indices = iBuffer;
            m.Vertices = vBuffer;

            m.Texture = c.Load<Texture2D>("Cube");

            return m;
        }
Esempio n. 14
0
        public Mesh End()
        {
            if (!hasBegun)
            {
                throw new Exception("Can't end a mesh without beginning it!");
            }

            hasBegun = false;

            short[] finalIndices = new short[(softTriangles.Count + hardTriangles.Count) * 3];

            for (int i = 0; i < softTriangles.Count; i++)
            {
                TriangleData tri = softTriangles[i];

                finalIndices[(i*3)] = (short)tri.A;
                finalIndices[(i * 3) + 1] = (short)tri.B;
                finalIndices[(i * 3) + 2] = (short)tri.C;

                Vector3 a = softVerts[tri.A].Position;
                Vector3 b = softVerts[tri.B].Position;
                Vector3 c = softVerts[tri.C].Position;

                Vector3 normal = Vector3.Cross(c - a, b - a);

                VertexPositionNormalTexture newVert = new VertexPositionNormalTexture(softVerts[tri.A].Position, softVerts[tri.A].Normal + normal, softVerts[tri.A].TextureCoordinate);
                softVerts[tri.A] = newVert;
                newVert = new VertexPositionNormalTexture(softVerts[tri.B].Position, softVerts[tri.B].Normal + normal, softVerts[tri.B].TextureCoordinate);
                softVerts[tri.B] = newVert;
                newVert = new VertexPositionNormalTexture(softVerts[tri.C].Position, softVerts[tri.C].Normal + normal, softVerts[tri.C].TextureCoordinate);
                softVerts[tri.C] = newVert;
            }

            for (int i = 0; i < hardTriangles.Count; i++)
            {
                finalIndices[(i * 3) + (softTriangles.Count * 3)] = (short)(hardTriangles[i].A + softVerts.Count);
                finalIndices[(i * 3) + 1 + (softTriangles.Count * 3)] = (short)(hardTriangles[i].B + softVerts.Count);
                finalIndices[(i * 3) + 2 + (softTriangles.Count * 3)] = (short)(hardTriangles[i].C + softVerts.Count);
            }

            VertexPositionNormalTexture[] finalVerts = new VertexPositionNormalTexture[softVerts.Count + hardVerts.Count];
            for (int i = 0; i < softVerts.Count; i++)
            {
                VertexPositionNormalTexture vert = softVerts[i];
                vert.Normal = Vector3.Normalize(vert.Normal);
                finalVerts[i] = vert;
            }

            for (int i = 0; i < hardVerts.Count; i++)
            {
                finalVerts[i + softVerts.Count] = hardVerts[i];
            }

            VertexBuffer vBuffer = new VertexBuffer(device, typeof(VertexPositionNormalTexture), softVerts.Count + hardVerts.Count, BufferUsage.None);
            IndexBuffer iBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, finalIndices.Length, BufferUsage.None);
            vBuffer.SetData<VertexPositionNormalTexture>(finalVerts);
            iBuffer.SetData<short>(finalIndices);

            Mesh m = new Mesh();
            m.Indices = iBuffer;
            m.Vertices = vBuffer;

            return m;
        }
Esempio n. 15
0
        public override void LoadContent(ContentManager content, GraphicsDevice g)
        {
            base.LoadContent(content, g);

            primBatch = new PrimitiveBatch(Device);
            cam = new FirstPersonCamera(0.5f, 10);
            //cam.Pos = new Vector3(3, 3, 13);

            Model bodyModel = content.Load<Model>("Models/Body");
            Model handleModel = content.Load<Model>("Models/Handle");
            Model triggerModel = content.Load<Model>("Models/Trigger");
            crosshair = content.Load<Texture2D>("Crosshair");
            fire = content.Load<SoundEffect>("fire_silenced");
            batch = new SpriteBatch(Device);

            Mesh bodyMesh = new Mesh();
            bodyMesh.Indices = bodyModel.Meshes[0].MeshParts[0].IndexBuffer;
            bodyMesh.Vertices = bodyModel.Meshes[0].MeshParts[0].VertexBuffer;
            body = new MeshNode(bodyMesh);

            Mesh handleMesh = new Mesh();
            handleMesh.Indices = handleModel.Meshes[0].MeshParts[0].IndexBuffer;
            handleMesh.Vertices = handleModel.Meshes[0].MeshParts[0].VertexBuffer;
            handle = new MeshNode(handleMesh);
            handle.SetPos(new Vector3(0, 0.13f, 0.477f));

            Mesh triggerMesh = new Mesh();
            triggerMesh.Indices = triggerModel.Meshes[0].MeshParts[0].IndexBuffer;
            triggerMesh.Vertices = triggerModel.Meshes[0].MeshParts[0].VertexBuffer;
            trigger = new MeshNode(triggerMesh);
            trigger.SetPos(new Vector3(0, 0.033f, -1.561f));

            body.AddChild(handle);
            body.AddChild(trigger);
            body.SetPos(new Vector3(0, 5, 0));

            pickableObjects = new List<MeshNode>();
            pickableObjects.Add(body);
            pickableObjects.Add(handle);
            pickableObjects.Add(trigger);

            //Extract triangles
            bodyTris = GetTriangles(bodyMesh);
            handleTris = GetTriangles(handleMesh);
            triggerTris = GetTriangles(triggerMesh);
        }