Exemplo n.º 1
0
            public Dictionary <long, NodeMeshData> Compile()
            {
                Dictionary <long, NodeMeshData> mesh = new Dictionary <long, NodeMeshData>();

                //calculate offset and rotation matrix
                var matrix = Mat4.Identity;

                if (parent != null || children.Count > 0)
                {
                    Mat4.Translate(matrix, matrix, offset);
                }

                Mat4.Multiply(matrix, matrix, Mat4.FromMat3(mat3, null));

                //generate new vertices
                for (int i = 0; i < vertices.Count; i++)
                {
                    float x = vertices[i].x;
                    float y = vertices[i].y;
                    float z = vertices[i].z;

                    //(vec3)vert = (mat4)modelViewMat * (vec3)vertices[i];
                    Vector3 transformed = new Vector3
                    {
                        x = matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12],
                        y = matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13],
                        z = matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14]
                    };

                    vertices[i] = transformed;
                }

                //initialize buffer
                for (int i = 0; i < textures.Length; i++)
                {
                    //initialize mesh for texture i
                    mesh[textures[i]] = new NodeMeshData
                    {
                        node = this
                    };
                }

                GenerateMesh(vertices, mesh);

                return(mesh);
            }
Exemplo n.º 2
0
    private static void CalcNodeBoundingBox(RSM.Node node, Mat4 _matrix)
    {
        var   v = new Vector3();
        var   box = node.box;
        var   nodes = node.model.nodes;
        var   vertices = node.vertices;
        float x, y, z;

        //find position
        node.matrix = _matrix.Clone();

        Mat4.Translate(node.matrix, node.matrix, node.pos);

        //dynamic or static model
        if (node.rotKeyframes.Count == 0)
        {
            Mat4.Rotate(node.matrix, node.matrix, node.rotAngle, node.rotAxis);
        }

        Mat4.Scale(node.matrix, node.matrix, node.scale);

        Mat4 matrix = node.matrix.Clone();

        if (!node.isOnly)
        {
            Mat4.Translate(matrix, matrix, node.offset);
        }

        Mat4.Multiply(matrix, matrix, Mat4.FromMat3(node.mat3, null));

        for (int i = 0, count = vertices.Count; i < count; i++)
        {
            x = vertices[i][0];
            y = vertices[i][1];
            z = vertices[i][2];

            v[0] = matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12];
            v[1] = matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13];
            v[2] = matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14];

            for (int j = 0; j < 3; j++)
            {
                box.min[j] = Math.Min(v[j], box.min[j]);
                box.max[j] = Math.Max(v[j], box.max[j]);
            }
        }

        for (int i = 0; i < 3; i++)
        {
            box.offset[i] = (box.max[i] + box.min[i]) / 2.0f;
            box.range[i]  = (box.max[i] - box.min[i]) / 2.0f;
            box.center[i] = box.min[i] + box.range[i];
        }

        for (int i = 0, count = nodes.Length; i < count; i++)
        {
            if (string.Equals(nodes[i].parentName, node.name) && !string.Equals(node.name, node.parentName))
            {
                nodes[i].parent = node;
                node.children.Add(nodes[i]);
                CalcNodeBoundingBox(nodes[i], node.matrix);
            }
        }
    }