public void Read(BinaryReaderEx br) { ptrVertex = br.ReadUInt32(); unk1 = br.ReadUInt32(); unk2 = br.ReadUInt32(); color = new Vector4b(br); }
public Vertex(float value) { var color = Color.White; this.position = new Vector2(value); this.texCoords = new Vector2(value); this.color = new Vector4b(color.R, color.G, color.B, color.A); }
public Vertex(Vector2 position, Vector2 texCoord) { var color = Color.White; this.position = position; this.texCoords = texCoord; this.color = new Vector4b(color.R, color.G, color.B, color.A); }
public Mesh(Vector3[] positions, Vector4b[] colors, uint[] elements) { vao = arrangeData(positions, colors, elements); positions = null; colors = null; elements = null; GC.Collect(); }
public void SetColor(Vector4b col, Vcolor mode) { switch (mode) { case Vcolor.Default: color = col; break; case Vcolor.Morph: color_morph = col; break; } }
public Voxel(Vector4b[] colors, int width, int height, int depth) { this.width = width; this.height = height; this.depth = depth; voxels = colors; List<Vertex> vertices = new List<Vertex>(); }
public void SetColor(Vcolor mode, Vector4b col) { switch (mode) { case Vcolor.Default: color = col; break; case Vcolor.Morph: color_morph = col; break; //case Vcolor.Flag: Vertex.flagColor = col; break; } }
public void Write(BinaryWriterEx bw) { coord.Write(bw); color.Write(bw); if (color_morph == null) { color_morph = new Vector4b(0, 0, 0, 0); } color_morph.Write(bw); }
/// <summary> /// Initializes a new instance of the <see cref="Vector8b"/> using the specified vector and values. /// </summary> /// <param name="value">A vector containing the values with which to initialize the first 4 components</param> /// <param name="v4">Value for the V4 component of the vector.</param> /// <param name="v5">Value for the V5 component of the vector.</param> /// <param name="v6">Value for the V6 component of the vector.</param> /// <param name="v7">Value for the V7 component of the vector.</param> public Vector8b(Vector4b value, byte v4, byte v5, byte v6, byte v7, byte v8, byte v9, byte v10, byte v11) { V0 = value.X; V1 = value.Y; V2 = value.Z; V3 = value.W; V4 = v4; V5 = v5; V6 = v6; V7 = v7; }
public Vector4b Read(int position, Vector4b fallback) { Vector4b o; o.Packed = 0; o.X = Read(position, fallback.X); o.Y = Read(position + 1, fallback.Y); o.Z = Read(position + 2, fallback.Z); o.W = Read(position + 3, fallback.Z); return(o); }
public Cube(Color color, Vector3 scale, GameObject parent) { PositionCount = 24; IndiceCount = 36; ColorCount = 24; colors = new Vector4b[ColorCount]; for (int i = 0; i < ColorCount; i++) { colors[i] = new Vector4b(color); } this.scale = scale; ParentGameObject = parent; }
/// <summary> /// Create the block vertex buffer. /// </summary> void CreateBlockVertexBuffer(GraphicsBuffer buffer, int offset) { Vector4b[] vertices = new Vector4b[BlockVertexCount]; for (int z = 0, i = 0; z < BlockVertexRow; z++) { int pz = Math.Max(0, Math.Min(BlockEdgeCount - 1, z - 1)); for (int x = 0; x < BlockVertexRow; x++) { int px = Math.Max(0, Math.Min(BlockEdgeCount - 1, x - 1)); int py = (x == 0 || z == 0 || x == BlockVertexRow - 1 || z == BlockVertexRow - 1) ? 0 : 1; vertices[i++] = new Vector4b((byte)px, (byte)py, (byte)pz, 0); } } buffer.Write(offset, vertices); }
void CreatePerlinPermutationTexture(byte[] permutations) { Vector4b[] data = new Vector4b[256 * 256]; for (int y = 0; y < 256; y++) { for (int x = 0; x < 256; x++) { int a = permutations[x] + y; int AA = permutations[a % 256]; int AB = permutations[(a + 1) % 256]; int B = permutations[(x + 1) % 256] + y; int BA = permutations[B % 256]; int BB = permutations[(B + 1) % 256]; data[x + y * 256] = new Vector4b((byte)AA, (byte)AB, (byte)BA, (byte)BB); } } var texture = new Texture2D(); texture.Data(256, 256, Formats.Vector4nb, data); Program.Uniforms["PerlinPermutationTexture"].Set(texture); }
private static Tuple<Vector3[], Vector4b[], uint[]> ParseZArray(ZArrayDescriptor desc, ColoringMethod coloring) { Vector3[,] vertexPositions = new Vector3[desc.width, desc.height]; Vector3[,] normals = new Vector3[desc.width, desc.height]; int z_max = 0, z_min = 0; for (int i = 0; i < desc.width; ++i) for (int j = 0; j < desc.height; ++j) { if (desc.array[i][j] > z_max) z_max = (int)desc.array[i][j]; if (desc.array[i][j] < z_min) z_min = (int)desc.array[i][j]; } int zCenterShift = (z_max + z_min) / 2; int xCenterShift = desc.width / 2; int yCenterShift = desc.height / 2; // this cycle to be optimized (?) for (int i = 0; i < desc.width - 1; ++i) { for (int j = 0; j < desc.height - 1; ++j) { int x = i - xCenterShift; int y = yCenterShift - j; vertexPositions[i + 1, j + 1] = new Vector3(x + 1, y + 1, desc.array[i + 1][j + 1] - zCenterShift); vertexPositions[i + 1, j] = new Vector3(x + 1, y, desc.array[i + 1][j] - zCenterShift); vertexPositions[i, j] = new Vector3(x, y, desc.array[i][j] - zCenterShift); vertexPositions[i, j + 1] = new Vector3(x, y + 1, desc.array[i][j + 1] - zCenterShift); vertexPositions[i + 1, j + 1] = new Vector3(x + 1, y + 1, desc.array[i + 1][j + 1] - zCenterShift); vertexPositions[i, j] = new Vector3(x, y, desc.array[i][j] - zCenterShift); Vector3 norm1 = Vector3.Cross( vertexPositions[i + 1, j + 1] - vertexPositions[i, j], vertexPositions[i + 1, j] - vertexPositions[i, j]).Normalized(); Vector3 norm2 = Vector3.Cross( vertexPositions[i, j + 1] - vertexPositions[i, j], vertexPositions[i + 1, j + 1] - vertexPositions[i, j]).Normalized(); Vector3.Add(ref normals[i, j], ref norm1, out normals[i, j]); Vector3.Add(ref normals[i + 1, j], ref norm1, out normals[i + 1, j]); Vector3.Add(ref normals[i + 1, j + 1], ref norm1, out normals[i + 1, j + 1]); Vector3.Add(ref normals[i, j], ref norm2, out normals[i, j]); Vector3.Add(ref normals[i, j + 1], ref norm2, out normals[i, j + 1]); Vector3.Add(ref normals[i + 1, j + 1], ref norm2, out normals[i + 1, j + 1]); } } // vertexes color calculated from average vertex normal Vector4b[] colors = new Vector4b[desc.width * desc.height]; uint ptr = 0; for (int i = 0; i < desc.width; ++i) { for (int j = 0; j < desc.height; ++j) { Vector3.Multiply(ref normals[i, j], 0.166666666f, out normals[i, j]); switch (coloring) { case ColoringMethod.Fullcolor: calcFullcolor(ref normals[i, j], ref colors[ptr++]); break; case ColoringMethod.Grayscale: calcGrayscale(ref normals[i, j], ref colors[ptr++]); break; } } } normals = null; GC.Collect(); // data arrangement Vector3[] positions = new Vector3[desc.width * desc.height]; uint[] elements = new uint[2 * (desc.width - 1) * (desc.height + 1)]; ptr = 0; for (int i = 0; i < desc.width; ++i) for (int j = 0; j < desc.height; ++j) positions[ptr++] = vertexPositions[i, j]; vertexPositions = null; GC.Collect(); ptr = 0; for (uint i = 0; i < desc.width - 1; ++i) { uint j; for (j = 0; j < desc.height; ++j) { elements[ptr++] = (uint)(i * desc.height + j); elements[ptr++] = (uint)((i + 1) * desc.height + j); } //elements[ptr++] = restartIndex; elements[ptr++] = (uint)((i + 1) * desc.height + j - 1); elements[ptr++] = (uint)((i + 1) * desc.height); } return new Tuple<Vector3[], Vector4b[], uint[]>(positions, colors, elements); }
public void ReadShort(BinaryReaderEx br) { coord = new Vector4s(br); color = new Vector4b(br); }
public void Read(BinaryReaderEx br) { coord = new Vector4s(br); color = new Vector4b(br); color_morph = new Vector4b(br); }
void CreatePerlinPermutationTexture(byte[] permutations) { Vector4b[] data = new Vector4b[256 * 256]; for (int y = 0; y < 256; y++) for (int x = 0; x < 256; x++) { int a = permutations[x] + y; int AA = permutations[a % 256]; int AB = permutations[(a + 1) % 256]; int B = permutations[(x + 1) % 256] + y; int BA = permutations[B % 256]; int BB = permutations[(B + 1) % 256]; data[x + y * 256] = new Vector4b((byte)AA, (byte)AB, (byte)BA, (byte)BB); } var texture = new Texture2D(); texture.Data(256, 256, Formats.Vector4nb, data); Program.Uniforms["PerlinPermutationTexture"].Set(texture); }
public void Read(BinaryReaderEx br) { coord = br.ReadVector3sPadded(1 / 100f); color = new Vector4b(br); color_morph = new Vector4b(br); }
void GetMeshData(int x, int y, int z) { int addedVertices = 0; int indiceOffset = vertices.Count; //Left if (IsSolid(x, y, z - 1)) { vertices.Add(new Vector3(x + 0, y + 0, z + 0)); vertices.Add(new Vector3(x + 1, y + 1, z + 0)); vertices.Add(new Vector3(x + 1, y + 0, z + 0)); vertices.Add(new Vector3(x + 0, y + 1, z + 0)); indices.AddRange(new int[] { indiceOffset + 0, indiceOffset + 1, indiceOffset + 2, indiceOffset + 0, indiceOffset + 3, indiceOffset + 1 }); normals.AddRange(new Vector3[] { -Vector3.UnitZ, -Vector3.UnitZ, -Vector3.UnitZ, -Vector3.UnitZ }); addedVertices += 4; indiceOffset += 4; } //Right if (IsSolid(x, y, z + 1)) { vertices.Add(new Vector3(x + 0, y + 0, z + 1)); vertices.Add(new Vector3(x + 1, y + 0, z + 1)); vertices.Add(new Vector3(x + 1, y + 1, z + 1)); vertices.Add(new Vector3(x + 0, y + 1, z + 1)); indices.AddRange(new int[] { indiceOffset + 0, indiceOffset + 1, indiceOffset + 2, indiceOffset + 0, indiceOffset + 2, indiceOffset + 3 }); normals.AddRange(new Vector3[] { Vector3.UnitZ, Vector3.UnitZ, Vector3.UnitZ, Vector3.UnitZ }); addedVertices += 4; indiceOffset += 4; } //Front if (IsSolid(x - 1, y, z)) { vertices.Add(new Vector3(x + 0, y + 0, z + 0)); vertices.Add(new Vector3(x + 0, y + 1, z + 1)); vertices.Add(new Vector3(x + 0, y + 1, z + 0)); vertices.Add(new Vector3(x + 0, y + 0, z + 1)); indices.AddRange(new int[] { indiceOffset + 0, indiceOffset + 1, indiceOffset + 2, indiceOffset + 0, indiceOffset + 3, indiceOffset + 1 }); normals.AddRange(new Vector3[] { -Vector3.UnitX, -Vector3.UnitX, -Vector3.UnitX, -Vector3.UnitX }); addedVertices += 4; indiceOffset += 4; } //Back if (IsSolid(x + 1, y, z)) { vertices.Add(new Vector3(x + 1, y + 0, z + 0)); vertices.Add(new Vector3(x + 1, y + 1, z + 0)); vertices.Add(new Vector3(x + 1, y + 1, z + 1)); vertices.Add(new Vector3(x + 1, y + 0, z + 1)); indices.AddRange(new int[] { indiceOffset + 0, indiceOffset + 1, indiceOffset + 2, indiceOffset + 0, indiceOffset + 2, indiceOffset + 3 }); normals.AddRange(new Vector3[] { Vector3.UnitX, Vector3.UnitX, Vector3.UnitX, Vector3.UnitX }); addedVertices += 4; indiceOffset += 4; } //Bottom if (IsSolid(x, y - 1, z)) { vertices.Add(new Vector3(x + 0, y + 0, z + 0)); vertices.Add(new Vector3(x + 1, y + 0, z + 0)); vertices.Add(new Vector3(x + 1, y + 0, z + 1)); vertices.Add(new Vector3(x + 0, y + 0, z + 1)); indices.AddRange(new int[] { indiceOffset + 0, indiceOffset + 1, indiceOffset + 2, indiceOffset + 0, indiceOffset + 2, indiceOffset + 3 }); normals.AddRange(new Vector3[] { -Vector3.UnitY, -Vector3.UnitY, -Vector3.UnitY, -Vector3.UnitY }); addedVertices += 4; indiceOffset += 4; } //Top if (IsSolid(x, y + 1, z)) { vertices.Add(new Vector3(x + 1, y + 1, z + 0)); vertices.Add(new Vector3(x + 0, y + 1, z + 0)); vertices.Add(new Vector3(x + 1, y + 1, z + 1)); vertices.Add(new Vector3(x + 0, y + 1, z + 1)); indices.AddRange(new int[] { indiceOffset + 1, indiceOffset + 2, indiceOffset + 0, indiceOffset + 1, indiceOffset + 3, indiceOffset + 2 }); normals.AddRange(new Vector3[] { Vector3.UnitY, Vector3.UnitY, Vector3.UnitY, Vector3.UnitY }); addedVertices += 4; indiceOffset += 4; } if (addedVertices > 0) { Vector4b voxelColor = VoxelManager.GetVoxel(parentChunk.voxels[x, y, z]).Color; for (int i = 0; i < addedVertices; i++) { colors.Add(voxelColor); } } }
/// <summary>Read an array of <c>Vector4b</c> values.</summary> public static Vector4b[] ReadArrayVector4b(this BinaryReader reader, int count) { Vector4b[] array = new Vector4b[count]; reader.ReadArray(array, 0, count); return array; }
/// <summary> /// Parses single line of OBJ file. /// </summary> /// <param name="line">OBJ line.</param> public void ParseLine(string line) { line = line.Split('#')[0]; if (line.Trim() == "") { Console.WriteLine("empty line"); return; } string[] words = line.Split(' '); if (words.Length == 0) { return; } if (words[0] == "o") { ObjectName = words[1]; Console.WriteLine("object name: " + line); return; } if (words[0] == "g") { Console.WriteLine("group name: " + line); return; } if (words[0] == "vn") { Console.WriteLine("vertex normal, skip: " + line); return; } if (words[0] == "vt") { Console.WriteLine("uv, skip: " + line); return; } if (words[0] == "v") { Console.WriteLine("it's a vertex! " + line); if (words.Length >= 4) { float[] coord = new float[3]; for (int i = 0; i < 3; i++) { Single.TryParse(words[i + 1], out coord[i]); } vertices.Add(new Vector3f(coord[0], coord[1], coord[2])); if (words.Length >= 7) { float[] color = new float[3]; for (int i = 0; i < 3; i++) { Single.TryParse(words[i + 3 + 1], out color[i]); } //assume color between 0..1 is float and multiply by 255 if ((color[0] > 1 || color[1] > 1 || color[2] > 1)) { color[0] /= 255; color[1] /= 255; color[2] /= 255; } Vector4b vv = new Vector4b((byte)(255 * color[0]), (byte)(255 * color[1]), (byte)(255 * color[2]), 0); colors.Add(vv); } } return; } if (words[0] == "f") { Console.WriteLine("it's a face! " + line); if (words.Length >= 4) { short[] coord = new short[3]; for (int i = 0; i < 3; i++) { Int16.TryParse(words[i + 1].Split('/')[0], out coord[i]); } faces.Add((short)(coord[0] - 1)); faces.Add((short)(coord[1] - 1)); faces.Add((short)(coord[2] - 1)); } return; } if (words[0] == "s") { Console.WriteLine("smoothing group, don't need. " + line); return; } if (words[0] == "usemtl") { Console.WriteLine("material, don't need. " + line); return; } if (words[0] == "mtllib") { Console.WriteLine("material lib, don't need. " + line); return; } Console.WriteLine("error or unimplemented obj command " + line); }
public static Vertex[] Mesh(Vector4b[] colors, int width, int height, int depth) { var voxel = new Voxel(colors, width, height, depth); return voxel.Mesh(); }
public Vertex(Vector4f position, Vector4f normal, Vector4b color) { Position = position; Normal = normal; Color = color; }
private static int arrangeData(Vector3[] positions, Vector4b[] colors, uint[] elements) { // send vertex positions int vbo = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, new IntPtr(positions.Length * Vector3.SizeInBytes), positions, BufferUsageHint.StaticDraw); // send vertex colors int cbo = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, cbo); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(colors.Length * 4), colors, BufferUsageHint.StaticDraw); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); // send index data int ibo = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo); GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(elements.Length * 4), elements, BufferUsageHint.StaticDraw); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); // create and setup vertex array object int vao = GL.GenVertexArray(); GL.BindVertexArray(vao); GL.EnableVertexAttribArray(0); GL.EnableVertexAttribArray(1); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, cbo); GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Byte, true, 0, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo); GL.BindVertexArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); return vao; }
private static void calcGrayscale(ref Vector3 v_in, ref Vector4b v_out) { invertIfNegative(ref v_in); v_out.V1 = (byte)(127.0f * v_in.Z); v_out.V2 = (byte)(127.0f * v_in.Z); v_out.V3 = (byte)(127.0f * v_in.Z); v_out.V4 = 0x7f; }
public void Read(BinaryReaderEx br) { name = br.ReadStringFixed(16); unk0 = br.ReadInt32(); lodDistance = br.ReadInt16(); billboard = br.ReadInt16(); Console.WriteLine($"CtrHeader: {name}"); if (unk0 != 0) { Helpers.Panic(this, $"check unusual unk0 value = {unk0}"); } if (billboard > 1) { Helpers.Panic(this, $"check unusual billboard value = {billboard}"); } scale = new Vector4s(br); ptrCmd = br.ReadInt32(); ptrVerts = br.ReadInt32(); ptrTex = br.ReadInt32(); ptrClut = br.ReadInt32(); unk3 = br.ReadInt32(); numAnims = br.ReadInt32(); ptrAnims = br.ReadInt32(); unk4 = br.ReadInt32(); if (unk3 != 0) { Helpers.Panic(this, $"check unusual unk3 value = {unk3}"); } if (unk4 != 0) { Helpers.Panic(this, $"check unusual unk4 value = {unk4}"); } long pos = br.BaseStream.Position; //read all drawing commands br.Jump(ptrCmd); cmdNum = br.ReadInt32(); uint x; do { x = br.ReadUInt32Big(); if (x != 0xFFFFFFFF) { drawList.Add(new CtrDraw(x)); } }while (x != 0xFFFFFFFF); //should read anims here /* * if (numAnims > 0) * { * for (int f = 0; f < numAnims; f++) * { * br.Jump(ptrAnims + f * 4); * br.Jump(br.ReadInt32()); * anims.Add(new CTRAnim(br)); * } * } */ //define temporary arrays Vector4b[] clr = new Vector4b[3]; //color buffer Vector3s[] crd = new Vector3s[4]; //face buffer Vector3s[] stack = new Vector3s[256]; //vertex buffer int maxv = 0; int maxc = 0; int maxt = 0; //one pass through all draw commands to get the array lengths foreach (var draw in drawList) { //only increase vertex count for commands that don't take vertex from stack if (!draw.flags.HasFlag(CtrDrawFlags.v)) { maxv++; } //simply find max color index if (draw.colorIndex > maxc) { maxc = draw.colorIndex; } //find max index, but 0 means no texture. if (draw.texIndex > 0) { if (draw.texIndex - 1 > maxt) { maxt = draw.texIndex; } } Console.WriteLine(draw.ToString()); } Console.WriteLine("maxv: " + maxv); Console.WriteLine("maxc: " + maxc); Console.WriteLine("maxt: " + maxt); //int ppos = (int)br.BaseStream.Position; br.Jump(ptrClut); for (int k = 0; k <= maxc; k++) { cols.Add(new Vector4b(br)); } //if static model if (!IsAnimated) { br.Jump(ptrVerts); posOffset = new Vector4s(br); Console.WriteLine(posOffset); br.Skip(16); vrenderMode = br.ReadInt32(); if (!(new List <int> { 0x1C, 0x22 }).Contains(vrenderMode)) { Helpers.Panic(this, $"check vrender {vrenderMode.ToString("X8")}"); } } else { //jump to first animation, read header and jump to vertex garbage br.Jump(ptrAnims); int ptr = br.ReadInt32(); br.Jump(ptr); CtrAnim anim = CtrAnim.FromReader(br); Console.WriteLine(anim.name + " " + anim.numFrames); br.Skip(0x1C); Console.WriteLine(br.HexPos()); posOffset = new Vector4s(0, 0, 0, 0); } //read vertices for (int k = 0; k < maxv; k++) { vtx.Add(new Vector3b(br)); } foreach (var v in vtx) { Console.WriteLine(v.ToString(VecFormat.Hex)); } List <Vector3s> vfixed = new List <Vector3s>(); foreach (var v in vtx) { vfixed.Add(new Vector3s(v.X, v.Y, v.Z)); } foreach (Vector3s v in vfixed) { //scale vertices v.X = (short)((((float)v.X / 255.0f - 0.5) * scale.X)); v.Y = (short)((((float)v.Y / 255.0f - 0.5) * scale.Z)); v.Z = (short)((((float)v.Z / 255.0f) * scale.Y)); //flip axis short zz = v.Z; v.Z = (short)-v.Y; v.Y = zz; } int vertexIndex = 0; int stripLength = 0; //process all commands foreach (CtrDraw d in drawList) { //if we got no stack vertex flag if (!d.flags.HasFlag(CtrDrawFlags.v)) { //push vertex from the array to the buffer stack[d.stackIndex] = vfixed[vertexIndex]; vertexIndex++; } //push new vertex from stack crd[0] = crd[1]; crd[1] = crd[2]; crd[2] = crd[3]; crd[3] = stack[d.stackIndex]; if (d.flags.HasFlag(CtrDrawFlags.l)) { crd[1] = crd[0]; } //push new color clr[0] = clr[1]; clr[1] = clr[2]; clr[2] = cols[d.colorIndex]; //if got reset flag, reset tristrip vertex counter if (d.flags.HasFlag(CtrDrawFlags.s)) { stripLength = 0; } //if we got 3 indices in tristrip (0,1,2) if (stripLength >= 2) { //read 3 vertices and push to the array for (int z = 1; z < 4; z++) { Vertex v = new Vertex(); v.coord = new Vector4s(crd[z].X, crd[z].Y, crd[z].Z, 0); v.color = clr[z - 1]; v.color_morph = v.color; verts.Add(v); } //ig got normal flag, change vertex order to flip normals if (!d.flags.HasFlag(CtrDrawFlags.n)) { Vertex v = verts[verts.Count - 1]; verts[verts.Count - 1] = verts[verts.Count - 3]; verts[verts.Count - 3] = v; } } stripLength++; } //read texture layouts br.Jump(ptrTex); uint[] texptrs = br.ReadArrayUInt32(maxt); Console.WriteLine("texptrs: " + texptrs.Length); foreach (uint t in texptrs) { Console.WriteLine(t.ToString("X8")); br.Jump(t); TextureLayout tx = TextureLayout.FromStream(br); tl.Add(tx); Console.WriteLine(tx.ToString()); } Console.WriteLine("tlcnt: " + tl.Count); br.BaseStream.Position = pos; }
public static List<Mesh> FromObject(string path) { uint lineno = 1; StreamReader sr = new StreamReader(path); PrimitiveType ptype = PrimitiveType.Triangles; List<Mesh> meshes = new List<Mesh>(); List<Vector3> vertices = new List<Vector3>(); List<Vector3> normals = new List<Vector3>(); List<uint> elements = new List<uint>(); int offset = 0, primitiveCnt = 0; Mesh mesh = new Mesh(0, ptype); for (string current_line = sr.ReadLine(); current_line != null; current_line = sr.ReadLine(), lineno++) { string[] split = current_line.Split(null); if (split.Length == 1 && split[0] == "") continue; // get rid of empty strings (caused by repeated delimiters): // v\x20\x20\x200.1234 converts to: {"v", "", "", "0.1234"} int notempty = 0; for (int i = 0; i < split.Length; ++i) if (split[i] != "") ++notempty; string[] tok = split; if (notempty != split.Length) { tok = new string[notempty]; for (int i = 0, j = 0; i < split.Length; ++i) if (split[i] != "") tok[j++] = split[i]; } switch (tok[0]) { case "v": if (tok.Length < 4) throw new Exception("Parsing error at line " + lineno + ": expected at least 4 tokens"); vertices.Add(new Vector3(float.Parse(tok[1], System.Globalization.CultureInfo.InvariantCulture), float.Parse(tok[2], System.Globalization.CultureInfo.InvariantCulture), float.Parse(tok[3], System.Globalization.CultureInfo.InvariantCulture))); normals.Add(new Vector3()); break; case "f": if (tok.Length < 4) throw new Exception("Parsing error at line " + lineno + ": expected at least 4 tokens"); int i1 = ParseIndex(int.Parse(tok[1].Split('/')[0]), vertices.Count); int i2 = ParseIndex(int.Parse(tok[2].Split('/')[0]), vertices.Count); int i3 = ParseIndex(int.Parse(tok[3].Split('/')[0]), vertices.Count); int i4 = tok.Length >= 5 ? int.Parse(tok[4].Split('/')[0]) : 0; AddNormal(vertices, normals, i1, i2, i3); elements.Add((uint)i1); elements.Add((uint)i2); elements.Add((uint)i3); offset += 3; primitiveCnt += 3; if (i4 != 0) { i4 = ParseIndex(i4, vertices.Count); elements.Add((uint)i1); elements.Add((uint)i3); elements.Add((uint)i4); AddNormal(vertices, normals, i1, i3, i4); offset += 3; primitiveCnt += 3; } break; case "o": case "g": if (mesh != null && primitiveCnt != 0) //wrong { mesh.primitiveCount = primitiveCnt; primitiveCnt = 0; meshes.Add(mesh); } mesh = new Mesh(offset, ptype); break; } } if (meshes.Count == 0 && primitiveCnt == 0) return null; if (primitiveCnt != 0) { mesh.primitiveCount = primitiveCnt; meshes.Add(mesh); } // normals Vector4b[] colors = new Vector4b[normals.Count]; for (int i = 0; i < normals.Count; ++i) { Vector3 n = normals[i].Normalized(); calcFullcolor(ref n, ref colors[i]); } // arrange Vector3[] positions = new Vector3[vertices.Count]; for (int i = 0; i < vertices.Count; ++i) positions[i] = vertices[i]; uint[] elems = new uint[elements.Count]; for (int i = 0; i < elements.Count; ++i) elems[i] = elements[i]; int vao = arrangeData(positions, colors, elems); foreach (Mesh m in meshes) m.vao = vao; return meshes; }
public Vertex(Vertex v) { this.position = v.position; this.texCoords = v.texCoords; this.color = v.color; }
public void Read(BinaryReaderEx br) { name = br.ReadStringFixed(16); Console.WriteLine(name); //Console.ReadKey(); unk0 = br.ReadInt32(); //0? lodDistance = br.ReadInt16(); billboard = br.ReadInt16(); //probably flags scale = new Vector4s(br); //ptr ptrCmd = br.ReadInt32(); ptrVerts = br.ReadInt32(); ptrTex = br.ReadInt32(); ptrClut = br.ReadInt32(); unk3 = br.ReadInt32(); //? numAnims = br.ReadInt32(); ptrAnims = br.ReadInt32(); unk4 = br.ReadInt32(); //? long pos = br.BaseStream.Position; br.Jump(ptrCmd); cmdNum = br.ReadInt32(); uint x; do { x = br.ReadUInt32Big(); if (x != 0xFFFFFFFF) { defs.Add(new CtrDraw(x)); } }while (x != 0xFFFFFFFF); /* * if (numAnims > 0) * { * for (int f = 0; f < numAnims; f++) * { * br.Jump(ptrAnims + f * 4); * br.Jump(br.ReadInt32()); * anims.Add(new CTRAnim(br)); * } * } */ StringBuilder sb = new StringBuilder(); sb.AppendFormat("o {0}\r\n", name); Vector4b[] clr = new Vector4b[3]; Vector3s[] crd = new Vector3s[4]; Vector3s[] stack = new Vector3s[256]; Vector3s curvert; int i = 0; int cur_i = 0; int maxv = 0; int maxc = 0; int maxt = 0; foreach (CtrDraw d in defs) { if (!d.flags.HasFlag(Flags.v)) { maxv++; } if (d.colorIndex > maxc) { maxc = d.colorIndex; } if (d.texIndex > 0) { if (d.texIndex - 1 > maxt) { maxt = d.texIndex; } } } Console.WriteLine("maxv: " + maxv); Console.WriteLine("maxc: " + maxc); Console.WriteLine("maxt: " + maxt); //int ppos = (int)br.BaseStream.Position; br.Jump(ptrClut); for (int k = 0; k <= maxc; k++) { cols.Add(new Vector4b(br)); } if (!IsAnimated) { br.Jump(ptrVerts); posOffset = new Vector4s(br); Console.WriteLine(posOffset); br.Skip(16); vrenderMode = br.ReadInt32(); if (vrenderMode != 0x1C) { Helpers.Panic(this, $"{vrenderMode.ToString("X8")}"); // Console.ReadKey(); } } else { br.Jump(ptrAnims); br.Jump(br.ReadInt32() + 0x1C + 0x18); posOffset = new Vector4s(0, 0, 0, 0); } for (int k = 0; k < maxv; k++) { vtx.Add(new Vector3b(br)); } List <Vector3s> vfixed = new List <Vector3s>(); foreach (var v in vtx) { vfixed.Add(new Vector3s(v.X, v.Y, v.Z)); } foreach (Vector3s v in vfixed) { v.X = (short)(((int)v.X / 255.0f - 0.5) * (scale.X / 16f)); v.Y = (short)(((int)v.Y / 255.0f - 0.5) * (scale.Z / 16f)); v.Z = (short)(((int)v.Z / 255.0f) * (scale.Y / 16f)); short zz = v.Z; v.Z = (short)-v.Y; v.Y = zz; } //br.Jump(ppos); foreach (CtrDraw d in defs) { if (d.flags.HasFlag(Flags.s)) { Console.WriteLine(cur_i); cur_i = 0; } if (d.flags.HasFlag(Flags.v)) { curvert = stack[d.value >> 16 & 0xFF]; } else { curvert = vfixed[i];//ReadVertex(br, i); stack[d.value >> 16 & 0xFF] = curvert; i++; } crd[0] = crd[1]; crd[1] = crd[2]; crd[2] = crd[3]; crd[3] = curvert; if (d.flags.HasFlag(Flags.l)) { crd[1] = crd[0]; } clr[0] = clr[1]; clr[1] = clr[2]; clr[2] = cols[d.colorIndex];//ReadColor(br, d.colorIndex); if (cur_i >= 2) { for (int z = 1; z < 4; z++) { Vertex v = new Vertex(); v.coord = new Vector4s(crd[z].X, crd[z].Y, crd[z].Z, 0); v.color = clr[z - 1]; v.color_morph = v.color; verts.Add(v); } } cur_i++; // Console.ReadKey(); } // Directory.CreateDirectory("mpk"); // Helpers.WriteToFile("mpk\\" + name + ".obj", sb.ToString()); br.Jump(ptrTex); uint[] texptrs = br.ReadArrayUInt32(maxt); Console.WriteLine("texptrs: " + texptrs.Length); foreach (uint t in texptrs) { Console.WriteLine(t.ToString("X8")); br.Jump(t); TextureLayout tx = TextureLayout.FromStream(br); tl.Add(tx); Console.WriteLine(tx.ToString()); } Console.WriteLine("tlcnt: " + tl.Count); br.BaseStream.Position = pos; }
public Vertex(Vector2 position, Vector2 texCoord, Color color) { this.position = position; this.texCoords = texCoord; this.color = new Vector4b(color.R, color.G, color.B, color.A); }
public Voxel(string name, int[] color) { Name = name; Color = new Vector4b((byte)color[0], (byte)color[1], (byte)color[2], 255); }
public void Read(BinaryReaderEx br) { int dataStart = (int)br.BaseStream.Position; ptrMeshInfo = br.ReadUIntPtr(); ptrSkybox = br.ReadUIntPtr(); ptrTexArray = br.ReadUIntPtr(); numInstances = br.ReadInt32(); ptrInstances = br.ReadUIntPtr(); numModels = br.ReadInt32(); ptrModelsPtr = br.ReadUIntPtr(); unkPtr1 = br.ReadUInt32(); unkPtr2 = br.ReadUInt32(); ptrInstancesPtr = br.ReadUIntPtr(); unkPtr3 = br.ReadUInt32(); null1 = br.ReadInt32(); null2 = br.ReadInt32(); if (null1 != 0 || null2 != 0) { Helpers.Panic(this, PanicType.Assume, "WARNING header.null1 = " + null1 + "; header.null2 = " + null2); } numWater = br.ReadUInt32(); ptrWater = br.ReadUIntPtr(); ptrIcons = br.ReadUIntPtr(); ptrIconsArray = br.ReadUIntPtr(); ptrRestartMain = br.ReadUIntPtr(); someData = new SomeData[3]; for (int i = 0; i < 3; i++) { SomeData sd = new SomeData(); sd.Read(br); someData[i] = sd; } startGrid = new Pose[8]; for (int i = 0; i < 8; i++) { Pose pos = new Pose(br); startGrid[i] = pos; //Console.WriteLine(startGrid[i].ToString()); } unkPtr4 = br.ReadUInt32(); unkPtr5 = br.ReadUInt32(); ptrLowTexArray = br.ReadUIntPtr(); backColor = new Vector4b(br); bgMode = br.ReadUInt32(); ptrBuildStart = br.ReadUIntPtr(); ptrBuildEnd = br.ReadUIntPtr(); ptrBuildType = br.ReadUIntPtr(); skip = br.ReadBytes(0x38); particleColorTop = new Vector4b(br); particleColorBottom = new Vector4b(br); particleRenderMode = br.ReadUInt32(); cntTrialData = br.ReadUInt32(); ptrTrialData = br.ReadUIntPtr(); cntu2 = br.ReadUInt32(); ptru2 = br.ReadUInt32(); numSpawnPts = br.ReadUInt32(); ptrSpawnPts = br.ReadUIntPtr(); numRestartPts = br.ReadUInt32(); ptrRestartPts = br.ReadUIntPtr(); skip2 = br.ReadBytes(16); bgColor = new Vector4b[4]; for (int i = 0; i < 4; i++) { bgColor[i] = new Vector4b(br); } skip2_unkPtr = br.ReadUInt32(); numVcolAnim = br.ReadUInt32(); ptrVcolAnim = br.ReadUIntPtr(); skip23 = br.ReadBytes(12); ptrAiNav = br.ReadUIntPtr(); skip3 = br.ReadBytes(0x24); long dataEnd = br.BaseStream.Position; if (dataEnd - dataStart != SizeOf) { throw new Exception("SceneHeader: size mismatch"); } if (ptrBuildStart != UIntPtr.Zero) { br.Jump(ptrBuildStart); compilationBegins = Helpers.ParseDate(br.ReadStringNT()); Console.WriteLine(compilationBegins); } if (ptrBuildEnd != UIntPtr.Zero) { br.Jump(ptrBuildEnd); compilationEnds = Helpers.ParseDate(br.ReadStringNT()); Console.WriteLine(compilationEnds); } if (ptrBuildType != UIntPtr.Zero) { br.Jump(ptrBuildType); Console.WriteLine(br.ReadStringNT()); } br.Jump(dataEnd); }
public Vertex(Vector3 position, Vector4b color, Vector3 normal) { Position = position; Color = color; Normal = normal; }
/// <summary>Read a <see cref="Vector4b"/>.</summary> public static void ReadVector4b(this BinaryReader reader , out Vector4b result) { result.X = reader.ReadByte(); result.Y = reader.ReadByte(); result.Z = reader.ReadByte(); result.W = reader.ReadByte(); return; }
public void Read(BinaryReaderEx br) { ptrMeshInfo = br.ReadUInt32(); ptrSkybox = br.ReadUInt32(); ptrTexArray = br.ReadUInt32(); numInstances = br.ReadInt32(); ptrInstances = br.ReadUInt32(); numModels = br.ReadInt32(); ptrModelsPtr = br.ReadUInt32(); unkptr3 = br.ReadUInt32(); unkptr4 = br.ReadUInt32(); ptrPickupHeadersPtrArray = br.ReadUInt32(); unkptr5 = br.ReadUInt32(); null1 = br.ReadInt32(); null2 = br.ReadInt32(); if (null1 != 0 || null2 != 0) { Console.WriteLine("WARNING header.null1 = " + null1 + "; header.null2 = " + null2); } cntWater = br.ReadUInt32(); ptrWater = br.ReadUInt32(); ptrNamedTex = br.ReadUInt32(); ptrNamedTexArray = br.ReadUInt32(); ptrRestartMain = br.ReadUInt32(); someData = new SomeData[3]; for (int i = 0; i < 3; i++) { SomeData sd = new SomeData(); sd.Read(br); someData[i] = sd; } startGrid = new PosAng[8]; for (int i = 0; i < 8; i++) { PosAng pos = new PosAng(br); startGrid[i] = pos; Console.WriteLine(startGrid[i].ToString()); } somePtr4 = br.ReadUInt32(); somePtr5 = br.ReadUInt32(); ptrLowTexArray = br.ReadUInt32(); backColor = new Vector4b(br); bgMode = br.ReadUInt32(); ptrBuildStart = br.ReadUInt32(); ptrBuildEnd = br.ReadUInt32(); ptrBuildType = br.ReadUInt32(); skip = br.ReadBytes(0x6C - 16 - 8 - 16); cntTrialData = br.ReadUInt32(); ptrTrialData = br.ReadUInt32(); cntu2 = br.ReadUInt32(); ptru2 = br.ReadUInt32(); cntSpawnPts = br.ReadUInt32(); ptrSpawnPts = br.ReadUInt32(); cntRestartPts = br.ReadUInt32(); ptrRestartPts = br.ReadUInt32(); //skip2 = br.ReadBytes(0x38); skip2 = br.ReadBytes(16); bgColor = new Vector4b[4]; for (int i = 0; i < 4; i++) { bgColor[i] = new Vector4b(br); } skip2_unkPtr = br.ReadUInt32(); cntVcolAnim = br.ReadUInt32();; ptrVcolAnim = br.ReadUInt32();; skip23 = br.ReadBytes(12); ptrAiNav = br.ReadUInt32(); skip3 = br.ReadBytes(0x24); long posx = br.BaseStream.Position; br.Jump(ptrBuildStart); compilationBegins = Helpers.ParseDate(br.ReadStringNT()); br.Jump(ptrBuildEnd); compilationEnds = Helpers.ParseDate(br.ReadStringNT()); br.Jump(ptrBuildType); Console.WriteLine(br.ReadStringNT()); br.Jump(posx); //Console.ReadKey(); }
public static Color ToColor(Vector4b s) { return(new Color(s.X, s.Y, s.Z, s.W)); }