private void DrawCharacter(string text, Vector3 origin, float w, float h, TextInfo info) { Positions.Clear(); Colors.Clear(); TextureCoordinates.Clear(); info.Offsets.Clear(); // CCW from top left var a = new Vector2(-w / 2, -h / 2); var b = new Vector2(-w / 2, h / 2); var c = new Vector2(w / 2, -h / 2); var d = new Vector2(w / 2, h / 2); var uv_a = new Vector2(0, 0); var uv_b = new Vector2(0, 1); var uv_c = new Vector2(1, 0); var uv_d = new Vector2(1, 1); // Create foreground data Positions.Add(info.Origin); Positions.Add(info.Origin); Positions.Add(info.Origin); Positions.Add(info.Origin); Colors.Add(FontColor); Colors.Add(FontColor); Colors.Add(FontColor); Colors.Add(FontColor); TextureCoordinates.Add(uv_b); TextureCoordinates.Add(uv_d); TextureCoordinates.Add(uv_a); TextureCoordinates.Add(uv_c); info.Offsets.Add(a); info.Offsets.Add(c); info.Offsets.Add(b); info.Offsets.Add(d); // Create background data Positions.Add(info.Origin); Positions.Add(info.Origin); Positions.Add(info.Origin); Positions.Add(info.Origin); Colors.Add(BackgroundColor); Colors.Add(BackgroundColor); Colors.Add(BackgroundColor); Colors.Add(BackgroundColor); TextureCoordinates.Add(uv_a); TextureCoordinates.Add(uv_a); TextureCoordinates.Add(uv_a); TextureCoordinates.Add(uv_a); info.Offsets.Add(a); info.Offsets.Add(c); info.Offsets.Add(b); info.Offsets.Add(d); }
private void DrawCharacter(Character character, Vector3 origin, float w, float h, float kerning, TextInfo info) { var cw = character.Bounds.Width; var ch = character.Bounds.Height; var cu = character.Bounds.Left; var cv = character.Bounds.Top; // CCW from top left var a = new Vector2(origin.X + kerning, origin.Y); var b = new Vector2(origin.X + kerning, origin.Y + ch); var c = new Vector2(origin.X + cw + kerning, origin.Y); var d = new Vector2(origin.X + cw + kerning, origin.Y + ch); var uv_a = new Vector2(cu / w, cv / h); var uv_b = new Vector2(cu / w, (cv + ch) / h); var uv_c = new Vector2((cu + cw) / w, cv / h); var uv_d = new Vector2((cu + cw) / w, (cv + ch) / h); Positions.Add(info.Origin); Positions.Add(info.Origin); Positions.Add(info.Origin); Positions.Add(info.Origin); Positions.Add(info.Origin); Positions.Add(info.Origin); var color = FontColor.ToColor4(); Colors.Add(color); Colors.Add(color); Colors.Add(color); Colors.Add(color); Colors.Add(color); Colors.Add(color); TextureCoordinates.Add(uv_b); TextureCoordinates.Add(uv_d); TextureCoordinates.Add(uv_a); TextureCoordinates.Add(uv_a); TextureCoordinates.Add(uv_d); TextureCoordinates.Add(uv_c); info.Offsets.Add(a); info.Offsets.Add(c); info.Offsets.Add(b); info.Offsets.Add(b); info.Offsets.Add(c); info.Offsets.Add(d); }
public override void DrawTexture() { Positions.Clear(); Colors.Clear(); TextureCoordinates.Clear(); mTextInfoOffsets = new List <Vector2>(4); var w = Width; var h = Height; // CCW from top left var a = new Vector2(-w / 2, -h / 2); var b = new Vector2(-w / 2, h / 2); var c = new Vector2(w / 2, -h / 2); var d = new Vector2(w / 2, h / 2); var uv_a = new Vector2(0, 0); var uv_b = new Vector2(0, 1); var uv_c = new Vector2(1, 0); var uv_d = new Vector2(1, 1); // Create foreground data Positions.Add(Center); Positions.Add(Center); Positions.Add(Center); Positions.Add(Center); Colors.Add(MaskColor); Colors.Add(MaskColor); Colors.Add(MaskColor); Colors.Add(MaskColor); TextureCoordinates.Add(uv_b); TextureCoordinates.Add(uv_d); TextureCoordinates.Add(uv_a); TextureCoordinates.Add(uv_c); mTextInfoOffsets.Add(a); mTextInfoOffsets.Add(c); mTextInfoOffsets.Add(b); mTextInfoOffsets.Add(d); UpdateBounds(); }
/// <summary> /// Loads an ascii format ply file. /// </summary> /// <param name="s"></param> private void Load_ascii(Stream s) { using (var reader = new StreamReader(s)) { while (!reader.EndOfStream) { var curline = reader.ReadLine(); string[] strarr = curline.Split(' '); if (curline == null) { //reader.Close(); } #region Heading //comment Line else if (strarr[0] == "comment" || strarr[0] == "format" || strarr[0] == "ply") { } //obj_info Line else if (strarr[0] == "obj_info") { //ObjectInformation.Add(strarr[1], double.Parse(strarr[2])); } //element Line else if (strarr[0] == "element") { /* Supported elements * vertex * face */ if (strarr[1] == "vertex") { VerticesNumber = int.Parse(strarr[2]); //Add the vertex element to the dictionary, including its contextual range elements_range.Add("vertex", new PLYElement(elementLines_Count + 1, elementLines_Count + VerticesNumber)); elementLines_Count += int.Parse(strarr[2]); //set the current element as a "vertex"element currentElement = PlyElements.vertex; } else if (strarr[1] == "face") { FacesNumber = int.Parse(strarr[2]); elements_range.Add("face", new PLYElement(elementLines_Count + 1, elementLines_Count + FacesNumber)); elementLines_Count += int.Parse(strarr[2]); currentElement = PlyElements.face; } else { int miscElementNumber = int.Parse(strarr[2]); elements_range.Add(strarr[1], new PLYElement(elementLines_Count, elementLines_Count + miscElementNumber)); elementLines_Count += int.Parse(strarr[2]); currentElement = PlyElements.none; } } //property Line else if (strarr[0] == "property") { //Ignore the numerical data type for now switch (currentElement) { case PlyElements.vertex: { int propInd = elements_range["vertex"].PropertyIndex; elements_range["vertex"].Property_with_Index.Add(strarr[2], propInd); //Increase the property index if there's an element which has/hasn't been supported. elements_range["vertex"].PropertyIndex += 1; if (strarr[2] == "nx" || strarr[2] == "ny" || strarr[2] == "nz") { elements_range["vertex"].ContainsNormals = true; } else if (strarr[2] == "s" || strarr[2] == "t") { elements_range["vertex"].ContainsTextureCoordinates = true; } break; } case PlyElements.face: { //nothing to do yet break; } default: break; } } else if (strarr[0] == "end_header") { //end info, begin number collection. elementLine_Current = 0; } #endregion /* We pick the elements and its properties by checking whether the current * element line is contained in the element range. * The picking occurs if the first character in the string indicates a number follows. */ else if (IsDigitChar(strarr[0][0]) == true) { elementLine_Current++; if (elements_range.ContainsKey("vertex")) { if (elements_range["vertex"].ContainsNumber(elementLine_Current) == true) { //Get vertices int x_Indx = elements_range["vertex"].Property_with_Index["x"]; int y_Indx = elements_range["vertex"].Property_with_Index["y"]; int z_Indx = elements_range["vertex"].Property_with_Index["z"]; Point3D pt = new Point3D() { X = double.Parse(strarr[x_Indx]), Y = double.Parse(strarr[y_Indx]), Z = double.Parse(strarr[z_Indx]) }; Vertices.Add(pt); if (elements_range["vertex"].ContainsNormals == true) { int nx_Indx = elements_range["vertex"].Property_with_Index["nx"]; int ny_Indx = elements_range["vertex"].Property_with_Index["ny"]; int nz_Indx = elements_range["vertex"].Property_with_Index["nz"]; Vector3D vect3d = new Vector3D { X = double.Parse(strarr[nx_Indx]), Y = double.Parse(strarr[ny_Indx]), Z = double.Parse(strarr[nz_Indx]) }; Normals.Add(vect3d); } if (elements_range["vertex"].ContainsTextureCoordinates == true) { int s_Indx = elements_range["vertex"].Property_with_Index["s"]; int t_Indx = elements_range["vertex"].Property_with_Index["t"]; Point texpt = new Point { X = double.Parse(strarr[s_Indx]), Y = double.Parse(strarr[t_Indx]), }; TextureCoordinates.Add(texpt); } } } if (elements_range.ContainsKey("face")) { if (elements_range["face"].ContainsNumber(elementLine_Current) == true) { List <int> facepos = new List <int>(); for (int i = 1; i <= int.Parse(strarr[0]); i++) { facepos.Add(int.Parse(strarr[i])); } Faces.Add(facepos.ToArray()); } } else { continue; } //should increase the elementLine_Current iff //the line contains element data. } else { continue; } } } }
private void GenerateCap(bool top, ref int index, ref List <List <int> > indexArray, ref float halfHeight) { // save the index of the first center vertex int centerIndexStart = index; Float3 uv = new(); Float3 vertex = new(); float radius = top ? radiusTop : radiusBottom; float sign = top ? 1.0f : -1.0f; // first we generate the center vertex data of the cap. // because the geometry needs one set of uvs per face, // we must generate a center vertex per face/segment for (int x = 1; x <= radialSegments; x++) { // vertex Positions.Add(new(0.0f, halfHeight * sign, 0.0f)); // normal Normals.Add(new(0.0f, sign, 0.0f)); // uv TextureCoordinates.Add(new(0.5f, 0.5f, 0.0f)); // increase index index++; } // save the index of the last center vertex int centerIndexEnd = index; // now we generate the surrounding vertices, normals and uvs for (int x = 0; x <= radialSegments; x++) { float u = (float)x / radialSegments; double theta = u * thetaLength + thetaStart; double cosTheta = Math.Cos(theta); double sinTheta = Math.Sin(theta); // vertex vertex.X = (float)(radius * sinTheta); vertex.Y = halfHeight * sign; vertex.Z = (float)(radius * cosTheta); Positions.Add(vertex); // normal Normals.Add(new(0.0f, sign, 0.0f)); // uv uv.X = (float)((cosTheta * 0.5) + 0.5); uv.Y = (float)((sinTheta * 0.5 * sign) + 0.5); TextureCoordinates.Add(uv); // increase index index++; } // generate indices for (int x = 0; x < radialSegments; x++) { int c = centerIndexStart + x; int i = centerIndexEnd + x; if (top) { // face top Indices.Add(c); Indices.Add(i + 1); Indices.Add(i); } else { // face bottom Indices.Add(c); Indices.Add(i); Indices.Add(i + 1); } } }
private void ReadObjFile(string pathObjFile) { using (var myStream = new FileStream(pathObjFile, FileMode.Open)) { var myReader = new StreamReader(myStream); var coord = new double[3]; var tri = new int[3]; var tex = new int[4]; var quad = new int[4]; string line; while ((line = myReader.ReadLine()) != null) { if (line != "") { line = line.Trim(' '); string[] array; switch (line[0]) { case 'v': { if (line[1] != 't' && line[1] != 'n') { line = line.Trim('v', ' '); array = line.Split(' '); for (var i = 0; i < 3; i++) { coord[i] = double.Parse(array[i], CultureInfo.InvariantCulture); } var v = new Vertex(coord[0], coord[1], coord[2]); Vertices.Add(v); } else { if (line[1] == 't') { line = line.Trim('t', 'v', ' '); array = line.Split(' '); for (var i = 0; i < 2; i++) { coord[i] = double.Parse(array[i], CultureInfo.InvariantCulture); } var v = new Vertex2D(coord[0], coord[1]); TextureCoordinates.Add(v); } } break; } case 'f': { line = line.Trim('f', ' '); array = line.Split(new[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries); if (array.Length == 9) { for (int i = 0, j = 0; i < 3 && j < array.Length; i++, j += 3) { tri[i] = int.Parse(array[j]); tex[i] = int.Parse(array[j + 1]); } var t = new Triangle(Vertices[tri[0] - 1], Vertices[tri[1] - 1], Vertices[tri[2] - 1], TextureCoordinates[tex[0] - 1], TextureCoordinates[tex[1] - 1], TextureCoordinates[tex[2] - 1]); Triangles.Add(t); } if (array.Length == 12) { for (int i = 0, j = 0; i < 4 && j < array.Length; i++, j += 3) { quad[i] = int.Parse(array[j]); tex[i] = int.Parse(array[j + 1]); } var q = new Quad(Vertices[quad[0] - 1], Vertices[quad[1] - 1], Vertices[quad[2] - 1], Vertices[quad[3] - 1], TextureCoordinates[tex[0] - 1], TextureCoordinates[tex[1] - 1], TextureCoordinates[tex[2] - 1], TextureCoordinates[tex[3] - 1]); Quads.Add(q); } } break; } } } } }
public void ReadJson(JsonReader reader) { int count = 0; while (reader.Read()) { if (reader.TokenType == JsonToken.EndObject) { break; } if (reader.TokenType == JsonToken.PropertyName) { switch ((string)reader.Value) { case nameof(Header): Header.ReadJson(reader); break; case nameof(ExtendedHeader): ExtendedHeader.ReadJson(reader); break; case nameof(UserDataEntries): while (reader.Read()) { if (reader.TokenType == JsonToken.StartArray) { continue; } if (reader.TokenType == JsonToken.EndArray) { break; } // TODO } break; case nameof(particleData): while (reader.Read()) { if (reader.TokenType == JsonToken.StartArray) { continue; } if (reader.TokenType == JsonToken.EndArray) { break; } // TODO } break; case nameof(NonUniformKeys): while (reader.Read()) { if (reader.TokenType == JsonToken.StartArray) { continue; } if (reader.TokenType == JsonToken.EndArray) { break; } NonUniformKeys.Add((float)(double)reader.Value); } break; case nameof(Attachpoints): while (reader.Read()) { if (reader.TokenType == JsonToken.StartArray) { continue; } if (reader.TokenType == JsonToken.EndArray) { break; } BrgAttachpoint a = new BrgAttachpoint(); a.ReadJson(reader); Attachpoints.Add(a); } break; case nameof(Vertices): reader.Read(); // StartArray while (reader.Read()) { if (reader.TokenType == JsonToken.EndArray) { break; } Vector3 v = reader.ReadAsVector3(); Vertices.Add(v); } break; case nameof(Normals): reader.Read(); // StartArray while (reader.Read()) { if (reader.TokenType == JsonToken.EndArray) { break; } Vector3 v = reader.ReadAsVector3(); Normals.Add(v); } break; case nameof(TextureCoordinates): reader.Read(); // StartArray while (reader.Read()) { if (reader.TokenType == JsonToken.EndArray) { break; } Vector3 v = reader.ReadAsVector3(); TextureCoordinates.Add(v); } break; case nameof(Colors): reader.Read(); // StartArray while (reader.Read()) { if (reader.TokenType == JsonToken.EndArray) { break; } Color4D c = new Color4D(); c.ReadJson(reader); Colors.Add(c); } break; case nameof(Faces): count = 0; reader.Read(); // StartArray while (reader.Read()) { if (reader.TokenType == JsonToken.EndArray) { break; } Face f; if (Faces.Count <= count) { f = new Face(); Faces.Add(f); } else { f = Faces[count]; } ++count; while (reader.Read()) { if (reader.TokenType == JsonToken.StartArray) { continue; } if (reader.TokenType == JsonToken.EndArray) { break; } f.Indices.Add((Int16)(Int64)reader.Value); } } break; case "FaceMaterials": count = 0; reader.Read(); // StartArray while (reader.Read()) { if (reader.TokenType == JsonToken.EndArray) { break; } Face f; if (Faces.Count <= count) { f = new Face(); Faces.Add(f); } else { f = Faces[count]; } ++count; f.MaterialIndex = (Int16)(Int64)reader.Value; } break; case nameof(MeshAnimations): reader.Read(); // StartArray while (reader.Read()) { if (reader.TokenType == JsonToken.EndArray) { break; } BrgMesh m = new BrgMesh(this.ParentFile); m.ReadJson(reader); MeshAnimations.Add(m); } break; default: throw new Exception("Unexpected property name!"); } } else if (reader.TokenType != JsonToken.StartObject) { throw new Exception("Unexpected token type! " + reader.TokenType); } } }
protected void AddVertex(Point3D position, Vector3D normal, Point2D textureCoordinate) { AddVertex(position, normal); TextureCoordinates.Add(textureCoordinate); }