fOceanC.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // ---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "fOceanC.h"
  6. // ---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.BaseClasses"
  9. #pragma link "GLS.Cadencer"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLS.Graph"
  12. #pragma link "GLS.Material"
  13. #pragma link "GLS.Objects"
  14. #pragma link "GLS.Scene"
  15. #pragma link "GLS.SimpleNavigation"
  16. #pragma link "GLS.SkyDome"
  17. #pragma link "GLSL.UserShader"
  18. #pragma link "GLS.SceneViewer"
  19. #pragma link "GLS.OpenGLTokens"
  20. #pragma link "GLS.OpenGLAdapter"
  21. #pragma resource "*.dfm"
  22. TForm1 *Form1;
  23. // ---------------------------------------------------------------------------
  24. __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
  25. }
  26. // ---------------------------------------------------------------------------
  27. void __fastcall TForm1::FormCreate(TObject *Sender) {
  28. SetGLSceneMediaDir();
  29. // Load the cube map which is used both for environment and as reflection texture
  30. TGLTexture *tex = MatLib->LibMaterialByName("water")->Material->Texture;
  31. tex->Image->LoadFromFile("noise.bmp");
  32. tex->ImageClassName = __classid(TGLCubeMapImage)->ClassName();
  33. TGLCubeMapImage *img = (TGLCubeMapImage*) tex->Image;
  34. // Load all 6 texture map components of the cube map
  35. // The 'PX', 'NX', etc. refer to 'positive X', 'negative X', etc.
  36. // and follow the RenderMan specs/conventions
  37. img->Picture[CmtPX]->LoadFromFile("CubeMaps\\cm_left.jpg");
  38. img->Picture[CmtNX]->LoadFromFile("CubeMaps\\cm_right.jpg");
  39. img->Picture[CmtPY]->LoadFromFile("CubeMaps\\cm_top.jpg");
  40. img->Picture[CmtNY]->LoadFromFile("CubeMaps\\cm_bottom.jpg");
  41. img->Picture[CmtPZ]->LoadFromFile("CubeMaps\\cm_back.jpg");
  42. img->Picture[CmtNZ]->LoadFromFile("CubeMaps\\cm_front.jpg");
  43. }
  44. // ---------------------------------------------------------------------------
  45. void __fastcall TForm1::DOInitializeRender(TObject *Sender,
  46. TGLRenderContextInfo &rci) {
  47. if (!((GL_SHADER_OBJECT_ARB) && (GL_VERTEX_PROGRAM_ARB) &&
  48. (GL_VERTEX_SHADER_ARB) && (GL_FRAGMENT_SHADER_ARB))) {
  49. ShowMessage(
  50. "Your hardware does not support GLSL to execute this demo!");
  51. }
  52. if (DOInitialize->Tag != 0)
  53. exit;
  54. DOInitialize->Tag = 1;
  55. GLSceneViewer1->Buffer->RenderingContext->Deactivate();
  56. GLMemoryViewer1->RenderCubeMapTextures
  57. (MatLib->LibMaterialByName("cubeMap")->Material->Texture);
  58. GLSceneViewer1->Buffer->RenderingContext->Activate();
  59. TGLProgramHandle *programObject = new TGLProgramHandle(true);
  60. programObject->AddShader(__classid(TGLVertexShaderHandle),
  61. LoadAnsiStringFromFile("Shaders\\ocean_vp.glsl"), true);
  62. programObject->AddShader(__classid(TGLFragmentShaderHandle),
  63. LoadAnsiStringFromFile("Shaders\\ocean_fp.glsl"), true);
  64. if (!programObject->LinkProgram())
  65. throw Exception(programObject->InfoLog());
  66. programObject->UseProgramObject();
  67. programObject->Uniform1i["NormalMap"] = 0;
  68. programObject->Uniform1i["EnvironmentMap"] = 1;
  69. programObject->EndUseProgramObject();
  70. // initialize the heightmap
  71. rci.GLStates->TextureBinding[0][ttTexture2D] =
  72. MatLib->LibMaterialByName("water")->Material->Texture->Handle;
  73. // initialize the heightmap
  74. rci.GLStates->TextureBinding[1][ttTextureCube] =
  75. MatLib->LibMaterialByName("cubeMap")->Material->Texture->Handle;
  76. if (!programObject->ValidateProgram())
  77. throw Exception(programObject->InfoLog());
  78. }
  79. // ---------------------------------------------------------------------------
  80. void __fastcall TForm1::GLUserShader1DoApply(TObject *Sender,
  81. TGLRenderContextInfo &rci)
  82. {
  83. TGLVector camPos;
  84. programObject = new TGLProgramHandle();
  85. programObject->UseProgramObject();
  86. /// programObject->Uniform1f["Time"] = GLCadencer1->CurrentTime * 0.05;
  87. camPos = GLCamera->AbsolutePosition;
  88. /// programObject->Uniform4f["EyePos"] = camPos;
  89. }
  90. // ---------------------------------------------------------------------------
  91. void __fastcall TForm1::GLUserShader1DoUnApply(TObject *Sender, int Pass,
  92. TGLRenderContextInfo &rci, bool &Continue) {
  93. programObject->EndUseProgramObject();
  94. }
  95. // ---------------------------------------------------------------------------
  96. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender,
  97. TShiftState Shift, int X, int Y) {
  98. if (Shift.Contains(ssLeft)) {
  99. GLCamera->MoveAroundTarget(my - Y, mx - X);
  100. }
  101. mx = X;
  102. my = Y;
  103. }
  104. // ---------------------------------------------------------------------------
  105. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender,
  106. const double deltaTime, const double newTime) {
  107. if ((dmx != 0) || (dmy != 0)) {
  108. GLCamera->MoveAroundTarget(dmy * 0.3, dmx * 0.3);
  109. dmx = 0;
  110. dmy = 0;
  111. }
  112. GLSceneViewer1->Invalidate();
  113. }
  114. // ---------------------------------------------------------------------------
  115. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender,
  116. TMouseButton Button, TShiftState Shift, int X, int Y) {
  117. mx = X;
  118. my = Y;
  119. }
  120. // ---------------------------------------------------------------------------
  121. void __fastcall TForm1::GLHeightField1GetHeight(const float x, const float y,
  122. float &z, TVector4f &color, TTexPoint &texPoint) {
  123. z = 0;
  124. }
  125. // ---------------------------------------------------------------------------
  126. const int cExtent = 200;
  127. TGLVBOArrayBufferHandle *vbo;
  128. int nbVerts;
  129. void __fastcall TForm1::DOOceanPlaneRender(TObject *Sender,
  130. TGLRenderContextInfo &rci)
  131. {
  132. int x, y;
  133. TTexPointList *v;
  134. bool cont;
  135. TGLExtensionsAndEntryPoints *GL;
  136. GLUserShader1DoApply(Sender, rci);
  137. GL = new TGLExtensionsAndEntryPoints();
  138. GL->EnableClientState(GL_VERTEX_ARRAY);
  139. if (!vbo) {
  140. v = new TTexPointList;
  141. v->Capacity = (cExtent + 1) * (cExtent + 1);
  142. y = -cExtent;
  143. while (y < cExtent) {
  144. x = -cExtent;
  145. while (x <= cExtent) {
  146. v->Add(y, x);
  147. v->Add(y + 2, x);
  148. x = x + 2;
  149. }
  150. y = y + 2;
  151. v->Add(y, cExtent);
  152. v->Add(y, -cExtent);
  153. }
  154. vbo = new TGLVBOArrayBufferHandle(true);
  155. vbo->Bind();
  156. vbo->BufferData(v->List, v->DataSize(), GL_STATIC_DRAW_ARB);
  157. nbVerts = v->Count;
  158. GL->VertexPointer(2, GL_FLOAT, 0, NULL);
  159. GL->DrawArrays(GL_QUAD_STRIP, 0, nbVerts);
  160. vbo->UnBind();
  161. delete v;
  162. }
  163. else {
  164. vbo->Bind();
  165. GL->VertexPointer(2, GL_FLOAT, 0, NULL);
  166. GL->DrawArrays(GL_TRIANGLE_STRIP, 0, nbVerts);
  167. vbo->UnBind();
  168. }
  169. GL->DisableClientState(GL_VERTEX_ARRAY);
  170. GLUserShader1DoUnApply(Sender, 0, rci, cont);
  171. }
  172. // ---------------------------------------------------------------------------
  173. void __fastcall TForm1::GLSceneViewer1BeforeRender(TObject *Sender) {
  174. /// GLMemoryViewer1->Buffer->RenderingContext->ShareLists(GLSceneViewer1->Buffer->RenderingContext);
  175. /// GLMemoryViewer1->BeforeRender = NULL;
  176. }
  177. // ---------------------------------------------------------------------------