Example #1
0
        public void Matrix3()
        {
            FMat3 fm  = FMat3.FromQuaternion(FQuat.Euler(( Fix64 )30, ( Fix64 )(-20), ( Fix64 )49.342f));
            Mat3  m   = Mat3.FromQuaternion(Quat.Euler(30, -20, 49.342f));
            FVec3 fv  = new FVec3(12.5f, 9, 8);
            FVec3 fv2 = new FVec3(4, 6, 9);
            Vec3  v   = new Vec3(12.5f, 9, 8);
            Vec3  v2  = new Vec3(4, 6, 9);

            fv  = fm.TransformPoint(fv);
            fv2 = fm.TransformVector(fv2);
            v   = m.TransformPoint(v);
            v2  = m.TransformVector(v2);
            this._output.WriteLine(fm.ToString());
            this._output.WriteLine(m.ToString());
            this._output.WriteLine(fv.ToString());
            this._output.WriteLine(fv2.ToString());
            this._output.WriteLine(v.ToString());
            this._output.WriteLine(v2.ToString());
            fm = FMat3.LookAt(fv, fv2);
            m  = Mat3.LookAt(v, v2);
            this._output.WriteLine(fm.ToString());
            this._output.WriteLine(m.ToString());
            fv = fm.Euler();
            v  = m.Euler();
            this._output.WriteLine(fv.ToString());
            this._output.WriteLine(v.ToString());
            fm = FMat3.FromEuler(fv);
            m  = Mat3.FromEuler(v);
            this._output.WriteLine(fm.ToString());
            this._output.WriteLine(m.ToString());
            fm = FMat3.FromScale(fv);
            m  = Mat3.FromScale(v);
            this._output.WriteLine(fm.ToString());
            this._output.WriteLine(m.ToString());
            fm = FMat3.FromCross(fv);
            m  = Mat3.FromCross(v);
            this._output.WriteLine(fm.ToString());
            this._output.WriteLine(m.ToString());
            fm = FMat3.FromOuterProduct(fv, fv2);
            m  = Mat3.FromOuterProduct(v, v2);
            this._output.WriteLine(fm.ToString());
            this._output.WriteLine(m.ToString());
            fm = FMat3.FromRotationAxis(( Fix64 )35, fv);
            m  = Mat3.FromRotationAxis(35, v);
            this._output.WriteLine(fm.ToString());
            this._output.WriteLine(m.ToString());
            fm = FMat3.NonhomogeneousInverse(fm);
            m  = Mat3.NonhomogeneousInvert(m);
            this._output.WriteLine(fm.ToString());
            this._output.WriteLine(m.ToString());
        }
        static void AddThicknessLine(Camera camera, Vec3 start, Vec3 end, float thickness)
        {
            Vec3  diff       = end - start;
            Vec3  direction  = diff.GetNormalize();
            Quat  rotation   = Quat.FromDirectionZAxisUp(direction);
            float length     = diff.Length();
            float thickness2 = thickness;

            Mat4 t = new Mat4(rotation.ToMat3() * Mat3.FromScale(new Vec3(length, thickness2, thickness2)),
                              (start + end) * .5f);

            if (addThicknessLinePositions == null)
            {
                GeometryGenerator.GenerateBox(new Vec3(1, 1, 1), out addThicknessLinePositions, out addThicknessLineIndices);
            }
            camera.DebugGeometry.AddVertexIndexBuffer(addThicknessLinePositions, addThicknessLineIndices, t, false, true);
        }
