예제 #1
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
        private void allocateWeightBuffer(RenderList mat, Dictionary <int, int> rename)
        {
            ByteBuffer rbb = ByteBuffer.AllocateDirect(Weight.Length);

            rbb.Order(ByteOrder.NativeOrder());
            mat.weight = rbb;
        }
예제 #2
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
        private RenderList buildNewMaterial(Material mat_orig, int offset, int count, Dictionary <int, int> rename, Dictionary <Dictionary <int, int>, ByteBuffer> rename_pool, int max_bone)
        {
            RenderList mat = new RenderList();

            mat.material         = mat_orig;
            mat.face_vert_offset = mat_orig.face_vert_offset + offset;
            mat.face_vert_count  = count - offset;
            mat.bone_num         = rename.Count;

            // find unoverwrapped hash
            foreach (var pool in rename_pool)
            {
                var map = pool.Key;
                var bb  = pool.Value;

                // check mapped
                foreach (var entry in rename)
                {
                    if (map.ContainsKey(entry.Key))
                    {
                        bb = null;
                    }
                }

                // find free byte buffer
                if (bb != null)
                {
                    rename_pool.Remove(map);
                    mat.weight = bb;
                    buildBoneRenamedWeightBuffers(mat, rename, max_bone);

                    foreach (var i in rename)
                    {
                        map [i.Key] = i.Value;
                    }
//					map.putAll(rename);
                    rename_pool[map] = bb;
                    Log.Debug("PMD", "Reuse buffer");
                    return(mat);
                }
            }

            // allocate new buffer
            Log.Debug("PMD", "Allocate new buffer");
            allocateWeightBuffer(mat, rename);
            buildBoneRenamedWeightBuffers(mat, rename, max_bone);

            var new_map = new Dictionary <int, int>(rename);

            rename_pool[new_map] = mat.weight;

            Log.Debug("PMD", "rename Bone for Material #" + mat.material.face_vert_offset + ", bones " + offset.ToString());
            foreach (var b in rename)
            {
                Log.Debug("PMD", String.Format("ID {0}: bone {1}", b.Value, b.Key));
            }
            return(mat);
        }
예제 #3
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
 private void buildBoneRenameInvMap(RenderList mat, Dictionary <int, int> rename, int max_bone)
 {
     mat.bone_inv_map = new int[max_bone];
     for (int i = 0; i < max_bone; i++)
     {
         mat.bone_inv_map[i] = -1;                 // initialize
     }
     foreach (var b in rename)
     {
         if (b.Value < max_bone)
         {
             mat.bone_inv_map[b.Value] = b.Key;
         }
     }
 }
예제 #4
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
        private int[] buildBoneRenameMap(RenderList mat, Dictionary <int, int> rename, int max_bone)
        {
            int[] rename_map = new int[Bone.Length];
            for (int i = 0; i < Bone.Length; i++)
            {
                rename_map[i] = 0;                 // initialize
            }
            foreach (var b in rename)
            {
                if (b.Value < max_bone)
                {
                    rename_map[b.Key] = b.Value;
                }
            }

            return(rename_map);
        }
예제 #5
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
        private void buildBoneRenamedWeightBuffers(RenderList mat, Dictionary <int, int> rename, int max_bone)
        {
            buildBoneRenameInvMap(mat, rename, max_bone);

            int[] map = buildBoneRenameMap(mat, rename, max_bone);

            short[] weight = new short[3];
            for (int i = mat.face_vert_offset; i < mat.face_vert_offset + mat.face_vert_count; i++)
            {
                int pos = (0x0000ffff & Index[i]);
                weight [0] = Weight [pos * 3 + 0];
                weight [1] = Weight [pos * 3 + 1];
                weight [2] = Weight [pos * 3 + 2];

                mat.weight.Position(pos * 3);
                mat.weight.Put((sbyte)map[weight[0]]);
                mat.weight.Put((sbyte)map[weight[1]]);
                mat.weight.Put((sbyte)weight[2]);
            }

            mat.weight.Position(0);
        }
