Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Sa Jul 05, 2025 23:16

Foren-Übersicht » Programmierung » Einsteiger-Fragen
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 4 Beiträge ] 
Autor Nachricht
BeitragVerfasst: Di Mai 08, 2007 04:39 
Offline
DGL Member
Benutzeravatar

Registriert: Do Mai 27, 2004 15:22
Beiträge: 25
Wohnort: Stuttgart
Hallo Leute,

nach weiterem rumprobieren und ein paar Test's wagte ich mal an daran, Texte dar zu stellen.
Nach mehreren ausprobieren von Bitmap Fontbuilder und diversen anderen Programmen habe ich mich für das FontStudio von Nitrogen entschieden. Bei allen anderen entstehen hässliche Kanteneffekte und die zwischenräume werden einfach zu groß.

http://www.nitrogen.za.org/projectlist.asp?cat=dogl

Es wird dann in ein TGA-File gespeichert samt Daten (akkurate Zwischenräume bzw. tex-koordinaten).

Das Funktioniert soweit auch ganz gut. Aber sollte ich ein Hintergrundbild nehmen, funktioniert es nicht mehr - soll heissen nach dem Aufruf von "GenTexture" (über glbitmap2d) habe ich obwohl die Texture noch nicht gebunden wurde die Texture vom Hintergrundbild auf den Quads für die Buchstaben beim Text.

Hier mal der Quellcode...

Code:
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, DGLOpenGL, ExtCtrls, StdCtrls, glbitmap, font4;
  8.  
  9. Const
  10.      NearClipping = 1;
  11.      FarClipping  = 1000;
  12.  
  13. type
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.     procedure FormResize(Sender: TObject);
  17.     procedure FormDestroy(Sender: TObject);
  18.     procedure IdleHandler(Sender: TObject; var Done: Boolean);
  19.     procedure FormShow(Sender: TObject);
  20.   private
  21.     { Private-Deklarationen }
  22.  
  23.  
  24.     procedure SetupGL;
  25.     procedure Render;
  26.  
  27.  
  28.   public
  29.     { Public-Deklarationen }
  30.     DC                                : HDC;  //Handle auf Zeichenfläche
  31.     RC                                : HGLRC;//Rendering Context
  32.  
  33.     StartTime, TimeCount, FrameCount  : Cardinal; //FrameCounter
  34.     Frames, DrawTime                  : Cardinal; //& Timebased Movement
  35.   end;
  36.  
  37. var
  38.   Form1    : TForm1;
  39.   fTexture : TglBitmap2D;
  40.  
  41.   fnt      : TFontObj; // Siehe Font4 Unit
  42.  
  43. implementation
  44.  
  45. {$R *.dfm}
  46.  
  47. procedure TForm1.Render;
  48. begin
  49.  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  50.  glMatrixMode(GL_PROJECTION);
  51.  glLoadIdentity;
  52.  glViewport(0,0,ClientWidth,ClientHeight);
  53.  
  54.  glOrtho(0,800,0,600, 0,128);
  55.  
  56.  glEnable(GL_TEXTURE_2D);
  57.  
  58.  glEnable(GL_Blend);
  59.  glEnable(GL_ALPHA_TEST);
  60.  glAlphaFunc(GL_GREATER, 0.1);
  61.  
  62.  
  63. //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  64.  fnt.Draw(10, 50, 'Hallo, dies ist ein Test...', 0);
  65.  fnt.Draw(10, 580, inttostr(Frames) + ' FPS', 0);
  66.  
  67.  ftexture.Bind();
  68.  
  69. glBegin(GL_QUADS);
  70.   glTexCoord2f(0,1); glVertex3f(1, 1, -1);
  71.   glTexCoord2f(1,1); glVertex3f(1024, 0, -1);
  72.   glTexCoord2f(1,0); glVertex3f(1024, 768, -1);
  73.   glTexCoord2f(0,0); glVertex3f(0, 768, -1);
  74. glEnd;
  75.  
  76. // glEnable(GL_BLEND);
  77. //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  78. //gldisable(GL_BLEND);
  79.  
  80.  SwapBuffers(DC);
  81. end;
  82.  
  83. // ----------------------------------------------------------------------------------
  84.  
  85. procedure TForm1.SetupGL;
  86.  
  87. begin
  88.   fnt := TFontObj.Create;
  89.   Fnt.LoadTGAComp('Font.tga');
  90.   Fnt.LoadData('Font.tga', true); // Look inside Font.tga for the font data.
  91.  
  92.   glClearColor(0.6, 0.3, 1.0, 0); //Hintergrundfarbe:
  93.   glEnable(GL_DEPTH_TEST);          //Tiefentest aktivieren
  94.  
  95.  
  96.  
  97. //  glFrontFace(GL_CW);
  98. //  glEnable(GL_CULL_FACE);          //Backface Culling aktivieren
  99.  
  100.  
  101.  
  102.  fTexture := TglBitmap2D.Create;
  103.  fTexture.LoadFromFile('space.jpg');
  104.  
  105.  ftexture.GenTexture;
  106.  
  107. // Wenn ich kein Hintergrundbild lade und an OGL per GenTexture übergebe
  108. // dann zeigt er den Text richtig an...
  109.  
  110. end;
  111.  
  112. // ----------------------------------------------------------------------------------
  113.  
  114. procedure TForm1.FormCreate(Sender: TObject);
  115. begin
  116.  
  117. DC:= GetDC(Handle);
  118.   if not InitOpenGL then
  119.   begin
  120.    showmessage('OpenGL konnte nicht initialisiert werden...');
  121.   end
  122.   else
  123.   begin
  124.    RC:= CreateRenderingContext( DC,
  125.                                 [opDoubleBuffered],
  126.                                 32,
  127.                                 24,
  128.                                 0,0,0,
  129.                                 0);
  130.    ActivateRenderingContext(DC, RC);
  131.  
  132.  
  133.   SetupGL;
  134.  
  135.   Application.OnIdle := IdleHandler;
  136.   end;
  137. end;
  138.  
  139. // ----------------------------------------------------------------------------------
  140.  
  141. procedure TForm1.FormResize(Sender: TObject);
  142. var tmpBool : Boolean;
  143. begin
  144.   glViewport(0, 0, ClientWidth, ClientHeight);
  145.   glMatrixMode(GL_PROJECTION);
  146.   glLoadIdentity;
  147.   gluPerspective(45.0, ClientWidth/ClientHeight, NearClipping, FarClipping);
  148.  
  149.   glMatrixMode(GL_MODELVIEW);
  150.   glLoadIdentity;
  151.   IdleHandler(Sender, tmpBool);
  152. end;
  153.  
  154. // ----------------------------------------------------------------------------------
  155.  
  156. procedure TForm1.FormDestroy(Sender: TObject);
  157. begin
  158.   ftexture.Free;
  159.   fnt.Free;
  160.  
  161.   DeactivateRenderingContext;
  162.   DestroyRenderingContext(RC);
  163.   ReleaseDC(Handle, DC);
  164. end;
  165.  
  166. // ----------------------------------------------------------------------------------
  167.  
  168. procedure TForm1.IdleHandler(Sender: TObject; var Done: Boolean);
  169. begin
  170.   StartTime:= GetTickCount;
  171.   Render;
  172.   DrawTime:= GetTickCount - StartTime;
  173.   Inc(TimeCount, DrawTime);
  174.   Inc(FrameCount);
  175.  
  176.   if TimeCount >= 1000 then begin
  177.     Frames:= FrameCount;
  178.     TimeCount:= TimeCount - 1000;
  179.     FrameCount:= 0;
  180.     Caption:= InttoStr(Frames) + ' FPS';
  181.  
  182.   end;
  183.  
  184.   Done:= false;
  185. end;
  186.  
  187.  
  188. procedure TForm1.FormShow(Sender: TObject);
  189. begin
  190.   form1.ClientHeight:=600;
  191.   form1.ClientWidth:=800;
  192. end;
  193.  
  194. end.