Example #3
0
        ColorValue RenderPixel(Vec2I pixelIndex)
        {
            int triangleIndex = lightmapTriangleMap[pixelIndex.Y][pixelIndex.X];

            if (triangleIndex == -1)
            {
                return(new ColorValue(-1, -1, -1, -1));
            }

            Mesh mesh = lightmapMeshObject.Mesh;

            Mat4 transform = new Mat4(lightmapMeshObject.Rotation.ToMat3() *
                                      Mat3.FromScale(lightmapMeshObject.Scale), lightmapMeshObject.Position);

            Vec3 position;
            Vec3 normal;
            {
                Vec2 texCoord = GetTexCoordByPixelIndex(pixelIndex);

                int index0 = mesh.Indices[triangleIndex * 3 + 0];
                int index1 = mesh.Indices[triangleIndex * 3 + 1];
                int index2 = mesh.Indices[triangleIndex * 3 + 2];

                Vec3 position0 = transform * mesh.Positions[index0];
                Vec3 position1 = transform * mesh.Positions[index1];
                Vec3 position2 = transform * mesh.Positions[index2];

                Vec3 normal0 = lightmapMeshObject.Rotation * mesh.Normals[index0];
                Vec3 normal1 = lightmapMeshObject.Rotation * mesh.Normals[index1];
                Vec3 normal2 = lightmapMeshObject.Rotation * mesh.Normals[index2];

                Vec2 texCoord0 = mesh.LightmapTexCoords[index0];
                Vec2 texCoord1 = mesh.LightmapTexCoords[index1];
                Vec2 texCoord2 = mesh.LightmapTexCoords[index2];

                float coef0;
                float coef1;
                GetTriangePositionCoefficients(ref texCoord0, ref texCoord1, ref texCoord2,
                                               ref texCoord, out coef0, out coef1);

                //calculate position
                {
                    Vec3 p01 = Vec3.Lerp(position0, position1, coef0);
                    Vec3 p02 = Vec3.Lerp(position0, position2, coef0);
                    position = Vec3.Lerp(p01, p02, coef1);
                }

                //calculate normal
                {
                    Vec3 p01 = Vec3.Lerp(normal0, normal1, coef0);
                    Vec3 p02 = Vec3.Lerp(normal0, normal2, coef0);
                    normal = Vec3.Lerp(p01, p02, coef1);
                    normal.Normalize();
                }
            }

            //calculate pixel color
            ColorValue resultColor = new ColorValue(0, 0, 0);

            foreach (MyLight light in lights)
            {
                //simple culling method. need use grid
                if (!light.Bounds.IsContainsPoint(position))
                {
                    continue;
                }

                //calculate illumination
                float illumination = light.GetIllumination(position, normal);
                if (illumination <= .00001f)
                {
                    continue;
                }

                //check for direct visibility
                bool shadowed = false;

                if (calculateShadows)
                {
                    Ray ray = light.GetCheckVisibilityRay(position);

                    RayCastResult result = physicsScene.RayCast(ray, contactGroup);
                    if (result.Shape != null)
                    {
                        shadowed = true;
                    }
                }

                ColorValue color = light.DiffuseColor * illumination;

                if (shadowed)
                {
                    color *= new ColorValue(1, 1, 1) - shadowColor;
                }

                resultColor += color;
            }

            resultColor.Alpha = 1;
            return(resultColor);
        }
