private void PrepareCamera() { var camera = this.Camera; if (camera != null) { Vertex back = camera.Position - camera.Target; Vertex right = Camera.UpVector.VectorProduct(back); Vertex up = back.VectorProduct(right); back.Normalize(); right.Normalize(); up.Normalize(); this.back = back; this.right = right; this.up = up; if (this.originalCamera == null) { this.originalCamera = new ScientificCamera(); } this.originalCamera.Position = camera.Position; this.originalCamera.UpVector = camera.UpVector; } }
public virtual void Read(BinaryReader stream) { centre = new Vertex(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle()); directionX = new Vertex(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle()); directionY = new Vertex(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle()); directionZ = new Vertex(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle()); // The X axis in SharpGL is always (1, 0, 0). xAxis = new Vertex(1, 0, 0); float angleX = xAxis.ScalarProduct(directionX); yAxis = new Vertex(0, 1, 0); float angleY = yAxis.ScalarProduct(directionY); zAxis = new Vertex(0, 0, 1); float angleZ = zAxis.ScalarProduct(directionZ); angleX = (float)System.Math.Asin(angleX); angleY = (float)System.Math.Asin(angleY); angleZ = (float)System.Math.Asin(angleZ); angleX = (180 * angleX) / (float)Math.PI; angleY = (180 * angleY) / (float)Math.PI; angleZ = (180 * angleZ) / (float)Math.PI; rotate = new Vertex(-angleX, -angleY, -angleZ); xAxis = xAxisGL = directionX; yAxis = zAxisGL = directionY; zAxis = yAxisGL = directionZ; xAxisGL.X = -xAxisGL.X; }
/// <summary> /// This finds out if a point is in front of, behind, or on this plane. /// </summary> /// <param name="point">The point to classify.</param> /// <returns> /// Less than 0 if behind, 0 if on, Greater than 0 if in front. /// </returns> public float ClassifyPoint(Vertex point) { // (X-P)*N = 0. Where, X is a point to test, P is a point // on the plane, and N is the normal to the plane. return normal.ScalarProduct(point); }
public static Vertex GetPackedTo01(this Vertex me) { Vertex temp = new Vertex(me); temp.Normalize(); temp= (temp * 0.5f) + new Vertex(0.5f, 0.5f, 0.5f); return temp; }
/// <summary> /// Apply a rotation transformation to a given Vertex. /// </summary> /// <param name="vertex">The given vertex</param> /// <param name="focalPoint">The point to rotate around</param> /// <param name="rotation">The rotation to apply (in degrees)</param> /// <returns>The transformed vertex</returns> public static Vertex VertexRotateTransform(Vertex vertex, Vertex focalPoint, Vertex rotation) { Vertex rotationRadians = rotation; rotationRadians.X = Convert.degreesToRadians(rotation.X); rotationRadians.Y = Convert.degreesToRadians(rotation.Y); rotationRadians.Z = Convert.degreesToRadians(rotation.Z); return VertexRotateRadianTransform(vertex, focalPoint, rotationRadians); }
public void MouseMove(int x, int y) { if (this.mouseDownFlag) { IViewCamera camera = this.Camera; if (camera == null) { return; } Vertex back = this.back; Vertex right = this.right; Vertex up = this.up; Size bound = this.bound; Point downPosition = this.downPosition; { float deltaX = -horizontalRotationFactor * (x - downPosition.X) / bound.Width; float cos = (float)Math.Cos(deltaX); float sin = (float)Math.Sin(deltaX); Vertex newBack = new Vertex( back.X * cos + right.X * sin, back.Y * cos + right.Y * sin, back.Z * cos + right.Z * sin); back = newBack; right = up.VectorProduct(back); back.Normalize(); right.Normalize(); } { float deltaY = verticalRotationFactor * (y - downPosition.Y) / bound.Height; float cos = (float)Math.Cos(deltaY); float sin = (float)Math.Sin(deltaY); Vertex newBack = new Vertex( back.X * cos + up.X * sin, back.Y * cos + up.Y * sin, back.Z * cos + up.Z * sin); back = newBack; up = back.VectorProduct(right); back.Normalize(); up.Normalize(); } camera.Position = camera.Target + back * (float)((camera.Position - camera.Target).Magnitude()); camera.UpVector = up; this.back = back; this.right = right; this.up = up; this.downPosition.X = x; this.downPosition.Y = y; } }
/// <summary> /// This is a SharpGL helper version, that projects the vertex passed, using the /// current matrixes. /// </summary> /// <param name="gl">The gl.</param> /// <param name="vertex">The object coordinates.</param> /// <returns> /// The screen coords. /// </returns> public static Vertex Project(this OpenGL gl, Vertex vertex) { // THIS CODE MUST BE TESTED double[] modelview = new double[16]; double[] projection = new double[16]; int[] viewport = new int[4]; gl.GetDouble(OpenGL.GL_MODELVIEW_MATRIX, modelview); gl.GetDouble(OpenGL.GL_PROJECTION_MATRIX, projection); gl.GetInteger(OpenGL.GL_VIEWPORT, viewport); double[] x = new double[1]; // kludgy double[] y = new double[1]; double[] z = new double[1]; gl.Project(vertex.X, vertex.Y, vertex.Z, modelview, projection, viewport, x, y, z); return new Vertex((float)x[0], (float)y[0], (float)z[0]); }
public static Vertex[] GenerateGeometry(uint numberOfVertices, float maxRadius) { // Create a random number generator. var random = new Random(); // Create the vertices. var vertices = new Vertex[numberOfVertices]; // Set each point. for (uint i = 0; i < numberOfVertices; i++ ) { vertices[i].X = (float)((random.NextDouble() - 0.5) * (maxRadius * 2)); vertices[i].Y = (float)((random.NextDouble() - 0.5) * (maxRadius * 2)); vertices[i].Z = (float)((random.NextDouble() - 0.5) * (maxRadius * 2)); } // Return the vertices. return vertices; }
/// <summary> /// Apply a rotation transformation to a given Vertex. /// </summary> /// <param name="vertex">The given vertex</param> /// <param name="focalPoint">The point to rotate around</param> /// <param name="rotation">The rotation to apply (in radians)</param> /// <returns>The transformed vertex</returns> public static Vertex VertexRotateRadianTransform(Vertex vertex, Vertex focalPoint, Vertex rotation) { //Convert to SlimMath Vectors SlimMath.Vector3 vector = Convert.VertexToSlimMathVector3( vertex); SlimMath.Vector3 focalPointV = Convert.VertexToSlimMathVector3( focalPoint); SlimMath.Vector3 rotationV = Convert.VertexToSlimMathVector3( rotation); SlimMath.Matrix transform = SlimMath.Matrix.Identity; transform.TranslationVector = vector; transform *= SlimMath.Matrix.Translation(focalPointV * -1); transform *= SlimMath.Matrix.RotationX(rotationV.X); transform *= SlimMath.Matrix.RotationY(rotationV.Y); transform *= SlimMath.Matrix.RotationZ(rotationV.Z); transform *= SlimMath.Matrix.Translation(focalPointV); return Convert.SlimMathVector3ToVertex(transform.TranslationVector); }
/// <summary> /// Initializes a new instance of the <see cref="Vertex"/> struct. /// </summary> /// <param name="vertex">The vertex.</param> public Vertex(Vertex vertex) { X = vertex.X; Y = vertex.Y; 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> /// This is part of the shadow casting code, it does a shadowpass for the /// polygon using the specified light. /// </summary> /// <param name="gl">The OpenGL object.</param> /// <param name="light">The light casting the shadow.</param> /// <param name="visibleArray">An array of bools.</param> protected virtual void DoShadowPass(OpenGL gl, Vertex lightPos, bool[] visibleArray) { // Go through each face. for(int i = 0; i < faces.Count; i++) { // Get a reference to the face. Face face = faces[i]; // Is the face visible... if(visibleArray[i]) { // Go through each edge... for(int j = 0; j < face.Indices.Count; j++) { // Get the neighbour of this edge. int neighbourIndex = face.neighbourIndices[j]; // If there's no neighbour, or the neighbour ain't visible, it's // an edge... if(neighbourIndex == -1 || visibleArray[neighbourIndex] == false ) { // Get the edge vertices. Vertex v1 = vertices[face.Indices[j].Vertex]; Vertex v2 = vertices[face.Indices[(j+1)%face.Indices.Count].Vertex]; // Create the two distant vertices. Vertex v3 = (v1 - lightPos) * 100; Vertex v4 = (v2 - lightPos) * 100; // Draw the shadow volume. gl.Begin(OpenGL.TRIANGLE_STRIP); gl.Vertex(v1); gl.Vertex(v1 + v3); gl.Vertex(v2); gl.Vertex(v2 + v4); gl.End(); } } } } }
/// <summary> /// This creates a polygon from a height map (any picture). Black is low, /// and the colors are high (the lighter the color, the higher the surface). /// </summary> /// <param name="filename">Path of the image file.</param> /// <param name="xPoints">Number of points along X.</param> /// <param name="yPoints">Number of points along Y.</param> /// <returns>True if sucessful, false otherwise.</returns> public virtual bool CreateFromMap(string filename, int xPoints, int yPoints) { // Try and load the image file. System.Drawing.Bitmap map = new System.Drawing.Bitmap(filename); if(map.Size.IsEmpty) return false; // Set the descriptive name. Name = "Map created from '" + filename + "'"; // Get points. for(int y=0; y < yPoints; y++) { int yValue = (map.Height / yPoints) * y; for(int x=0; x < xPoints; x++) { int xValue = (map.Width / xPoints) * x; // Get the pixel. System.Drawing.Color col = map.GetPixel(xValue, yValue); float xPos = (float)x / (float)xPoints; float yPos = (float)y / (float)yPoints; // Create a control point from it. Vertex v = new Vertex(xPos, 0, yPos); // Add the 'height', based on color. v.Y = (float)col.R / 255.0f + (float)col.G / 255.0f + (float)col.B / 255.0f; // Add this vertex to the vertices array. Vertices.Add(v); } } // Create faces for the polygon. for(int y=0; y < (yPoints-1); y++) { for(int x=0; x < (xPoints-1); x++) { // Create the face. Face face = new Face(); // Create vertex indicies. int nTopLeft = (y * xPoints) + x; int nBottomLeft = ((y + 1) * xPoints) + x; face.Indices.Add(new Index(nTopLeft)); face.Indices.Add(new Index(nTopLeft + 1)); face.Indices.Add(new Index(nBottomLeft + 1)); face.Indices.Add(new Index(nBottomLeft)); // Add the face. Faces.Add(face); } } return true; }
/// <summary> /// Initializes a new instance of the <see cref="VerticesPair" /> /// </summary> /// <param name="x">The X coordinate of the <see cref="Vertex" />.</param> /// <param name="y">The Y coordinate of the <see cref="Vertex" />.</param> /// <param name="offsetZ">The Z coordinate offset of the top <see cref="Vertex" />. The base <see cref="Vertex" /> has the offset "0".</param> /// <param name="addendum">The addendum to the <see cref="offsetZ" /> - to extend the axiliary geometry out of the solid body.</param> public VerticesPair(float x, float y, float offsetZ, float addendum) { Top = new Vertex(x, y, offsetZ + addendum); Base = new Vertex(x, y, 0 - addendum); }
/// <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.Translate = axies.centre; // 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; }
protected override object LoadData(Stream stream) { // We use a binary reader for this data. BinaryReader reader = new BinaryReader(stream, System.Text.Encoding.ASCII); // Create a polygon. Polygon poly = new Polygon(); // Read the translate, scale, rotate. poly.Translate.X = reader.ReadSingle(); poly.Translate.Y = reader.ReadSingle(); poly.Translate.Z = reader.ReadSingle(); poly.Scale.X = reader.ReadSingle(); poly.Scale.Y = reader.ReadSingle(); poly.Scale.Z = reader.ReadSingle(); poly.Rotate.X = reader.ReadSingle(); poly.Rotate.Y = reader.ReadSingle(); poly.Rotate.Z = reader.ReadSingle(); // 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()); // 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 index count int indices = reader.ReadInt32(); // Now read the indices, a vertex index and a uv index and a color index. for(int j=0; j<indices; j++) { face.Indices.Add(new Index(reader.ReadInt32(), reader.ReadInt32())); int colour = reader.ReadInt32(); //not used. } } // Finally, we update normals. poly.Validate(true); return poly; }
/// <summary> /// Constructs a new Keyframe /// </summary> /// <param name="time">Keyframe time</param> /// <param name="type">Keyframe type</param> /// <param name="transformation">Keyframe transformation</param> public Keyframe(float time, KeyframeType type, Vertex transformation) { this.time = time; this.type = type; transform = transformation; }
/// <summary> /// Vertexes the pointer. /// </summary> /// <param name="gl">The gl.</param> /// <param name="size">The size.</param> /// <param name="type">The type.</param> /// <param name="stride">The stride.</param> /// <param name="pointer">The pointer.</param> public static void VertexPointer(this OpenGL gl, int size, uint type, int stride, Vertex[] pointer) { // TODO debug this. var handle = GCHandle.Alloc(pointer, GCHandleType.Pinned); gl.VertexPointer(size, type, stride, handle.AddrOfPinnedObject()); handle.Free(); }
private static Vertex OffsetLookAtCamera(float dx, float dy, float dz, Vertex pos) { return new Vertex(pos.X + dx, pos.Y + dy, pos.Z + dz); }
public void setKeyframe(string boneName, int index, float time, int type, string transform) { KeyframeType typ = KeyframeType.Translation; if (type == 1) typ = KeyframeType.Rotation; string[] xyz = transform.Split(':'); Vertex vertex; vertex = new Vertex( float.Parse(xyz[0]), float.Parse(xyz[1]), float.Parse(xyz[2])); foreach (Bone bone in model.Bones) { if (bone.Name == boneName) { if (index > -1 && index < bone.Animation.Count) { bone.Animation[index].Time = time; bone.Animation[index].Type = typ; bone.Animation[index].Transformation = vertex; bone.Translation = new Vertex(); bone.Rotation = new Vertex(); if (ModelUpdated != null) ModelUpdated(this, new EventArgs()); } } } }
public static SlimMath.Vector3 VertexToSlimMathVector3(Vertex vertex) { return new SlimMath.Vector3(vertex.X, vertex.Y, vertex.Z); }
/// <summary> /// Constructs a new Keyframe /// </summary> /// <param name="type">Keyframe type</param> public Keyframe(KeyframeType type) { time = 0.0f; this.type = type; transform = new Vertex(0, 0, 0); }
/// <summary> /// Calculate the distance between two specified 3D vectors. /// </summary> /// <param name="v1">The first of the two vectors</param> /// <param name="v2">The second of the two vectors</param> /// <returns>The distance between the two points</returns> public static float VertexDistance(Vertex v1, Vertex v2) { return (float)Math.Sqrt(Math.Pow(v1.X - v2.X, 2) + Math.Pow(v1.Y - v2.Y, 2) + Math.Pow(v1.Z - v2.Z, 2)); }
/// <summary> /// This function is cool, just stick in a set of points, it'll add them to the /// array, and create a face. It will take account of duplicate vertices too! /// </summary> /// <param name="vertexData">A set of vertices to make into a face.</param> public virtual void AddFaceFromVertexData(Vertex[] vertexData) { // Create a face. Face newFace = new Face(); // Go through the vertices... foreach(Vertex v in vertexData) { // Do we have this vertex already? int at = vertices.Find(0, v, 0.01f); // Add the vertex, and index it. newFace.Indices.Add(new Index( (at == -1) ? vertices.Add(v) : at)); } // Add the face. faces.Add(newFace); }
public virtual void Clamp(Vertex v) { float diff = v.X % size; if(diff > size/2) diff -=size; v.X -= diff; diff = v.Y % size; if(diff > size/2) diff -=size; v.Y -= diff; diff = v.Z % size; if(diff > size/2) diff -=size; v.Z -= diff; }
public Keyframe createKeyframe(string boneName, float time, int type, string vector) { KeyframeType typ = KeyframeType.Translation; if (type == 2) typ = KeyframeType.Rotation; string[] xyz = vector.Split(':'); Vertex vertex = new Vertex( float.Parse(xyz[0]), float.Parse(xyz[1]), float.Parse(xyz[2])); Keyframe keyframe = new Keyframe(time, typ, vertex); foreach (Bone bone in model.Bones) { if (bone.Name == boneName) { bone.Animation.Add(keyframe); if (ModelUpdated != null) ModelUpdated(this, new EventArgs()); break; } } return keyframe; }
/// <summary> /// Zoom the camera given a zoom factor. /// </summary> /// <param name="factor">The zoom factor</param> public void Zoom(float factor) { Vertex A = this.Target; Vertex B = initialPosition; Vertex magnitude = new Vertex(B.X - A.X, B.Y - A.Y, B.Z - A.Z); initialPosition = magnitude * factor; updateRotation(); }
/// <summary> /// Rotates this vertex around an axel /// </summary> /// <param name="n">The vertex to rotate</param> /// <param name="a">The amount to rotate by (angle)</param> /// <param name="v">The axel to rotate around</param> /// <returns></returns> public static Vertex RotateAroundAxis(this Vertex n, float a, Vertex v) { return v*(float) Math.Cos(a) + ( n * v.ScalarProduct(n) * (1 - (float) Math.Cos(a)) + (n.VectorProduct(v)*(float) Math.Sin(a))); }
private static vec3 ToVec3(SharpGL.SceneGraph.Vertex vertex) { vec3 result = new vec3(vertex.X, vertex.Y, vertex.Z); return(result); }
/// <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; }
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> /// Construct a new empty keyframe /// (Defaulted to a type of Translation) /// </summary> public Keyframe() { time = 0.0f; type = KeyframeType.Translation; transform = new Vertex(0, 0, 0); }