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

Aktuelle Zeit: So Jul 06, 2025 10:09

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



Ein neues Thema erstellen Auf das Thema antworten  [ 3 Beiträge ] 
Autor Nachricht
 Betreff des Beitrags: @Tutorial Terrain2 OpenGL
BeitragVerfasst: Sa Okt 15, 2011 14:11 
Offline
DGL Member

Registriert: Mo Okt 10, 2011 10:17
Beiträge: 5
Moin,
ich habe mal wieder ein Poblem: Ich versuche gerade das Tutorial zur Lightningmap (http://wiki.delphigl.com/index.php/Tutorial_Terrain2#Schattenspiele) zu machen. Allerdings war das Ergebnis nicht gerade zufriedenstellend :(. Daher habe ich jetzt einmal versucht das gesamte Projekt wie es in dem Tutorial vorgestellt wird zu "CopyPasten" in eine eigene Unit anzupassen und zu sehen wie und ob es funktioniert.


Es funktioniert nicht....

Hier wäre der Code der Unit:
Code:
  1. unit u_Lightmap;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Graphics, Math, Geometry, dglOpenGl;
  7.  
  8. type
  9.   TRay = record
  10.     Origin, Direction : TVertex
  11.   end;
  12.  
  13. type
  14.   TNormalMap = Array of Array[0..2] of ShortInt;
  15.  
  16. function RayIntersected(Ray : TRay; HM : TBitMap; HeightMapScale : Single):Boolean;
  17. procedure CreateLightMap(HM : TBitMap; HeightMapScale : Single;NormalMap : TNormalMap; Sun : TVertex;
  18.                          LightMap : TBitMap; LightAmbient, LightDiffuse : Single; Raytracing : Boolean);
  19. procedure PrecacheHeightmap(HM : TBitmap);
  20. procedure CreateNormalMap(HM : TBitMap; HeightScale : Single; var NormalMap : TNormalMap);
  21. procedure LightingCreate;
  22.  
  23. var LightMapTex   : TBitmap;
  24.     HeightMapData : Array of Array of byte;
  25.  
  26. implementation
  27.  
  28. uses u_PerlinNoise;
  29.  
  30. function RayIntersected(Ray : TRay; HM : TBitMap; HeightMapScale : Single):Boolean;
  31. var Position : TVertex;
  32.     Vor      : TVertex;
  33. begin
  34. Result := False;
  35. Position := Ray.Origin;
  36. Vor := VectorNormalize(Ray.Direction);
  37. Position := VectorAdd(Position, Vor);
  38. while (Position[0] > 0) and (Position[0] < HM.Width - 1) and (Position[2] > 0) and (Position[2] < HM.Height - 1) do
  39.       begin
  40.       if HeightMapData[Trunc(Position[0]), Trunc(Position[2])]*HeightMapScale >= Position[1] then
  41.          begin
  42.          Result := True;
  43.          Break;
  44.          end;
  45.       Position := VectorAdd(Position, Vor);
  46.       end;
  47. end;
  48.  
  49. procedure CreateLightMap(HM : TBitMap; HeightMapScale : Single;NormalMap : TNormalMap; Sun : TVertex;
  50.                          LightMap : TBitMap; LightAmbient, LightDiffuse : Single; Raytracing : Boolean);
  51. var X,Z : LongInt;
  52.     Ray : TRay;
  53.     n   : TVertex;
  54.     f   : Single;
  55. begin
  56. PrecacheHeightmap(HM);
  57. Ray.Direction := VectorNormalize(Sun);
  58. for Z := 0 to HM.Height - 1 do
  59.     begin
  60.     for X := 0 to HM.Width - 1 do
  61.         begin
  62.         Ray.Origin[0] := X;
  63.         Ray.Origin[1] := HM.Canvas.Pixels[X,Z]/clWhite*255*HeightMapScale;
  64.         Ray.Origin[2] := Z;
  65.         if RayIntersected(Ray, HM, HeightMapScale) then f := LightAmbient
  66.          else
  67.            begin
  68.            n[0] := NormalMap[Z*(HM.Height) + X][0];
  69.            n[1] := NormalMap[Z*(HM.Height) + X][1];
  70.            n[2] := NormalMap[Z*(HM.Height) + X][2];
  71.            f := LightAmbient+ LightDiffuse*(VectorDotProduct(VectorNormalize(n),VectorNormalize(Ray.Direction)));
  72.            if f > 1.0 then f := 1.0;
  73.            if f < 0.0 then f := 0.0;
  74.            end;
  75.         LightMap.Canvas.Pixels[X,Z] := RGB(Trunc(f*255), Trunc(f*255), Trunc(f*255));
  76.         end;
  77.     end;
  78. end;
  79.  
  80. procedure PrecacheHeightmap(HM : TBitmap);
  81. var X,Z : LongInt;
  82. begin
  83. SetLength(HeightMapData, HM.Width);
  84. for X := 0 to Length(HeightMapData) -1 do SetLength(HeightMapData[X], HM.Height);
  85. for Z := 0 to HM.Height - 1 do
  86.     for X := 0 to HM.Width - 1 do HeightMapData[X,Z] := Trunc(HM.Canvas.Pixels[X,Z]/clWhite * 255);
  87. end;
  88.  
  89. procedure CreateNormalMap(HM : TBitMap; HeightScale : Single; var NormalMap : TNormalMap);
  90. const VertexPoss : Array[0..8] of Array[0..1] of ShortInt = ((0,0),(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1));
  91.       Faces : Array[0..7] of Array[0..2] of Byte = ((0,1,8),(0,2,1),(0,4,2),(4,3,2),(0,5,4),(0,6,5),(0,8,6),(8,7,6));
  92. var X,Z               : LongInt;
  93.     I                 : LongInt;
  94.     VertexPosMultiply : LongInt;
  95.     Vertieces         : Array[0..8] of TVertex;
  96.     Normals           : Array[0..7] of TVertex;
  97.     Normale           : TVertex;
  98. begin
  99. SetLength(NormalMap, HM.Width*HM.Height);
  100. FillChar(Vertieces[0,0], SizeOf(Vertieces), 0);
  101. for Z := 0 to HM.Height - 1 do
  102.     begin
  103.     for X := 0 to HM.Width - 1 do
  104.         begin
  105.         Normale[0] := 0;
  106.         Normale[1] := 0;
  107.         Normale[2] := 0;
  108.         VertexPosMultiply := 64;
  109.         for I := 0 to 8 do
  110.             begin
  111.             Vertieces[I][0] := X + VertexPoss[I][0]*VertexPosMultiply;
  112.             Vertieces[I][2] := Z + VertexPoss[I][1]*VertexPosMultiply;
  113.             Vertieces[I][1] := HM.Canvas.Pixels[X + VertexPoss[I][0]*VertexPosMultiply,
  114.                                Z + VertexPoss[I][1]*VertexPosMultiply]/clWhite*255*HeightScale;
  115.             end;
  116.         for I := 0 to 7 do
  117.             begin
  118.             Normals[I] := VectorCrossProduct(VectorNormalize(VectorSubtract(Vertieces[Faces[I][0]],
  119.                           Vertieces[Faces[I][1]])),VectorNormalize(VectorSubtract(Vertieces[Faces[I][1]],
  120.                           Vertieces[Faces[I][2]])));
  121.             Normals[I] := VectorNormalize(Normals[I]);
  122.             end;
  123.         for I := 0 to 7 do Normale := VectorAdd(Normale, Normals[I]);
  124.         NormalizeVector(Normale);
  125.         Normale := VectorScale(Normale,127.0);
  126.         NormalMap[Z*(HM.Height) + X][0] := Trunc(Normale[0]);
  127.         NormalMap[Z*(HM.Height) + X][1] := Trunc(Normale[1]);
  128.         NormalMap[Z*(HM.Height) + X][2] := Trunc(Normale[2]);
  129.         end;
  130.     end;
  131. end;
  132.  
  133. procedure LightingCreate;
  134. var Sun            : TVertex;
  135.     HeightMapScale : Single;
  136.     NormalMap      : TNormalMap;
  137.     Value          : SmallInt;
  138.     HM             : TBitmap;
  139. begin
  140. Value := Trunc(Random(180)*Pi)+180;
  141. HM := u_PerlinNoise.Heightmap;                                               //Heightmap einer anderen Unit die hier übergeben wird
  142. Sun[0] := Cos(Value/180);
  143. Sun[1] := Value/100;
  144. Sun[2] := Sin(Value/180);
  145. HeightMapScale := 512;
  146. LightMapTex.Width := 512;                                                     //LightMapTex wird im OnCreate der Form in u_Main erzeugt
  147. LightMapTex.Height := 512;
  148. CreateNormalMap(HM, HeightMapScale, NormalMap);
  149. CreateLightMap(HM, HeightMapScale, NormalMap, Sun, LightMapTex, Random(100)/100, Random(100)/100, true);
  150. end;
  151.  
  152. end.
  153.  


Ein Beispielergebnis für ein Bild welches mir mit der Unit geliefert wird ist angehängt.
Kann mir jemand einen Tipp geben woran es liegen könnte, dass es nicht so funktioniert wie im Tutorial?

mfg
Hering


Du hast keine ausreichende Berechtigung, um die Dateianhänge dieses Beitrags anzusehen.


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Re: @Tutorial Terrain2 OpenGL
BeitragVerfasst: Sa Okt 15, 2011 22:45 
Offline
DGL Member
Benutzeravatar

Registriert: Sa Aug 18, 2007 18:47
Beiträge: 694
Wohnort: Köln
Programmiersprache: Java
Bin schon was länger weg von Delphi...

...aber müsste man LightMapTex nicht irgendwie instanziieren? LightMapTex.Create; oder sowas?

_________________
Es werde Licht.
glEnable(GL_LIGHTING);
Und es ward Licht.


Zitat aus einem Java Buch: "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off"

on error goto next


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Re: @Tutorial Terrain2 OpenGL
BeitragVerfasst: So Okt 16, 2011 09:58 
Offline
DGL Member

Registriert: Mo Okt 10, 2011 10:17
Beiträge: 5
Moin,

Zitat:
//LightMapTex wird im OnCreate der Form in u_Main erzeugt


Meinst du sowas? Weil das habe ich ja gemacht.

mfg
Hering

//EDIT

So ich hab die Unit jetzt mal angepasst, vllt kann mir ja dann jemand helfen :) :
Code:
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //                     Unit zum Erzeugen einer Lightmap                       //
  3. //  Benutzung: Beispiel: var Lightmap : TLightmap;                            //
  4. //                       Lightmap.Lightmap := TBitmap.Create;                 //
  5. //                       Lightmap.GenLightMap(Heightmap);                     //
  6. //                       Img_Lightmap.Picture.Bitmap := Lightmap.Lightmap;    //
  7. ////////////////////////////////////////////////////////////////////////////////
  8. unit u_Lightmap;
  9.  
  10. interface
  11.  
  12. uses
  13.   Windows, Graphics, Geometry;
  14.  
  15. type
  16.   TRay = record
  17.     Origin, Direction : TVertex
  18.   end;
  19.  
  20. type TNormalMap = Array of Array[0..2] of ShortInt;
  21.  
  22. type
  23.   TLightmap = record
  24.    LightMap      : TBitmap;
  25.    HeightMapData : Array of Array of byte;
  26.    function RayIntersected(Ray : TRay; HM : TBitMap; HeightMapScale : Single):Boolean;
  27.    procedure CreateLightMap(HM : TBitMap; HeightMapScale : Single;NormalMap : TNormalMap; Sun : TVertex;
  28.                             LightMap : TBitMap; LightAmbient, LightDiffuse : Single; Raytracing : Boolean);
  29.    procedure PrecacheHeightmap(HM : TBitmap);
  30.    procedure CreateNormalMap(HM : TBitMap; HeightScale : Single; var NormalMap : TNormalMap);
  31.    procedure GenLightMap(Heightmap : TBitmap);
  32.   end;
  33.  
  34. implementation
  35.  
  36. function TLightmap.RayIntersected(Ray : TRay; HM : TBitMap; HeightMapScale : Single):Boolean;
  37. var Position : TVertex;
  38.     Vor      : TVertex;
  39. begin
  40. Result := False;
  41. Position := Ray.Origin;
  42. Vor := VectorNormalize(Ray.Direction);
  43. Position := VectorAdd(Position, Vor);
  44. while (Position[0] > 0) and (Position[0] < HM.Width - 1) and (Position[2] > 0) and (Position[2] < HM.Height - 1) do
  45.       begin
  46.       if HeightMapData[Trunc(Position[0]), Trunc(Position[2])]*HeightMapScale >= Position[1] then
  47.          begin
  48.          Result := True;
  49.          Break;
  50.          end;
  51.       Position := VectorAdd(Position, Vor);
  52.       end;
  53. end;
  54.  
  55. procedure TLightmap.CreateLightMap(HM : TBitMap; HeightMapScale : Single;NormalMap : TNormalMap; Sun : TVertex;
  56.                                    LightMap : TBitMap; LightAmbient, LightDiffuse : Single; Raytracing : Boolean);
  57. var X,Z : LongInt;
  58.     Ray : TRay;
  59.     n   : TVertex;
  60.     f   : Single;
  61. begin
  62. PrecacheHeightmap(HM);
  63. Ray.Direction := VectorNormalize(Sun);
  64. for Z := 0 to HM.Height - 1 do
  65.     begin
  66.     for X := 0 to HM.Width - 1 do
  67.         begin
  68.         Ray.Origin[0] := X;
  69.         Ray.Origin[1] := HM.Canvas.Pixels[X,Z]/clWhite*255*HeightMapScale;
  70.         Ray.Origin[2] := Z;
  71.         if RayIntersected(Ray, HM, HeightMapScale) then f := LightAmbient
  72.          else
  73.            begin
  74.            n[0] := NormalMap[Z*(HM.Height) + X][0];
  75.            n[1] := NormalMap[Z*(HM.Height) + X][1];
  76.            n[2] := NormalMap[Z*(HM.Height) + X][2];
  77.            f := LightAmbient+ LightDiffuse*(VectorDotProduct(VectorNormalize(n),VectorNormalize(Ray.Direction)));
  78.            if f > 1.0 then f := 1.0;
  79.            if f < 0.0 then f := 0.0;
  80.            end;
  81.         LightMap.Canvas.Pixels[X,Z] := RGB(Trunc(f*255), Trunc(f*255), Trunc(f*255));
  82.         end;
  83.     end;
  84. end;
  85.  
  86. procedure TLightmap.PrecacheHeightmap(HM : TBitmap);
  87. var X,Z : LongInt;
  88. begin
  89. SetLength(HeightMapData, HM.Width);
  90. for X := 0 to Length(HeightMapData) -1 do SetLength(HeightMapData[X], HM.Height);
  91. for Z := 0 to HM.Height - 1 do
  92.     for X := 0 to HM.Width - 1 do HeightMapData[X,Z] := Trunc(HM.Canvas.Pixels[X,Z]/clWhite * 255);
  93. end;
  94.  
  95. procedure TLightmap.CreateNormalMap(HM : TBitMap; HeightScale : Single; var NormalMap : TNormalMap);
  96. const VertexPoss : Array[0..8] of Array[0..1] of ShortInt = ((0,0),(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1));
  97.       Faces : Array[0..7] of Array[0..2] of Byte = ((0,1,8),(0,2,1),(0,4,2),(4,3,2),(0,5,4),(0,6,5),(0,8,6),(8,7,6));
  98. var X,Z               : LongInt;
  99.     I                 : LongInt;
  100.     VertexPosMultiply : LongInt;
  101.     Vertieces         : Array[0..8] of TVertex;
  102.     Normals           : Array[0..7] of TVertex;
  103.     Normale           : TVertex;
  104. begin
  105. SetLength(NormalMap, HM.Width*HM.Height);
  106. FillChar(Vertieces[0,0], SizeOf(Vertieces), 0);
  107. for Z := 0 to HM.Height - 1 do
  108.     begin
  109.     for X := 0 to HM.Width - 1 do
  110.         begin
  111.         Normale[0] := 0;
  112.         Normale[1] := 0;
  113.         Normale[2] := 0;
  114.         VertexPosMultiply := 64;
  115.         for I := 0 to 8 do
  116.             begin
  117.             Vertieces[I][0] := X + VertexPoss[I][0]*VertexPosMultiply;
  118.             Vertieces[I][2] := Z + VertexPoss[I][1]*VertexPosMultiply;
  119.             Vertieces[I][1] := HM.Canvas.Pixels[X + VertexPoss[I][0]*VertexPosMultiply,
  120.                                Z + VertexPoss[I][1]*VertexPosMultiply]/clWhite*255*HeightScale;
  121.             end;
  122.         for I := 0 to 7 do
  123.             begin
  124.             Normals[I] := VectorCrossProduct(VectorNormalize(VectorSubtract(Vertieces[Faces[I][0]],
  125.                           Vertieces[Faces[I][1]])),VectorNormalize(VectorSubtract(Vertieces[Faces[I][1]],
  126.                           Vertieces[Faces[I][2]])));
  127.             Normals[I] := VectorNormalize(Normals[I]);
  128.             end;
  129.         for I := 0 to 7 do Normale := VectorAdd(Normale, Normals[I]);
  130.         NormalizeVector(Normale);
  131.         Normale := VectorScale(Normale,127.0);
  132.         NormalMap[Z*(HM.Height) + X][0] := Trunc(Normale[0]);
  133.         NormalMap[Z*(HM.Height) + X][1] := Trunc(Normale[1]);
  134.         NormalMap[Z*(HM.Height) + X][2] := Trunc(Normale[2]);
  135.         end;
  136.     end;
  137. end;
  138.  
  139. procedure TLightmap.GenLightMap(Heightmap : TBitmap);
  140. var Sun            : TVertex;
  141.     HeightMapScale : Single;
  142.     NormalMap      : TNormalMap;
  143.     Value          : SmallInt;
  144.     HM             : TBitmap;
  145. begin
  146. Value := Trunc(Random(180)*Pi)+180;
  147. HM := Heightmap;
  148. Sun[0] := Cos(Value/180);
  149. Sun[1] := Value/100;
  150. Sun[2] := Sin(Value/180);
  151. HeightMapScale := Heightmap.Width;
  152. LightMap.Width := Heightmap.Width;
  153. LightMap.Height := Heightmap.Height;
  154. CreateNormalMap(HM, HeightMapScale, NormalMap);
  155. CreateLightMap(HM, HeightMapScale, NormalMap, Sun, LightMap, Random(100)/100, Random(100)/100, true);
  156. end;
  157.  
  158. end.
  159.  
  160.  


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


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 4 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:  
cron
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.008s | 16 Queries | GZIP : On ]