Einzelnen Beitrag anzeigen

EWeiss
(Gast)

n/a Beiträge
 
#14

AW: Beschleunigung von Laderoutine

  Alt 16. Aug 2016, 10:12
Wie sieht denn die LoadTexture-Methode aus?
Wäre es nicht auch zweckmäßig diese zu untersuchen, da du im ersten Post schreibst, das diese Methode blockt?
Ich möchte den Prozess des Laden nicht unterbrechen..
Das macht man ja auch nicht wenn man normale Bitmaps laden tut.

Ich denke auch nicht das man hier etwas manipulieren sollte und ob sich da letztendlich was tut mag dahingestellt sein.
Aber gut hier die für JPG!

Delphi-Quellcode:
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);
  {Texture blends with object background}

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  { only first two can be used }
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  { all of the above can be used }

  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;
Delphi-Quellcode:
function LoadJPGTexture(Filename: string; var Texture: GLuint;
  LoadFromResource: boolean): boolean;
var
  Data: array of longword;
  W, Width: integer;
  H, Height: integer;
  BMP: TBitmap;
  JPG: TJPEGImage;
  C: longword;
  Line: ^longword;
  ResStream: TResourceStream; // used for loading from resource
begin
  Result := False;
  JPG := TJPEGImage.Create;

  if LoadFromResource then // Load from resource
  begin
    try
      ResStream := TResourceStream.Create(hInstance,
        PChar(copy(Filename, 1, Pos('.', Filename) - 1)), 'JPEG');
      JPG.LoadFromStream(ResStream);
      ResStream.Free;
    except
      on
        EResNotFound do
      begin
        MessageBox(0, PChar('File not found in resource - ' + Filename),
          PChar('JPG Texture'), MB_OK);
        Exit;
      end
      else
      begin
        MessageBox(0, PChar('Couldn''t load JPG Resource - "' + Filename + '"'),
          PChar('BMP Unit'), MB_OK);
        Exit;
      end;
    end;
  end
  else
  begin
    try
      JPG.LoadFromFile(Filename);
    except
      MessageBox(0, PChar('Couldn''t load JPG - "' + Filename + '"'),
        PChar('BMP Unit'), MB_OK);
      Exit;
    end;
  end;

  // Create Bitmap
  BMP := TBitmap.Create;
  BMP.pixelformat := pf32bit;
  BMP.Width := JPG.Width;
  BMP.Height := JPG.Height;
  BMP.canvas.draw(0, 0, JPG); // Copy the JPEG onto the Bitmap

  // BMP.SaveToFile('D:\test.bmp');
  Width := BMP.Width;
  Height := BMP.Height;
  SetLength(Data, Width * Height);

  for H := 0 to Height - 1 do
  begin
    Line := BMP.scanline[Height - H - 1]; // flip JPEG
    for W := 0 to Width - 1 do
    begin
      c := Line^ and $FFFFFF; // Need to do a color swap
      Data[W + (H * Width)] := (((c and $FF) shl 16) + (c shr 16) + (c and $FF00)) or $FF000000;
      // 4 channel.
      Inc(Line);
    end;
  end;

  BMP.Free;
  JPG.Free;

  Texture := CreateTexture(Width, Height, GL_RGBA, addr(Data[0]));
  SetLength(Data, 0);

  Result := True;
end;
gruss
  Mit Zitat antworten Zitat