/// <summary> /// Actualiza la posicion de los vertices que componen las tapas /// </summary> private void updateDraw() { if (this.vertices == null) this.vertices = new CustomVertex.PositionColored[END_CAPS_VERTEX_COUNT + 4]; //4 para los bordes laterales int color = this.color.ToArgb(); Vector3 zeroVector = this.center; //matriz que vamos a usar para girar el vector de dibujado float angle = FastMath.TWO_PI / (float)END_CAPS_RESOLUTION; Vector3 upVector = this.halfHeight; Matrix rotationMatrix = Matrix.RotationAxis(new Vector3(0, 1, 0), angle); //vector de dibujado Vector3 n = new Vector3(this.radius, 0, 0); //array donde guardamos los puntos dibujados Vector3[] draw = new Vector3[END_CAPS_VERTEX_COUNT]; for (int i = 0; i < END_CAPS_VERTEX_COUNT / 2; i += 2) { //vertice inicial de la tapa superior draw[i] = zeroVector + upVector + n; //vertice inicial de la tapa inferior draw[END_CAPS_VERTEX_COUNT / 2 + i] = zeroVector - upVector + n; //rotamos el vector de dibujado n.TransformNormal(rotationMatrix); //vertice final de la tapa superior draw[i + 1] = zeroVector + upVector + n; //vertice final de la tapa inferior draw[END_CAPS_VERTEX_COUNT / 2 + i + 1] = zeroVector - upVector + n; } for (int i = 0; i < END_CAPS_VERTEX_COUNT; i++) this.vertices[i] = new CustomVertex.PositionColored(draw[i], color); }
private void updateDraw() { //vectores utilizados para el dibujado Vector3 upVector = new Vector3(0, 1, 0); Vector3 n = new Vector3(1, 0, 0); int capsResolution = END_CAPS_RESOLUTION; //matriz de rotacion del vector de dibujado float angleStep = FastMath.TWO_PI / (float)capsResolution; Matrix rotationMatrix = Matrix.RotationAxis(-upVector, angleStep); float angle = 0; //transformacion que se le aplicara a cada vertice Matrix transformation = this.Transform; float bcInverseRadius = 1 / this.boundingCylinder.Radius; //arrays donde guardamos los puntos dibujados Vector3[] topCapDraw = new Vector3[capsResolution]; Vector3[] bottomCapDraw = new Vector3[capsResolution]; //variables temporales utilizadas para el texture mapping float u; for (int i = 0; i < capsResolution; i++) { //establecemos los vertices de las tapas topCapDraw[i] = upVector + (n * this.topRadius * bcInverseRadius); bottomCapDraw[i] = -upVector + (n * this.bottomRadius * bcInverseRadius); u = angle / FastMath.TWO_PI; //triangulos de la cara lateral (strip) this.sideTrianglesVertices[2 * i] = new CustomVertex.PositionColoredTextured(topCapDraw[i], color, u, 0); this.sideTrianglesVertices[2 * i + 1] = new CustomVertex.PositionColoredTextured(bottomCapDraw[i], color, u, 1); //rotamos el vector de dibujado n.TransformNormal(rotationMatrix); angle += angleStep; } //cerramos la cara lateral this.sideTrianglesVertices[2 * capsResolution] = new CustomVertex.PositionColoredTextured(topCapDraw[0], color, 1, 0); this.sideTrianglesVertices[2 * capsResolution + 1] = new CustomVertex.PositionColoredTextured(bottomCapDraw[0], color, 1, 1); }
/// <summary> /// Actualiza la posicion de los vertices que componen las tapas /// </summary> private void updateDraw() { if (this.vertices == null) this.vertices = new CustomVertex.PositionColored[END_CAPS_VERTEX_COUNT + 4]; //4 para los bordes laterales int color = this.color.ToArgb(); //matriz que vamos a usar para girar el vector de dibujado float angle = FastMath.TWO_PI / (float)END_CAPS_RESOLUTION; Vector3 upVector = new Vector3(0, 1, 0); Matrix rotationMatrix = Matrix.RotationAxis(upVector, angle); //vector de dibujado Vector3 n = new Vector3(1, 0, 0); //array donde guardamos los puntos dibujados Vector3[] draw = new Vector3[this.vertices.Length]; for (int i = 0; i < END_CAPS_VERTEX_COUNT / 2; i += 2) { //vertice inicial de la tapa superior draw[i] = upVector + n; //vertice inicial de la tapa inferior draw[END_CAPS_VERTEX_COUNT / 2 + i] = -upVector + n; //rotamos el vector de dibujado n.TransformNormal(rotationMatrix); //vertice final de la tapa superior draw[i + 1] = upVector + n; //vertice final de la tapa inferior draw[END_CAPS_VERTEX_COUNT / 2 + i + 1] = -upVector + n; } //rotamos y trasladamos los puntos, y los volcamos al vector de vertices Matrix transformation = this.transformation; for (int i = 0; i < END_CAPS_VERTEX_COUNT; i++) this.vertices[i] = new CustomVertex.PositionColored(Vector3.TransformCoordinate(draw[i], transformation), color); }
/// <summary> /// The extract model as single mesh. /// </summary> /// <param name="path">The path.</param> /// <remarks></remarks> public void ExtractModelAsSingleMesh(string path) { int ndex = path.LastIndexOf('\\'); string f**k = path.Remove(ndex + 1); string texturepath = f**k + "Textures\\"; Directory.CreateDirectory(texturepath); List<string> names = new List<string>(); string mtllib = this.Name + ".mtl"; FileStream FS = new FileStream(f**k + mtllib, FileMode.Create); StreamWriter SW = new StreamWriter(FS); Panel p = new Panel(); Renderer r = new Renderer(); r.CreateDevice(p); for (int x = 0; x < this.Shaders.Shader.Length; x++) { string[] namesplit = this.Shaders.Shader[x].shaderName.Split('\\'); string temps = namesplit[namesplit.Length - 1]; names.Add(temps); this.Shaders.Shader[x].MakeTextures(ref r.device); TextureLoader.Save( texturepath + temps + ".dds", ImageFileFormat.Dds, this.Shaders.Shader[x].MainTexture); if (x > 0) { SW.WriteLine(string.Empty); } SW.WriteLine("newmtl " + temps); SW.WriteLine("Ka 1.000000 1.000000 1.000000"); SW.WriteLine("Kd 1.000000 1.000000 1.000000"); SW.WriteLine("Ks 1.000000 1.000000 1.000000"); SW.WriteLine("Ns 0.000000"); SW.WriteLine(@"map_Kd .\\Textures\\" + temps + ".dds"); } SW.Close(); FS.Close(); #region ExportBSPMeshes FS = new FileStream(path, FileMode.Create); SW = new StreamWriter(FS); SW.WriteLine("# ------------------------------------"); SW.WriteLine("# Halo 2 BSP Mesh - Extracted with Entity"); SW.WriteLine("# ------------------------------------"); SW.WriteLine("mtllib " + mtllib); int vertcount = 0; for (int x = 0; x < this.BSPRawDataMetaChunks.Length; x++) { if (this.BSPRawDataMetaChunks[x].VerticeCount == 0) { continue; } for (int y = 0; y < this.BSPRawDataMetaChunks[x].VerticeCount; y++) { string temps = "v " + this.BSPRawDataMetaChunks[x].Vertices[y].X.ToString("R") + " " + this.BSPRawDataMetaChunks[x].Vertices[y].Y.ToString("R") + " " + this.BSPRawDataMetaChunks[x].Vertices[y].Z.ToString("R"); SW.WriteLine(temps); } SW.WriteLine("# " + this.BSPRawDataMetaChunks[x].Vertices.Count + " vertices"); for (int y = 0; y < this.BSPRawDataMetaChunks[x].VerticeCount; y++) { string temps = "vt " + this.BSPRawDataMetaChunks[x].UVs[y].X.ToString("R") + " " + (1 - this.BSPRawDataMetaChunks[x].UVs[y].Y).ToString("R"); SW.WriteLine(temps); } SW.WriteLine("# " + this.BSPRawDataMetaChunks[x].Vertices.Count + " texture vertices"); for (int y = 0; y < this.BSPRawDataMetaChunks[x].VerticeCount; y++) { string temps = "vn " + this.BSPRawDataMetaChunks[x].Normals[y].X.ToString("R") + " " + this.BSPRawDataMetaChunks[x].Normals[y].Y.ToString("R") + " " + this.BSPRawDataMetaChunks[x].Normals[y].Z.ToString("R"); SW.WriteLine(temps); } SW.WriteLine("# " + this.BSPRawDataMetaChunks[x].Vertices.Count + " normals"); for (int y = 0; y < this.BSPRawDataMetaChunks[x].SubMeshInfo.Length; y++) { SW.WriteLine("g " + x + "." + y); // SW.WriteLine("s "+x.ToString()+"." + y.ToString()); SW.WriteLine("usemtl " + names[this.BSPRawDataMetaChunks[x].SubMeshInfo[y].ShaderNumber]); // int[] s***e = new int[100000]; int[] s***e = new int[this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount]; int s = 0; if (this.BSPRawDataMetaChunks[x].FaceCount * 3 != this.BSPRawDataMetaChunks[x].IndiceCount) { int m = this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart; bool dir = false; short tempx; short tempy; short tempz; do { // if (mode.EndOfIndices[x][j]>m+2){break;} tempx = this.BSPRawDataMetaChunks[x].Indices[m]; tempy = this.BSPRawDataMetaChunks[x].Indices[m + 1]; tempz = this.BSPRawDataMetaChunks[x].Indices[m + 2]; if (tempx != tempy && tempx != tempz && tempy != tempz) { if (dir == false) { s***e[s] = vertcount + tempx; s***e[s + 1] = vertcount + tempy; s***e[s + 2] = vertcount + tempz; s += 3; dir = true; } else { s***e[s] = vertcount + tempx; s***e[s + 1] = vertcount + tempz; s***e[s + 2] = vertcount + tempy; s += 3; dir = false; } m += 1; } else { if (dir) { dir = false; } else { dir = true; } m += 1; } } while (m < this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart + this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount - 2); } else { for (int u = 0; u < this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount; u++) { s***e[u] = vertcount + this.BSPRawDataMetaChunks[x].Indices[ this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart + u]; } s = this.BSPRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount; } for (int xx = 0; xx < s; xx += 3) { string temps = "f " + (s***e[xx] + 1) + "/" + (s***e[xx] + 1) + "/" + (s***e[xx] + 1) + " " + (s***e[xx + 1] + 1) + "/" + (s***e[xx + 1] + 1) + "/" + (s***e[xx + 1] + 1) + " " + (s***e[xx + 2] + 1) + "/" + (s***e[xx + 2] + 1) + "/" + (s***e[xx + 2] + 1); SW.WriteLine(temps); } SW.WriteLine("# " + (s / 3) + " elements"); } vertcount += this.BSPRawDataMetaChunks[x].VerticeCount; } #endregion SW.Close(); FS.Close(); #region ExportBSPPermutationMeshes FS = new FileStream(path.Substring(0, path.LastIndexOf('.')) + "-permutations.obj", FileMode.Create); SW = new StreamWriter(FS); SW.WriteLine("# ------------------------------------"); SW.WriteLine("# Halo 2 BSP Permutation Mesh"); SW.WriteLine("# ------------------------------------"); SW.WriteLine("mtllib " + mtllib); vertcount = 0; for (int tx = 0; tx < this.PermutationInfo.Length; tx++) { int x = this.PermutationInfo[tx].sceneryIndex; if ((this.BSPPermutationRawDataMetaChunks[x].RawDataChunkInfo.Length == 0) || (this.BSPPermutationRawDataMetaChunks[x].VerticeCount == 0)) { continue; } for (int y = 0; y < this.BSPPermutationRawDataMetaChunks[x].VerticeCount; y++) { Vector3 tv3 = new Vector3( this.BSPPermutationRawDataMetaChunks[x].Vertices[y].X, this.BSPPermutationRawDataMetaChunks[x].Vertices[y].Y, this.BSPPermutationRawDataMetaChunks[x].Vertices[y].Z); tv3.TransformCoordinate(this.PermutationInfo[tx].mat); string temps = "v " + tv3.X.ToString("R") + " " + tv3.Y.ToString("R") + " " + tv3.Z.ToString("R"); SW.WriteLine(temps); } SW.WriteLine("# " + this.BSPPermutationRawDataMetaChunks[x].Vertices.Count + " vertices"); for (int y = 0; y < this.BSPPermutationRawDataMetaChunks[x].VerticeCount; y++) { string temps = "vt " + this.BSPPermutationRawDataMetaChunks[x].UVs[y].X.ToString("R") + " " + this.BSPPermutationRawDataMetaChunks[x].UVs[y].Y.ToString("R"); SW.WriteLine(temps); } SW.WriteLine("# " + this.BSPPermutationRawDataMetaChunks[x].Vertices.Count + " texture vertices"); for (int y = 0; y < this.BSPPermutationRawDataMetaChunks[x].VerticeCount; y++) { Vector3 tv3 = new Vector3( this.BSPPermutationRawDataMetaChunks[x].Normals[y].X, this.BSPPermutationRawDataMetaChunks[x].Normals[y].Y, this.BSPPermutationRawDataMetaChunks[x].Normals[y].Z); tv3.TransformNormal(this.PermutationInfo[tx].mat); string temps = "vn " + tv3.X.ToString("R") + " " + tv3.Y.ToString("R") + " " + tv3.Z.ToString("R"); /* string temps = "vn " + this.BSPPermutationRawDataMetaChunks[x].Normals[y].X.ToString() + " " + this.BSPPermutationRawDataMetaChunks[x].Normals[y].Y.ToString() + " " + this.BSPPermutationRawDataMetaChunks[x].Normals[y].Z.ToString(); */ SW.WriteLine(temps); } SW.WriteLine("# " + this.BSPPermutationRawDataMetaChunks[x].Vertices.Count + " normals"); for (int y = 0; y < this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo.Length; y++) { SW.WriteLine("g " + tx + "." + y); SW.WriteLine( "usemtl " + names[this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].ShaderNumber]); // int[] s***e = new int[100000]; int[] s***e = new int[this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount]; int s = 0; if (this.BSPPermutationRawDataMetaChunks[x].FaceCount * 3 != this.BSPPermutationRawDataMetaChunks[x].IndiceCount) { int m = this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart; bool dir = false; short tempx; short tempy; short tempz; do { // if (mode.EndOfIndices[x][j]>m+2){break;} tempx = this.BSPPermutationRawDataMetaChunks[x].Indices[m]; tempy = this.BSPPermutationRawDataMetaChunks[x].Indices[m + 1]; tempz = this.BSPPermutationRawDataMetaChunks[x].Indices[m + 2]; if (tempx != tempy && tempx != tempz && tempy != tempz) { if (dir == false) { s***e[s] = vertcount + tempx; s***e[s + 1] = vertcount + tempy; s***e[s + 2] = vertcount + tempz; s += 3; dir = true; } else { s***e[s] = vertcount + tempx; s***e[s + 1] = vertcount + tempz; s***e[s + 2] = vertcount + tempy; s += 3; dir = false; } m += 1; } else { if (dir) { dir = false; } else { dir = true; } m += 1; } } while (m < this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart + this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount - 2); } else { for (int u = 0; u < this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount; u++) { s***e[u] = vertcount + this.BSPPermutationRawDataMetaChunks[x].Indices[ this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceStart + u]; } s = this.BSPPermutationRawDataMetaChunks[x].SubMeshInfo[y].IndiceCount; } for (int xx = 0; xx < s; xx += 3) { string temps = "f " + (s***e[xx] + 1) + "/" + (s***e[xx] + 1) + "/" + (s***e[xx] + 1) + " " + (s***e[xx + 1] + 1) + "/" + (s***e[xx + 1] + 1) + "/" + (s***e[xx + 1] + 1) + " " + (s***e[xx + 2] + 1) + "/" + (s***e[xx + 2] + 1) + "/" + (s***e[xx + 2] + 1); SW.WriteLine(temps); } SW.WriteLine("# " + (s / 3) + " elements"); } vertcount += this.BSPPermutationRawDataMetaChunks[x].VerticeCount; } SW.Close(); FS.Close(); #endregion }
public override int Intersect( Vector3 rayPosition, Vector3 rayDirection, ref Object3DCommon obj, Matrix worldSpace ) { Matrix myModelSpace = scalingMatrix * rotationMatrix * translationMatrix * worldSpace; // transform world space to object space Vector3 pickRayOriginTemp = new Vector3(rayPosition.X, rayPosition.Y, rayPosition.Z); Vector3 pickRayDirectionTemp = new Vector3(rayDirection.X,rayDirection.Y,rayDirection.Z); // convert ray from 3d space to model space pickRayOriginTemp.TransformCoordinate(Matrix.Invert(myModelSpace)); pickRayDirectionTemp.TransformNormal (Matrix.Invert(myModelSpace)); // check to see if I intersect int zDistance = CheckIntersect(pickRayOriginTemp,pickRayDirectionTemp); if (zDistance > 0) { obj = this; } if (_children != null) { foreach (Object3DCommon childObj in _children) { int newZDistance = childObj.Intersect(rayPosition, rayDirection, ref obj, myModelSpace); if (newZDistance > zDistance) { zDistance = newZDistance; } } } return zDistance; }
private void updateDraw() { //vectores utilizados para el dibujado Vector3 upVector = new Vector3(0, 1, 0); Vector3 n = new Vector3(1, 0, 0); int capsResolution = END_CAPS_RESOLUTION; //matriz de rotacion del vector de dibujado float angleStep = FastMath.TWO_PI / (float)capsResolution; Matrix rotationMatrix = Matrix.RotationAxis(-upVector, angleStep); float angle = 0; //transformacion que se le aplicara a cada vertice Matrix transformation = this.Transform; //arrays donde guardamos los puntos dibujados Vector3[] topCapDraw = new Vector3[capsResolution]; Vector3[] bottomCapDraw = new Vector3[capsResolution]; //Vector3 topCapCenter = Vector3.TransformCoordinate(upVector, transformation); //Vector3 bottomCapCenter = Vector3.TransformCoordinate(-upVector, transformation); Vector3 topCapCenter = upVector; Vector3 bottomCapCenter = -upVector; for (int i = 0; i < capsResolution; i++) { //establecemos los vertices de las tapas //topCapDraw[i] = Vector3.TransformCoordinate(upVector + n, transformation); //bottomCapDraw[i] = Vector3.TransformCoordinate(-upVector + n, transformation); topCapDraw[i] = upVector + n; bottomCapDraw[i] = -upVector + n; float u = angle / FastMath.TWO_PI; //triangulos de la cara lateral (strip) this.sideTrianglesVertices[2 * i] = new CustomVertex.PositionColoredTextured(topCapDraw[i], color, u, 0); this.sideTrianglesVertices[2 * i + 1] = new CustomVertex.PositionColoredTextured(bottomCapDraw[i], color, u, 1); //triangulos de la tapa superior (list) if (i > 0) this.capsTrianglesVertices[3 * i] = new CustomVertex.PositionColoredTextured(topCapDraw[i - 1], color, FastMath.Cos(angle - angleStep), FastMath.Sin(angle - angleStep)); this.capsTrianglesVertices[3 * i + 1] = new CustomVertex.PositionColoredTextured(topCapDraw[i], color, FastMath.Cos(angle), FastMath.Sin(angle)); this.capsTrianglesVertices[3 * i + 2] = new CustomVertex.PositionColoredTextured(topCapCenter, color, 0.5f, 0.5f); //triangulos de la tapa inferior (list) if (i > 0) this.capsTrianglesVertices[3 * i + 3 * capsResolution] = new CustomVertex.PositionColoredTextured(bottomCapDraw[i - 1], color, FastMath.Cos(angle - angleStep), FastMath.Sin(angle - angleStep)); this.capsTrianglesVertices[3 * i + 1 + 3 * capsResolution] = new CustomVertex.PositionColoredTextured(bottomCapDraw[i], color, FastMath.Cos(angle), FastMath.Sin(angle)); ; this.capsTrianglesVertices[3 * i + 2 + 3 * capsResolution] = new CustomVertex.PositionColoredTextured(bottomCapCenter, color, 0.5f, 0.5f); //rotamos el vector de dibujado n.TransformNormal(rotationMatrix); angle += angleStep; } //cerramos la cara lateral this.sideTrianglesVertices[2 * capsResolution] = new CustomVertex.PositionColoredTextured(topCapDraw[0], color, 1, 0); this.sideTrianglesVertices[2 * capsResolution + 1] = new CustomVertex.PositionColoredTextured(bottomCapDraw[0], color, 1, 1); //cerramos la tapa superior this.capsTrianglesVertices[0] = new CustomVertex.PositionColoredTextured(topCapDraw[capsResolution - 1], color, FastMath.Cos(-angleStep), FastMath.Sin(-angleStep)); //Cerramos la tapa inferior this.capsTrianglesVertices[3 * capsResolution] = new CustomVertex.PositionColoredTextured(bottomCapDraw[capsResolution - 1], color, FastMath.Cos(-angleStep), FastMath.Sin(-angleStep)); }