Unit1.cpp 3.1 KB

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