Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

102 linhas
3.1 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. data: TglBitmapData;
  12. tex: TglBitmap2D;
  13. function WindowProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
  14. begin
  15. case Msg of
  16. WM_DESTROY: begin
  17. running := false;
  18. end;
  19. end;
  20. result := DefWindowProc(hWnd, Msg, wParam, lParam);
  21. end;
  22. procedure RenderLoop;
  23. begin
  24. tex.Bind();
  25. glColor4f(1, 1, 1, 1);
  26. glBegin(GL_QUADS);
  27. glTexCoord2f(0, 0); glVertex2f(100, 100);
  28. glTexCoord2f(1, 0); glVertex2f(700, 100);
  29. glTexCoord2f(1, 1); glVertex2f(700, 500);
  30. glTexCoord2f(0, 1); glVertex2f(100, 500);
  31. glEnd;
  32. tex.Unbind();
  33. end;
  34. { function to generate texture data }
  35. procedure GenerateTextureFunc1(var FuncRec: TglBitmapFunctionRec);
  36. var
  37. g1, g2, g3, g4: Single;
  38. begin
  39. g1 := (sin(FuncRec.Position.X / 25) + 1) / 2; // generator function 1: large sinus on x position (0.0 to 1.0)
  40. g2 := (sin(FuncRec.Position.Y / 25) + 1) / 2; // generator function 2: large sinus on y position (0.0 to 1.0)
  41. g3 := FuncRec.Position.X / FuncRec.Size.X; // generator function 3: linear fade on x position (0.0 to 1.0)
  42. g4 := FuncRec.Position.Y / FuncRec.Size.Y; // generator function 4: linear fade on y position (0.0 to 1.0)
  43. FuncRec.Dest.Data.r := Trunc(g1 * FuncRec.Dest.Range.r);
  44. FuncRec.Dest.Data.g := Trunc(g2 * FuncRec.Dest.Range.g);
  45. FuncRec.Dest.Data.b := Trunc(g3 * FuncRec.Dest.Range.b);
  46. FuncRec.Dest.Data.a := Trunc(g4 * FuncRec.Dest.Range.a);
  47. end;
  48. { function to generate texture data }
  49. procedure GenerateTextureFunc2(var FuncRec: TglBitmapFunctionRec);
  50. var
  51. x, y: Single;
  52. begin
  53. x := FuncRec.Position.X / FuncRec.Size.X;
  54. y := FuncRec.Position.Y / FuncRec.Size.Y;
  55. if (x < 0.05) or (x > 0.95) or (y < 0.05) or (y > 0.95) then
  56. begin
  57. FuncRec.Dest.Data := FuncRec.Dest.Range;
  58. end else if (y < 0.333) then begin
  59. FuncRec.Dest.Data := glBitmapRec4ui(0, 0, 0, 0);
  60. end else if (y < 0.666) then begin
  61. FuncRec.Dest.Data := glBitmapRec4ui(FuncRec.Dest.Range.r, 0, 0, 0);
  62. end else begin
  63. FuncRec.Dest.Data := glBitmapRec4ui(FuncRec.Dest.Range.r, FuncRec.Dest.Range.g, 0, 0);
  64. end;
  65. end;
  66. begin
  67. oglWindow := CreateOpenGLWindow('TextureFromFunction', 800, 600, @WindowProc);
  68. try
  69. tex := TglBitmap2D.Create; // create texture object
  70. data := TglBitmapData.Create; // create texture data object
  71. try
  72. data.LoadFromFunc( // generate texture data using either GenerateTextureFunc1 or GenerateTextureFunc2
  73. glBitmapSize(512, 512),
  74. tfRGBA8ub4,
  75. @GenerateTextureFunc1
  76. //@GenerateTextureFunc2
  77. );
  78. tex.UploadData(data); // upload data to video card
  79. finally
  80. FreeAndNil(data); // after upload is done, the data object could be freed to save memory
  81. end;
  82. while running and ProgressMesages do begin
  83. RenderLoop;
  84. SwapBuffers(oglWindow.DC);
  85. end;
  86. finally
  87. FreeAndNil(tex);
  88. DestroyOpenGLWindow(oglWindow);
  89. end;
  90. end.