Ejemplo n.º 1
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>());
                }

            }
        }