Ejemplo n.º 1
0
        //---------------------------------------------
        static public Image loadTextureToThumbnail(string filename, int desiredSize)
        {
            string fullFileName = (filename);

            Bitmap bitmap = null;

            if (File.Exists(fullFileName))
            {
                Texture tex = TextureLoader.FromFile(BRenderDevice.getDevice(), fullFileName);
                Image   img = Image.FromStream(TextureLoader.SaveToStream(ImageFileFormat.Bmp, tex));
                bitmap = new Bitmap(img);

                tex.Dispose();
                tex = null;
            }
            else
            {
                return(null);

                /*Texture tex = TextureLoader.FromFile(BRenderDevice.getDevice(), EditorCore.CoreGlobals.getWorkPaths().mBlankTextureName);
                 * Image img = Image.FromStream(TextureLoader.SaveToStream(ImageFileFormat.Bmp, tex));
                 * bitmap = new Bitmap(img);
                 *
                 * tex.Dispose();
                 * tex = null;
                 * */
            }

            Image myThumbnail = bitmap.GetThumbnailImage(desiredSize, desiredSize, null, IntPtr.Zero);

            return(myThumbnail);
        }
Ejemplo n.º 2
0
        //-----------------------------------------------------------------------------
        public void camUpdate()
        {
            Matrix View;

            View = Matrix.LookAtLH(mEye, mLookAt, mUp);
            BRenderDevice.getDevice().SetTransform(TransformType.View, View);
        }
Ejemplo n.º 3
0
 public void loadFromDisk(string filename, TextureManagerCallbackType callback)
 {
     mFilename         = filename;
     mTexture          = TextureLoader.FromFile(BRenderDevice.getDevice(), filename);
     mCallbackFunction = callback;
     //Debug.Print("TextureManager loaded: " + filename);
 }
