void bezierVb_Created(object sender, EventArgs e) { bezierVbLock.WaitOne(); ReadOnlyCollection <Q3BspFace> faces = map.Faces; Q3BspFace face; GraphicsStream bezierVbStream = bezierVb.Lock(0, 0, LockFlags.Discard); int vertexBufferindex = 0; for (int faceIndex = 0; faceIndex < faces.Count; faceIndex++) { face = faces [faceIndex]; if (face.type == Q3BspFaceType.Patch) { Q3BspPatch patch = face.patch; if (patch != null) { for (int bezierIndex = 0; bezierIndex < patch.size; bezierIndex++) { Q3BezierPatch bezier = patch.bezier [bezierIndex]; bezier.baseVertexIndex = vertexBufferindex; for (uint vertex = 0; vertex < bezier.vertices.Length; vertex++) { Q3BspVertex q3v = bezier.vertices [vertex]; bezierVbStream.Write(new Q3VertexFormats.PositionNormalTexturedLightened( q3v.position, q3v.normal, q3v.texcoord [0], q3v.texcoord [1])); vertexBufferindex++; } } } } } bezierVb.Unlock(); bezierVbLock.Set(); }
void bezierIb_Created(object sender, EventArgs e) { bezierIbLock.WaitOne(); GraphicsStream bezierIbStream = bezierIb.Lock(0, 0, LockFlags.Discard); ReadOnlyCollection <Q3BspFace> faces = map.Faces; Q3BspFace face; int indexBufferindex = 0; for (int faceIndex = 0; faceIndex < faces.Count; faceIndex++) { face = faces [faceIndex]; if (face.type == Q3BspFaceType.Patch) { Q3BspPatch patch = face.patch; if (patch != null) { for (int bezierIndex = 0; bezierIndex < patch.size; bezierIndex++) { Q3BezierPatch bezier = patch.bezier [bezierIndex]; bezier.baseBufferIndex = indexBufferindex; for (uint index = 0; index < bezier.indices.Length; index++) { bezierIbStream.Write(bezier.indices [index]); indexBufferindex++; } } } } } bezierIb.Unlock(); bezierIbLock.Set(); }
public void FromUnsafe(Q3BspFaceUnsafe s, Q3BspVertex [] vertices) { texture = s.texture; effect = s.effect; type = ( Q3BspFaceType )s.type; vertex = s.vertex; n_vertexes = s.n_vertexes; meshvert = s.meshvert; n_meshverts = s.n_meshverts; n_triangles = s.n_meshverts / 3; lm_index = s.lm_index; lm_start = new int [2]; lm_size = new int [2]; lm_vecs = new Vector3 [2]; unsafe { lm_start [0] = s.lm_start [0]; lm_start [1] = s.lm_start [1]; lm_size [0] = s.lm_size [0]; lm_size [1] = s.lm_size [1]; lm_origin = new Vector3(s.lm_origin [0], s.lm_origin [2], s.lm_origin [1]); lm_vecs [0] = new Vector3(s.lm_vecs [0], s.lm_vecs [1], s.lm_vecs [2]); lm_vecs [1] = new Vector3(s.lm_vecs [3], s.lm_vecs [4], s.lm_vecs [5]); // swapping axes normal = new Vector3(s.normal [0], s.normal [2], s.normal [1]); width = s.size [0]; height = s.size [1]; } if (type == Q3BspFaceType.Patch) { Q3BspPatch q3patch = new Q3BspPatch(); int patch_size_x = (width - 1) / 2; int patch_size_y = (height - 1) / 2; int num_bezier_patches = patch_size_y * patch_size_x; q3patch.size = num_bezier_patches; q3patch.bezier = new Q3BezierPatch [q3patch.size]; int patchIndex = 0; int ii, n, j, nn; for (ii = 0, n = 0; n < patch_size_x; n++, ii = 2 * n) { for (j = 0, nn = 0; nn < patch_size_y; nn++, j = 2 * nn) { Q3BezierPatch bezier = new Q3BezierPatch(); for (int ctr = 0, index = 0; ctr < 3; ctr++) { int pos = ctr * width; int vIdx = vertex + ii + width * j + pos; bezier.controls [index++] = vertices [vIdx].Copy(); bezier.controls [index++] = vertices [vIdx + 1].Copy(); bezier.controls [index++] = vertices [vIdx + 2].Copy(); } bezier.Tessellate(5); q3patch.bezier [patchIndex] = bezier; patchIndex++; } } patch = q3patch; } }
private void DrawFace(int faceIndex) { ReadOnlyCollection <Q3BspFace> faces = map.Faces; Q3BspFace face = faces [faceIndex]; d3dDevice.SetTextureStageState(0, TextureStageStates.ColorOperation, ( int )TextureOperation.SelectArg1); if (texturingFlags == TexturingFlags.Nothing) { lastLightmapId = -1; lastTextureId = -1; SetShaderTexture(0, null); SetShaderTexture(1, null); d3dDevice.SetTexture(0, null); d3dDevice.SetTexture(1, null); } else { if (((texturingFlags & TexturingFlags.Textures) == TexturingFlags.Textures)) { if (lastTextureId != face.texture) { d3dDevice.SetTexture(0, textures [face.texture] != null ? textures [face.texture] : textureNotFound); SetShaderTexture(0, textures [face.texture] != null ? textures [face.texture] : textureNotFound); lastTextureId = face.texture; } } if (((texturingFlags & TexturingFlags.Lightmaps) == TexturingFlags.Lightmaps)) { if (face.lm_index < 0) { d3dDevice.SetTextureStageState(1, TextureStageStates.ColorOperation, ( int )TextureOperation.Disable); if (texturingFlags == TexturingFlags.Lightmaps) { d3dDevice.SetTextureStageState(0, TextureStageStates.ColorOperation, ( int )TextureOperation.Disable); } } else if (lastLightmapId != face.lm_index) { if (texturingFlags == TexturingFlags.Lightmaps) { d3dDevice.SetTextureStageState(1, TextureStageStates.ColorOperation, ( int )TextureOperation.SelectArg1); } else { d3dDevice.SetTextureStageState(1, TextureStageStates.ColorOperation, ( int )lightmapTexOp); } d3dDevice.SetTexture(1, lightmaps [face.lm_index]); SetShaderTexture(1, lightmaps [face.lm_index]); lastLightmapId = face.lm_index; } } else { d3dDevice.SetTextureStageState(1, TextureStageStates.ColorOperation, ( int )TextureOperation.Disable); } } switch (face.type) { case Q3BspFaceType.Mesh: case Q3BspFaceType.Polygon: ibLock.WaitOne(); vbLock.WaitOne(); if (!ib.Disposed && !vb.Disposed) { if (ibNeedSet) { d3dDevice.SetStreamSource(0, vb, 0); d3dDevice.Indices = ib; ibNeedSet = false; } bezierIbNeedSet = true; try { d3dDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, face.vertex, 0, face.n_vertexes, face.meshvert, face.n_triangles); } catch {} } vbLock.Set(); ibLock.Set(); break; case Q3BspFaceType.Patch: bezierIbLock.WaitOne(); bezierVbLock.WaitOne(); if (!bezierIb.Disposed && !bezierVb.Disposed) { if (bezierIbNeedSet) { d3dDevice.SetStreamSource(0, bezierVb, 0); d3dDevice.Indices = bezierIb; bezierIbNeedSet = false; } ibNeedSet = true; for (int i = 0; i < face.patch.size; i++) { for (int j = 0; j < 5; j++) { Q3BezierPatch bezier = face.patch.bezier [i]; try { d3dDevice.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, bezier.baseVertexIndex, 0, bezier.vertices.Length, ( int )(bezier.baseBufferIndex + bezier.rowIndices [j]), ( int )(bezier.trisPerRow [j] - 2)); } catch {} } } } bezierVbLock.Set(); bezierIbLock.Set(); break; //case BILLBOARD: // break; default: break; } }