DGL
https://delphigl.com/forum/

DDS
https://delphigl.com/forum/viewtopic.php?f=19&t=3396
Seite 1 von 1

Autor:  Stucuk [ Fr Okt 22, 2004 19:32 ]
Betreff des Beitrags:  DDS

Below is a modifyed version of Jan Horn's Textures.pas and Martin Waldegger's DDS.

My question is how can the pByte be modifyed so its's Y coords are reversed(so 0,0 is bottom left not top left)?

Dateianhänge:
Dateikommentar: Textures.pas And DDS.pas
textures_and_dds.zip [7.18 KiB]
283-mal heruntergeladen

Autor:  Mars [ Fr Okt 22, 2004 21:22 ]
Betreff des Beitrags: 

A PByte is just a pointer to a byte (respective a byte array), referring to DDS you cant easily turn or mirror the texture image while loading, since the decompression is done by OpenGL itself. However, you can retrieve the texture image via glGetTexImage after uploading it and do what you want with the uncompressed picture data (and send it to OpenGL afterwards).

Autor:  Stucuk [ Fr Okt 22, 2004 22:24 ]
Betreff des Beitrags: 

I can't get it to work

Code:
  1.  
  2. Procedure SwapY(Var Pixels : pBytes; xSize,ySize : Integer);
  3. Type
  4. TRGB = record
  5. R,G,B,A : Byte;
  6. end;
  7. Var
  8. Temp : array of array of TRGB;
  9. x,y,c : integer;
  10. begin
  11.  
  12. SetLength(Temp,xSize,ySize);
  13. c := -1;
  14. for x := 0 to xSize-1 do
  15. for y := 1 to ySize do
  16. begin
  17. Inc(c);
  18. Temp[x,ySize-y].R := Pixels[c];
  19. Inc(c);
  20. Temp[x,ySize-y].G := Pixels[c];
  21. Inc(c);
  22. Temp[x,ySize-y].B := Pixels[c];
  23. Inc(c);
  24. Temp[x,ySize-y].A := Pixels[c];
  25. end;
  26.  
  27. c := -1;
  28.  
  29. for x := 0 to xSize-1 do
  30. for y := 0 to ySize-1 do
  31. begin
  32. Inc(c);
  33. Pixels[c] := Temp[x,y].R;
  34. Inc(c);
  35. Pixels[c] := Temp[x,y].G;
  36. Inc(c);
  37. Pixels[c] := Temp[x,y].B;
  38. Inc(c);
  39. Pixels[c] := Temp[x,y].A;
  40. end;
  41.  
  42. SetLength(Temp,0,0);
  43.  
  44. end;
  45.  
  46. function LoadDDS(Stream: TStream; Var Texture : Cardinal; Const NoPicMip : Boolean; Var Width,Height : Integer): boolean;
  47. var
  48.   hdr: TDDSHeader;
  49.   mipMapCount, x, y, xSize, ySize: Cardinal;
  50.   li: ^TDDSLoadInfo;
  51.   data,pixels: PBytes;
  52.   unpacked: PGLuints;
  53.   size, ix, zz: Cardinal;
  54.   palette: array[0..255] of Cardinal;
  55. begin
  56.   result := false;
  57.   //  DDS is so simple to read, too
  58.   Stream.Read(hdr, sizeof(hdr));
  59.   if (hdr.dwMagic<>DDS_MAGIC) or (hdr.dwSize<>124) or
  60.      ((hdr.dwFlags and DDSD_PIXELFORMAT)=0) or ((hdr.dwFlags and DDSD_CAPS)=0) then exit;
  61.  
  62.   if (addr(glTexParameteri)=NIL) or
  63.      (addr(glCompressedTexImage2D)=NIL) or
  64.      (addr(glPixelStorei)=NIL) then exit;
  65.   xSize := hdr.dwWidth;
  66.   ySize := hdr.dwHeight;
  67.  
  68.   if (PF_IS_DXT1(hdr.PixelFormat)) then li := @loadInfoDXT1 else
  69.   if (PF_IS_DXT3(hdr.PixelFormat)) then li := @loadInfoDXT3 else
  70.   if (PF_IS_DXT5(hdr.PixelFormat)) then li := @loadInfoDXT5 else
  71.   if (PF_IS_BGRA8(hdr.PixelFormat)) then li := @loadInfoBGRA8 else
  72.   if (PF_IS_BGR8(hdr.PixelFormat)) then li := @loadInfoBGR8 else
  73.   if (PF_IS_BGR5A1(hdr.PixelFormat)) then li := @loadInfoBGR5A1 else
  74.   if (PF_IS_BGR565(hdr.PixelFormat)) then li := @loadInfoBGR565 else
  75.   if (PF_IS_INDEX8(hdr.PixelFormat)) then li := @loadInfoIndex8 else
  76.   exit;
  77.  
  78.   x := xSize;
  79.   y := ySize;
  80.   Width := x;
  81.   Height := y;
  82.   GetMem(pixels, xSize*ySize*4);
  83.  
  84.  
  85.   glGenTextures(1, @Texture);
  86.   glBindTexture(GL_TEXTURE_2D, Texture);
  87.  
  88.   glEnable(GL_TEXTURE_2D);
  89.  
  90. //  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  {Texture blends with object background}
  91.  
  92.   if NoPicMip then begin
  93.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  94.   end
  95.   else begin
  96.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  97.   end;
  98.  
  99.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  100.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  101.  
  102. {$IFDEF GL_14}
  103.   if GL_14 then
  104.     glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
  105. {$ENDIF}
  106.   if hdr.dwFlags and DDSD_MIPMAPCOUNT<>0 then mipMapCount := hdr.dwMipMapCount else mipMapCount := 1;
  107.  
  108.   if li.compressed then begin
  109.     size := max(li.divSize, x ) div li.divSize * max(li.divSize, y) div li.divSize * li.blockBytes;
  110.     GetMem(data, size);
  111.     for ix:=0 to mipMapCount-1 do begin
  112.       Stream.Read(data^, size);
  113.       glCompressedTexImage2D(GL_TEXTURE_2D, ix, li.internalFormat, x, y, 0, size, data);
  114.       glGetTexImage(GL_TEXTURE_2D, ix, GL_RGBA, GL_BYTE, pixels);
  115.       SwapY(pixels,xSize,ySize);
  116.       glTexImage2D(GL_TEXTURE_2D, ix, GL_RGBA, xsize, ysize, 0, GL_RGBA, GL_BYTE, pixels);
  117.       x := (x+1) shr 1;
  118.       y := (y+1) shr 1;
  119.       size := max(li.divSize, x ) div li.divSize * max(li.divSize, y ) div li.divSize * li.blockBytes;
  120.     end;
  121.     FreeMem(data);
  122.   end
  123.   else if li.palette then begin
  124.     size := hdr.dwPitchOrLinearSize * ySize;
  125.     GetMem(data, size);
  126.     GetMem(unpacked, size * sizeof(cardinal));
  127.     Stream.Read(palette, 1024);
  128.     for ix:=0 to mipMapCount-1 do begin
  129.       Stream.Read(data^, size);
  130.       for zz:=0 to size-1 do
  131.         unpacked[zz] := palette[data[zz]];
  132.       glPixelStorei(GL_UNPACK_ROW_LENGTH, y);
  133.       glTexImage2D(GL_TEXTURE_2D, ix, li.internalFormat, x, y, 0, li.externalFormat, li.typ, unpacked);
  134.       glGetTexImage(GL_TEXTURE_2D, ix, GL_RGBA, GL_BYTE, pixels);
  135.       SwapY(pixels,xSize,ySize);
  136.       glTexImage2D(GL_TEXTURE_2D, ix, GL_RGBA, xsize, ysize, 0, GL_RGBA, GL_BYTE, pixels);
  137.       x := (x+1) shr 1;
  138.       y := (y+1) shr 1;
  139.       size := x * y * li.blockBytes;
  140.     end;
  141.     FreeMem(data);
  142.     FreeMem(unpacked);
  143.   end
  144.   else begin
  145.     if li.swap then
  146.       glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);
  147.     size := x * y * li.blockBytes;
  148.     GetMem(data, size);
  149.     for ix:=0 to mipMapCount-1 do begin
  150.       Stream.Read(data^, size);
  151.       glPixelStorei(GL_UNPACK_ROW_LENGTH, y);
  152.       glTexImage2D(GL_TEXTURE_2D, ix, li.internalFormat, x, y, 0, li.externalFormat, li.typ, data);
  153.       glGetTexImage(GL_TEXTURE_2D, ix, GL_RGBA, GL_BYTE, pixels);
  154.       SwapY(pixels,xSize,ySize);
  155.       glTexImage2D(GL_TEXTURE_2D, ix, GL_RGBA, xsize, ysize, 0, GL_RGBA, GL_BYTE, pixels);
  156.       x := (x+1) shr 1;
  157.       y := (y+1) shr 1;
  158.       size := x * y * li.blockBytes;
  159.     end;
  160.     FreeMem(data);
  161.     glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
  162.   end;
  163. {$IFDEF GL_12}
  164.   if GL_12 then
  165.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mipMapCount-1);
  166. {$ENDIF}
  167.   FreeMem(pixels);
  168.  
  169.   result := true;
  170. end;
  171.  