Wenn ich das erstellen von Texture := GlBitmap2d.Create ... und Texture.gentexture weglasse zeigt er den Text an.


Und hier die Unit (Font4.pas - auch auf der Seite zum Download).

Code:
  1. // ---------------------
  2. // Unit: Font4.pas
  3. //
  4. // FontObjs now load data and images separately
  5. // Uses proper ABC font widths
  6. // Supports Unicode Widestrings. (not Win 98 and earlier compatible?)
  7. //
  8. // Can Load JPG (separate RGB and Alpha) and 32bit TGAs
  9. // Can Load Binary Fnt Data which has been combined with another file or separately.
  10. //
  11. // Author: Michael Pote
  12. // Date: 30 April 2007
  13. // -------------------
  14. unit Font4;
  15.  
  16. interface
  17.  
  18. Uses dglOpenGL;
  19.  
  20. const
  21.       MAX_CHARS = high(Word);
  22.       USE_MIPMAP = true; //if you dont want mipmaps set this to false.
  23.  
  24. type  FontInfo = record
  25.         A, C: integer;
  26.         Wid, Hgt: cardinal;
  27.         char: WideChar;
  28.         x1,y1,x2,y2: double;
  29.       end;
  30.  
  31.   TFontObj = class
  32.                  private
  33.                   CharLookup: array[0..MAX_CHARS] of integer;
  34.                  public
  35.  
  36.                   F: array of FontInfo;
  37.                   NumFonts: cardinal;
  38.                   TexInd: glUInt;
  39.  
  40.                   SpaceWidth, MaxHeight: cardinal;
  41.  
  42.                   constructor Create; overload;
  43.  
  44.                   function LoadTGAComp(const Path: string): boolean;
  45.                   function LoadJPEGs(const rgb, alpha: string): boolean;
  46.                   function LoadData(const Filename: string; MergedFile: boolean): boolean;
  47.  
  48.                   function TextLen(const Txt: widestring): integer;
  49.  
  50.                   Procedure Draw(Const X,Y: single;Const Txt: widestring; Lev:single); Overload;
  51.                 end;
  52.  
  53. Procedure DrawQuadRT(X, Y, Wid, Hgt, Lev, Tu, Tu2, Tv,Tv2: single);
  54.  
  55. implementation
  56.  
  57. Uses Classes, Windows, JPEG, Graphics, SysUtils;
  58.  
  59. const
  60.     MajorVersion: byte = 4;
  61.     MinorVersion: byte = 0;
  62.  
  63.  
  64. function LoadJPGs(Filename1, Filename2: String; var Texture: GLuint): Boolean; forward;
  65. function LoadTGA( filename : string; var TexId: glUint; MipMap: boolean) : boolean; forward;        // Loads A TGA File Into Memory
  66.  
  67. ////////////////////////////////////////////////////////////////////////////////
  68. //  TFONTOBJ
  69. ///////////////////////////////////////////////////////////////////////////////
  70.  
  71. constructor TFontObj.Create;
  72. begin
  73.   inherited Create;
  74.   NumFonts := 0;
  75. end;
  76.  
  77. function TFontObj.TextLen(const Txt: widestring): integer;
  78. var CurX: integer;
  79.     Ch: Widechar;
  80.     Chaar, I, Ind: integer;
  81. begin
  82.    CurX := 0;
  83.  
  84.    for I := 1 to length(Txt) do
  85.    begin
  86.        Ch := Txt[I];
  87.        Chaar := integer(ch);
  88.  
  89.        if Chaar = 32 then
  90.        begin
  91.          Ind := -1;
  92.          CurX := CurX + SpaceWidth;
  93.        end
  94.        else
  95.        begin
  96.          Ind := CharLookup[Chaar];
  97.        end;
  98.  
  99.        if ind > -1 then
  100.        begin
  101.          CurX := CurX + F[Ind].A;
  102.  
  103.          //DrawQuadRT(CurX, Y, F[ind].Wid, F[ind].Hgt, 0, F[ind].x1,F[ind].x2,F[ind].y1,F[ind].y2);
  104.  
  105.          CurX := CurX + F[Ind].C;
  106.        end;
  107.    end;
  108.  
  109.    result := CurX;
  110.  
  111. end;
  112.  
  113.  
  114. procedure TFontObj.Draw(const X, Y: single; const Txt: widestring; Lev: single);
  115. var CurX: single;
  116.     Ch: Widechar;
  117.     Chaar, I, Ind: integer;
  118. begin
  119.    CurX := X;
  120.  
  121.    for I := 1 to length(Txt) do
  122.    begin
  123.        Ch := Txt[I];
  124.        Chaar := integer(ch);
  125.  
  126.        if Chaar = 32 then
  127.        begin
  128.          Ind := -1;
  129.          CurX := CurX + SpaceWidth;
  130.        end
  131.        else
  132.        begin
  133.          Ind := CharLookup[Chaar];
  134.        end;
  135.  
  136.        if ind > -1 then
  137.        begin
  138.          CurX := CurX + F[Ind].A;
  139.  
  140.          DrawQuadRT(CurX, Y, F[ind].Wid, F[ind].Hgt, lev, F[ind].x1,F[ind].x2,F[ind].y1,F[ind].y2);
  141.  
  142.          CurX := CurX + F[Ind].C;
  143.        end;
  144.    end;
  145.  
  146. end;
  147.  
  148. //Loads the data part of the font.
  149. //Use MergedFile if you saved the data into the graphic composite in Font Studio.
  150. //Can only load binary font data (.Fnt filetype)
  151. function TFontObj.LoadData(const Filename: string; MergedFile: boolean): boolean;
  152. var Fs: TFileStream;
  153.     Pos: int64;
  154.     I: integer;
  155.     Header: array[0..3] of char;
  156. begin
  157.    Fs := nil;
  158.    try
  159.       Fs := TFileStream.Create(filename, fmOpenRead);
  160.  
  161.       if MergedFile then
  162.       begin
  163.         Fs.Seek(-10, soFromEnd);
  164.         Fs.Read(Header[0], 2);
  165.         if (header[0] <> 'F') or (header[1] <> 'S') then
  166.         begin
  167.           Messagebox(0, 'TFontObj.LoadData: This is not a valid Merged file.', 'Error', MB_OK);
  168.           result := false; //Not a valid merged file.
  169.           exit;
  170.         end;
  171.  
  172.         Fs.Read(Pos, sizeof(int64));
  173.         Fs.Seek(Pos, soFromBeginning); //Move to the correct position.
  174.       end;
  175.  
  176.     Fs.Read(Header[0], 4);
  177.     if (header[0] <> 'F') or (header[1] <> 'S') then
  178.     begin
  179.           Messagebox(0, 'TFontObj.LoadData: This is not a valid font file (must be .fnt format).', 'Error', MB_OK);
  180.           result := false; //Not a valid file.
  181.           exit;
  182.     end;
  183.     if (byte(header[2]) <> MajorVersion) or (byte(header[3]) <> MinorVersion) then
  184.     begin
  185.        Messagebox(0, 'TFontObj.LoadData: Warning! Version Mismatch!', 'Error', MB_OK);
  186.     end;
  187.  
  188.     Fs.Read(NumFonts, sizeof(Cardinal));
  189.     Fs.Read(SpaceWidth, sizeof(Cardinal));
  190.  
  191.     setlength(F, NumFonts);
  192.  
  193.     for I := 0 to MAX_CHARS-1 do
  194.        CharLookup[I] := -1;
  195.  
  196.  
  197.     MaxHeight := 0;
  198.     for I := 0 to NumFonts-1 do
  199.     begin
  200.        fs.Read(F[I].char, sizeof(WideChar));
  201.        fs.Read(F[I].A, sizeof(integer));
  202.        fs.Read(F[I].C, sizeof(integer));
  203.        fs.Read(F[I].Wid, sizeof(cardinal));
  204.        fs.Read(F[I].Hgt, sizeof(cardinal));
  205.        fs.Read(F[I].X1, sizeof(double));
  206.        fs.Read(F[I].Y1, sizeof(double));
  207.        fs.Read(F[I].X2, sizeof(double));
  208.        fs.Read(F[I].Y2, sizeof(double));
  209.  
  210.        if F[I].Hgt > MaxHeight Then MaxHeight := F[I].Hgt;
  211.  
  212.        if (integer(F[I].char) >= 0) and (integer(F[I].char) < MAX_CHARS) then
  213.           CharLookup[integer(F[I].char)] := I;
  214.     end;
  215.  
  216.     Result := true;
  217.  
  218.    finally
  219.       if assigned(Fs) then Fs.Free;
  220.    end;
  221. end;
  222.  
  223.  
  224. // Loads two jpeg images as the font texture.
  225. function TFontObj.LoadJPEGs(const rgb, alpha: string): boolean;
  226. begin
  227.   LoadJpgs(rgb, alpha, TexInd);
  228. end;
  229.  
  230. // Loads a 32bit TGA image as the font texture.
  231. function TFontObj.LoadTGAComp(const Path: string): boolean;
  232. begin
  233.   LoadTGA(path, TexInd, USE_MIPMAP);
  234. end;
  235.  
  236.  
  237.  
  238. ////////////////////////////////////////////////////////////////////////////////
  239.  
  240.  
  241. function CreateTexture(Width, Height, Format : Word; pData : Pointer; Mipmap: boolean) : Integer;
  242. var
  243.   Texture : GLuint;
  244. begin
  245.   glGenTextures(1, @Texture);
  246.   glBindTexture(GL_TEXTURE_2D, Texture);
  247.   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  {Texture blends with object background}
  248.  
  249.   { Select a filtering type. BiLinear filtering produces very good results with little performance impact
  250.     GL_NEAREST               - Basic texture (grainy looking texture)
  251.     GL_LINEAR                - BiLinear filtering
  252.     GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
  253.     GL_LINEAR_MIPMAP_LINEAR  - BiLinear Mipmapped texture
  254.   }
  255.  
  256.   if Mipmap then
  257.   begin
  258.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); { only first two can be used }
  259.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); { all of the above can be used }
  260.   end
  261.   else
  262.   begin
  263.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); { only first two can be used }
  264.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); { all of the above can be used }
  265.   end;
  266.  
  267.   if Format = GL_RGBA then
  268.     gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData)
  269.   else
  270.     gluBuild2DMipmaps(GL_TEXTURE_2D, format, Width, Height, format, GL_UNSIGNED_BYTE, pData);
  271.  
  272.   result :=Texture;
  273. end;
  274.  
  275.  
  276. function LoadJPGs(Filename1, Filename2: String; var Texture: GLuint): Boolean;
  277. var
  278.   Data : Array of Byte;
  279.   W, Width : Integer;
  280.   H, Height : Integer;
  281.   BMP : TBitmap;
  282.   JPG: TJPEGImage;
  283.   C : LongWord;
  284.   Line : PByteArray;
  285.  
  286.   MaxSize: integer;
  287.   TD: Array of LongWord;
  288.   ScaleF: single;
  289.   OldW, OldH, X, Y, I: integer;
  290.  
  291. begin
  292.   result :=FALSE;
  293.   JPG:=TJPEGImage.Create;
  294.  
  295.     try
  296.       JPG.LoadFromFile(Filename1);
  297.     except
  298.       MessageBox(0, PChar('Couldn''t load JPG - "'+ Filename1 +'"'), PChar('BMP Unit'), MB_OK);
  299.       Exit;
  300.     end;
  301.  
  302.   // Create Bitmap
  303.   BMP:=TBitmap.Create;
  304.   BMP.pixelformat:=pf24bit;
  305.   BMP.width:=JPG.width;
  306.   BMP.height:=JPG.height;
  307.   BMP.canvas.draw(0,0,JPG);        // Copy the JPEG onto the Bitmap
  308.  
  309.   Width :=BMP.Width;
  310.   Height :=BMP.Height;
  311.   SetLength(Data, Width*Height*4);
  312.  
  313.   For H:=0 to Height-1 do
  314.   Begin
  315.     Line :=BMP.scanline[Height-H-1];   // flip JPEG
  316.     For W:=0 to Width-1 do
  317.     Begin
  318.  
  319.       Data[(W*4)+(H*Width*4)] := Line[W*3+2];
  320.       Data[(W*4)+1+(H*Width*4)] := Line[W*3+1];
  321.       Data[(W*4)+2+(H*Width*4)] := Line[W*3];
  322.  
  323.     End;
  324.   End;
  325.  
  326.     try
  327.       JPG.LoadFromFile(Filename2);
  328.     except
  329.       MessageBox(0, PChar('Couldn''t load JPG - "'+ Filename2 +'"'), PChar('BMP Unit'), MB_OK);
  330.       Exit;
  331.     end;
  332.   BMP.canvas.draw(0,0,JPG);        // Copy the JPEG onto the Bitmap
  333.  
  334.   For H:=0 to Height-1 do
  335.   Begin
  336.     Line :=BMP.scanline[Height-H-1];   // flip JPEG
  337.     For W:=0 to Width-1 do
  338.     Begin
  339.  
  340.       Data[(W*4)+3+(H*Width*4)] := Line[W*3];
  341.    
  342.  
  343.     End;
  344.   End;
  345.  
  346.   BMP.free;
  347.   JPG.free;
  348.  
  349.  glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxSize);
  350.  
  351.   OldW := -1;
  352.  
  353.   if width > Height then
  354.   begin
  355.    if width > Maxsize then
  356.    begin
  357.  
  358.      ScaleF := MaxSize / width;
  359.      OldW := width;
  360.      OldH := Height;
  361.      width := Maxsize;
  362.      Height := round(Height*ScaleF);
  363.    end;
  364.   end
  365.   else
  366.   begin
  367.    if height > Maxsize then
  368.    begin
  369.      ScaleF := MaxSize / height;
  370.      OldW := width;
  371.      OldH := Height;
  372.      height := Maxsize;
  373.      width := round(width*ScaleF);
  374.    end;
  375.   end;
  376.  
  377.   if OldW > -1 then
  378.   begin
  379.      ScaleF := 1/ScaleF;
  380.      SetLength(TD, Width*Height);
  381.      For X := 0 to Width-1 do
  382.      For Y := 0 to Height-1 do
  383.      begin
  384.         TD[(Y*Width+X)] := data[
  385.                         round(((Y*OldW)*ScaleF+(X*ScaleF)))
  386.                         ];
  387.      end;
  388.      SetLength(Data, Width*Height);
  389.      For Y := 0 to high(Td) do
  390.       Data[Y] := Td[Y];
  391.      SetLength(TD, 0);
  392.  
  393.   end;
  394.  
  395.  
  396.  
  397.  
  398.   Texture :=CreateTexture(Width, Height, GL_RGBA, addr(Data[0]), USE_MIPMAP);
  399.   result :=TRUE;
  400. end;
  401.  
  402.  
  403. Procedure DrawQuadRT(X, Y, Wid, Hgt, Lev, Tu, Tu2, Tv,Tv2: single);
  404. begin
  405.    Tv := 1-Tv;
  406.   Tv2 := 1-Tv2;
  407.   glBegin(GL_QUADS);
  408.     glTexCoord2f(Tu,  Tv); glVertex3f(X,     Y, -lev);
  409.     glTexCoord2f(Tu2, Tv); glVertex3f(X+Wid, Y, -lev);
  410.     glTexCoord2f(Tu2,Tv2); glVertex3f(X+Wid, Y-Hgt, -lev);
  411.     glTexCoord2f(Tu, Tv2); glVertex3f(X,     Y-Hgt, -lev);
  412.   glEnd;
  413. end;
  414.  
  415.  
  416. function LoadTGA( filename : string; var TexId: glUint; MipMap: boolean) : boolean;         // Loads A TGA File Into Memory
  417. const
  418.   TGAheader : array [0..11] of GLubyte = (0,0,2,0,0,0,0,0,0,0,0,0); // Uncompressed TGA Header
  419. TGAComheader : array [0..11] of GLubyte = (0,0,10,0,0,0,0,0,0,0,0,0);   // Compressed TGA Header
  420.  
  421. type TextureImage = record                                      // Structure Name
  422.     imageData : PChar;                                      // Image Data (Up To 32 Bits)
  423.     bpp : GLuint;                                           // Image Color Depth In Bits Per Pixel.
  424.     width : GLuint;                                         // Image Width
  425.     height : GLuint;                                            // Image Height
  426.     texID : GLuint;
  427.      end;
  428.  
  429. var
  430.   TGAcompare : array [0..11] of GLubyte;                                // Used To Compare TGA Header
  431.   header : array [0..5] of GLubyte;                                 // First 6 Useful Bytes From The Header
  432.   bytesPerPixel : GLuint;                   // Holds Number Of Bytes Per Pixel Used In The TGA File
  433.   imageSize : GLuint;                                   // Used To Store The Image Size When Setting Aside Ram
  434.   i : GLuint;                                       // Temporary Variable
  435.   gltype : GLuint;                              // Set The Default GL Mode To RBGA (32 BPP)
  436.   Compressed:boolean;
  437.  
  438.   Tm: Char;
  439.   Texture:  TextureImage;
  440.   tgafile : integer;
  441.   TD: pchar;
  442.   ScaleF: single;
  443.   OldW, OldH, X, Y: integer;
  444.  
  445.   PixelCount, CurrentPixel, CurrentByte: gluInt;
  446.   ColorBuffer: Pchar;
  447.   ChunkHeader: gluByte;
  448.   Counter, Ret: integer;
  449. begin
  450.   tgafile := FileOpen(filename, fmOpenReadWrite);
  451.  
  452.   Ret := FileRead(tgafile, TGAcompare, sizeof(TGAcompare));
  453.  
  454.   if (
  455.       (tgafile = -1) or                             // Does File Even Exist?
  456.       (Ret <> sizeof(TGAcompare))   // Are There 12 Bytes To Read?
  457.      )
  458.   then
  459.   begin
  460.     //ShowError(10, 'TGATexture.LoadTGA', False);
  461.     if (tgafile = -1) then                                  // Did The File Even Exist? *Added Jim Strong*
  462.     begin
  463.       result := false;                                  // Return False
  464.       exit;
  465.     end
  466.     else
  467.     begin
  468.       fileclose(tgafile);                                   // If Anything Failed, Close The File
  469.       result := false;                                  // Return False
  470.       exit;
  471.     end;
  472.   end;
  473.  
  474.   if (CompareMem(@TGAheader, @TGAcompare, sizeof(TGAheader)) = false)                   // Does The Header Match What We Want?
  475.   Then
  476.   begin //File is not uncompressed...
  477.  
  478.    if (CompareMem(@TGAComheader, @TGAcompare, sizeof(TGAComheader)) = True)                 // Does The Header Match What We Want?
  479.    Then
  480.    Begin
  481.       Compressed := True;
  482.    end
  483.    else
  484.    Begin
  485.      i := 0;
  486.      //ShowError(17, 'TGATexture.LoadTGA', False);
  487.      if (tgafile = -1) then                                 // Did The File Even Exist? *Added Jim Strong*
  488.      begin
  489.        result := false;                                 // Return False
  490.        exit;
  491.      end
  492.      else
  493.      begin
  494.        fileclose(tgafile);                                  // If Anything Failed, Close The File
  495.        result := false;                                 // Return False
  496.        exit;
  497.      end;
  498.     end;
  499.  
  500.   end
  501.   else
  502.   begin
  503.    Compressed := False;
  504.   end;
  505.  
  506.    if (FileRead(tgafile, header, sizeof(header)) <> sizeof(header)) then
  507.    begin
  508.      //ShowError(10, 'TGATexture.LoadTGA', False);
  509.      if (tgafile = -1) then                                 // Did The File Even Exist? *Added Jim Strong*
  510.      begin
  511.        result := false;                                 // Return False
  512.        exit;
  513.      end
  514.      else
  515.      begin
  516.        fileclose(tgafile);                                  // If Anything Failed, Close The File
  517.        result := false;                                 // Return False
  518.        exit;
  519.      end;
  520.    end;
  521.  
  522.   texture.width  := header[1] * 256 + header[0];            // Determine The TGA Width  (highbyte*256+lowbyte)
  523.   texture.height := header[3] * 256 + header[2];            // Determine The TGA Height (highbyte*256+lowbyte)
  524.  
  525.   if (texture.width <= 0)   or                              // Is The Width Less Than Or Equal To Zero
  526.      (texture.height <= 0)  or                              // Is The Height Less Than Or Equal To Zero
  527.      ((header[4] <> 24) and (header[4] <> 32)) then                 // Is The TGA 24 or 32 Bit?
  528.   begin
  529.     //ShowError(11, 'TGATexture.LoadTGA', False);
  530.     fileclose(tgafile);                                     // If Anything Failed, Close The File
  531.     result := false;                                        // Return False
  532.     exit;
  533.   end;
  534.  
  535.   texture.bpp   := header[4];                           // Grab The TGA's Bits Per Pixel (24 or 32)
  536.   bytesPerPixel := texture.bpp div 8;                       // Divide By 8 To Get The Bytes Per Pixel
  537.   imageSize := texture.width * texture.height * bytesPerPixel;  // Calculate The Memory Required For The TGA Data
  538.   If BytesPerPixel = 4 then glType := GL_RGBA else
  539.   glType := GL_RGB;
  540.  
  541.   GetMem(texture.imageData, imageSize);     // Reserve Memory To Hold The TGA Data
  542.   //Setlength(texture.imagedata, imagesize);
  543.   if Not Compressed then
  544.   begin
  545.    if (texture.imageData = nil) or         // Does The Storage Memory Exist?
  546.       (fileread(tgafile, texture.imageData^, integer(imageSize)) <> imageSize)  // Does The Image Size Match The Memory Reserved?
  547.       then
  548.       begin
  549.         if (texture.imageData <> nil)                       // Was Image Data Loaded
  550.            then freemem(texture.imageData);                     // If So, Release The Image Data
  551.  
  552.         fileclose(tgafile);                                     // Close The File
  553.         //ShowError(10, 'TGATexture.LoadTGA', False);
  554.         result := false;                                        // Return False
  555.         exit;
  556.       end;
  557.  
  558.     i := 0;
  559.     while i < imageSize do
  560.     with texture do
  561.     begin
  562.      Tm := ImageData[I+2];
  563.      imageData[i+2] := imageData[i];                    // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
  564.      imageData[i] := Tm;                          // Set The 1st Byte To The Value Of The 3rd Byte
  565.  
  566.      i := i + bytesPerPixel;
  567.     end;
  568.  
  569.  end
  570.  else  //COMPRESSED TGA'S
  571.  begin
  572.    PixelCount := texture.width * Texture.Height;
  573.    CurrentPixel := 0;
  574.    CurrentByte := 0;
  575.    GetMem(ColorBuffer, BytesPerPixel);
  576.  
  577.    Repeat
  578.       ChunkHeader := 0;
  579.       if FileRead(tgaFile, ChunkHeader, sizeof(gluByte)) = 0 then
  580.       begin
  581.         //ERROR reading Chunk!
  582.         fileclose(tgafile);                                     // Close The File
  583.         result := false;                                        // Return False
  584.         exit;
  585.       end;
  586.  
  587.  
  588.       if ChunkHeader < 128 then
  589.       begin
  590.          ChunkHeader := ChunkHeader + 1;
  591.          For Counter := 0 to ChunkHeader-1 do
  592.          begin
  593.             if fileRead(tgafile, ColorBuffer^, BytesPerPixel) <> BytesPerPixel then
  594.             begin
  595.              fileclose(tgafile);                                        // Close The File
  596.              result := false;                                       // Return False
  597.              exit;
  598.             end;
  599.  
  600.             Texture.imageData[CurrentByte] := (ColorBuffer[2]);
  601.             Texture.imageData[CurrentByte+1] := (ColorBuffer[1]);
  602.             Texture.imageData[CurrentByte+2] := (ColorBuffer[0]);
  603.             if BytesPerPixel = 4 then
  604.             Texture.imageData[CurrentByte+3] := (ColorBuffer[3]);
  605.  
  606.             CurrentByte := CurrentByte + bytesPerPixel;
  607.             inc(CurrentPixel);
  608.             if CurrentPixel > PixelCount then
  609.             begin
  610.              fileclose(tgafile);                                        // Close The File
  611.              result := false;                                       // Return False
  612.              exit;
  613.             end;
  614.          end;
  615.       end
  616.       else //Chunkheader > 128
  617.       begin
  618.          ChunkHeader := ChunkHeader - 128;
  619.          if fileRead(tgafile, ColorBuffer^, BytesPerPixel) <> BytesPerPixel then
  620.          begin
  621.           fileclose(tgafile);                                       // Close The File
  622.           result := false;                                      // Return False
  623.           exit;
  624.          end;
  625.          For Counter := 0 to ChunkHeader do
  626.          begin
  627.             Texture.imageData[CurrentByte] := ColorBuffer[2];
  628.             Texture.imageData[CurrentByte+1] := ColorBuffer[1];
  629.             Texture.imageData[CurrentByte+2] := ColorBuffer[0];
  630.             if BytesPerPixel = 4 then
  631.             Texture.imageData[CurrentByte+3] := ColorBuffer[3];
  632.  
  633.             CurrentByte := CurrentByte + bytesPerPixel;
  634.             inc(CurrentPixel);
  635.          end;
  636.  
  637.       end;
  638.    Until CurrentPixel >= PixelCount;
  639.    FreeMem(ColorBuffer);
  640.  end;
  641.  fileclose (tgafile);                                           // Close The File
  642.  
  643.  
  644.  
  645.   // Build A Texture From The Data
  646.  Texture.texId := CreateTexture(Texture.Width, Texture.Height, glType, Texture.Imagedata, Mipmap);
  647.  FreeMem(Texture.ImageData);
  648.  
  649.   TexId := Texture.texID;
  650.   result := true;                                           // Texture Building Went Ok, Return True
  651. end;
  652.  
  653. end.


