fOceanC.cpp 6.7 KB

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