Ejemplo n.º 4
0
        static public Microsoft.DirectX.Direct3D.Effect loadShader(string filename, Macro [] defines)
        {
            Microsoft.DirectX.Direct3D.Effect shader = null;
            //Load Our Shader
            string errors = "";

            // if (mTerrainGPUShader == null || mTerrainGPUShader.Disposed == true)
            {
                try
                {
                    // if (File.Exists(filename))
                    {
                        shader = Microsoft.DirectX.Direct3D.Effect.FromFile(BRenderDevice.getDevice(), filename, defines, null, ShaderFlags.None, null, out errors);
                        if (shader == null)
                        {
                            MessageBox.Show("Shader did not load:\n " + errors);
                            return(null);
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Shader did not load");
                    throw (ex);
                }
            }
            return(shader);
        }
Ejemplo n.º 5
0
        private void compileShader(FixedFuncShaders.eFixedFuncShaderIndex id, string shaderText, string[] defines, bool vertexShader)
        {
            Macro [] Macrodefines = null;
            if (defines != null)
            {
                Macrodefines = new Macro[defines.Length];
                for (int k = 0; k < defines.Length; k++)
                {
                    Macrodefines[k].Definition = defines[k];
                }
            }
            GraphicsStream gs = ShaderLoader.CompileShader(shaderText, "main", Macrodefines, null, vertexShader ? "vs_2_0" : "ps_2_0", ShaderFlags.None);

            if (vertexShader)
            {
                BFixedFuncVS vsA = new BFixedFuncVS();
                vsA.mVS = new VertexShader(BRenderDevice.getDevice(), gs);
                vsA.mID = id;
                mVertexShaders.Add(vsA);
            }
            else
            {
                BFixedFuncPS vsA = new BFixedFuncPS();
                vsA.mPS = new PixelShader(BRenderDevice.getDevice(), gs);
                vsA.mID = id;
                mPixelShaders.Add(vsA);
            }
            gs.Close();
            gs = null;
        }
Ejemplo n.º 6
0
        //------------------------------------------------
        //abstracted functions to wrap internals

        static public void clear(bool zbuf, bool color, int colorVal, float zdepth, int stencil)
        {
            if (mbDeviceLost == true)
            {
                return;
            }

            ClearFlags flags = (zbuf ? ClearFlags.ZBuffer : 0) | (color? ClearFlags.Target:0);

            BRenderDevice.getDevice().Clear(flags, unchecked ((int)colorVal), zdepth, stencil);
        }
Ejemplo n.º 7
0
 public void reload()
 {
     if (mTexture != null)
     {
         mTexture.Dispose();
         mTexture = null;
     }
     mTexture = TextureLoader.FromFile(BRenderDevice.getDevice(), mFilename);
     if (mCallbackFunction != null)
     {
         mCallbackFunction(mFilename);
     }
 }
Ejemplo n.º 8
0
            public Pos(float _x, float _y, float _z)
            {
                x = _x; y = _y; z = _z;

                if (Pos.vertDecl == null)
                {
                    VertexElement[] elements = new VertexElement[]
                    {
                        new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                        VertexElement.VertexDeclarationEnd,
                    };
                    Pos.vertDecl = new VertexDeclaration(BRenderDevice.getDevice(), elements);
                }
            }
Ejemplo n.º 9
0
            public Pos16(float _x, float _y, float _z)
            {
                x = new float16(_x); y = new float16(_y); z = new float16(_z);
                w = new float16(0);

                if (Pos16.vertDecl == null)
                {
                    VertexElement[] elements = new VertexElement[]
                    {
                        new VertexElement(0, 0, DeclarationType.Float16Four, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                        VertexElement.VertexDeclarationEnd,
                    };
                    Pos16.vertDecl = new VertexDeclaration(BRenderDevice.getDevice(), elements);
                }
            }
Ejemplo n.º 10
0
            public Pos_Color(float _x, float _y, float _z, int _color)
            {
                x     = _x; y = _y; z = _z;
                color = _color;

                if (Pos_Color.vertDecl == null)
                {
                    VertexElement[] elements = new VertexElement[]
                    {
                        new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                        new VertexElement(0, 12, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),

                        VertexElement.VertexDeclarationEnd,
                    };
                    vertDecl = new VertexDeclaration(BRenderDevice.getDevice(), elements);
                }
            }
Ejemplo n.º 11
0
            public Pos_uv0(float _x, float _y, float _z, float _u0, float _v0)
            {
                x  = _x; y = _y; z = _z;
                u0 = _u0;
                v0 = _v0;

                if (Pos_uv0.vertDecl == null)
                {
                    VertexElement[] elements = new VertexElement[]
                    {
                        new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                        new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
                        VertexElement.VertexDeclarationEnd,
                    };
                    vertDecl = new VertexDeclaration(BRenderDevice.getDevice(), elements);
                }
            }
Ejemplo n.º 12
0
            public Pos_Color_uv0(float _x, float _y, float _z, float _u, float _v, int _color)
            {
                x     = _x; y = _y; z = _z;
                u     = _u; v = _v;
                color = _color;

                if (Pos_Color_uv0.vertDecl == null)
                {
                    VertexElement[] elements = new VertexElement[]
                    {
                        new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                        new VertexElement(0, 12, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
                        new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
                        VertexElement.VertexDeclarationEnd,
                    };
                    vertDecl = new VertexDeclaration(BRenderDevice.getDevice(), elements);
                }
            }
Ejemplo n.º 13
0
            public Pos_Normal_uv0(float _x, float _y, float _z, float _nx, float _ny, float _nz, float _u, float _v)
            {
                x  = _x; y = _y; z = _z;
                nx = _nx; ny = _ny; nz = _nz;
                u  = _u; v = _v;

                if (Pos_Normal_uv0.vertDecl == null)
                {
                    VertexElement[] elements = new VertexElement[]
                    {
                        new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                        new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
                        new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
                        new VertexElement(0, 32, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Tangent, 0),
                        VertexElement.VertexDeclarationEnd,
                    };
                    vertDecl = new VertexDeclaration(BRenderDevice.getDevice(), elements);
                }
            }
Ejemplo n.º 14
0
        unsafe private void initDefaultTextures()
        {
            int texSize = 2;

            mDefaultTextures[(int)eDefaultTextures.cDefTex_White] = new Texture(BRenderDevice.getDevice(), texSize, texSize, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
            GraphicsStream streamLht = mDefaultTextures[(int)eDefaultTextures.cDefTex_White].LockRectangle(0, 0);
            uint *         lht       = (uint *)streamLht.InternalDataPointer;

            lht[0] = 0xFFFFFFFF;
            mDefaultTextures[(int)eDefaultTextures.cDefTex_White].UnlockRectangle(0);


            mDefaultTextures[(int)eDefaultTextures.cDefTex_Black] = new Texture(BRenderDevice.getDevice(), texSize, texSize, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
            streamLht = mDefaultTextures[(int)eDefaultTextures.cDefTex_Black].LockRectangle(0, 0);
            lht       = (uint *)streamLht.InternalDataPointer;
            lht[0]    = 0xFF000000;
            mDefaultTextures[(int)eDefaultTextures.cDefTex_Black].UnlockRectangle(0);

            mDefaultTextures[(int)eDefaultTextures.cDefTex_Red] = new Texture(BRenderDevice.getDevice(), texSize, texSize, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
            streamLht = mDefaultTextures[(int)eDefaultTextures.cDefTex_Red].LockRectangle(0, 0);
            lht       = (uint *)streamLht.InternalDataPointer;
            lht[0]    = 0xFFFF0000;
            mDefaultTextures[(int)eDefaultTextures.cDefTex_Red].UnlockRectangle(0);

            mDefaultTextures[(int)eDefaultTextures.cDefTex_Green] = new Texture(BRenderDevice.getDevice(), texSize, texSize, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
            streamLht = mDefaultTextures[(int)eDefaultTextures.cDefTex_Green].LockRectangle(0, 0);
            lht       = (uint *)streamLht.InternalDataPointer;
            lht[0]    = 0xFF00FF00;
            mDefaultTextures[(int)eDefaultTextures.cDefTex_Green].UnlockRectangle(0);

            mDefaultTextures[(int)eDefaultTextures.cDefTex_Blue] = new Texture(BRenderDevice.getDevice(), texSize, texSize, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
            streamLht = mDefaultTextures[(int)eDefaultTextures.cDefTex_Blue].LockRectangle(0, 0);
            lht       = (uint *)streamLht.InternalDataPointer;
            lht[0]    = 0xFF0000FF;
            mDefaultTextures[(int)eDefaultTextures.cDefTex_Blue].UnlockRectangle(0);

            mDefaultTextures[(int)eDefaultTextures.cDefTex_Yellow] = new Texture(BRenderDevice.getDevice(), texSize, texSize, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
            streamLht = mDefaultTextures[(int)eDefaultTextures.cDefTex_Yellow].LockRectangle(0, 0);
            lht       = (uint *)streamLht.InternalDataPointer;
            lht[0]    = 0xFFFFFF00;
            mDefaultTextures[(int)eDefaultTextures.cDefTex_Yellow].UnlockRectangle(0);
        }
Ejemplo n.º 15
0
 void loadInternal(string filename)
 {
     string errors = "";
     {
         try
         {
             mShader = Microsoft.DirectX.Direct3D.Effect.FromFile(BRenderDevice.getDevice(), filename, null /*defines*/, null, null, ShaderFlags.None, null, out errors);
             if (mShader == null)
             {
                 MessageBox.Show("Shader did not load:\n " + errors);
                 return;
             }
         }
         catch (System.Exception ex)
         {
             MessageBox.Show("Shader did not load");
             throw (ex);
         }
     }
 }
Ejemplo n.º 16
0
            public Pos16_Color(float _x, float _y, float _z, int _color)
            {
                float16 tmp = new float16(0);

                tmp.fromFloat(_x); x = tmp.getInternalDat();
                tmp.fromFloat(_y); y = tmp.getInternalDat();
                tmp.fromFloat(_z); z = tmp.getInternalDat();
                w = 0;

                color = _color;

                if (Pos16_Color.vertDecl == null)
                {
                    VertexElement[] elements = new VertexElement[]
                    {
                        new VertexElement(0, 0, DeclarationType.Float16Four, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                        new VertexElement(0, 8, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
                        VertexElement.VertexDeclarationEnd,
                    };
                    Pos16_Color.vertDecl = new VertexDeclaration(BRenderDevice.getDevice(), elements);
                }
            }
Ejemplo n.º 17
0
 void loadInternalFromString(string shaderFile)
 {
     string errors = "";
     {
         try
         {
             mShader = Microsoft.DirectX.Direct3D.Effect.FromString(BRenderDevice.getDevice(), shaderFile, null /*defines*/, null, null, ShaderFlags.None, null, out errors);
             if (mShader == null)
             {
                 MessageBox.Show("Shader did not load:\n " + errors);
                 return;
             }
         }
         catch (System.Exception ex)
         {
             Console.ForegroundColor = ConsoleColor.Red;
             Console.WriteLine("Shader from string did not load");
             Console.ForegroundColor = ConsoleColor.White;
             throw (ex);
         }
     }
 }
Ejemplo n.º 18
0
 public PosW_uv0_uv1(float _x, float _y, float _z, float _w, float _u0, float _v0, float _u1, float _v1)
 {
     x  = _x;
     y  = _y;
     z  = _z;
     w  = _w;
     u0 = _u0;
     v0 = _v0;
     u1 = _u1;
     v1 = _v1;
     if (PosW_uv0_uv1.vertDecl == null)
     {
         VertexElement[] elements = new VertexElement[]
         {
             new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Position, 0),
             new VertexElement(0, 16, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
             new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 1),
             VertexElement.VertexDeclarationEnd,
         };
         vertDecl = new VertexDeclaration(BRenderDevice.getDevice(), elements);
     }
 }
Ejemplo n.º 19
0
        public static VertexDeclaration genVertexDecl(List <eVertexDeclElement> decls, bool sortForFVF, ref short vertexStructSize)
        {
            if (sortForFVF)
            {
                decls.Sort();
            }
            List <VertexElement> vList = new List <VertexElement>();

            vertexStructSize = 0;
            int textureCount = 0;

            for (int i = 0; i < decls.Count; i++)
            {
                switch (decls[i])
                {
                case eVertexDeclElement.cVDE_Position:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0));
                    vertexStructSize += sizeof(float) * 3;
                    break;

                case eVertexDeclElement.cVDE_BlendIndicies:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.BlendIndices, 0));
                    vertexStructSize += sizeof(int);
                    break;

                case eVertexDeclElement.cVDE_BlendWeight1:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float1, DeclarationMethod.Default, DeclarationUsage.BlendWeight, 0));
                    vertexStructSize += sizeof(float);
                    break;

                case eVertexDeclElement.cVDE_BlendWeight4:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.BlendWeight, 0));
                    vertexStructSize += sizeof(int);
                    break;

                case eVertexDeclElement.cVDE_Normal:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0));
                    vertexStructSize += sizeof(float) * 3;
                    break;

                case eVertexDeclElement.cVDE_Tangent:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Tangent, 0));
                    vertexStructSize += sizeof(float) * 3;
                    break;

                case eVertexDeclElement.cVDE_BiNormal:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.BiNormal, 0));
                    vertexStructSize += sizeof(float) * 3;
                    break;

                case eVertexDeclElement.cVDE_TexCoord2:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, (byte)textureCount));
                    vertexStructSize += sizeof(float) * 2;
                    textureCount++;
                    break;

                case eVertexDeclElement.cVDE_ColorDWORD:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0));
                    vertexStructSize += sizeof(int);
                    break;

                case eVertexDeclElement.cVDE_Pos16_4:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float16Four, DeclarationMethod.Default, DeclarationUsage.Position, 0));
                    vertexStructSize += sizeof(short) * 4;
                    break;
                }
            }

            VertexElement[] elements = new VertexElement[vList.Count + 1];
            int             c        = 0;

            for (c = 0; c < vList.Count; c++)
            {
                elements[c] = vList[c];
            }


            //CLM HACKOTRON
            //add in elements for instancing
            //elements[vList.Count + 0] = new VertexElement(1, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 4);
            //elements[vList.Count + 1] = new VertexElement(1, 16, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 5);
            //elements[vList.Count + 2] = new VertexElement(1, 32, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 6);
            //elements[vList.Count + 3] = new VertexElement(1, 48, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 7);

            elements[vList.Count] = VertexElement.VertexDeclarationEnd;

            return(new VertexDeclaration(BRenderDevice.getDevice(), elements));
        }
