예제 #1
0
파일: SkyBox.cs 프로젝트: maesse/CubeHags
        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)));
            }
        }
예제 #2
0
        public new void Init()
        {
            this.SharedTexture2 = true;
            this.tex2 = world.LightmapTexture;
            this.stride = VertexPositionNormalTexturedLightmap.SizeInBytes;

            // Make renderitem for each face
            for (int fi = 0; fi < world.faces.Length;fi++ )
            {
                Face face = world.faces[fi];
                face.Format = VertexPositionNormalTexturedLightmap.Format;
                RenderItem item = new RenderItem(this, face.texinfo.texdata_t.mat);

                //if (face.texinfo.texdata_t.mat != null && face.texinfo.texdata_t.mat.Bumpmap)
                //    face.Format = VertexPositionNormalTexturedLightmapTangent.Format;

                // Create index list for non-displacement faces only
                if (face.face_t.dispinfo == -1 && !face.HasDisplacement && face.VertexOffset != -1)
                {
                    // Make TriangleList
                    int newIndices = (face.face_t.numedges - 2) * 3;
                    face.indices = new uint[newIndices];
                    face.nVerts = HagsIndexBuffer.GetVertexCount(item.indices);
                    for (int i = 0; i < (face.face_t.numedges - 2); i++)
                    {
                        face.indices[3 * i] = (uint)(face.VertexOffset);
                        face.indices[3 * i + 1] = (uint)(face.VertexOffset + i + 1);
                        face.indices[3 * i + 2] = (uint)(face.VertexOffset + i + 2);
                    }
                    item.indices = new List<uint>(face.indices);
                }
                else
                {
                    // Use pre-generated displacement index list
                    if (face.indices != null)
                    {
                        item.indices = new List<uint>(face.indices);
                        //face.nVerts = HagsIndexBuffer.GetVertexCount(item.indices);
                    }
                }

                // Setup item
                item.DontOptimize = true;
                item.face = face;
                item.Type = PrimitiveType.TriangleList;
                item.nVerts = face.nVerts;
                item.Init();
                world.faces[fi].item = item;
                items.Add(item);
            }

            // Create shared vertex buffer
            int vertexBytes = world.verts.Count * VertexPositionNormalTexturedLightmap.SizeInBytes;
            vb = new HagsVertexBuffer();
            vb.SetVB<VertexPositionNormalTexturedLightmap>(world.verts.ToArray(), vertexBytes, VertexPositionNormalTexturedLightmap.Format, Usage.WriteOnly);
            ib = new HagsIndexBuffer();

            Entity light_environment = null;
            foreach (Entity ent in world.Entities)
            {
                //System.Console.WriteLine("\n"+ ent.ClassName);
                foreach (string val in ent.Values.Keys)
                {
                    //System.Console.WriteLine("\t"+val + ": " + ent.Values[val]);
                }
                if (ent.ClassName == "light_environment")
                {
                    light_environment = ent;
                }
                else if (ent.ClassName.Equals("sky_camera"))
                {
                    skybox3d = new SkyBox3D(this, ent);
                }
                else if (ent.ClassName.Equals("env_fog_controller"))
                {
                    fogController = new FogController(ent.Values);
                    fogController.Init();
                }
            }

            if (skybox3d == null)
            {
                // Look for area 1
            }

            // Handle worldspawn entity (skybox)
            if (world.Entities[0].ClassName == "worldspawn")
            {
                if (world.Entities[0].Values.ContainsKey("skyname"))
                {
                    string skyname = world.Entities[0].Values["skyname"];
                    skybox = new SkyBox(this, skyname, light_environment);
                }
            }

            // Make leafs point towards nodes also
            SetParent(ref world.nodes[0], ref world.nodes[0]);
            world.nodes[0].parent = null;

            // Prepare visibleRenderItems memorisation structure
            foreach (RenderItem item in items)
            {
                if (!visibleRenderItems.ContainsKey(item.material.MaterialID))
                {
                    visibleRenderItems.Add(item.material.MaterialID, new List<RenderItem>());
                    if (skybox3d != null)
                        skybox3d.visibleRenderItems.Add(item.material.MaterialID, new List<RenderItem>());
                }

            }
        }