You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.7 KiB

  1. program SimpleLoadFromFile;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}{$IFDEF UseCThreads}
  5. cthreads,
  6. {$ENDIF}{$ENDIF}
  7. Classes, Windows, SysUtils, dglOpenGL, glBitmap, Helper;
  8. var
  9. oglWindow: TOpenGLWindow;
  10. running: Boolean = true;
  11. tex: TglBitmap2D;
  12. function WindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
  13. begin
  14. case Msg of
  15. WM_DESTROY: begin
  16. running := false;
  17. end;
  18. end;
  19. result := DefWindowProc(hWnd, Msg, wParam, lParam);
  20. end;
  21. procedure RenderLoop;
  22. begin
  23. tex.Bind();
  24. glColor4f(1, 1, 1, 1);
  25. glBegin(GL_QUADS);
  26. glTexCoord2f(0, 0); glVertex2f(100, 100);
  27. glTexCoord2f(1, 0); glVertex2f(700, 100);
  28. glTexCoord2f(1, 1); glVertex2f(700, 500);
  29. glTexCoord2f(0, 1); glVertex2f(100, 500);
  30. glEnd;
  31. tex.Unbind();
  32. end;
  33. { function to generate texture data }
  34. procedure GenerateTextureFunc1(var FuncRec: TglBitmapFunctionRec);
  35. var
  36. g1, g2, g3, g4: Single;
  37. begin
  38. g1 := (sin(FuncRec.Position.X / 25) + 1) / 2; // generator function 1: large sinus on x position (0.0 to 1.0)
  39. g2 := (sin(FuncRec.Position.Y / 25) + 1) / 2; // generator function 2: large sinus on y position (0.0 to 1.0)
  40. g3 := FuncRec.Position.X / FuncRec.Size.X; // generator function 3: linear fade on x position (0.0 to 1.0)
  41. g4 := FuncRec.Position.Y / FuncRec.Size.Y; // generator function 4: linear fade on y position (0.0 to 1.0)
  42. FuncRec.Dest.Data.r := Trunc(g1 * FuncRec.Dest.Range.r);
  43. FuncRec.Dest.Data.g := Trunc(g2 * FuncRec.Dest.Range.g);
  44. FuncRec.Dest.Data.b := Trunc(g3 * FuncRec.Dest.Range.b);
  45. FuncRec.Dest.Data.a := Trunc(g4 * FuncRec.Dest.Range.a);
  46. end;
  47. { function to generate texture data }
  48. procedure GenerateTextureFunc2(var FuncRec: TglBitmapFunctionRec);
  49. var
  50. x, y: Single;
  51. begin
  52. x := FuncRec.Position.X / FuncRec.Size.X;
  53. y := FuncRec.Position.Y / FuncRec.Size.Y;
  54. if (x < 0.05) or (x > 0.95) or (y < 0.05) or (y > 0.95) then
  55. begin
  56. FuncRec.Dest.Data := FuncRec.Dest.Range;
  57. end else if (y < 0.333) then begin
  58. FuncRec.Dest.Data := glBitmapRec4ui(0, 0, 0, 0);
  59. end else if (y < 0.666) then begin
  60. FuncRec.Dest.Data := glBitmapRec4ui(FuncRec.Dest.Range.r, 0, 0, 0);
  61. end else begin
  62. FuncRec.Dest.Data := glBitmapRec4ui(FuncRec.Dest.Range.r, FuncRec.Dest.Range.g, 0, 0);
  63. end;
  64. end;
  65. begin
  66. oglWindow := CreateOpenGLWindow('TextureFromFunction', 800, 600, @WindowProc);
  67. try
  68. // create texture use either GenerateTextureFunc1 or GenerateTextureFunc2
  69. tex := TglBitmap2D.Create(
  70. glBitmapSize(512, 512),
  71. tfRGBA8ub4,
  72. @GenerateTextureFunc1
  73. //@GenerateTextureFunc2
  74. );
  75. tex.GenTexture;
  76. while running and ProgressMesages do begin
  77. RenderLoop;
  78. SwapBuffers(oglWindow.DC);
  79. end;
  80. finally
  81. FreeAndNil(tex);
  82. DestroyOpenGLWindow(oglWindow);
  83. end;
  84. end.