Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Loading textures from stream

  1. #1
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    33

    Loading textures from stream

    Hi,

    I'm trying to load a texture from a stream to apply on a plane face, but it only load if the texture is loaded from file, from the stream the glGenTexture doesn't seem to be generating a new texture

    can anyone help me ont his?

    thks

  2. #2
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    33

    Re: Loading textures from stream

    Using all delphi code:

    delphi code:

    Code :
    procedure LoadTextures
    var
      Stream: TFileStream;
      Texture: Integer;
    begin
      Stream := TFileStream.Create('File.bmp', fmOpenRead);
      LoadTextureFromStream(Stream, Texture);
      Stream.Free;
    end;

    Code for loading textures from a Stream

    Code :
    procedure LoadTextureFromStream(Stream: TStream; var Texture: GLuint);
    var
      FileHeader: BITMAPFILEHEADER;
      InfoHeader: BITMAPINFOHEADER;
      Palette: array of RGBQUAD;
      BitmapLength: LongWord;
      PaletteLength: LongWord;
      Width, Height : Integer;
      pData : Pointer;
      p,j,ExpandBMP,Rmask,Gmask,Bmask : Integer;
      Format : Word;
    begin
      Stream.Position := 0;
       Stream.ReadBuffer(FileHeader, SizeOf(FileHeader));  // FileHeader
       Stream.ReadBuffer(InfoHeader, SizeOf(InfoHeader));  // InfoHeader
       PaletteLength := InfoHeader.biClrUsed;
       case InfoHeader.biBitCount of
       1,4:
       begin
        ExpandBMP := InfoHeader.biBitCount;
        InfoHeader.biBitCount := 8;
       end;
       else
        ExpandBMP := 0;
       end;
       case InfoHeader.biCompression of
       0: // BI_RGB
          begin
            case InfoHeader.biBitCount of
            15,16: begin Rmask := $7c00; GMask := $03e0; BMask := $001f; end;
            // assuming big endian
            24: begin Rmask := $ff; GMask := $ff00; Bmask := $ff0000; end;
            32: begin Rmask := $ff0000; Gmask := $ff00; Bmask := $ff; end;
            end;
          end;
       1, // BI_RLE8
       2: // BI_RLE4
          raise EInvalidGraphic.create('Compressed Bitmaps not handled');
       3: // BI_BitFields
          begin
            case InfoHeader.biBitCount of
            15,16,32: begin
                        Stream.ReadBuffer(Rmask,sizeof(Rmask));
                        Stream.ReadBuffer(Gmask,sizeof(Gmask));
                        Stream.ReadBuffer(Bmask,sizeof(Bmask));
                      end;
            end;
          end;
       end;
       if (InfoHeader.biBitCount=24) then
        Format := GL_RGB
       else
        Format := GL_RGBA;
     
       SetLength(Palette, PaletteLength);
       Stream.ReadBuffer(Palette, PaletteLength);          // Palette
       Width := InfoHeader.biWidth;
       Height := InfoHeader.biHeight;
     
       BitmapLength := InfoHeader.biSizeImage;
       if BitmapLength = 0 then
         BitmapLength := Width * Height * InfoHeader.biBitCount Div 8;
       GetMem(pData, BitmapLength);
       try
         Stream.ReadBuffer(pData^, BitmapLength);            // Bitmap Data
      // Bitmaps are stored BGR and not RGB, so swap the R and B bytes.
        if (Rmask<>$ff0000) then
            SwapRGB(pData, Width*Height);
        if (InfoHeader.biBitCount=32) then
        begin
          SwapRGBA(pData,Width*Height);
        end;
        Texture :=CreateTexture(Width, Height, format, pData);
      finally
        FreeMem(pData);
      end;
    end;

    OpenGL Code:

    Code :
    function CreateTexture(Width, Height, Format: Word; pData: Pointer): Integer;
    var
      Texture: GLuint;
    begin
      glGenTextures(1, @Texture);
      glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
      glBindTexture(GL_TEXTURE_2D, Texture);
      glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
     
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     
      if Format = GL_RGBA
      then gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData)
      else gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pData);
     
      result := Texture;
    end;

    But it doesn't seem to work, the texture doesn't load.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •