Ejemplo n.º 1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="direction"></param>
 public DirectionalLight(vec3 direction)
 {
     this.Direction = direction;
 }
Ejemplo n.º 2
0
 public RectangleModel()
 {
     this.ModelSize = new vec3(xLength * 2, yLength * 2, (xLength + yLength) * 0.02f);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets max and min position of the AABB box that wraps specified <paramref name="model"/>.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="maxPosition"></param>
        /// <param name="minPosition"></param>
        public static void GetMaxMinPosition(this IModelSpace model, out vec3 maxPosition, out vec3 minPosition)
        {
            if (model == null)
            {
                throw new System.ArgumentNullException();
            }

            vec3 lengths = model.Lengths * model.Scale;

            maxPosition = model.WorldPosition + lengths / 2.0f;
            minPosition = model.WorldPosition - lengths / 2.0f;
        }
Ejemplo n.º 4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="v"></param>
 public vec2(vec3 v)
 {
     this.x = v.x;
     this.y = v.y;
 }
Ejemplo n.º 5
0
        public override void Parse(ObjVNFContext context)
        {
            ObjVNFMesh mesh = context.Mesh;

            vec3[] vertexes  = mesh.vertexes;// positions
            vec2[] texCoords = mesh.texCoords;
            vec3[] normals   = mesh.normals;
            if (vertexes == null || texCoords == null || normals == null)
            {
                return;
            }
            if (vertexes.Length != texCoords.Length || vertexes.Length != normals.Length)
            {
                return;
            }

            ObjVNFFace[] faces = mesh.faces;
            if (faces == null || faces.Length == 0)
            {
                return;
            }
            // if (not triangles) { return; }
            if (faces[0].VertexIndexes().Count() != 3)
            {
                return;
            }

            var tangents = new vec3[normals.Length];

            for (int i = 0; i < faces.Length; i++)
            {
                var face = faces[i] as ObjVNFTriangle;
                if (face == null)
                {
                    return;
                }                             // no dealing with quad.
                uint[] vertexIndexes = face.VertexIndexes();
                vec3   p0 = vertexes[vertexIndexes[0]];
                vec3   p1 = vertexes[vertexIndexes[1]];
                vec3   p2 = vertexes[vertexIndexes[2]];
                vec2   uv0 = texCoords[vertexIndexes[0]];
                vec2   uv1 = texCoords[vertexIndexes[1]];
                vec2   uv2 = texCoords[vertexIndexes[2]];
                vec3   q0 = p1 - p0, q1 = p2 - p0;
                float  u0 = uv0.x, v0 = uv0.y, u1 = uv1.x, v1 = uv1.y, u2 = uv2.x, v2 = uv2.y;
                float  coefficient = 1.0f / ((u1 - u0) * (v2 - v0) - (v1 - v0) * (u2 - u0));
                vec3   tangentFace;
                tangentFace.x = (v2 - v0) * q0.x + (v0 - v1) * q1.x;
                tangentFace.y = (v2 - v0) * q0.y + (v0 - v1) * q1.y;
                tangentFace.z = (v2 - v0) * q0.z + (v0 - v1) * q1.z;
                //vec3 binormalFace;
                //binormalFace.x = (u0 - u2) * q0.x + (u1 - u0) * q1.x;
                //binormalFace.y = (u0 - u2) * q0.y + (u1 - u0) * q1.y;
                //binormalFace.z = (u0 - u2) * q0.z + (u1 - u0) * q1.z;
                for (int t = 0; t < vertexIndexes.Length; t++)
                {
                    vec3 n = normals[vertexIndexes[t]].normalize();
                    tangents[vertexIndexes[t]] = tangentFace - tangentFace.dot(n) * n;
                }
            }

            mesh.tangents = tangents;
        }
Ejemplo n.º 6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="param"></param>
 /// <param name="camera"></param>
 /// <param name="ambient"></param>
 public ShadowVolumeAmbientEventArgs(ActionParams param, ICamera camera, vec3 ambient)
     : base(param, camera)
 {
     this.Ambient = ambient;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// (x, y, z) equals Color.FromArgb(255, x * 255, y * 255, z * 255);
 /// </summary>
 /// <param name="color"></param>
 /// <returns></returns>
 public static Color ToColor(this vec3 color)
 {
     return(Color.FromArgb(255, (int)(color.x * 255), (int)(color.y * 255), (int)(color.z * 255)));
 }
Ejemplo n.º 8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="col0"></param>
 /// <param name="col1"></param>
 /// <param name="col2"></param>
 public mat3(vec3 col0, vec3 col1, vec3 col2)
 {
     this.col0 = col0;
     this.col1 = col1;
     this.col2 = col2;
 }
Ejemplo n.º 9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="param"></param>
 /// <param name="camera"></param>
 /// <param name="ambient"></param>
 public BlinnPhongAmbientEventArgs(ActionParams param, ICamera camera, vec3 ambient)
     : base(param, camera)
 {
     this.Ambient = ambient;
 }
Ejemplo n.º 10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="axis"></param>
 /// <param name="angleInDegree"></param>
 /// <param name="button">MouseButtons 值之一,它指示曾按下的是哪个鼠标按钮。</param>
 /// <param name="clicks">鼠标按钮曾被按下的次数。</param>
 /// <param name="x">鼠标单击的 x 坐标(以像素为单位,以left为0)。相对<see cref="CtrlRoot"/>而言。</param>
 /// <param name="y">鼠标单击的 y 坐标(以像素为单位,以bottom为0)。相对<see cref="CtrlRoot"/>而言。</param>
 /// <param name="delta">鼠标轮已转动的制动器数的有符号计数。</param>
 public Rotation(vec3 axis, float angleInDegree, GLMouseButtons button, int clicks, int x, int y, int delta)
     : base(button, clicks, x, y, delta)
 {
     this.axis          = axis;
     this.angleInDegree = angleInDegree;
 }
Ejemplo n.º 11
0
        // <AnimTrack name="RootNode" count="61" has_scale="true">
        /// <summary>
        /// Tracks a bone's states of all frames in one pose.
        /// </summary>
        /// <param name="xAnimTrack"></param>
        /// <returns></returns>
        public static EZMAnimTrack Parse(System.Xml.Linq.XElement xElement)
        {
            EZMAnimTrack result = null;

            if (xElement.Name == "AnimTrack")
            {
                result = new EZMAnimTrack();
                {
                    var name = xElement.Attribute("name");
                    if (name != null)
                    {
                        result.BoneName = name.Value;
                    }
                }
                int  count    = 0;
                bool hasScale = false;
                {
                    var c = xElement.Attribute("count");
                    if (c != null)
                    {
                        count = int.Parse(c.Value);
                    }
                    var h = xElement.Attribute("has_scale");
                    if (h != null)
                    {
                        hasScale = bool.Parse(h.Value);
                    }
                }
                {
                    string[] parts = xElement.Value.Split(Separator.separators, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length % 10 != 0)
                    {
                        throw new Exception("Parsing failed.");
                    }
                    var  values = new float[parts.Length];
                    var  states = new EZMBoneState[parts.Length / 10];
                    int  index = 0;
                    vec3 p = new vec3(); Quaternion o = new Quaternion(); vec3 s = new vec3();
                    while (index < parts.Length)
                    {
                        {
                            var x = float.Parse(parts[index++]);
                            var y = float.Parse(parts[index++]);
                            var z = float.Parse(parts[index++]);
                            p = new vec3(x, y, z);
                        }
                        {
                            var x = float.Parse(parts[index++]);
                            var y = float.Parse(parts[index++]);
                            var z = float.Parse(parts[index++]);
                            var w = float.Parse(parts[index++]);
                            o = new Quaternion(x, y, z, w);
                        }
                        {
                            var x = float.Parse(parts[index++]);
                            var y = float.Parse(parts[index++]);
                            var z = float.Parse(parts[index++]);
                            s = new vec3(x, y, z);
                        }

                        states[index / 10 - 1] = new EZMBoneState(p, o, s);
                    }
                    result.States = states;
                }
            }

            return(result);
        }
Ejemplo n.º 12
0
        public static vec3 ToVec3(this float[] array, int startIndex = 0)
        {
            vec3 result = new vec3(array[startIndex], array[startIndex + 1], array[startIndex + 2]);

            return(result);
        }
Ejemplo n.º 13
0
        public DiskModel(float radius, float holeRadius, float thickness, uint sliceCount, uint secondSliceCount)
        {
            uint upPlane, downPlane;

            uint[] upSphere   = new uint[sliceCount];
            uint[] downSphere = new uint[sliceCount];
            {
                var  positions = new vec3[sliceCount * secondSliceCount + sliceCount + sliceCount];
                uint t         = 0;
                var  y         = new vec3(0, 1, 0);
                for (int i = 0; i < sliceCount; i++)
                {
                    upSphere[i] = t;
                    double di     = 2 * Math.PI * i / sliceCount;
                    var    center = new vec3(radius * (float)Math.Sin(di), 0, radius * (float)Math.Cos(di));
                    float  length = center.length();
                    center = center.normalize();
                    for (int j = 0; j < secondSliceCount; j++)
                    {
                        double dj = Math.PI / 2 + Math.PI * j / (secondSliceCount - 1);
                        positions[t++] = center * (length - thickness * (float)Math.Cos(dj))
                                         + y * (thickness * (float)Math.Sin(dj));
                    }
                    downSphere[i] = t - 1;
                }
                upPlane = t;
                for (int i = 0; i < sliceCount; i++)
                {
                    double di = 2 * Math.PI * i / sliceCount;
                    positions[t++] = new vec3(holeRadius * (float)Math.Sin(di), thickness, holeRadius * (float)Math.Cos(di));
                }
                downPlane = t;
                for (int i = 0; i < sliceCount; i++)
                {
                    double di = 2 * Math.PI * i / sliceCount;
                    positions[t++] = new vec3(holeRadius * (float)Math.Sin(di), -thickness, holeRadius * (float)Math.Cos(di));
                }
                BoundingBox box = positions.Move2Center();
                this.positions = positions;
                this.modelSize = box.MaxPosition - box.MinPosition;
            }
            {
                var indexes = new uint[sliceCount * (secondSliceCount - 1) * 2 * 3
                                       + sliceCount * 2 * 3 + sliceCount * 2 * 3
                                       + sliceCount * 2 * 3];
                uint t = 0;
                for (uint i = 0; i < sliceCount; i++)
                {
                    for (uint j = 0; j < secondSliceCount - 1; j++)
                    {
                        indexes[t++] = i * secondSliceCount + j;
                        indexes[t++] = i * secondSliceCount + (j + 1) % secondSliceCount;
                        indexes[t++] = (i * secondSliceCount + secondSliceCount + (j + 1) % secondSliceCount) % (sliceCount * secondSliceCount);

                        indexes[t++] = i * secondSliceCount + j;
                        indexes[t++] = (i * secondSliceCount + secondSliceCount + (j + 1) % secondSliceCount) % (sliceCount * secondSliceCount);
                        indexes[t++] = (i * secondSliceCount + secondSliceCount + j) % (sliceCount * secondSliceCount);
                    }
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = upPlane + i;
                    indexes[t++] = upSphere[i];
                    indexes[t++] = upPlane + (i + 1) % sliceCount;

                    indexes[t++] = upPlane + (i + 1) % sliceCount;
                    indexes[t++] = upSphere[i];
                    indexes[t++] = upSphere[(i + 1) % sliceCount];
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = downPlane + i;
                    indexes[t++] = downPlane + (i + 1) % sliceCount;
                    indexes[t++] = downSphere[i];

                    indexes[t++] = downPlane + (i + 1) % sliceCount;
                    indexes[t++] = downSphere[(i + 1) % sliceCount];
                    indexes[t++] = downSphere[i];
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = upPlane + i;
                    indexes[t++] = upPlane + (i + 1) % sliceCount;
                    indexes[t++] = downPlane + i;

                    indexes[t++] = downPlane + i;
                    indexes[t++] = upPlane + (i + 1) % sliceCount;
                    indexes[t++] = downPlane + (i + 1) % sliceCount;
                }
                this.indexes = indexes;
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="mat3"/> struct.
 /// This matrix is the identity matrix scaled by <paramref name="scale"/>.
 /// </summary>
 /// <param name="scale">The scale.</param>
 public mat3(float scale)
 {
     this.col0 = new vec3(scale, 0, 0);
     this.col1 = new vec3(0, scale, 0);
     this.col2 = new vec3(0, 0, scale);
 }
Ejemplo n.º 15
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="angleDegree">ANgle in Degree.</param>
 /// <param name="v"></param>
 /// <returns></returns>
 public static mat4 rotate(float angleDegree, vec3 v)
 {
     return(rotate(mat4.identity(), angleDegree, v));
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="mat3"/> struct.
 /// The matrix is initialised with the <paramref name="cols"/>.
 /// </summary>
 /// <param name="cols">The colums of the matrix.</param>
 public mat3(vec3[] cols)
 {
     this.col0 = cols[0];
     this.col1 = cols[1];
     this.col2 = cols[2];
 }
Ejemplo n.º 17
0
        public override void SetUniform(ShaderProgram program)
        {
            vec3 value = this.value;

            program.SetUniform(VarName, value.x, value.y, value.z);
        }
Ejemplo n.º 18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="projection"></param>
        /// <param name="view"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public string ToString(mat4 projection, mat4 view, mat4 model)
        {
            StringBuilder builder = BasicInfo();

            builder.AppendLine();

            vec3[] positions = this.Positions;
            if (positions == null)
            {
                positions = new vec3[0];
            }
            uint[] indexes       = this.Indexes;
            var    worldPos      = new vec4[positions.Length];
            var    viewPos       = new vec4[positions.Length];
            var    projectionPos = new vec4[positions.Length];
            var    normalizedPos = new vec3[positions.Length];
            var    screenPos     = new vec3[positions.Length];

            builder.Append("Positions in World Space:");
            builder.AppendLine();

            for (int i = 0; i < positions.Length; i++)
            {
                var pos4 = new vec4(positions[i], 1);
                worldPos[i] = model * pos4;
                builder.Append('['); builder.Append(indexes[i]); builder.Append("]: ");
                builder.Append(worldPos[i]);
                builder.AppendLine();
            }

            builder.Append("Positions in Camera/View Space:");
            builder.AppendLine();

            for (int i = 0; i < positions.Length; i++)
            {
                var pos4 = new vec4(positions[i], 1);
                viewPos[i] = view * worldPos[i];
                builder.Append('['); builder.Append(indexes[i]); builder.Append("]: ");
                builder.Append(viewPos[i]);
                builder.AppendLine();
            }
            builder.Append("Positions in Projection Space:");
            builder.AppendLine();
            for (int i = 0; i < positions.Length; i++)
            {
                projectionPos[i] = projection * viewPos[i];
                builder.Append('['); builder.Append(indexes[i]); builder.Append("]: ");
                builder.Append(projectionPos[i]);
                builder.AppendLine();
            }
            builder.Append("Positions in Normalized Space:");
            builder.AppendLine();
            for (int i = 0; i < positions.Length; i++)
            {
                builder.Append('['); builder.Append(indexes[i]); builder.Append("]: ");
                normalizedPos[i] = new vec3(projectionPos[i] / projectionPos[i].w);
                builder.Append(normalizedPos[i]);
                builder.AppendLine();
            }
            builder.Append("Positions in Screen Space:");
            builder.AppendLine();
            int x, y, width, height;

            OpenGL.GetViewport(out x, out y, out width, out height);
            float near, far;

            OpenGL.GetDepthRange(out near, out far);
            for (int i = 0; i < positions.Length; i++)
            {
                builder.Append('['); builder.Append(indexes[i]); builder.Append("]: ");
                screenPos[i] = new vec3(
                    normalizedPos[i].x * width / 2 + (x + width / 2),
                    normalizedPos[i].y * height / 2 + (y + height / 2),
                    normalizedPos[i].z * (far - near) / 2 + ((far + near) / 2)
                    );
                builder.Append(screenPos[i]);
                builder.AppendLine();
            }

            return(builder.ToString());
        }
Ejemplo n.º 19
0
 public GroundModel()
 {
     this.ModelSize = new vec3(xLength * 2, yLength * 2, zLength * 2);
 }
Ejemplo n.º 20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="radius"></param>
        /// <param name="latitudeParts">用纬线把地球切割为几块。</param>
        /// <param name="longitudeParts">用经线把地球切割为几块。</param>
        /// <param name="colorGenerator"></param>
        /// <returns></returns>
        internal SphereModel(float radius = 1.0f, int latitudeParts = 10, int longitudeParts = 40, Func <int, int, vec3> colorGenerator = null)
        {
            if (radius <= 0.0f || latitudeParts < 2 || longitudeParts < 3)
            {
                throw new Exception();
            }

            if (colorGenerator == null)
            {
                colorGenerator = defaultColorGenerator;
            }

            int vertexCount = (latitudeParts + 1) * (longitudeParts);

            this.positions = new vec3[vertexCount];
            this.normals   = new vec3[vertexCount];
            this.colors    = new vec3[vertexCount];

            int indexCount = (latitudeParts) * (2 * (longitudeParts + 1) + 1);

            this.indexes = new uint[indexCount];

            int index = 0;

            for (int i = 0; i < latitudeParts + 1; i++)
            {
                double theta = (latitudeParts - i * 2) * Math.PI / 2 / latitudeParts;
                double y     = radius * Math.Sin(theta);
                for (int j = 0; j < longitudeParts; j++)
                {
                    double x = radius * Math.Cos(theta) * Math.Sin(j * Math.PI * 2 / longitudeParts);
                    double z = radius * Math.Cos(theta) * Math.Cos(j * Math.PI * 2 / longitudeParts);

                    vec3 position = new vec3((float)x, (float)y, (float)z);
                    this.positions[index] = position;

                    this.normals[index] = position.normalize();

                    this.colors[index] = colorGenerator(i, j);

                    index++;
                }
            }

            // 索引
            index = 0;
            for (int i = 0; i < latitudeParts; i++)
            {
                for (int j = 0; j < (longitudeParts); j++)
                {
                    this.indexes[index++] = (uint)((longitudeParts) * (i + 0) + j);
                    this.indexes[index++] = (uint)((longitudeParts) * (i + 1) + j);
                }
                {
                    this.indexes[index++] = (uint)((longitudeParts) * (i + 0) + 0);
                    this.indexes[index++] = (uint)((longitudeParts) * (i + 1) + 0);
                }
                // use
                // GL.Enable(GL.GL_PRIMITIVE_RESTART);
                // GL.PrimitiveRestartIndex(uint.MaxValue);
                // GL.Disable(GL.GL_PRIMITIVE_RESTART);
                this.indexes[index++] = uint.MaxValue;
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Get front vector in world space.
        /// </summary>
        /// <returns></returns>
        public static vec3 GetFront(this IViewCamera camera)
        {
            vec3 result = camera.Target - camera.Position;

            return(result);
        }
Ejemplo n.º 22
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="position"></param>
 public PointLight(vec3 position)
 {
     this.WorldPosition = position;
 }
Ejemplo n.º 23
0
        /// <summary>
        /// Get back vector in world space.
        /// </summary>
        /// <returns></returns>
        public static vec3 GetBack(this IViewCamera camera)
        {
            vec3 result = camera.Position - camera.Target;

            return(result);
        }
Ejemplo n.º 24
0
 public CubeModel()
 {
     this.ModelSize = new vec3(xLength * 2, yLength * 2, zLength * 2);
 }
Ejemplo n.º 25
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="angleDegree"></param>
 /// <param name="axis"></param>
 public void Parse(out float angleDegree, out vec3 axis)
 {
     angleDegree = (float)(Math.Acos(w) * 2 * 180.0 / Math.PI);
     axis        = (new vec3(x, y, z)).normalize();
 }