Example #4
0
        bool ParseColladaNode(XmlNode colladaNode)
        {
            //globalScale, yAxisUp
            {
                XmlNode assetNode = XmlUtils.FindChildNode(colladaNode, "asset");
                if (assetNode != null)
                {
                    XmlNode upAxisNode = XmlUtils.FindChildNode(assetNode, "up_axis");
                    if (upAxisNode != null)
                    {
                        if (upAxisNode.InnerText == "Z_UP")
                        {
                            yAxisUp = false;
                        }
                        else if (upAxisNode.InnerText == "X_UP")
                        {
                            Error("X up axis is not supported.");
                            return(false);
                        }
                    }

                    XmlNode unitNode = XmlUtils.FindChildNode(assetNode, "unit");
                    if (unitNode != null)
                    {
                        string meterStr = XmlUtils.GetAttribute(unitNode, "meter");
                        if (!string.IsNullOrEmpty(meterStr))
                        {
                            string fixedStr = meterStr.Replace(',', '.');

                            if (!float.TryParse(fixedStr, out globalScale))
                            {
                                Error("Invalid \"meter\" attribute of \"unit\" node.");
                                return(false);
                            }
                        }
                    }
                }
            }

            //library_geometries
            if (!ParseGeometries(colladaNode))
            {
                return(false);
            }

            //library_visual_scenes
            foreach (XmlNode visualScenesNode in colladaNode.ChildNodes)
            {
                if (visualScenesNode.Name != "library_visual_scenes")
                {
                    continue;
                }

                foreach (XmlNode visualSceneNode in visualScenesNode.ChildNodes)
                {
                    if (visualSceneNode.Name != "visual_scene")
                    {
                        continue;
                    }

                    foreach (XmlNode nodeNode in visualSceneNode.ChildNodes)
                    {
                        if (nodeNode.Name != "node")
                        {
                            continue;
                        }

                        Mat4 currentTransform = Mat4.Identity;

                        if (globalScale != 1)
                        {
                            Mat4 m = Mat3.FromScale(new Vec3(globalScale, globalScale, globalScale)).ToMat4();
                            currentTransform *= m;
                        }

                        if (yAxisUp)
                        {
                            Mat4 mayaToNeoAxisRotation = new Mat4(
                                new Vec4(0, 1, 0, 0),
                                new Vec4(0, 0, 1, 0),
                                new Vec4(1, 0, 0, 0),
                                new Vec4(0, 0, 0, 1));

                            currentTransform *= mayaToNeoAxisRotation;
                        }

                        if (!ParseNode(currentTransform, nodeNode))
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Example #5
0
        bool ParseNode(Mat4 nodeTransform, XmlNode node)
        {
            string nodeId = XmlUtils.GetAttribute(node, "id");

            Mat4 currentTransform = nodeTransform;

            foreach (XmlNode childNode in node.ChildNodes)
            {
                if (childNode.Name == "matrix")
                {
                    float[] values = ConvertStringToFloatArray(childNode.InnerText);
                    if (values == null || values.Length != 16)
                    {
                        Error("Invalid format of \"matrix\" node. Node \"{0}\".", nodeId);
                        return(false);
                    }
                    Mat4 matrix = new Mat4(values);
                    currentTransform *= matrix;
                    continue;
                }

                if (childNode.Name == "translate")
                {
                    float[] values = ConvertStringToFloatArray(childNode.InnerText);
                    if (values == null || values.Length != 3)
                    {
                        Error("Invalid format of \"translate\" node. Node \"{0}\".", nodeId);
                        return(false);
                    }
                    Vec3 translate = new Vec3(values[0], values[1], values[2]);
                    currentTransform *= Mat4.FromTranslate(translate);
                    continue;
                }

                if (childNode.Name == "rotate")
                {
                    float[] values = ConvertStringToFloatArray(childNode.InnerText);
                    if (values == null || values.Length != 4)
                    {
                        Error("Invalid format of \"rotate\" node. Node \"{0}\".", nodeId);
                        return(false);
                    }

                    Vec3   axis  = new Vec3(values[0], values[1], values[2]);
                    Radian angle = new Degree(values[3]).InRadians();

                    if (axis != Vec3.Zero)
                    {
                        axis.Normalize();
                        float halfAngle = .5f * angle;
                        float sin       = MathFunctions.Sin(halfAngle);
                        float cos       = MathFunctions.Cos(halfAngle);
                        Quat  r         = new Quat(axis * sin, cos);
                        r.Normalize();

                        currentTransform *= r.ToMat3().ToMat4();
                    }

                    continue;
                }

                if (childNode.Name == "scale")
                {
                    float[] values = ConvertStringToFloatArray(childNode.InnerText);
                    if (values == null || values.Length != 3)
                    {
                        Error("Invalid format of \"scale\" node. Node \"{0}\".", nodeId);
                        return(false);
                    }
                    Vec3 scale = new Vec3(values[0], values[1], values[2]);
                    currentTransform *= Mat3.FromScale(scale).ToMat4();
                    continue;
                }

                if (childNode.Name == "node")
                {
                    if (!ParseNode(currentTransform, childNode))
                    {
                        return(false);
                    }

                    continue;
                }

                if (childNode.Name == "instance_geometry")
                {
                    if (!ParseNodeInstanceGeometry(currentTransform, childNode))
                    {
                        return(false);
                    }

                    continue;
                }
            }

            return(true);
        }