- unit uTexture;
- interface
- uses SDL, SDLUtils, SDLImage, dglOpenGL, SysUtils, uBase;
- type
- TTextur = record
- Id : Integer;
- Textur : GluInt;
- end;
- TDynTexArr = Array of TTextur;
- function LoadTexture(Filename : String; aId : Integer) : Boolean; // Textur aus Datei laden und mit definierten Index in Texturarray schreiben
- procedure SwapBGR(SDLSurface : PSDL_Surface); // Farben vertauschen
- procedure Bind(Id : Integer); // Textur binden
- implementation
- uses
- uVars;
- // OpenGL-Textur erstellen
- function CreateTexture(SDL_Surface : PSDL_Surface) : Integer;
- var tmpInitial : PSDL_Surface;
- Texture : GluInt;
- MaxAni : Real;
- begin
- // Es ist nicht klar, ob die Funktion erfolgreich durchläuft.
- Result := -1;
- if Assigned(SDL_Surface) then
- begin
- // Für Alphakanal
- tmpInitial := SDL_DisplayFormatAlpha(SDL_Surface);
- SDL_FreeSurface(SDL_Surface);
- // Da der Alphakanal die Farben vertauscht, muss dies über SwapRGB gerade gebogen werden.
- SwapBGR(tmpInitial);
- glGenTextures(1, @Texture);
- glBindTexture(GL_TEXTURE_2D, Texture);
- // Die Größen werden automatisch berechnet. Sonst wären nur Potenzen von 2 möglich (16x16, 32x32, usw.).
- if tmpInitial.format.BytesPerPixel > 3 then
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tmpInitial^.w, tmpInitial^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpInitial^.pixels)
- else
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tmpInitial^.w, tmpInitial^.h, 0, GL_RGB, GL_UNSIGNED_BYTE, tmpInitial^.pixels);
- // Textureigenschaften setzen
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- if IsExtensionSupported('GL_EXT_texture_filter_anisotropic') then
- begin
- glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAni);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, MaxAni/2);
- end;
- glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
- glGenerateMipmap(GL_TEXTURE_2D);
- // Mipmaps generieren
- if tmpInitial.format.BytesPerPixel > 3 then
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, tmpInitial^.w, tmpInitial^.h, GL_RGBA, GL_UNSIGNED_BYTE, tmpInitial^.pixels)
- else
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, tmpInitial^.w, tmpInitial^.h, GL_RGB, GL_UNSIGNED_BYTE, tmpInitial^.pixels);
- // Freigabe
- SDL_FreeSurface(tmpInitial);
- // Rückgabe
- Result := Texture;
- end;
- end;
- // Textur aus Datei laden und mit definierten Index in Texturarray schreiben
- function LoadTexture(Filename : String; aId : Integer) : Boolean;
- begin
- Result := False;
- if FileExists(Filename) then
- begin
- SetLength(Tex, High(Tex)+2);
- with Tex[High(Tex)] do
- begin
- Id := Succ(aId);
- Textur := CreateTexture(IMG_Load(PChar(Filename)));
- end;
- Result := True;
- end;
- end;
- // Farben vertauschen
- procedure SwapBGR(SDLSurface : PSDL_Surface);
- var x, y : Integer;
- p : UInt32;
- pR, pG, pB, pA : PUInt8;
- r, g, b, a : UInt8;
- begin
- for y := 0 to Pred(SDLSurface.h) do
- for x := 0 to Pred(SDLSurface.w) do
- begin
- p := SDL_GetPixel(SDLSurface, x, y);
- pR := @r;
- pG := @g;
- pB := @b;
- pA := @a;
- SDL_GetRGBA(p, SDLSurface.format, pR, pG, pB, pA);
- p := SDL_MapRGBA(SDLSurface.format, b, g, r, a);
- SDL_PutPixel(SDLSurface, x, y, p);
- end;
- end;
- // Textur binden
- procedure Bind(Id : Integer);
- var i : Integer;
- begin
- for i := 0 to High(Tex) do
- if Tex[i].Id = Id then
- begin
- glBindTexture(GL_TEXTURE_2D, Tex[i].Textur);
- Break;
- end;
- end;
- end.