Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1452 chris 1
#region --- License ---
2
/* Licensed under the MIT/X11 license.
3
 * Copyright (c) 2006-2008 the OpenTK Team.
4
 * This notice may not be removed from any source distribution.
5
 * See license.txt for licensing details.
6
 */
7
#endregion
8
 
9
// TODO: Find paint program that can properly export 8/16-bit Textures and make sure they are loaded correctly.
10
 
11
using System;
12
using System.Diagnostics;
13
using System.Drawing;
14
using System.Drawing.Imaging;
15
 
16
using OpenTK;
17
using OpenTK.Graphics;
18
using OpenTK.Graphics.OpenGL;
19
 
20
namespace Examples.TextureLoaders
21
{
22
    class ImageGDI
23
    {
24
 
25
        public static void LoadFromDisk( string filename, out uint texturehandle, out TextureTarget dimension )
26
        {
27
            dimension = (TextureTarget) 0;
28
            texturehandle = TextureLoaderParameters.OpenGLDefaultTexture;
29
            ErrorCode GLError = ErrorCode.NoError;
30
 
31
            Bitmap CurrentBitmap = null;
32
 
33
            try // Exceptions will be thrown if any Problem occurs while working on the file. 
34
            {
35
                CurrentBitmap = new Bitmap( filename );
36
                if ( TextureLoaderParameters.FlipImages )
37
                    CurrentBitmap.RotateFlip( RotateFlipType.RotateNoneFlipY );
38
 
39
                if ( CurrentBitmap.Height > 1 )
40
                    dimension = TextureTarget.Texture2D;
41
                else
42
                    dimension = TextureTarget.Texture1D;
43
 
44
                GL.GenTextures( 1, out texturehandle );
45
                GL.BindTexture( dimension, texturehandle );
46
 
47
                #region Load Texture
48
                OpenTK.Graphics.OpenGL.PixelInternalFormat pif;
49
                OpenTK.Graphics.OpenGL.PixelFormat pf;
50
                OpenTK.Graphics.OpenGL.PixelType pt;
51
 
52
                if (TextureLoaderParameters.Verbose)
53
                   Trace.WriteLine( "File: " + filename + " Format: " + CurrentBitmap.PixelFormat );
54
 
55
                switch ( CurrentBitmap.PixelFormat )
56
                {
57
                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: // misses glColorTable setup
58
                    pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgb8;
59
                    pf = OpenTK.Graphics.OpenGL.PixelFormat.ColorIndex;
60
                    pt = OpenTK.Graphics.OpenGL.PixelType.Bitmap;
61
                    break;
62
                case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555:
63
                case System.Drawing.Imaging.PixelFormat.Format16bppRgb555: // does not work
64
                    pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgb5A1;
65
                    pf = OpenTK.Graphics.OpenGL.PixelFormat.Bgr;
66
                    pt = OpenTK.Graphics.OpenGL.PixelType.UnsignedShort5551Ext;
67
                    break;
68
              /*  case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
69
                    pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.R5G6B5IccSgix;
70
                    pf = OpenTK.Graphics.OpenGL.PixelFormat.R5G6B5IccSgix;
71
                    pt = OpenTK.Graphics.OpenGL.PixelType.UnsignedByte;
72
                    break;
73
*/
74
                case System.Drawing.Imaging.PixelFormat.Format24bppRgb: // works
75
                    pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgb8;
76
                    pf = OpenTK.Graphics.OpenGL.PixelFormat.Bgr;
77
                    pt = OpenTK.Graphics.OpenGL.PixelType.UnsignedByte;
78
                    break;
79
                case System.Drawing.Imaging.PixelFormat.Format32bppRgb: // has alpha too? wtf?
80
                case System.Drawing.Imaging.PixelFormat.Canonical:
81
                case System.Drawing.Imaging.PixelFormat.Format32bppArgb: // works
82
                    pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgba;
83
                    pf = OpenTK.Graphics.OpenGL.PixelFormat.Bgra;
84
                    pt = OpenTK.Graphics.OpenGL.PixelType.UnsignedByte;
85
                    break;
86
                default:
87
                    throw new ArgumentException( "ERROR: Unsupported Pixel Format " + CurrentBitmap.PixelFormat );
88
                }
89
 
90
                BitmapData Data = CurrentBitmap.LockBits( new System.Drawing.Rectangle( 0, 0, CurrentBitmap.Width, CurrentBitmap.Height ), ImageLockMode.ReadOnly, CurrentBitmap.PixelFormat );
91
 
92
                if ( Data.Height > 1 )
93
                { // image is 2D
94
                    if (TextureLoaderParameters.BuildMipmapsForUncompressed)
95
                    {
96
                        throw new Exception("Cannot build mipmaps, Glu is deprecated.");
97
                      //  Glu.Build2DMipmap(dimension, (int)pif, Data.Width, Data.Height, pf, pt, Data.Scan0);
98
                    }
99
                    else
100
                        GL.TexImage2D(dimension, 0, pif, Data.Width, Data.Height, TextureLoaderParameters.Border, pf, pt, Data.Scan0);
101
                } else
102
                { // image is 1D
103
                    if (TextureLoaderParameters.BuildMipmapsForUncompressed)
104
                    {
105
                        throw new Exception("Cannot build mipmaps, Glu is deprecated.");
106
                      //  Glu.Build1DMipmap(dimension, (int)pif, Data.Width, pf, pt, Data.Scan0);
107
                    }
108
                    else
109
                        GL.TexImage1D(dimension, 0, pif, Data.Width, TextureLoaderParameters.Border, pf, pt, Data.Scan0);
110
                }
111
 
112
                GL.Finish( );
113
                GLError = GL.GetError( );
114
                if ( GLError != ErrorCode.NoError )
115
                {
116
                    throw new ArgumentException( "Error building TexImage. GL Error: " + GLError );
117
                }
118
 
119
                CurrentBitmap.UnlockBits( Data );
120
                #endregion Load Texture
121
 
122
                #region Set Texture Parameters
123
                GL.TexParameter( dimension, TextureParameterName.TextureMinFilter, (int) TextureLoaderParameters.MinificationFilter );
124
                GL.TexParameter( dimension, TextureParameterName.TextureMagFilter, (int) TextureLoaderParameters.MagnificationFilter );
125
 
126
                GL.TexParameter( dimension, TextureParameterName.TextureWrapS, (int) TextureLoaderParameters.WrapModeS );
127
                GL.TexParameter( dimension, TextureParameterName.TextureWrapT, (int) TextureLoaderParameters.WrapModeT );
128
 
129
                GL.TexEnv( TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int) TextureLoaderParameters.EnvMode );
130
 
131
                GLError = GL.GetError( );
132
                if ( GLError != ErrorCode.NoError )
133
                {
134
                    throw new ArgumentException( "Error setting Texture Parameters. GL Error: " + GLError );
135
                }
136
                #endregion Set Texture Parameters
137
 
138
                return; // success
139
            } catch ( Exception e )
140
            {
141
                dimension = (TextureTarget) 0;
142
                texturehandle = TextureLoaderParameters.OpenGLDefaultTexture;
143
                throw new ArgumentException( "Texture Loading Error: Failed to read file " + filename + ".\n" + e );
144
                // return; // failure
145
            } finally
146
            {
147
                CurrentBitmap = null;
148
            }
149
        }
150
 
151
    }
152
}