Exemple #1
0
        private static TexInfo CreateTexInfoFromBitmap(Bitmap bitmap)
        {
            TexInfo ret = null;

            if (bitmap != null)
            {
                ret = new TexInfo();

                ret.has_alpha        = bitmap.HasAlpha;
                ret.needs_alpha_test = false;                   // ad-hock

                GLES20.GlPixelStorei(GLES20.GlUnpackAlignment, 1);
                int[] tex = new int[1];
                GLES20.GlGenTextures(1, tex, 0);
                ret.tex = tex [0];

                GLES20.GlBindTexture(GLES20.GlTexture2d, ret.tex);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear);

                GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0);
                bitmap.Recycle();
            }

            return(ret);
        }
Exemple #2
0
        private void drawNonAlpha(ShellSurface surface, GLSL glsl)
        {
            var mats = surface.RenderLists;

            int max = mats.Count;

            // draw non-alpha material
//			GLES20.GlEnable(GLES20.GlCullFace);
            GLES20.GlEnable(2884);
            GLES20.GlCullFace(GLES20.GlBack);
            GLES20.GlDisable(GLES20.GlBlend);
            for (int r = 0; r < max; r++)
            {
                var     mat = mats[r];
                TexInfo tb  = null;
                if (mat.material.texture != null)
                {
                    tb = TextureFile.FetchTexInfo(mat.material.texture);
                }
                if (mat.material.diffuse_color[3] >= 1.0 && (tb == null || !tb.has_alpha))
                {
                    drawOneMaterial(glsl, surface, mat);
                }
            }
        }
Exemple #3
0
        private static TexInfo texture2DTGA(string path, int scale)
        {
            FileStream   fs = File.OpenRead(path);
            BinaryReader br = new BinaryReader(fs);

            // read header
            br.ReadBytes(2);
            int type = br.ReadByte();

            if (type != 2 && type != 10)
            {
                Log.Debug("TextureFile", "Unsupported TGA TYPE: " + type.ToString());
                Log.Debug("TextureFile", "fail to create png from tga: " + path);

                br.Close();
                fs.Close();
                return(null);
            }
            fs.Seek(12, SeekOrigin.Begin);
            short w     = br.ReadInt16();
            short h     = br.ReadInt16();
            byte  depth = br.ReadByte();
            byte  mode  = br.ReadByte();

            TexInfo tx = null;

            // create bitmap
            if (depth == 24 || depth == 32)
            {
                int[] buf = loadTGA(br, w, h, depth, type, mode);

                Bitmap bitmap = Bitmap.CreateBitmap(buf, w, h, Bitmap.Config.Argb8888);
                tx = CreateTexInfoFromBitmap(bitmap);
            }
            else
            {
                Log.Debug("TextureFile", "Unsupported TGA depth:" + depth.ToString());
                Log.Debug("TextureFile", "fail to create png from tga: " + path);
            }

            br.Close();
            fs.Close();

            return(tx);
        }
Exemple #4
0
        private static TexInfo CreateTexInfoFromBitmap(Bitmap bitmap)
        {
            TexInfo ret = null;
            if (bitmap != null) {
                ret = new TexInfo ();

                ret.has_alpha = bitmap.HasAlpha;
                ret.needs_alpha_test = false;	// ad-hock

                GLES20.GlPixelStorei(GLES20.GlUnpackAlignment, 1);
                int[] tex = new int[1];
                GLES20.GlGenTextures(1, tex, 0);
                ret.tex = tex [0];

                GLES20.GlBindTexture(GLES20.GlTexture2d, ret.tex);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear);

                GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0);
                bitmap.Recycle ();
            }

            return ret;
        }
Exemple #5
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");
        }
Exemple #6
0
        private void drawAlpha(ShellSurface surface, GLSL glsl, bool alpha_test)
        {
            int max = surface.RenderLists.Count();

            // draw alpha material
            GLES20.GlEnable(GLES20.GlBlend);
            for (int r = 0; r < max; r++)
            {
                var     mat = surface.RenderLists[r];
                TexInfo tb  = null;
                if (mat.material.texture != null)
                {
                    tb = TextureFile.FetchTexInfo(mat.material.texture);
                }
                if (alpha_test)
                {
                    if (tb != null && tb.needs_alpha_test)
                    {
                        if (mat.material.diffuse_color[3] < 1.0)
                        {
//							GLES20.GlDisable(GLES20.GL_CULL_FACE);
                            GLES20.GlDisable(2884);
                        }
                        else
                        {
//							GLES20.GlEnable(GLES20.GL_CULL_FACE);
                            GLES20.GlEnable(2884);
                        }
                        drawOneMaterial(glsl, surface, mat);
                    }
                }
                else
                {
                    if (tb != null)                      // has texture
                    {
                        if (!tb.needs_alpha_test)
                        {
                            if (tb.has_alpha)
                            {
                                if (mat.material.diffuse_color[3] < 1.0)
                                {
//									GLES20.GlDisable(GLES20.GL_CULL_FACE);
                                    GLES20.GlDisable(2884);
                                }
                                else
                                {
//									GLES20.GlEnable(GLES20.GL_CULL_FACE);
                                    GLES20.GlEnable(2884);
                                }
                                drawOneMaterial(glsl, surface, mat);
                            }
                            else if (mat.material.diffuse_color[3] < 1.0)
                            {
//								GLES20.GlDisable(GLES20.GL_CULL_FACE);
                                GLES20.GlDisable(2884);
                                drawOneMaterial(glsl, surface, mat);
                            }
                        }
                    }
                    else
                    {
                        if (mat.material.diffuse_color[3] < 1.0)
                        {
//							GLES20.GlDisable(GLES20.GL_CULL_FACE);
                            GLES20.GlDisable(2884);
                            drawOneMaterial(glsl, surface, mat);
                        }
                    }
                }
            }
        }