Autor:  Mars [ Fr Okt 22, 2004 22:44 ]
Betreff des Beitrags: 

What do you mean with that? Does it crash, or does it show no effect?
Double check, that glGetTexImage returns valid data (just use the Delphi Debugger after preinitializing pixels with zero).
Your SwapY seems a bit overkill to me, since you won't need another copy of the texture just to flip the y-coordinate, it's wrong too, since you use y as inner loop variable to walk through temp vertically, while incrementing c will walk through pixels horizontally.
Try something like that:
Code:
  1.  
  2. Procedure SwapY(Pixels: pBytes; xSize, ySize: Integer);
  3. var x, y: integer;
  4.     P1, P2: ^Cardinal;
  5.     Temp: Cardinal;
  6. begin
  7.   for y:=0 to Pred(ySize shr 1) do
  8.     for x:=0 to Pred(xSize) do begin
  9.       P1 := Addr(Pixels^[(y*xSize + x)*4]);
  10.       P2 := Addr(Pixels^[((ySize-y-1)*xSize + x)*4]);
  11.       Temp := P1^;
  12.       P1^ := P2^;
  13.       P2^ := Temp;
  14.     end;
  15. end;
  16.  

Autor:  Stucuk [ Sa Okt 23, 2004 00:16 ]
Betreff des Beitrags: 

After trying ur SwapY it worked, thx.

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/