Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi BMP-Texture-LOADER [verDREHT] (https://www.delphipraxis.net/109577-bmp-texture-loader-%5Bverdreht%5D.html)

olee 3. Mär 2008 22:00


BMP-Texture-LOADER [verDREHT]
 
Hi ich hab ein kleines Problem

Bei meiner laden funktion für bmp texturen steckt der wurm drin.

Die lädt zwar die Texturen, verdreht diese aber.

Ich habe einen für TGA der lädt die schön und gut.

Aber der BMP - LOADER lädt die genau um 180° verdreht!

Also das im Vergleich zum TGA Oben Unten ist und umgekehrt!

Weiß einer wie ichj im Quelltext oder durch OpenGl das bild beim Laden drehen kann?

Muetze1 3. Mär 2008 23:58

Re: BMP-Texture-LOADER [verDREHT]
 
Hinweis, der das Rätsel wohl auflöst: Wenn im Bitmapheader ein positive Höhenangabe steht, dann ist das Bitmap Bottom Up gespeichert, also von der untersten ScanLine hoch zur ersten. Wenn die biHeight Angabe negativ ist, dann ist es eine Top Down Bitmap, also oberste ScanLine zuerst. Dies ist so im Bitmap Format definiert.

turboPASCAL 4. Mär 2008 19:50

Re: BMP-Texture-LOADER [verDREHT]
 
Zeig doch mal wie diene Bitmap-Laderoutiene ausschaut. Dann lässt es sich bestimmt leichter helfen.
MEin eingener (leichtester) Ansatz schaut so aus:

Delphi-Quellcode:
unit BmpTextures;


interface

uses
  Windows,
  OpenGL;

  function LoadBMPTexture(ImageName: PChar; var Texture : GLuint;
    LoadFromRes: Boolean) : Boolean;

  function gluBuild2DMipmaps(Target: GLenum; Components, Width, Height: GLint;
    Format, atype: GLenum; Data: Pointer): GLint; stdcall; external glu32;

  procedure glGenTextures(n: GLsizei; var textures: GLuint); stdcall; external OpenGL32;
  procedure glBindTexture(target: GLenum; texture: GLuint); stdcall; external OpenGL32;
  procedure glDeleteTextures(n: Integer; textures: PGLuint); stdcall; external OpenGL32;


implementation

resourcestring
  szUse4BitBitmaps = 'Use Bitmaps with 4Bits per Pixel or greater.';
  szBitmapNotFound = 'Bitmap not found (Resource or File)';

{------------------------------------------------------------------}
{  Create the Texture                                             }
{------------------------------------------------------------------}
function CreateTexture(Width, Height, Format: Word; pData: Pointer): Integer;
var
  Texture: GLuint;
begin
  glGenTextures(1, Texture);
  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;

function LoadBMPTexture(ImageName: PChar; var Texture : GLuint;
  LoadFromRes: Boolean) : Boolean;
type
  TByteArray = array of byte;
var
  BitmapLength: LongWord;
  bmpBits: TByteArray;
  hBmp: HBitmap;
  bi: tagBITMAP;

  procedure _SwapRGB(pData: Pointer; Size: Integer; Alpha: Byte);
  asm
    push ebx
    test edx,edx
    jz @@end
  @@loop :
    mov bl,[eax+0]
    mov bh,[eax+2]
    mov [eax+2],bl
    mov [eax+0],bh
    mov [eax+3],cl
    add eax, 4
    sub edx, 4
    jnle @@loop
  @@end:
    pop ebx
  end;

begin
  Result := FALSE;

  if LoadFromRes
    then hBmp := Loadimage(HInstance, ImageName, IMAGE_BITMAP, 0, 0, 0)
    else hBmp := Loadimage(0, ImageName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

  if hBmp > 0  then
  begin
    GetObject(hBmp, sizeof(bi), @bi);

    if bi.bmBitsPixel > 1 then
    begin
      BitmapLength := bi.bmWidth * bi.bmHeight * (bi.bmBitsPixel div 8);
      SetLength(bmpBits, BitmapLength+1);
      GetBitmapBits(hBmp, BitmapLength, @bmpBits[0]);

      // Bitmaps are stored BGR and not RGB, so swap the R and B bytes.
      if @bmpBits[0] <> nil then
      begin
        _SwapRGB(bmpBits, Length(bmpBits), 255);
        Texture := CreateTexture(bi.bmWidth, bi.bmHeight, GL_RGBA, @bmpBits[0]);
        Result := (Texture <> 0);
      end else
        Result := FALSE;

    end else
      MessageBox(0, PCHAR(szUse4BitBitmaps), 'Bitmap Formaterror:', MB_OK);
  end else
    MessageBox(0, PCHAR(szBitmapNotFound), 'Bitmap Formaterror:', MB_OK);
 

  DeleteObject(hBmp);
end;

end.
;)

Bunny99s 14. Jun 2008 18:14

Re: BMP-Texture-LOADER [verDREHT]
 
Hi, ich wollte nur Anmerken dass sich in deiner function CreateTexture sich ein Fehler eingeschlichen hat:

Deine Function geht, solange man keine RGBA Texturen erstellen möchte.
Denn du hast bei components nicht 4 sondern GL_RGBA (=$1908) angegeben
was die function sicherlich nicht dazu bringt das zu tun was sie soll. ;)

Delphi-Quellcode:
if Format = GL_RGBA
  then gluBuild2DMipmaps(GL_TEXTURE_2D, 4, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData)
  else gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pData);
statt

Delphi-Quellcode:
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);

greez Bunny99s


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:08 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz