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))); } }
public void UpdateMesh() { // copy mesh from srcModel, and apply lighting, etc.. if (srcModel == null) return; items.Clear(); int leaf = Renderer.Instance.SourceMap.FindLeaf(prop_t.LightingOrigin); CompressedLightCube ambient = Renderer.Instance.SourceMap.world.leafs[leaf].ambientLighting; Matrix matrix = Matrix.RotationYawPitchRoll(prop_t.Angles.X * (float)(Math.PI / 180f), -prop_t.Angles.Z * (float)(Math.PI / 180f), prop_t.Angles.Y * (float)(Math.PI / 180f)) * Matrix.Translation(prop_t.Origin.X, prop_t.Origin.Y, prop_t.Origin.Z); //Renderer.Instance.SourceMap. foreach (BodyPart part in srcModel.BodyParts) { foreach (Model model in part.Models) { MDLMesh mesh = model.GetLODMesh(0); foreach (RenderItem item in mesh.items) { // Here we have the vertex data RenderItem child = new RenderItem(this, item.material); VertexPositonNormalColorTexture[] v = new VertexPositonNormalColorTexture[item.verts.Count]; item.verts.CopyTo(v); for (int i = 0; i < v.Length; i++) { Vector4 temp = Vector3.Transform(v[i].Position, matrix); v[i].Position = new Vector3(temp.X, temp.Y, temp.Z); v[i].Normal = Vector3.TransformNormal(v[i].Normal, matrix); Vector3 col; bool inSolid = SourceParser.Instance.ComputeVertexLightFromSpericalSamples(v[i].Position, v[i].Normal, this, out col); // add in ambient light Vector3 normal = v[i].Normal; col += (normal.X * normal.X) * ambient.Color[(normal.X < 0.0f ? 1 : 0)]; col += (normal.Y * normal.Y) * ambient.Color[(normal.Y < 0.0f ? 1 : 0) + 2]; col += (normal.Z * normal.Z) * ambient.Color[(normal.Z < 0.0f ? 1 : 0) + 4]; col *= 255f; // Convert to RGBE8 // Determine the largest color component float maxComponent = Math.Max(Math.Max(col.X, col.Y), col.Z); if (maxComponent < 0.000125f) { v[i].Color = new Color4(0.5f, 0f, 0f, 0f).ToArgb(); continue; } // Round to the nearest integer exponent float fExp = (float)Math.Ceiling(Math.Log(maxComponent,2)); // Divide the components by the shared exponent Vector3 enc = col / (float)(Math.Pow(2, fExp)); // Store the shared exponent in the alpha channel float a = ((fExp + 128f) /255f); v[i].Color = new Color4(a, enc.X, enc.Y, enc.Z).ToArgb(); } child.verts = new List<VertexPositonNormalColorTexture>(v); child.vb = new HagsVertexBuffer(); int vertexBytes = v.Length * VertexPositonNormalColorTexture.SizeInBytes; child.vb.SetVB<VertexPositonNormalColorTexture>(v, vertexBytes, VertexPositonNormalColorTexture.Format, Usage.WriteOnly); child.nVerts = v.Length; child.DontOptimize = true; child.indices = item.indices; child.GenerateIndexBuffer(); items.Add(child); } } } int test = 2; }