public unsafe SkyBox(Device dev) { device = dev; // sqrt(3)/3 const float l = 1f / MathEx.Root3; vtxDecl = new VertexDeclaration(device, D3DX.DeclaratorFromFVF(SkyVertex.Format)); box = new VertexBuffer(dev, sizeof(SkyVertex) * 8, Usage.WriteOnly, VertexPT1.Format, Pool.Managed); SkyVertex *dst = (SkyVertex *)box.Lock(0, 0, LockFlags.None).DataPointer.ToPointer(); dst[0] = new SkyVertex { pos = new Vector3(-50f, -50f, -50f), texCoord = new Vector3(-l, -l, -l) }; dst[1] = new SkyVertex { pos = new Vector3(50f, -50f, -50f), texCoord = new Vector3(l, -l, -l) }; dst[2] = new SkyVertex { pos = new Vector3(-50f, -50f, 50f), texCoord = new Vector3(-l, -l, l) }; dst[3] = new SkyVertex { pos = new Vector3(50f, -50f, 50f), texCoord = new Vector3(l, -l, l) }; dst[4] = new SkyVertex { pos = new Vector3(-50f, 50f, -50f), texCoord = new Vector3(-l, l, -l) }; dst[5] = new SkyVertex { pos = new Vector3(50f, 50f, -50f), texCoord = new Vector3(l, l, -l) }; dst[6] = new SkyVertex { pos = new Vector3(-50f, 50f, 50f), texCoord = new Vector3(-l, l, l) }; dst[7] = new SkyVertex { pos = new Vector3(50f, 50, 50f), texCoord = new Vector3(l, l, l) }; box.Unlock(); indexBuffer = new IndexBuffer(dev, sizeof(ushort) * 36, Usage.WriteOnly, Pool.Managed, true); ushort *ibDst = (ushort *)indexBuffer.Lock(0, 0, LockFlags.None).DataPointer.ToPointer(); ibDst[0] = 0; ibDst[1] = 1; ibDst[2] = 3; ibDst[3] = 0; ibDst[4] = 3; ibDst[5] = 2; ibDst[6] = 0; ibDst[7] = 4; ibDst[8] = 5; ibDst[9] = 0; ibDst[10] = 5; ibDst[11] = 1; ibDst[12] = 2; ibDst[13] = 6; ibDst[14] = 4; ibDst[15] = 2; ibDst[16] = 4; ibDst[17] = 0; ibDst[18] = 3; ibDst[19] = 7; ibDst[20] = 6; ibDst[21] = 3; ibDst[22] = 6; ibDst[23] = 2; ibDst[24] = 1; ibDst[25] = 5; ibDst[26] = 7; ibDst[27] = 1; ibDst[28] = 7; ibDst[29] = 3; ibDst[30] = 6; ibDst[31] = 7; ibDst[32] = 5; ibDst[33] = 6; ibDst[34] = 5; ibDst[35] = 4; //ibDst[0] = 0; //ibDst[1] = 1; //ibDst[2] = 3; //ibDst[3] = 0; //ibDst[4] = 2; //ibDst[5] = 3; //ibDst[6] = 4; //ibDst[7] = 5; //ibDst[8] = 7; //ibDst[9] = 4; //ibDst[10] = 6; //ibDst[11] = 7; //ibDst[12] = 0; //ibDst[13] = 1; //ibDst[14] = 4; //ibDst[15] = 1; //ibDst[16] = 4; //ibDst[17] = 5; //ibDst[18] = 0; //ibDst[19] = 4; //ibDst[20] = 2; //ibDst[21] = 4; //ibDst[22] = 6; //ibDst[23] = 2; //ibDst[24] = 1; //ibDst[25] = 3; //ibDst[26] = 5; //ibDst[27] = 5; //ibDst[28] = 7; //ibDst[29] = 3; //ibDst[30] = 2; //ibDst[31] = 3; //ibDst[32] = 6; //ibDst[33] = 2; //ibDst[34] = 7; //ibDst[35] = 3; indexBuffer.Unlock(); FileLocation fl = FileSystem.Instance.Locate(FileSystem.CombinePath(Paths.Effects, "DayNight.fx"), FileLocateRules.Default); ContentStreamReader sr = new ContentStreamReader(fl); string code = sr.ReadToEnd(); string err; effect = Effect.FromString(device, code, null, IncludeHandler.Instance, null, ShaderFlags.None, null, out err); effect.Technique = new EffectHandle("DayNight"); nightAlpha = new EffectHandle("nightAlpha"); day = new EffectHandle("Day"); night = new EffectHandle("Night"); }
private void genVertices(int phiSteps, int thetaSteps) { int nVerts = 3 * (phiSteps - thetaSteps + 1); // triangle fan triStrips = new int[thetaSteps]; this.phiSteps = phiSteps; this.thetaSteps = thetaSteps; int phiRes = phiSteps - thetaSteps + 1; for(int i=0; i<thetaSteps; i++) { nVerts += 2*(phiRes+1) + 1; // triangle strip phiRes++; triStrips[i] = 2*(phiRes+1) - 1; } vertexDecl = new VertexDeclaration(SkyVertex.VertexElements); SkyVertex[] verts = new SkyVertex[nVerts]; int idx = 0; int res = phiSteps - thetaSteps + 1; float phiStep = 2 * (float)Math.PI / (float)res; float thetaStep = 0.5f * (float)Math.PI / (float)thetaSteps; float phi = 0; for(int i=0; i<res; i++) // triangle fan { verts[idx++] = new SkyVertex(0, 1, 0, phi, 0); // triangle fan verts[idx++] = new SkyVertex(phi, thetaStep); verts[idx++] = new SkyVertex(phi + phiStep, thetaStep); phi += phiStep; } phiRes = phiSteps - thetaSteps + 1; float theta = thetaStep; for(int i=0; i<thetaSteps-1; i++) // triangle strips { float phiDown = 0; float phiUp = 0; for(int j=0; j<phiRes+1; j++) { verts[idx++] = new SkyVertex(phiDown, theta + thetaStep); verts[idx++] = new SkyVertex(phiUp, theta); phiDown += 2 * (float)Math.PI / (float)(phiRes+1); phiUp += 2 * (float)Math.PI / (float)phiRes; } verts[idx++] = new SkyVertex(2 * (float)Math.PI, theta + thetaStep); theta += thetaStep; phiRes++; } vBuffer = new VertexBuffer(device, vertexDecl, nVerts, BufferUsage.WriteOnly); vBuffer.SetData(verts); }