예제 #6
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
        private void reconstructMaterial1(Material mat, int offset, Dictionary <Dictionary <int, int>, ByteBuffer> rename_pool, int max_bone)
        {
            var rename = new Dictionary <int, int>();
            int acc    = 0;

            for (int j = offset; j < mat.face_vert_count; j += 3)
            {
                int acc_prev = acc;
                acc = renameBone1(rename, mat.face_vert_offset + j + 0, acc);
                acc = renameBone1(rename, mat.face_vert_offset + j + 1, acc);
                acc = renameBone1(rename, mat.face_vert_offset + j + 2, acc);
                if (acc > max_bone)
                {
                    RenderList mat_new = buildNewMaterial(mat, offset, j, rename, rename_pool, acc_prev);
                    RenderLists.Add(mat_new);
                    reconstructMaterial1(mat, j, rename_pool, max_bone);
                    return;
                }
            }
            RenderList mat_new2 = buildNewMaterial(mat, offset, mat.face_vert_count, rename, rename_pool, max_bone);

            RenderLists.Add(mat_new2);
        }
예제 #7
0
        public override void SetupShellSurface()
        {
            // create buffers for render
            Log.Debug("Ruly", "Setup ShellSurface - the number of VertNormUv is " + VertNormUv.Length.ToString());
            ByteBuffer buf = ByteBuffer.AllocateDirect(VertNormUv.Length * sizeof(float));

            buf.Order(ByteOrder.NativeOrder());
            base.VertNormUvBuffer = buf.AsFloatBuffer() as FloatBuffer;
            base.VertNormUvBuffer.Put(VertNormUv);
            base.VertNormUvBuffer.Position(0);

            Log.Debug("Ruly", "Setup ShellSurface - the number of Index is " + Index.Length.ToString());
            buf = ByteBuffer.AllocateDirect(Index.Length * sizeof(short));
            buf.Order(ByteOrder.NativeOrder());
            base.IndexBuffer = buf.AsShortBuffer() as Java.Nio.ShortBuffer;
            base.IndexBuffer.Put(Index);
            base.IndexBuffer.Position(0);

            Log.Debug("Ruly", "Setup ShellSurface - the number of Bone is " + Bones.Length.ToString());
            foreach (var i in Bones)
            {
                RenderBones.Add(new RenderBone()
                {
                    bone           = i,
                    matrix         = new float[16],
                    matrix_current = new float[16],
                    quaternion     = new double[4],
                    updated        = false
                });
            }

            Log.Debug("Ruly", "Setup ShellSurface - the number of Materials is " + Materials.Length.ToString());
            foreach (var i in Materials)
            {
                var r = new RenderList()
                {
                    material = new Material()
                    {
                        diffuse_color    = i.diffuse_color,
                        power            = i.power,
                        specular_color   = i.specular_color,
                        emmisive_color   = i.emmisive_color,
                        toon_index       = i.toon_index,
                        edge_flag        = i.edge_flag,
                        face_vert_count  = i.face_vert_count,
                        texture          = i.texture,
                        sph              = i.sph,
                        spa              = i.spa,
                        face_vert_offset = i.face_vert_offset
                    },
                    face_vert_count  = i.face_vert_count,
                    face_vert_offset = i.face_vert_offset,
                    bone_num         = i.bone_num,
                    weight           = ByteBuffer.AllocateDirect(VertNormUv.Length / 8 * 3),
                    bone_inv_map     = i.bone_inv_map
                };
                r.weight.Order(ByteOrder.NativeOrder());
                r.weight.Put(i.weight);
                r.weight.Position(0);
                Log.Debug("RMC", "texture in material: " + i.texture);

                RenderLists.Add(r);
            }

//			IK = IKs;
            IK = null;                  // ad-hock

            toon_name = ToonNames;

            // clearnup buffers
            VertNormUv = null;
            Index      = null;

            Log.Debug("Ruly", "Setup ShellSurface - finish.");
        }
