Exemplo n.º 1
0
        public MegaTexture(Device device)
        {
            this.device = device;
            simple = TextureManager.Instance.LoadTexture("client/gfx/heightmaps/heightmap.dds");
            //simple = TextureManager.Instance.LoadTexture("client/gfx/uvDetective_512.dds");

            // Create plane w/ tex-coords
            VertexPositionNormalTextured[] vert = new VertexPositionNormalTextured[6];
            //vert[0].X = -100.0f; vert[0].Y = 100.0f; vert[0].Z = 1.0f;
            //vert[1].X = 100.0f; vert[1].Y = 100.0f; vert[1].Z = 1.0f;
            //vert[2].X = -100.0f; vert[2].Y = -100.0f; vert[2].Z = 1.0f;
            //vert[3].X = -100.0f; vert[3].Y = -100.0f; vert[3].Z = 1.0f;
            //vert[4].X = 100.0f; vert[4].Y = 100.0f; vert[4].Z = 1.0f;
            //vert[5].X = 100.0f; vert[5].Y = -100.0f; vert[5].Z = 1.0f;
            //vert[0].Tu = 0.0f; vert[0].Tv = 0.0f;
            //vert[1].Tu = 1.0f; vert[1].Tv = 0.0f;
            //vert[2].Tu = 0.0f; vert[2].Tv = 1.0f;
            //vert[3].Tu = 0.0f; vert[3].Tv = 1.0f;
            //vert[4].Tu = 1.0f; vert[4].Tv = 0.0f;
            //vert[5].Tu = 1.0f; vert[5].Tv = 1.0f;

            //group.vf = VertexPositionNormalTextured.Format;
            //group.vb = new VertexBuffer(typeof(VertexPositionNormalTextured), 6, device, Usage.Dynamic, group.vf, Pool.Default);
            //group.vb.SetData(vert, 0, LockFlags.None);
            group.tex = simple;
        }