Nach dem Durchschauen der Unit dachte ich es hängt einfach damit zusammen, daß einmal nur dur die Funktion "CreateTexture" eine Texture gebunden wurde und dann in der Zeichenroutine nicht wieder.

Ist da ein Fehler drin oder wie kann man Text richtig darstellen?

Bild
Funzt alles

Bild
Hier ohne Zeichnen des Bildes auf einem Quad - aber schon geladen und an OGL mit texture.Gentexture übergeben.

Bild
Und hier mit gezeichneten Quad und Texture drauf

Wäre echt dankbar wenn ihr mir dabei helfen könnt das Ding zum laufen zu bringen auch mittels meiner nicht gross vorhandenen Englischkenntnisse um mich bei Nitrogen im Forum über das Problem auszulassen...

Mfg
Thomas


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Di Mai 08, 2007 05:50 
Offline
Ernährungsberater
Benutzeravatar

Registriert: Sa Jan 01, 2005 17:11
Beiträge: 2068
Programmiersprache: C++
Schonmal auf die Idee gekommen, dies:
Code:
  1.  ftexture.Bind();

auch für die Fonttexture zu machen?
So bleibt natürlich nach dem ersten Rendern nur die Hintergrundtextur aktiv.

_________________
Steppity,steppity,step,step,step! :twisted:
❆ ❄ ❄ ❄ ❅ ❄ ❆ ❄ ❅ ❄ ❅ ❄ ❅ ❄ ❄
❄ ❄ ❄ ❅ ❄ ❄ ❄ ❅ ❄ ❄ ❆ ❄ ❄


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Di Mai 08, 2007 07:15 
Offline
DGL Member
Benutzeravatar