예제 #8
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
        private RenderList buildNewMaterial(Material mat_orig, int offset, int count, Dictionary<int, int> rename, Dictionary<Dictionary<int, int>, ByteBuffer> rename_pool, int max_bone)
        {
            RenderList mat = new RenderList();
            mat.material = mat_orig;
            mat.face_vert_offset = mat_orig.face_vert_offset + offset;
            mat.face_vert_count  = count - offset;
            mat.bone_num = rename.Count;

            // find unoverwrapped hash
            foreach(var pool in rename_pool) {
                var map = pool.Key;
                var bb = pool.Value;

                // check mapped
                foreach(var entry in rename) {
                    if(map.ContainsKey(entry.Key)) {
                        bb = null;
                    }
                }

                // find free byte buffer
                if(bb != null) {
                    rename_pool.Remove(map);
                    mat.weight = bb;
                    buildBoneRenamedWeightBuffers(mat, rename, max_bone);

                    foreach (var i in rename) {
                        map [i.Key] = i.Value;
                    }
            //					map.putAll(rename);
                    rename_pool[map] = bb;
                    Log.Debug("PMD", "Reuse buffer");
                    return mat;
                }
            }

            // allocate new buffer
            Log.Debug("PMD", "Allocate new buffer");
            allocateWeightBuffer(mat, rename);
            buildBoneRenamedWeightBuffers(mat, rename, max_bone);

            var new_map = new Dictionary<int, int>(rename);
            rename_pool[new_map] = mat.weight;

            Log.Debug("PMD", "rename Bone for Material #" + mat.material.face_vert_offset + ", bones " + offset.ToString());
            foreach (var b in rename) {
                Log.Debug("PMD", String.Format("ID {0}: bone {1}", b.Value, b.Key));
            }
            return mat;
        }
예제 #9
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
        private int[] buildBoneRenameMap(RenderList mat, Dictionary<int, int> rename, int max_bone)
        {
            int[] rename_map = new int[Bone.Length];
            for (int i = 0; i < Bone.Length; i++) {
                rename_map[i] = 0; // initialize
            }
            foreach (var b in rename) {
                if (b.Value < max_bone) {
                    rename_map[b.Key] = b.Value;
                }
            }

            return rename_map;
        }
예제 #10
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
 private void buildBoneRenameInvMap(RenderList mat, Dictionary<int, int> rename, int max_bone)
 {
     mat.bone_inv_map = new int[max_bone];
     for (int i = 0; i < max_bone; i++) {
         mat.bone_inv_map[i] = -1; // initialize
     }
     foreach (var b in rename) {
         if (b.Value < max_bone) {
             mat.bone_inv_map[b.Value] = b.Key;
         }
     }
 }
예제 #11
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
        private void buildBoneRenamedWeightBuffers(RenderList mat, Dictionary<int, int> rename, int max_bone)
        {
            buildBoneRenameInvMap(mat, rename, max_bone);

            int[] map = buildBoneRenameMap(mat, rename, max_bone);

            short[] weight = new short[3];
            for (int i = mat.face_vert_offset; i < mat.face_vert_offset + mat.face_vert_count; i++) {
                int pos = (0x0000ffff & Index[i]);
                weight [0] = Weight [pos * 3 + 0];
                weight [1] = Weight [pos * 3 + 1];
                weight [2] = Weight [pos * 3 + 2];

                mat.weight.Position(pos * 3);
                mat.weight.Put((sbyte) map[weight[0]]);
                mat.weight.Put((sbyte) map[weight[1]]);
                mat.weight.Put((sbyte) weight[2]);
            }

            mat.weight.Position(0);
        }
예제 #12
0
파일: PMD.cs 프로젝트: ruly-rudel/ruly
 private void allocateWeightBuffer(RenderList mat, Dictionary<int, int> rename)
 {
     ByteBuffer rbb = ByteBuffer.AllocateDirect(Weight.Length);
     rbb.Order(ByteOrder.NativeOrder());
     mat.weight = rbb;
 }