Ejemplo n.º 20
0
        static public Vector3 getRayPosFromMouseCoords(bool farPlane, Point pt)
        {
            Point cursorPos = Point.Empty;

            cursorPos = pt;


            //Point p1 = mParentWindow.PointToScreen(new Point(0, 0));
            //Point p2 = mParentWindow.PointToScreen(new Point(mParentWindow.Width, mParentWindow.Height));

            //int h = p2.Y - p1.Y;

            //// UIManager.ScreenToClient(ref cursorPos);

            //float xrat = (float)(cursorPos.X) / (float)(mParentWindow.Width);// - p1.X);
            //float yrat = (float)(cursorPos.Y) / (float)(mParentWindow.Height);// - p1.Y );

            ////update the cursor pos to fit into our d3d window
            ////cursorPos.X = (int)(mWidth * xrat) + p1.X;
            ////cursorPos.Y = (int)(mHeight * yrat) + p1.Y;

            ////cursorPos.X = (int)(mWidth * xrat);// +p1.X;
            ////cursorPos.Y = (int)(mHeight * yrat);// +p1.Y;

            ////cursorPos.X = (int)(mParentWindow.Width * xrat);// +p1.X;
            ////cursorPos.Y = (int)(mParentWindow.Height * yrat);// +p1.Y;

            //cursorPos.X = (int)((mParentWindow.Width + p1.X) * xrat);// +p1.X;
            //cursorPos.Y = (int)((mParentWindow.Height + p1.Y ) * yrat);// +p1.Y;
            ////mWidth

            //mParentWindow.Width
            float xrat = (float)(cursorPos.X) / (float)(mWindowWidth);
            float yrat = (float)(cursorPos.Y) / (float)(mWindowHeight);

            //cursorPos.X = (int)((mWidth + mXOffset) * xrat);
            //cursorPos.Y = (int)((mHeight + mYOffset) * yrat);
            cursorPos.X = (int)((mWidth) * xrat);
            cursorPos.Y = (int)((mHeight) * yrat);

            //cursorPos.X = (int)((mWindowWidth + mXOffset) * xrat);
            //cursorPos.Y = (int)((mWindowHeight + mYOffset) * yrat);

            //cursorPos = pt;

            // cursorPos = mParentWindow.PointToScreen(pt);


            Vector3 pos    = new Vector3(cursorPos.X, cursorPos.Y, (farPlane) ? 1f : 0f);
            Vector3 retval = new Vector3(0, 0, 0);


            Matrix mV, mP, mW;

            mW = BRenderDevice.getDevice().GetTransform(TransformType.World);
            mV = BRenderDevice.getDevice().GetTransform(TransformType.View);
            mP = BRenderDevice.getDevice().GetTransform(TransformType.Projection);
            Viewport vp;

            vp     = BRenderDevice.getDevice().Viewport;
            retval = Vector3.Unproject(pos, vp, mP, mV, mW);

            return(retval);
        }
