fBasicSDL_C.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //---------------------------------------------------------------------------
  2. #pragma hdrstop
  3. #include "fBasicSDL_C.h"
  4. //---------------------------------------------------------------------------
  5. #pragma package(smart_init)
  6. #pragma classgroup "Vcl.Controls.TControl"
  7. #pragma link "GLS.BaseClasses"
  8. #pragma link "GLS.Coordinates"
  9. #pragma link "GLS.GeomObjects"
  10. #pragma link "GLS.Scene"
  11. #pragma link "GLS.SDL.Context"
  12. #pragma resource "*.dfm"
  13. TDataModule2* DataModule2;
  14. //---------------------------------------------------------------------------
  15. __fastcall TDataModule2::TDataModule2(TComponent* Owner) : TDataModule(Owner) {}
  16. //---------------------------------------------------------------------------
  17. void __fastcall TDataModule2::DataModuleCreate(TObject* Sender)
  18. {
  19. // When using SDL, the standard VCL message queue is no longer operational,
  20. // so you must have/make your own loop to prevent the application from
  21. // terminating immediately
  22. GLSDLViewer1->Render();
  23. while (GLSDLViewer1->Active()) {
  24. // Message queue is not operational, but there may still be some messages
  25. Forms::Application->ProcessMessages();
  26. // Relinquish some of that CPU time
  27. SDL_Delay(1);
  28. // Slowly rotate the teapot
  29. Teapot1->RollAngle = 4 * Frac(24 * double(Now())) * 3600;
  30. }
  31. }
  32. //---------------------------------------------------------------------------
  33. void __fastcall TDataModule2::GLSDLViewer1EventPollDone(TObject* Sender)
  34. {
  35. TFileName Path = GetCurrentAssetPath();
  36. TFileName PathCM = GetCurrentDir() + "\\Cubemaps";
  37. SetCurrentDir(PathCM);
  38. if (!firstPassDone) {
  39. // Loads a texture map for the teapot
  40. // (see materials/cubemap for details on that)
  41. //
  42. // The odd bit is that it must be done upon first render, otherwise
  43. // SDL OpenGL support has not been initialized and things like checking
  44. // an extension support (cube maps here) would fail...
  45. // Something less clunky will be introduced, someday...
  46. firstPassDone = true;
  47. GLSDLViewer1->Buffer->RenderingContext->Activate();
  48. try {
  49. if (!GL_ARB_texture_cube_map)
  50. ShowMessage("Your graphics board does not support cube maps");
  51. else {
  52. TGLTexture* tex = Teapot1->Material->Texture;
  53. tex->ImageClassName = __classid(TGLCubeMapImage)->ClassName();
  54. TGLCubeMapImage* img = (TGLCubeMapImage*)tex->Image;
  55. img->Picture[CmtPX]->LoadFromFile("cm_left.jpg");
  56. img->Picture[CmtNX]->LoadFromFile("cm_right.jpg");
  57. img->Picture[CmtPY]->LoadFromFile("cm_top.jpg");
  58. img->Picture[CmtNY]->LoadFromFile("cm_bottom.jpg");
  59. img->Picture[CmtPZ]->LoadFromFile("cm_back.jpg");
  60. img->Picture[CmtNZ]->LoadFromFile("cm_front.jpg");
  61. Teapot1->Material->Texture->MappingMode = tmmCubeMapReflection;
  62. Teapot1->Material->Texture->Disabled = false;
  63. }
  64. } __finally
  65. {
  66. GLSDLViewer1->Buffer->RenderingContext->Deactivate();
  67. }
  68. }
  69. GLSDLViewer1->Render();
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TDataModule2::GLSDLViewer1Resize(TObject* Sender)
  73. {
  74. // Zoom if SDL window gets smaller/bigger
  75. GLCamera1->SceneScale = GLSDLViewer1->Width / 160;
  76. }
  77. //---------------------------------------------------------------------------