예제 #13
0
파일: PMF.cs 프로젝트: ruly-rudel/ruly
        public override void SetupShellSurface()
        {
            // create buffers for render
            Log.Debug("Ruly", "Setup ShellSurface - the number of VertNormUv is " + VertNormUv.Length.ToString());
            ByteBuffer buf = ByteBuffer.AllocateDirect (VertNormUv.Length * sizeof(float));
            buf.Order (ByteOrder.NativeOrder ());
            base.VertNormUvBuffer = buf.AsFloatBuffer() as FloatBuffer;
            base.VertNormUvBuffer.Put (VertNormUv);
            base.VertNormUvBuffer.Position (0);

            Log.Debug("Ruly", "Setup ShellSurface - the number of Index is " + Index.Length.ToString());
            buf = ByteBuffer.AllocateDirect (Index.Length * sizeof(short));
            buf.Order (ByteOrder.NativeOrder ());
            base.IndexBuffer = buf.AsShortBuffer() as Java.Nio.ShortBuffer;
            base.IndexBuffer.Put (Index);
            base.IndexBuffer.Position (0);

            Log.Debug("Ruly", "Setup ShellSurface - the number of Bone is " + Bones.Length.ToString());
            foreach (var i in Bones) {
                RenderBones.Add (new RenderBone () {
                    bone = i,
                    matrix = new float[16],
                    matrix_current = new float[16],
                    quaternion = new double[4],
                    updated = false
                });
            }

            Log.Debug("Ruly", "Setup ShellSurface - the number of Materials is " + Materials.Length.ToString());
            foreach (var i in Materials) {
                var r = new RenderList () {
                    material = new Material () {
                        diffuse_color = i.diffuse_color,
                        power = i.power,
                        specular_color = i.specular_color,
                        emmisive_color = i.emmisive_color,
                        toon_index = i.toon_index,
                        edge_flag = i.edge_flag,
                        face_vert_count = i.face_vert_count,
                        texture = i.texture,
                        sph = i.sph,
                        spa = i.spa,
                        face_vert_offset = i.face_vert_offset
                    },
                    face_vert_count = i.face_vert_count,
                    face_vert_offset = i.face_vert_offset,
                    bone_num = i.bone_num,
                    weight = ByteBuffer.AllocateDirect (VertNormUv.Length / 8 * 3),
                    bone_inv_map = i.bone_inv_map
                };
                r.weight.Order (ByteOrder.NativeOrder());
                r.weight.Put (i.weight);
                r.weight.Position (0);
                Log.Debug ("RMC", "texture in material: " + i.texture);

                RenderLists.Add (r);
            }

            //			IK = IKs;
            IK = null;	// ad-hock

            toon_name = ToonNames;

            // clearnup buffers
            VertNormUv = null;
            Index = null;

            Log.Debug("Ruly", "Setup ShellSurface - finish.");
        }