Ejemplo n.º 21
0
        //---------------------------------------------
        static public Image loadTextureToThumbnail(string filename, int desiredSize)
        {
            string fullFileName = (filename);

            try
            {
                Bitmap bitmap = null;
                if (File.Exists(fullFileName))
                {
                    try
                    {
                        Texture tex = TextureLoader.FromFile(BRenderDevice.getDevice(), fullFileName);
                        Image   img = Image.FromStream(TextureLoader.SaveToStream(ImageFileFormat.Bmp, tex));
                        bitmap = new Bitmap(img);

                        tex.Dispose();
                        tex = null;
                    }
                    catch (System.Exception ex)
                    {
                        Texture tex = BRenderDevice.getTextureManager().getDefaultTexture(eDefaultTextures.cDefTex_Red);
                        Image   img = Image.FromStream(TextureLoader.SaveToStream(ImageFileFormat.Bmp, tex));
                        bitmap = new Bitmap(img);
                        tex.Dispose();
                        tex = null;

                        ex.ToString();
                    }
                }
                else
                {
                    Texture tex = BRenderDevice.getTextureManager().getDefaultTexture(eDefaultTextures.cDefTex_Red);
                    try
                    {
                        Image img = Image.FromStream(TextureLoader.SaveToStream(ImageFileFormat.Bmp, tex));
                        bitmap = new Bitmap(img);
                    }
                    catch (System.Exception ex)
                    {
                        bitmap = null;

                        ex.ToString();
                    }


                    tex.Dispose();
                    tex = null;
                }
                if (bitmap != null)
                {
                    return(bitmap.GetThumbnailImage(desiredSize, desiredSize, null, IntPtr.Zero));
                }
            }
            catch (System.Exception ex)
            {
                CoreGlobals.getErrorManager().OnException(ex);
            }


            return(null);
        }