Registriert: Do Mai 27, 2004 15:22
Beiträge: 25
Wohnort: Stuttgart
Hi,

war wohl meine Faulheit. Kaum zu glauben bei der Uhrzeit und das ich auf Arbeit bin :oops:

Nachdem ich die font4.pas nochmal durchforstet hab kam die Lösung (nach ner Tasse Kaffee).

Einfach wie Du sagtest "bind" aufrufen vorher... mit glBindTexture(GL_TEXTURE_2D, fnt.TexInd)

danke und gut n8


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Di Mai 08, 2007 07:41 
Offline
DGL Member
Benutzeravatar

Registriert: Do Dez 05, 2002 10:35
Beiträge: 4234
Wohnort: Dortmund
Ja also beim Erstellen muss eine Textur erst einmal gebunden werden. Und wenn ich mir so den Code der Fontklasse einmal anschaue, dann kann ich dort nirgends etwas entdecken was die Textur des Fonts bindet. Außer beim Erstellen.

Im Draw müsste er die entsprechende Textur erneut binden. Bzw denke ich, dass er es aus Optimierungsgründen so gemacht, dass er nur die TexturID rausreicht und du sie selber binden musst. Damit man bei richtig großen Texten nicht zu häufig ein und die selbe Textur binden muss. Ob das ein Fehler ist darüber kann man sich streiten. Ich finde es in jedem Fall unglück, dass ich da nirgends einen Hinweis gefunden habe.

Folgendes sollte ausreichen.
Code:
  1. glBindTexture(GL_TEXTURE_2D, fnt.TexInd);
  2. fnt.Draw(...);


PS: Ey. Das ist unfair selber auf die Lösung zu kommen wärend ich gerade am Schreiben bin. :twisted:
PPS: Aber schön, dass du es selber gefunden. ;)


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 4 Beiträge ] 
Foren-Übersicht » Programmierung » Einsteiger-Fragen


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 8 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.014s | 14 Queries | GZIP : On ]