/// <summary> /// A <see cref="VertexArrayTexGenDelegate"/> mapping a sphere. /// </summary> /// <param name="v"></param> /// <returns></returns> public static Vertex2f SphereMap(Vertex3f v) { v.Normalize(); float x = (float)(Math.Atan2(v.z, v.x) / Math.PI) * 0.5f + 0.5f; float y = (float)(Math.Asin(v.y) / (Math.PI / 2.0)) * 0.5f + 0.5f; Debug.Assert(x >= 0.0f && x <= 1.0f); Debug.Assert(y >= 0.0f && y <= 1.0f); return(new Vertex2f(x, y)); }
/// <summary> /// Generate the texture coordinate for the specified vertex. /// </summary> /// <param name="v"></param> /// <returns></returns> public override Vertex2f Generate(Vertex3f v) { v.Normalize(); float x = (float)(Math.Atan2(v.z, v.x) / Math.PI) * 0.5f + 0.5f; float y = (float)(Math.Asin(v.y) / (Math.PI / 2.0)) * 0.5f + 0.5f; Debug.Assert(x >= 0.0f && x <= 1.0f); Debug.Assert(y >= 0.0f && y <= 1.0f); return(new Vertex2f(x * Repeat.x, y * Repeat.y)); }
private void GenerateTangentsTriangle4f(IVertexArray positionArray, IVertexArray normalArray, IVertexArray texArray, IVertexArray tanArray, IVertexArray bitanArray) { uint count = (ElementCount != 0 ? ElementCount : _VertexArrayObject.ArrayLength) / 3; for (uint i = 0, v = ElementOffset; i < count; i++, v += 3) { Vertex3f v0 = (Vertex3f)positionArray.GetElement <Vertex4f>(v); Vertex3f v1 = (Vertex3f)positionArray.GetElement <Vertex4f>(v + 1); Vertex3f v2 = (Vertex3f)positionArray.GetElement <Vertex4f>(v + 2); Vertex2f t0 = texArray.GetElement <Vertex2f>(v); Vertex2f t1 = texArray.GetElement <Vertex2f>(v + 1); Vertex2f t2 = texArray.GetElement <Vertex2f>(v + 2); Vertex3f dv1 = v1 - v0, dv2 = v2 - v0; Vertex2f dt1 = t1 - t0, dt2 = t2 - t0; float w = 1.0f / (dt1.x * dt2.y - dt1.y * dt2.x); Vertex3f tgVector, btVector; if (Single.IsInfinity(w) == false) { tgVector = (dv1 * dt2.y - dv2 * dt1.y) * w; tgVector.Normalize(); btVector = (dv2 * dt1.x - dv1 * dt2.x) * w; btVector.Normalize(); Vertex3f n = normalArray.GetElement <Vertex3f>(v).Normalized; n.Normalize(); if (((n ^ tgVector) * btVector) < 0.0f) { tgVector = tgVector * -1.0f; } } else { // Degenerate triangles does not contribute tgVector = btVector = Vertex3f.Zero; } tanArray.SetElement <Vertex3f>(tgVector, v); tanArray.SetElement <Vertex3f>(tgVector, v + 1); tanArray.SetElement <Vertex3f>(tgVector, v + 2); bitanArray.SetElement <Vertex3f>(btVector, v); bitanArray.SetElement <Vertex3f>(btVector, v + 1); bitanArray.SetElement <Vertex3f>(btVector, v + 2); } }
/// <summary> /// Setup this matrix to view the universe in a certain direction. /// </summary> /// <param name="eyePosition"> /// A <see cref="Vertex3f"/> that specify the eye position, in local coordinates. /// </param> /// <param name="forwardVector"> /// A <see cref="Vertex3f"/> that specify the direction of the view. It will be normalized. /// </param> /// <param name="upVector"> /// A <see cref="Vertex3f"/> that specify the up vector of the view camera abstraction. It will be normalized /// </param> /// <returns> /// It returns a view transformation matrix used to transform the world coordinate, in order to view /// the world from <paramref name="eyePosition"/>, looking at <paramref name="forwardVector"/> having /// an up direction equal to <paramref name="upVector"/>. /// </returns> public void LookAtDirection(Vertex3f eyePosition, Vertex3f forwardVector, Vertex3f upVector) { Vertex3f rightVector; // Normalize forward vector forwardVector.Normalize(); // Normalize up vector upVector.Normalize(); // Compute the right vector (cross-product between forward and up vectors; right is perperndicular to the plane) rightVector = forwardVector ^ upVector; rightVector.Normalize(); // Derive up vector upVector = rightVector ^ forwardVector; upVector.Normalize(); // Compute view matrix ModelMatrix lookatMatrix = new ModelMatrix(), positionMatrix = new ModelMatrix(); // Row 0: right vector lookatMatrix[0, 0] = rightVector.x; lookatMatrix[1, 0] = rightVector.y; lookatMatrix[2, 0] = rightVector.z; // Row 1: up vector lookatMatrix[0, 1] = upVector.x; lookatMatrix[1, 1] = upVector.y; lookatMatrix[2, 1] = upVector.z; // Row 2: opposite of forward vector lookatMatrix[0, 2] = forwardVector.x; lookatMatrix[1, 2] = forwardVector.y; lookatMatrix[2, 2] = forwardVector.z; // Eye position positionMatrix.Translate(-eyePosition); // Complete look-at matrix Set(lookatMatrix * positionMatrix); Set(GetInverseMatrix()); }
/// <summary> /// Setup this matrix to view the universe in a certain direction. /// </summary> /// <param name="eyePosition"> /// A <see cref="Vertex3f"/> that specify the eye position, in local coordinates. /// </param> /// <param name="forwardVector"> /// A <see cref="Vertex3f"/> that specify the direction of the view. It will be normalized. /// </param> /// <param name="upVector"> /// A <see cref="Vertex3f"/> that specify the up vector of the view camera abstraction. It will be normalized /// </param> /// <returns> /// It returns a view transformation matrix used to transform the world coordinate, in order to view /// the world from <paramref name="eyePosition"/>, looking at <paramref name="forwardVector"/> having /// an up direction equal to <paramref name="upVector"/>. /// </returns> public void LookAtDirection(Vertex3f eyePosition, Vertex3f forwardVector, Vertex3f upVector) { Vertex3f rightVector; forwardVector.Normalize(); upVector.Normalize(); rightVector = forwardVector ^ upVector; if (rightVector.Module() <= 0.0f) { rightVector = Vertex3f.UnitX; } rightVector.Normalize(); upVector = rightVector ^ forwardVector; upVector.Normalize(); // Compute view matrix ModelMatrix lookatMatrix = new ModelMatrix(); // Row 0: right vector lookatMatrix[0, 0] = rightVector.x; lookatMatrix[1, 0] = rightVector.y; lookatMatrix[2, 0] = rightVector.z; // Row 1: up vector lookatMatrix[0, 1] = upVector.x; lookatMatrix[1, 1] = upVector.y; lookatMatrix[2, 1] = upVector.z; // Row 2: opposite of forward vector lookatMatrix[0, 2] = -forwardVector.x; lookatMatrix[1, 2] = -forwardVector.y; lookatMatrix[2, 2] = -forwardVector.z; // Eye position lookatMatrix.Translate(-eyePosition); // Complete look-at matrix Set(lookatMatrix); }
/// <summary> /// Setup this matrix to view the universe in a certain direction. /// </summary> /// <param name="eyePosition"> /// A <see cref="Vertex3f"/> that specify the eye position, in local coordinates. /// </param> /// <param name="forwardVector"> /// A <see cref="Vertex3f"/> that specify the direction of the view. It will be normalized. /// </param> /// <param name="upVector"> /// A <see cref="Vertex3f"/> that specify the up vector of the view camera abstraction. It will be normalized /// </param> /// <returns> /// It returns a view transformation matrix used to transform the world coordinate, in order to view /// the world from <paramref name="eyePosition"/>, looking at <paramref name="forwardVector"/> having /// an up direction equal to <paramref name="upVector"/>. /// </returns> public void LookAtDirection(Vertex3f eyePosition, Vertex3f forwardVector, Vertex3f upVector) { Vertex3f rightVector; // Normalize forward vector forwardVector.Normalize(); // Normalize up vector upVector.Normalize(); // Compute the right vector (cross-product between forward and up vectors; right is perperndicular to the plane) rightVector = forwardVector ^ upVector; rightVector.Normalize(); // Derive up vector upVector = rightVector ^ forwardVector; upVector.Normalize(); // Compute view matrix ModelMatrix lookatMatrix = new ModelMatrix(), positionMatrix = new ModelMatrix(); // Row 0: right vector lookatMatrix[0, 0] = rightVector.x; lookatMatrix[1, 0] = rightVector.y; lookatMatrix[2, 0] = rightVector.z; // Row 1: up vector lookatMatrix[0, 1] = upVector.x; lookatMatrix[1, 1] = upVector.y; lookatMatrix[2, 1] = upVector.z; // Row 2: opposite of forward vector lookatMatrix[0, 2] = forwardVector.x; lookatMatrix[1, 2] = forwardVector.y; lookatMatrix[2, 2] = forwardVector.z; // Eye position positionMatrix.Translate(-eyePosition); // Complete look-at matrix Set(lookatMatrix * positionMatrix); Set(GetInverseMatrix()); }