예제 #14
0
        private void drawOneMaterial(GLSL glsl, ShellSurface surface, RenderList mat)
        {
            // set motion
            if (surface.Animation) {
                if(mat.bone_inv_map == null) {
                    for (int j = 0; j < surface.RenderBones.Count; j++) {
                        var b = surface.RenderBones[j];
                        if(b != null) {
                            Array.Copy(b.matrix, 0, mBoneMatrix, j * 16, 16);
                        }
                    }
                } else {
                    for (int j = 0; j < mat.bone_inv_map.Length; j++) {
                        int inv = mat.bone_inv_map[j];
                        if (inv >= 0) {
                            var b = surface.RenderBones[inv];
                            Array.Copy(b.matrix, 0, mBoneMatrix, j * 16, 16);
                        }
                    }
                }
                GLES20.GlUniformMatrix4fv(glsl.muMBone, mat.bone_num, false, mBoneMatrix, 0);

                GLES20.GlEnableVertexAttribArray(glsl.maBlendHandle);
                GLES20.GlVertexAttribPointer(glsl.maBlendHandle, 3, GLES20.GlUnsignedByte, false, 0, mat.weight);
            }

            // initialize color
            for(int i = 0; i < mDifAmb.Count(); i++) {
                mDifAmb[i] = 1.0f;
            }

            // diffusion and ambient
            float wi = 0.6f;	// light color = (0.6, 0.6, 0.6)
            for(int i = 0; i < 3; i++) {
                mDifAmb[i] *= mat.material.diffuse_color[i] * wi + mat.material.emmisive_color[i];
            }
            mDifAmb[3] *= mat.material.diffuse_color[3];
            Vector.min(mDifAmb, 1.0f);
            GLES20.GlUniform4fv(glsl.muDif, 1, mDifAmb, 0);

            // speculation
            if (glsl.muPow >= 0) {
                GLES20.GlUniform4f(glsl.muSpec, mat.material.specular_color[0], mat.material.specular_color[1], mat.material.specular_color[2], 0);
                GLES20.GlUniform1f(glsl.muPow, mat.material.power);
            }

            // toon
            GLES20.GlUniform1i(glsl.msToonSampler, 0);
            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, TextureFile.FetchTexInfo(surface.toon_name[mat.material.toon_index]).tex);

            // texture
            GLES20.GlUniform1i(glsl.msTextureSampler, 1);
            GLES20.GlActiveTexture(GLES20.GlTexture1);
            if (mat.material.texture != null) {
                TexInfo tb = TextureFile.FetchTexInfo(mat.material.texture);
                if(tb != null) {
                    GLES20.GlBindTexture(GLES20.GlTexture2d, tb.tex);
                } else {	// avoid crash
                    GLES20.GlBindTexture(GLES20.GlTexture2d, TextureFile.FetchTexInfo(surface.toon_name[0]).tex);	// white texture using toon0.bmp
                    for(int i = 0; i < 3; i++) {	// for emulate premultiplied alpha
                        mDifAmb[i] *= mat.material.diffuse_color[3];
                    }
                }
            } else {
                GLES20.GlBindTexture(GLES20.GlTexture2d, TextureFile.FetchTexInfo(surface.toon_name[0]).tex);	// white texture using toon0.bmp
                for(int i = 0; i < 3; i++) {	// for emulate premultiplied alpha
                    mDifAmb[i] *= mat.material.diffuse_color[3];
                }
            }

            // sphere(sph)
            GLES20.GlUniform1i(glsl.msSphSampler, 2);
            GLES20.GlActiveTexture(GLES20.GlTexture2);
            if (mat.material.sph != null) {
                TexInfo tb = TextureFile.FetchTexInfo (mat.material.sph);
                if (tb != null) {
                    GLES20.GlBindTexture (GLES20.GlTexture2d, tb.tex);
                } else {	// avoid crash
                    GLES20.GlBindTexture (GLES20.GlTexture2d, TextureFile.FetchTexInfo (surface.toon_name [0]).tex);	// white texture using toon0.bmp
                }
            } else {
                GLES20.GlBindTexture(GLES20.GlTexture2d, TextureFile.FetchTexInfo(surface.toon_name[0]).tex);	// white texture using toon0.bmp
            }

            // sphere(spa)
            GLES20.GlUniform1i(glsl.msSpaSampler, 3);
            GLES20.GlActiveTexture(GLES20.GlTexture3);
            if (mat.material.spa != null) {
                TexInfo tb = TextureFile.FetchTexInfo (mat.material.spa);
                if (tb != null) {
                    GLES20.GlBindTexture (GLES20.GlTexture2d, tb.tex);
                }
            }

            // draw
            surface.IndexBuffer.Position (mat.face_vert_offset);
            GLES20.GlDrawElements(GLES20.GlTriangles, mat.face_vert_count, GLES20.GlUnsignedShort, surface.IndexBuffer);
            checkGlError("glDrawElements");
        }