public override void ReadData(Scene scene, BinaryReader stream) { // Read number of vertices. short vertexCount = 0; vertexCount = stream.ReadInt16(); // Read each vertex and add it. for (short i = 0; i < vertexCount; i++) { Vertex v = new Vertex(); v.X = stream.ReadSingle(); v.Y = stream.ReadSingle(); v.Z = stream.ReadSingle(); vertices.Add(v); } }
/// <summary> /// Initializes a new instance of the <see cref="Vertex"/> struct. /// </summary> /// <param name="vertex">The vertex.</param> public Vertex(Vertex vertex) { this.x = vertex.X; this.y = vertex.Y; this.z = vertex.Z; }
/// <summarY> /// Find the Vector product (cross product) of two vectors. /// </summarY> /// <param name="rhs">The right hand side of the equation.</param> /// <returns>The Cross Product.</returns> public Vertex VectorProduct(Vertex rhs) { return new Vertex((Y * rhs.Z) - (Z * rhs.Y), (Z * rhs.X) - (X * rhs.Z), (X * rhs.Y) - (Y * rhs.X)); }
/// <summarY> /// This finds the Scalar Product (Dot Product) of two vectors. /// </summarY> /// <param name="rhs">The right hand side of the equation.</param> /// <returns>A Scalar Representing the Dot-Product.</returns> public float ScalarProduct(Vertex rhs) { return X * rhs.X + Y * rhs.Y + Z * rhs.Z; }
/// <summary> /// Generates the normalisation cube map. /// </summary> /// <returns></returns> private bool GenerateNormalisationCubeMap() { var gl = openGLControl1.OpenGL; // First we create space to hold the data for a single face. // Each face is 32x32, and we need to store the R, G and B components of the color at each point. byte[] data = new byte[32 * 32 * 3]; // Some useful variables. int size = 32; float offset = 0.5f; float halfSize = 16.0f; Vertex tempVector = new Vertex(); uint byteCounter = 0; // Positive x. for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { tempVector.X = (halfSize); tempVector.Y = (-(j + offset - halfSize)); tempVector.Z = (-(i + offset - halfSize)); tempVector.UnitLength(); tempVector = tempVector.GetPackedTo01(); data[byteCounter++] = (byte)(tempVector.X * 255f); data[byteCounter++] = (byte)(tempVector.Y * 255f); data[byteCounter++] = (byte)(tempVector.Z * 255f); } } // Set the texture image. gl.TexImage2D(OpenGL.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, OpenGL.GL_RGBA8, 32, 32, 0, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, data); //negative x byteCounter = 0; for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { tempVector.X = (-halfSize); tempVector.Y = (-(j + offset - halfSize)); tempVector.Z = ((i + offset - halfSize)); tempVector.UnitLength(); tempVector = tempVector.GetPackedTo01(); data[byteCounter++] = (byte)(tempVector.X * 255f); data[byteCounter++] = (byte)(tempVector.Y * 255f); data[byteCounter++] = (byte)(tempVector.Z * 255f); } } gl.TexImage2D(OpenGL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, OpenGL.GL_RGBA8, 32, 32, 0, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, data); //positive y byteCounter = 0; for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { tempVector.X = (i + offset - halfSize); tempVector.Y = (halfSize); tempVector.Z = ((j + offset - halfSize)); tempVector.UnitLength(); tempVector = tempVector.GetPackedTo01(); data[byteCounter++] = (byte)(tempVector.X * 255f); data[byteCounter++] = (byte)(tempVector.Y * 255f); data[byteCounter++] = (byte)(tempVector.Z * 255f); } } gl.TexImage2D(OpenGL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, OpenGL.GL_RGBA8, 32, 32, 0, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, data); //negative y byteCounter = 0; for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { tempVector.X = (i + offset - halfSize); tempVector.Y = (-halfSize); tempVector.Z = (-(j + offset - halfSize)); tempVector.UnitLength(); tempVector = tempVector.GetPackedTo01(); data[byteCounter++] = (byte)(tempVector.X * 255f); data[byteCounter++] = (byte)(tempVector.Y * 255f); data[byteCounter++] = (byte)(tempVector.Z * 255f); } } gl.TexImage2D(OpenGL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, OpenGL.GL_RGBA8, 32, 32, 0, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, data); //positive z byteCounter = 0; for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { tempVector.X = (i + offset - halfSize); tempVector.Y = (-(j + offset - halfSize)); tempVector.Z = (halfSize); tempVector.UnitLength(); tempVector = tempVector.GetPackedTo01(); data[byteCounter++] = (byte)(tempVector.X * 255f); data[byteCounter++] = (byte)(tempVector.Y * 255f); data[byteCounter++] = (byte)(tempVector.Z * 255f); } } gl.TexImage2D(OpenGL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, OpenGL.GL_RGBA8, 32, 32, 0, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, data); //negative z byteCounter = 0; for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { tempVector.X = (-(i + offset - halfSize)); tempVector.Y = (-(j + offset - halfSize)); tempVector.Z = (-halfSize); tempVector.UnitLength(); tempVector = tempVector.GetPackedTo01(); data[byteCounter++] = (byte)(tempVector.X * 255f); data[byteCounter++] = (byte)(tempVector.Y * 255f); data[byteCounter++] = (byte)(tempVector.Z * 255f); } } gl.TexImage2D(OpenGL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, OpenGL.GL_RGBA8, 32, 32, 0, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, data); return true; }
/// <summary> /// This function reads a polygon. /// </summary> /// <param name="reader"></param> /// <returns></returns> protected override object ReadData(BinaryReader reader) { // Create a polygon. Polygon poly = new Polygon(); // Read a name chunk. CaligariName name = new CaligariName(); name.Read(reader); poly.Name = name.name; // Read the local axies. CaligariAxies axies = new CaligariAxies(); axies.Read(reader); poly.Transformation.TranslateX = axies.centre.X; poly.Transformation.TranslateY = axies.centre.Y; poly.Transformation.TranslateZ = axies.centre.Z; // poly.Rotate = axies.rotate; // Read the position matrix. CaligariPosition pos = new CaligariPosition(); pos.Read(reader); // Read number of verticies. int verticesCount = reader.ReadInt32(); // Get them all for (int i = 0; i < verticesCount; i++) { // Read a vertex. Vertex vertex = new Vertex(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); // Multiply it by the position matrix. vertex = vertex * pos.matrix; // Add it. poly.Vertices.Add(vertex); } // Read UV count. int uvsCount = reader.ReadInt32(); // Read all of the UVs for (int i = 0; i < uvsCount; i++) poly.UVs.Add(new UV(reader.ReadInt32(), reader.ReadInt32())); // Read faces count. int faces = reader.ReadInt32(); // Read each face. for (int f = 0; f < faces; f++) { Face face = new Face(); poly.Faces.Add(face); // Read face type flags. byte flags = reader.ReadByte(); // Read vertex count short verticesInFace = reader.ReadInt16(); // Do we read a material number? if ((flags & 0x08) == 0) // is it a 'hole' face? reader.ReadInt16(); // Now read the indices, a vertex index and a uv index. for (short j = 0; j < verticesInFace; j++) face.Indices.Add(new Index(reader.ReadInt32(), reader.ReadInt32())); } // Any extra stuff? if (header.minorVersion > 4) { // read flags. reader.ReadChars(4); if ((header.minorVersion > 5) && (header.minorVersion < 8)) reader.ReadChars(2); } // Now that we've loaded the polygon, we triangulate it. poly.Triangulate(); // Finally, we update normals. poly.Validate(true); return poly; }