Ejemplo n.º 22
0
        unsafe void loadAndGrabMip(string filename, int mipToGrab)
        {
            try
            {
                bool bCacheBitmaps = false;
                bool bCacheDXT     = true;

                System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);

                ImageInformation imageInfo = TextureLoader.ImageInformationFromFile(filename);

                Format desiredFormat = Format.X8R8G8B8;
                if (mFilename.EndsWith("_op.tga") || mFilename.EndsWith("_xf.tga"))
                {
                    desiredFormat = Format.L8;
                }

                int desiredWidth  = imageInfo.Width >> mipToGrab;
                int desiredHeight = imageInfo.Height >> mipToGrab;
                if (desiredFormat == Format.X8R8G8B8)
                {
                    if (desiredWidth < 16 && desiredHeight < 16)
                    {
                        mTexture = TextureLoader.FromFile(BRenderDevice.getDevice(), filename, desiredWidth, desiredHeight, 1, 0, desiredFormat, Pool.Managed, Filter.Box, Filter.Box, 0);
                        TextureStats.mSmallTextureSize += fileInfo.Length;
                    }
                    else
                    {
                        Image  img;
                        Bitmap bmr;

                        string cachedDXTName = filename.Replace("\\work\\art\\", "\\work\\artcache\\");
                        cachedDXTName = Path.ChangeExtension(cachedDXTName, ".dxt");

                        long dxtFileLen     = 0;
                        bool bLoadFromCache = false;
                        if (bCacheDXT && File.Exists(cachedDXTName))
                        {
                            System.IO.FileInfo dxtFileInfo = new System.IO.FileInfo(cachedDXTName);
                            dxtFileLen = dxtFileInfo.Length;
                            if (dxtFileInfo.LastWriteTimeUtc >= fileInfo.LastWriteTimeUtc)
                            {
                                bLoadFromCache = true; //DXT exists and is current
                            }
                        }

                        if (bLoadFromCache == false)
                        {
                            string cacheDir = Path.GetDirectoryName(cachedDXTName);
                            if ((bCacheDXT || bCacheBitmaps) && Directory.Exists(cacheDir) == false)
                            {
                                Directory.CreateDirectory(cacheDir);
                            }

                            //if (!bCacheBitmaps || mipToGrab == 0)
                            {
                                lock (DevIL.sSyncRoot)
                                {
                                    img = DevIL.LoadImageFromFile(filename);
                                    bmr = new Bitmap(img, desiredWidth, desiredHeight);
                                }
                                TextureStats.mLargeTextureSizes += fileInfo.Length;
                                TextureStats.mRaw += fileInfo.Length;
                            }
                            //else
                            //{
                            //   string cachedBMPName = filename.Replace("\\work\\art\\", "\\work\\artcache\\");
                            //   cachedBMPName = Path.ChangeExtension(cachedBMPName, ".bmp");
                            //   if (File.Exists(cachedBMPName) == false)
                            //   {
                            //      TextureStats.mLargeTextureSizes += fileInfo.Length;
                            //      lock (DevIL.sSyncRoot)
                            //      {
                            //         img = DevIL.LoadImageFromFile(filename);
                            //         bmr = new Bitmap(img, desiredWidth, desiredHeight);
                            //      }
                            //      bmr.Save(cachedBMPName, ImageFormat.Bmp);

                            //      System.IO.FileInfo tfileInfo = new System.IO.FileInfo(cachedBMPName);
                            //      TextureStats.mCCreate += tfileInfo.Length;
                            //   }
                            //   else
                            //   {
                            //      System.IO.FileInfo tfileInfo = new System.IO.FileInfo(cachedBMPName);
                            //      TextureStats.mLargeTextureSizes += tfileInfo.Length;

                            //      lock (DevIL.sSyncRoot)
                            //      {
                            //         img = DevIL.LoadImageFromFile(cachedBMPName);
                            //         bmr = new Bitmap(img, desiredWidth, desiredHeight);
                            //      }
                            //   }
                            //}


                            Rectangle  r          = new Rectangle(0, 0, desiredWidth, desiredHeight);
                            BitmapData sourceData = bmr.LockBits(r, ImageLockMode.ReadOnly, bmr.PixelFormat);

                            byte *pInData = (byte *)sourceData.Scan0;
                            int   size    = 0;
                            //  byte[] pko = new byte[desiredWidth * desiredHeight * 4];
                            // fixed (byte* pPko = pko)
                            //DXT_CLI.compressDXT(pInData, desiredWidth, desiredHeight, 1, pPko, &size);

                            mTexture = new Texture(BRenderDevice.getDevice(), desiredWidth, desiredHeight, 1, Usage.None, Format.Dxt1, Pool.Managed);
                            GraphicsStream gsOut    = mTexture.LockRectangle(0, LockFlags.None);
                            byte *         pOutData = (byte *)gsOut.InternalDataPointer;

                            DXT_CLI.compressDXT(pInData, desiredWidth, desiredHeight, 1, pOutData, &size);
                            //  for (int i = 0; i < size; i++)
                            //    pOutData[i] = pko[i];

                            if (bCacheDXT)
                            {
                                byte[] byteArrayName = new byte[size];
                                Marshal.Copy(new IntPtr(pOutData), byteArrayName, 0, size);
                                BinaryWriter wr = new BinaryWriter(new FileStream(cachedDXTName, FileMode.Create));
                                wr.Write(size);
                                wr.Write(byteArrayName, 0, size);
                                wr.Close();

                                TextureStats.mCCreate += size;
                            }


                            mTexture.UnlockRectangle(0);
                            bmr.UnlockBits(sourceData);

                            gsOut    = null;
                            pOutData = null;
                            bmr      = null;
                            pInData  = null;
                            img      = null;
                            //pko = null;
                        }
                        else //Load DXT from cached file
                        {
                            BinaryReader r = new BinaryReader(new FileStream(cachedDXTName, FileMode.Open));
                            int          size;
                            size = r.ReadInt32();

                            if (size != (desiredWidth * desiredHeight * 0.5))
                            {
                                r.Close();
                                File.Delete(cachedDXTName);
                                loadAndGrabMip(filename, mipToGrab);
                                return;
                            }

                            byte[] byteArrayName = new byte[size];
                            r.Read(byteArrayName, 0, size);
                            //long bytesread = r.BaseStream.Position;
                            r.Close();

                            //byte* pByteArrayName = stackalloc byte[size];
                            //IntPtr pByteArrayName = Marshal.AllocHGlobal(size);
                            //Marshal.Copy(byteArrayName, 0, pByteArrayName, size);


                            TextureStats.mLargeTextureSizes += size;
                            TextureStats.mCLoad             += size;

                            mTexture = new Texture(BRenderDevice.getDevice(), desiredWidth, desiredHeight, 1, Usage.None, Format.Dxt1, Pool.Managed);
                            GraphicsStream gsOut    = mTexture.LockRectangle(0, LockFlags.None);
                            byte *         pOutData = (byte *)gsOut.InternalDataPointer;

                            Marshal.Copy(byteArrayName, 0, (IntPtr)pOutData, size);

                            //byte* pBytes = (byte*)(pByteArrayName.ToPointer());
                            //for (int i = 0; i < size; i++)
                            //{
                            //   pOutData[i] = pBytes[i];
                            //}

                            mTexture.UnlockRectangle(0);
                        }
                    }
                }
                else if (desiredFormat == Format.L8)
                {
                    mTexture = TextureLoader.FromFile(BRenderDevice.getDevice(), filename, desiredWidth, desiredHeight, 1, 0, desiredFormat, Pool.Managed, Filter.Box, Filter.Box, 0);
                    TextureStats.mL16FileSize += fileInfo.Length;
                }
                else
                {
                    Debug.Assert(false);
                }
            }
            catch (OutOfVideoMemoryException ve)
            {
                MessageBox.Show("Out of video memory.. Talk to the editor guys..");
            }
            catch (OutOfMemoryException me)
            {
                MessageBox.Show("Out of System memory.. Talk to the editor guys..");
            }
        }