Exemplo n.º 2
0
        private void Init(string texturename)
        {
            VertexPositionNormalTextured[] verts = new VertexPositionNormalTextured[36];

            // Front face
             verts[0] = new VertexPositionNormalTextured(-1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
             verts[1] = new VertexPositionNormalTextured(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f);
             verts[2] = new VertexPositionNormalTextured(1.0f, 1.0f, 1.0f, 0.0f, 0.0f);
             verts[3] = new VertexPositionNormalTextured(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f);
             verts[4] = new VertexPositionNormalTextured(1.0f, -1.0f, 1.0f, 0.0f, 1.0f);
            verts[5] = new VertexPositionNormalTextured(1.0f, 1.0f, 1.0f, 0.0f, 0.0f);

            // Back face (remember this is facing *away* from the camera, so vertices should be
            //    clockwise order)
            verts[6] = new VertexPositionNormalTextured(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f);
            verts[7] = new VertexPositionNormalTextured(1.0f, 1.0f, -1.0f, 1.0f, 0.0f);
            verts[8] = new VertexPositionNormalTextured(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f);
            verts[9] = new VertexPositionNormalTextured(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f);
            verts[10] = new VertexPositionNormalTextured(1.0f, 1.0f, -1.0f, 1.0f, 0.0f);
            verts[11] = new VertexPositionNormalTextured(1.0f, -1.0f, -1.0f, 1.0f, 1.0f);

            // Top face
            verts[12] = new VertexPositionNormalTextured(-1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
            verts[13] = new VertexPositionNormalTextured(1.0f, 1.0f, -1.0f, 0.0f, 1.0f);
            verts[14] = new VertexPositionNormalTextured(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f);
            verts[15] = new VertexPositionNormalTextured(-1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
            verts[16] = new VertexPositionNormalTextured(1.0f, 1.0f, 1.0f, 1.0f, 1.0f);
            verts[17] = new VertexPositionNormalTextured(1.0f, 1.0f, -1.0f, 0.0f, 1.0f);

            // Bottom face (remember this is facing *away* from the camera, so vertices should be
            //    clockwise order)
            verts[18] = new VertexPositionNormalTextured(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f);
            verts[19] = new VertexPositionNormalTextured(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f);
            verts[20] = new VertexPositionNormalTextured(1.0f, -1.0f, -1.0f, 1.0f, 1.0f);
            verts[21] = new VertexPositionNormalTextured(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f);
            verts[22] = new VertexPositionNormalTextured(1.0f, -1.0f, -1.0f, 1.0f, 1.0f);
            verts[23] = new VertexPositionNormalTextured(1.0f, -1.0f, 1.0f, 1.0f, 0.0f);

            // Left face
            verts[24] = new VertexPositionNormalTextured(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f);
            verts[25] = new VertexPositionNormalTextured(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f);
            verts[26] = new VertexPositionNormalTextured(-1.0f, -1.0f, 1.0f, 0.0f, 1.0f);

            verts[27] = new VertexPositionNormalTextured(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f);
            verts[28] = new VertexPositionNormalTextured(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f);
            verts[29] = new VertexPositionNormalTextured(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f);

            // Right face (remember this is facing *away* from the camera, so vertices should be
            //    clockwise order)
            verts[30] = new VertexPositionNormalTextured(1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
            verts[31] = new VertexPositionNormalTextured(1.0f, -1.0f, 1.0f, 1.0f, 1.0f);
            verts[32] = new VertexPositionNormalTextured(1.0f, -1.0f, -1.0f, 0.0f, 1.0f);
            verts[33] = new VertexPositionNormalTextured(1.0f, 1.0f, -1.0f, 0.0f, 0.0f);
            verts[34] = new VertexPositionNormalTextured(1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
            verts[35] = new VertexPositionNormalTextured(1.0f, -1.0f, -1.0f, 0.0f, 1.0f);
            Matrix translation = Matrix.RotationYawPitchRoll(0f, 90f * (float)(Math.PI / 180f), 0f);
            for (int i = 0; i < verts.Length; i++)
            {
                //verts[i].Position =
                verts[i].Position = Vector3.Multiply(verts[i].Position, 5000f);
                verts[i].Position = Vector3.TransformCoordinate(verts[i].Position, translation);
                verts[i].TextureCoordinate.X = (verts[i].TextureCoordinate.X + (1f / 512)) * (510f / 512f);
                verts[i].TextureCoordinate.Y = (verts[i].TextureCoordinate.Y + (1f / 512)) * (510f / 512f);
            }
            vb = new HagsVertexBuffer();
            vb.SetVB<VertexPositionNormalTextured>(verts, verts.Length * VertexPositionNormalTextured.SizeInBytes, VertexPositionNormalTextured.Format, Usage.WriteOnly);

            for (int i = 0; i < 6; i++)
            {
                List<uint> ind = new List<uint>();
                string texname = "skybox/"+ texturename;
                switch(i)
                {
                    case 0:
                        texname += "ft";
                        ind.AddRange(new uint[] { 0, 2, 1, 3, 5, 4 });
                        break;
                    case 1:
                        texname += "bk";
                        ind.AddRange(new uint[] {6,8,7,9,11,10 });
                        break;
                    case 2:
                        texname += "up";
                        ind.AddRange(new uint[] {12,14,13,15,17,16 });
                        break;
                    case 3:
                        texname += "dn";
                        ind.AddRange(new uint[] {18,20,19,21,23,22 });
                        break;
                    case 4:
                        texname += "lf";
                        ind.AddRange(new uint[] { 24,26,25,27,29,28});
                        break;
                    case 5:
                        texname += "rt";
                        ind.AddRange(new uint[] {30,32,31,33,35,34 });
                        break;
                }
                RenderItem item = new RenderItem(this, TextureManager.Instance.LoadMaterial(texname));
                item.DontOptimize = true;
                item.indices = ind;
                item.GenerateIndexBuffer();
                items.Add(item);
                rendercalls.Add(new KeyValuePair<ulong, RenderDelegate>(SortItem.GenerateBits(SortItem.FSLayer.GAME, SortItem.Viewport.STATIC, SortItem.VPLayer.SKYBOX, SortItem.Translucency.OPAQUE, item.material.MaterialID, 0, item.ib.IndexBufferID, item.vb.VertexBufferID), new RenderDelegate(item.Render)));
            }
        }
Exemplo n.º 3
0
        public void DrawFullScreenQuad(float fLeftU, float fTopV, float fRightU, float fBottomV)
        {
            Surface target = device.GetRenderTarget(0);
            SurfaceDescription rtDescr = target.Description;
            device.SetRenderState(RenderState.ZEnable, false);

            // Ensure that we're directly mapping texels to pixels by offset by 0.5
            // For more info see the doc page titled "Directly Mapping Texels to Pixels"
            float fWidth5 = (1 / (float)rtDescr.Width);// (float)rtDescr.Width - 1f;
            float fHeight5 = (-1 / (float)rtDescr.Height);//(float)rtDescr.Height -1f;

            VertexPositionNormalTextured[] quad = new VertexPositionNormalTextured[4];
            quad[0] = new VertexPositionNormalTextured(-1f - fWidth5, -1f - fHeight5, 0.5f, fLeftU, fTopV);
            quad[1] = new VertexPositionNormalTextured(1f - fWidth5, -1f - fHeight5, 0.5f, fRightU, fTopV);
            quad[2] = new VertexPositionNormalTextured(-1f - fWidth5, 1f - fHeight5, 0.5f, fLeftU, fBottomV);
            quad[3] = new VertexPositionNormalTextured(1f - fWidth5, 1f - fHeight5, 0.5f, fRightU, fBottomV);

            device.VertexFormat = VertexPositionNormalTextured.Format;
            device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, quad);
            device.SetRenderState(RenderState.ZEnable, true);
        }
Exemplo n.º 4
0
        void BuildGridGeometry()
        {
            Vector3[] globalverts, gridverts;
            int[] globalindices, gridindices, gridindices2;

            float dx = 10.0f;
            float dz = 10.0f;

            // Generate global grid
            GenTriGrid(inputImageHeight, inputImageWidth, dx, dz, new Vector3(0.0f, -1000f, 0f), out globalverts, out globalindices);

            // Number of sub-grids
            int nGridsY = GetGoodAverage(inputImageHeight);
            int nGridsX = GetGoodAverage(inputImageWidth);

            // Subgrid size
            int GridW = inputImageWidth / nGridsX;
            int GridD = inputImageHeight / nGridsY;

            int gridNumVerts = (GridW+1) * (GridD+1);
            int gridNumTris = (GridD ) * (GridW ) * 2;

            // Generate subgrid indices
            GenTriGrid(GridD+1, GridW+1, dx, dz, new Vector3(0.0f, -5000f, 0f), out gridverts, out gridindices);
            GenTriGrid(GridD, GridW, dx, dz, new Vector3(0.0f, -5000f, 0f), out gridverts, out gridindices2);

            // Define some often used variables
            bool overflowX = false, overflowY = false;
            float w = (GridW*nGridsX) * dx;
            float d = (GridD * nGridsY) * dz;
            Vector3 normal = new Vector3(0f, 1f, 0f);
            Mesh mesh;
            VertexPositionNormalTextured[] vertexData = new VertexPositionNormalTextured[gridNumVerts];
            int subgridX, subgridY, globalIndex, gridIndex;

            // foreach subgrid
            for (int gridX = 0; gridX < nGridsX; gridX++)
            {
                for (int gridY = 0; gridY < nGridsY; gridY++)
                {
                    overflowY = false;
                    overflowX = false;
                    mesh = new Mesh(Renderer.Instance.device, gridNumTris, gridNumVerts, MeshFlags.Use32Bit, VertexPositionNormalTextured.Format);

                    // Check for overflow
                    if ((gridX+1) * (GridW + 1) > inputImageWidth)
                        overflowX = true;
                    else if ((gridY+1) * (GridD + 1) > inputImageHeight)
                        overflowY = true;
                    if (overflowY || overflowX)
                    {
                    }
                    else
                    {
                        for (int subD = 0; subD < GridD + 1; ++subD)
                        {
                            for (int subW = 0; subW < GridW + 1; ++subW)
                            {
                                subgridX = gridX * GridW + subW;
                                subgridY = gridY * GridD + subD;
                                globalIndex = (subgridY * inputImageHeight) + subgridX;
                                gridIndex = (subD * (GridD + 1)) + subW;

                                vertexData[gridIndex].Position = globalverts[globalIndex];
                                //vertexData[gridIndex].Y += GetHeightFromImage(subgridY, subgridX) * scale;
                                vertexData[gridIndex].Position.Y += SampleHeightMap3x3(subgridY, subgridX) * scale;
                                vertexData[gridIndex].Normal = normal;
                                vertexData[gridIndex].TextureCoordinate = new Vector2((vertexData[gridIndex].Position.X + (0.5f * w)) / w, (vertexData[gridIndex].Position.Z - (0.5f * d)) / -d);
                            }
                        }

                        DataStream gs = mesh.LockVertexBuffer(LockFlags.None);
                        gs.WriteRange<VertexPositionNormalTextured>(vertexData);
                        gs.Seek(0, SeekOrigin.Begin);
                        // Todo: Fix AABB and frustrum culling
                        Vector3 min, max;
                        //Geometry.ComputeBoundingBox(gs, gridNumVerts, VertexPositionNormalTextured.Format, out min, out max);
                        mesh.UnlockVertexBuffer();

                        //int[] meshIndices = new int[gridNumTris * 3];
                        DataStream ds = mesh.LockAttributeBuffer(LockFlags.None);
                        for (int i = 0; i < gridNumTris; ++i)
                        {
                            ds.Write<int>(0);
                        }
                        mesh.UnlockAttributeBuffer();

                        //meshIndices = ;
                        gs = mesh.LockIndexBuffer(LockFlags.None);
                        gs.WriteRange<int>(gridindices);
                        mesh.UnlockIndexBuffer();

                        //gs = mesh.LockAttributeBuffer(LockFlags.None);
                        //gs.Write(meshAttr);

                        mesh.ComputeNormals();
                        int[] adj = new int[mesh.FaceCount * 3];
                        mesh.GenerateAdjacency(float.Epsilon);
                        //mesh.OptimizeInPlace(MeshFlags.OptimizeAttributeSort | MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeCompact, adj);
                        Mesh newmesh = mesh.Clone(Renderer.Instance.device, MeshFlags.Managed, VertexPosTexNormalTanBitan.Elements);
                        newmesh.ComputeTangent(0, 0, -1, false);

                        meshes.Add(newmesh);
                    }
                }
            }
        }