public void Init() { Q3Map map = new Q3Map(mapPath); CreateDevice(); renderer = new Renderer(map, d3dDevice); renderer.OnResetDevice(); }
public Renderer(Q3Map map, Device d3dDevice) { bufferLocks [0] = ibLock; bufferLocks [1] = vbLock; bufferLocks [2] = bezierIbLock; bufferLocks [3] = bezierVbLock; this.d3dDevice = d3dDevice; this.Map = map; input = new Input(d3dDevice.PresentationParameters.DeviceWindow, DI.CooperativeLevelFlags.Background | DI.CooperativeLevelFlags.NonExclusive, DI.CooperativeLevelFlags.Background | DI.CooperativeLevelFlags.NonExclusive); int w = d3dDevice.PresentationParameters.BackBufferWidth; int h = d3dDevice.PresentationParameters.BackBufferHeight; fpsCamera = new FpsCamera(input, ( float )w / ( float )h, 105.0f * ( float )System.Math.PI / 360.0f, 1.0f, 10000.0f); List <Dictionary <string, object> > respawns; if (map.GroupedEntities.TryGetValue("info_player_deathmatch", out respawns)) { Random rnd = new Random(); int respIdx = rnd.Next(respawns.Count); Dictionary <string, object> props = respawns [respIdx]; Vector3 respPos = ( Vector3 )props ["origin"]; //float angle = ( float ) props ["angle"]; fpsCamera.Position = respPos; } // Create Vertex Buffer vb = new VertexBuffer(typeof(Q3VertexFormats.PositionNormalTexturedLightened), map.Vertices.Count, d3dDevice, Usage.WriteOnly, Q3VertexFormats.PositionNormalTexturedLightened.Format, Pool.Default); vb.Created += new EventHandler(vb_Created); vb_Created(this, null); // Create Index Buffer ib = new IndexBuffer(typeof(int), map.MeshVertices.Count, d3dDevice, Usage.WriteOnly, Pool.Default); ib.Created += new EventHandler(ib_Created); ib_Created(this, null); // Calculate Bezier Index Buffer size and ViertexBuffer Size int numIndex = 0; int numVertex = 0; ReadOnlyCollection <Q3BspFace> faces = map.Faces; Q3BspFace face; for (int i = 0; i < faces.Count; i++) { face = faces [i]; if (face.type == Q3BspFaceType.Patch) { Q3BspPatch patch = face.patch; if (patch != null) { for (int j = 0; j < patch.size; j++) { numIndex += patch.bezier [j].indices.Length; numVertex += patch.bezier [j].vertices.Length; } } } } if (numIndex != 0 && numVertex != 0) { // Create bezier buffers bezierIb = new IndexBuffer(typeof(int), numIndex, d3dDevice, Usage.WriteOnly, Pool.Default); bezierIb.Created += new EventHandler(bezierIb_Created); bezierIb_Created(this, null); bezierVb = new VertexBuffer(typeof(Q3VertexFormats.PositionNormalTexturedLightened), numVertex, d3dDevice, Usage.WriteOnly, Q3VertexFormats.PositionNormalTexturedLightened.Format, Pool.Default); bezierVb.Created += new EventHandler(bezierVb_Created); bezierVb_Created(this, null); } font = new Font(d3dDevice, new SysDraw.Font("Courier New", fontSize, fontStyle)); }