Ejemplo n.º 23
0
        public static VertexDeclaration genVertexDecl(List <eVertexDeclElement> decls, bool sortForFVF, ref short vertexStructSize)
        {
            if (sortForFVF)
            {
                decls.Sort();
            }
            List <VertexElement> vList = new List <VertexElement>();

            vertexStructSize = 0;
            for (int i = 0; i < decls.Count; i++)
            {
                switch (decls[i])
                {
                case eVertexDeclElement.cVDE_Position:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0));
                    vertexStructSize += sizeof(float) * 3;
                    break;

                case eVertexDeclElement.cVDE_BlendIndicies:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.BlendIndices, 0));
                    vertexStructSize += sizeof(int);
                    break;

                case eVertexDeclElement.cVDE_BlendWeight1:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float1, DeclarationMethod.Default, DeclarationUsage.BlendWeight, 0));
                    vertexStructSize += sizeof(float);
                    break;

                case eVertexDeclElement.cVDE_BlendWeight4:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.BlendWeight, 0));
                    vertexStructSize += sizeof(int);
                    break;

                case eVertexDeclElement.cVDE_Normal:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0));
                    vertexStructSize += sizeof(float) * 3;
                    break;

                case eVertexDeclElement.cVDE_Tangent:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Tangent, 0));
                    vertexStructSize += sizeof(float) * 3;
                    break;

                case eVertexDeclElement.cVDE_BiNormal:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.BiNormal, 0));
                    vertexStructSize += sizeof(float) * 3;
                    break;

                case eVertexDeclElement.cVDE_TexCoord2:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0));
                    vertexStructSize += sizeof(float) * 2;
                    break;

                case eVertexDeclElement.cVDE_ColorDWORD:
                    vList.Add(new VertexElement(0, vertexStructSize, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0));
                    vertexStructSize += sizeof(int);
                    break;
                }
            }

            VertexElement[] elements = new VertexElement[vList.Count + 1];
            for (int i = 0; i < vList.Count; i++)
            {
                elements[i] = vList[i];
            }
            elements[vList.Count] = VertexElement.VertexDeclarationEnd;

            return(new VertexDeclaration(BRenderDevice.getDevice(), elements));
        }