コード例 #1
0
        /// <summary>
        /// This function load an uncompressed TGA
        /// </summary>
        static TexttureInfo LoadUncompressedTGA(FileStream file, string name)
        {
            byte[] header = new byte[6];                        // First 6 Useful Bytes From The Header
            int    bytesPerPixel;                               // Holds Number Of Bytes Per Pixel Used In The TGA File
            int    imageSize;                                   // Used To Store The Image Size When Setting Aside Ram
            int    temp;                                        // Temporary Variable
            int    type = Gl.GL_RGBA;                           // Set The Default GL Mode To RBGA (32 BPP)

            TexttureInfo info = new TexttureInfo();

            if (file == null || file.Read(header, 0, 6) != 6)
            //falta la comparacion de memcmp
            {
                if (file == null)
                {
                    return(info);
                }
                else
                {
                    file.Close();
                    return(info);
                }
            }
            texture.width  = header[1] * 256 + header[0];                                          // Determine The TGA Width	(highbyte*256+lowbyte)
            texture.height = header[3] * 256 + header[2];                                          // Determine The TGA Height	(highbyte*256+lowbyte)

            if (texture.width <= 0 || texture.height <= 0 || (header[4] != 24 && header[4] != 32)) // Is The TGA 24 or 32 Bit?
            {
                file.Close();
                return(info);
            }
            texture.bpp       = header[4];                                      // Grab The TGA's Bits Per Pixel (24 or 32)
            bytesPerPixel     = texture.bpp / 8;                                // Divide By 8 To Get The Bytes Per Pixel
            imageSize         = texture.width * texture.height * bytesPerPixel; // Calculate The Memory Required For The TGA Data
            texture.imageData = new byte[imageSize];                            // Reserve Memory To Hold The TGA Data
            if (imageSize == 0 || file.Read(texture.imageData, 0, imageSize) != imageSize)
            {
                if (texture.imageData != null)
                {
                    texture.imageData = null;
                }

                file.Close();
                return(info);
            }
            for (int i = 0; i < imageSize; i += bytesPerPixel)                  // Loop Through The Image Data
            {                                                                   // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
                temp = texture.imageData[i];                                    // Temporarily Store The Value At Image Data 'i'
                texture.imageData[i]     = texture.imageData[i + 2];            // Set The 1st Byte To The Value Of The 3rd Byte
                texture.imageData[i + 2] = (byte)temp;                          // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
            }

            file.Close();
            // Build A Texture From The Data
            int[] textureArray = new int[1];
            textureArray[0] = texture.texID;

            Gl.glEnable(GL_EXT_TEXTURE_RECTANGLE);                       //1

            Gl.glGenTextures(1, textureArray);                           //2 Generate OpenGL texture IDs

            Gl.glBindTexture(GL_EXT_TEXTURE_RECTANGLE, textureArray[0]); //3			// Bind Our Texture



            if (texture.bpp == 24)                                                      // Was The TGA 24 Bits
            {
                type = Gl.GL_RGB;                                                       // If So Set The 'type' To GL_RGB
            }

            Gl.glTexParameteri(GL_EXT_TEXTURE_RECTANGLE, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
            Gl.glTexParameteri(GL_EXT_TEXTURE_RECTANGLE, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

            Gl.glTexImage2D(GL_EXT_TEXTURE_RECTANGLE, 0, Gl.GL_RGBA, texture.width, texture.height,
                            0, Gl.GL_RGB8, Gl.GL_UNSIGNED_BYTE, texture.imageData);

            //if the texture is intended to be rendered anisotropically
            //if (ContentManager.IsAnisotropic(name) && maximumAnisotropy != 0)
            //{
            //    Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnisotropy);	// anisotropic Filtered
            //}

            //Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);	// Linear Filtered
            //Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);	// Linear Filtered
            //Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, 4, texture.width, texture.height, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, texture.imageData);

            //check for NPOT textures
            //if (texture.width != texture.height || Math.Log(texture.width, 2) - (int)Math.Log(texture.width, 2) != 0)
            //{
            //    Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);	// Linear Filtered
            //    Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);	// Linear Filtered
            //  Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, (int)Gl.GL_RGB8, texture.width, texture.height, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, texture.imageData);
            //}
            //else
            //{
            //    Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);	// Linear Filtered
            //    Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);	// Linear Filtered
            //    Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, type, texture.width, texture.height, 0, type, Gl.GL_UNSIGNED_BYTE, texture.imageData);
            //}

            texture.texID = textureArray[0];
            info.ID       = texture.texID;
            info.height   = texture.height;
            info.width    = texture.width;
            return(info);
        }
コード例 #2
0
        /// <summary>
        /// This method performs a low level load of a texture
        /// </summary>
        static TexttureInfo TextureOne(string s, int i, int[] texture)
        {
            Bitmap bitmap = new Bitmap(s);

            Rectangle  rect       = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bitmapData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
                                                    System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            //esto es para asegurar que no se mueva del lugar de memoria el bitmap.

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[i]);

            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

            Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, bitmap.Width, bitmap.Height,
                            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);

            //if (ContentManager.IsAnisotropic(s) && maximumAnisotropy > 0)
            //    Gl.glTexParameterf(GL_EXT_TEXTURE_RECTANGLE, Gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnisotropy);	// anisotropic Filtered
            //else
            //    EnableFiltering(textFiltering, TexureSize.NPOT);

            // Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, 3, bitmap.Width, bitmap.Height, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);


            // este codigo es si quiero diferenciar que las texturas NPOT se
            //cargen con mimapping y las POT(Power Of Two) normalmente

            /*
             * if (bitmap.Width != bitmap.Height || Math.Log(bitmap.Width, 2) - (int)Math.Log(bitmap.Width, 2) != 0)
             * {
             *  if (EngineContent.IsAnisotropic(s) && maximumAnisotropy > 0)
             *      Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnisotropy);	// anisotropic Filtered
             *  else
             *      EnableFiltering(textFiltering, TexureSize.NPOT);
             *
             *  Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, (int)Gl.GL_RGB8, bitmap.Width, bitmap.Height, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);
             * }
             * else
             * {
             *  if (EngineContent.IsAnisotropic(s) && maximumAnisotropy > 0)
             *      Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnisotropy);	// anisotropic Filtered
             *  else
             *      EnableFiltering(textFiltering, TexureSize.POT);
             *
             *  Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, 3, bitmap.Width, bitmap.Height, 0, Gl.GL_BGR, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);
             * }
             */


            TexttureInfo info = new TexttureInfo();

            info.ID     = texture[i];
            info.width  = bitmap.Width;
            info.height = bitmap.Height;

            bitmap.UnlockBits(bitmapData);
            bitmap.Dispose();


            return(info);
        }