Unit1.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLBaseClasses"
  9. #pragma link "GLCadencer"
  10. #pragma link "GLCoordinates"
  11. #pragma link "GLCrossPlatform"
  12. #pragma link "GLGraph"
  13. #pragma link "GLMaterial"
  14. #pragma link "GLObjects"
  15. #pragma link "GLScene"
  16. #pragma link "GLSimpleNavigation"
  17. #pragma link "GLSkydome"
  18. #pragma link "GLUserShader"
  19. #pragma link "GLWin32Viewer"
  20. #pragma link "OpenGLTokens"
  21. #pragma link "OpenGLAdapter"
  22. #pragma resource "*.dfm"
  23. TForm1 *Form1;
  24. //---------------------------------------------------------------------------
  25. __fastcall TForm1::TForm1(TComponent* Owner)
  26. : TForm(Owner)
  27. {
  28. }
  29. //---------------------------------------------------------------------------
  30. void __fastcall TForm1::FormCreate(TObject *Sender)
  31. {
  32. SetGLSceneMediaDir();
  33. // Load the cube map which is used both for environment and as reflection texture
  34. TGLTexture *tex = MatLib->LibMaterialByName("water")->Material->Texture;
  35. tex->Image->LoadFromFile("noise.bmp");
  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, TGLRenderContextInfo &rci)
  50. {
  51. if (! ((GL_SHADER_OBJECT_ARB) &&
  52. (GL_VERTEX_PROGRAM_ARB) &&
  53. (GL_VERTEX_SHADER_ARB) &&
  54. (GL_FRAGMENT_SHADER_ARB)))
  55. {
  56. ShowMessage("Your hardware does not support GLSL to execute this demo!");
  57. }
  58. if (DOInitialize->Tag != 0)
  59. exit;
  60. DOInitialize->Tag = 1;
  61. GLSceneViewer1->Buffer->RenderingContext->Deactivate();
  62. GLMemoryViewer1->RenderCubeMapTextures(MatLib->LibMaterialByName("cubeMap")->Material->Texture);
  63. GLSceneViewer1->Buffer->RenderingContext->Activate();
  64. TGLProgramHandle *programObject = new TGLProgramHandle(true);
  65. programObject->AddShader(__classid(TGLVertexShaderHandle), LoadAnsiStringFromFile("ocean_vp.glsl"), true);
  66. programObject->AddShader(__classid(TGLFragmentShaderHandle), LoadAnsiStringFromFile("ocean_fp.glsl"), true);
  67. if (! programObject->LinkProgram())
  68. throw Exception(programObject->InfoLog());
  69. programObject->UseProgramObject();
  70. programObject->Uniform1i["NormalMap"] = 0;
  71. programObject->Uniform1i["EnvironmentMap"] = 1;
  72. programObject->EndUseProgramObject();
  73. // initialize the heightmap
  74. rci.GLStates->TextureBinding[0][ttTexture2D] =
  75. MatLib->LibMaterialByName("water")->Material->Texture->Handle;
  76. // initialize the heightmap
  77. rci.GLStates->TextureBinding[1][ttTextureCube] =
  78. MatLib->LibMaterialByName("cubeMap")->Material->Texture->Handle;
  79. if (! programObject->ValidateProgram())
  80. throw Exception(programObject->InfoLog());
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TForm1::GLUserShader1DoApply(TObject *Sender, TGLRenderContextInfo &rci)
  84. {
  85. Glvectorgeometry::TVector camPos;
  86. programObject = new TGLProgramHandle();
  87. programObject->UseProgramObject();
  88. /// programObject->Uniform1f["Time"] = GLCadencer1->CurrentTime * 0.05;
  89. camPos = GLCamera->AbsolutePosition;
  90. /// programObject->Uniform4f["EyePos"] = camPos;
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TForm1::GLUserShader1DoUnApply(TObject *Sender, int Pass, TGLRenderContextInfo &rci,
  94. bool &Continue)
  95. {
  96. programObject->EndUseProgramObject();
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  100. int X, int Y)
  101. {
  102. if (Shift.Contains(ssLeft))
  103. {
  104. GLCamera->MoveAroundTarget(my-Y, mx-X);
  105. }
  106. mx = X;
  107. my = Y;
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  111. const double newTime)
  112. {
  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, TMouseButton Button,
  122. TShiftState Shift, int X, int Y)
  123. {
  124. mx = X;
  125. my = Y;
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TForm1::GLHeightField1GetHeight(const float x, const float y, float &z,
  129. TVector4f &color, TTexPoint &texPoint)
  130. {
  131. z = 0;
  132. }
  133. //---------------------------------------------------------------------------
  134. const int
  135. cExtent = 200;
  136. TGLVBOArrayBufferHandle *vbo;
  137. int nbVerts;
  138. void __fastcall TForm1::DOOceanPlaneRender(TObject *Sender, TGLRenderContextInfo &rci)
  139. {
  140. int x, y;
  141. TTexPointList *v;
  142. bool cont;
  143. TGLExtensionsAndEntryPoints *GL;
  144. GLUserShader1DoApply(Sender, rci);
  145. GL = new TGLExtensionsAndEntryPoints();
  146. GL->EnableClientState(GL_VERTEX_ARRAY);
  147. if (! vbo)
  148. {
  149. v = new TTexPointList;
  150. v->Capacity = (cExtent + 1)*(cExtent + 1);
  151. y = -cExtent;
  152. while (y < cExtent)
  153. {
  154. x = -cExtent;
  155. while (x <= cExtent) {
  156. v->Add(y, x);
  157. v->Add(y + 2, x);
  158. x = x + 2;
  159. }
  160. y = y + 2;
  161. v->Add(y, cExtent);
  162. v->Add(y, -cExtent);
  163. }
  164. vbo = new TGLVBOArrayBufferHandle(true);
  165. vbo->Bind();
  166. vbo->BufferData(v->List, v->DataSize(), GL_STATIC_DRAW_ARB);
  167. nbVerts = v->Count;
  168. GL->VertexPointer(2, GL_FLOAT, 0, NULL);
  169. GL->DrawArrays(GL_QUAD_STRIP, 0, nbVerts);
  170. vbo->UnBind();
  171. delete v;
  172. }
  173. else
  174. {
  175. vbo->Bind();
  176. GL->VertexPointer(2, GL_FLOAT, 0, NULL);
  177. GL->DrawArrays(GL_TRIANGLE_STRIP, 0, nbVerts);
  178. vbo->UnBind();
  179. }
  180. GL->DisableClientState(GL_VERTEX_ARRAY);
  181. GLUserShader1DoUnApply(Sender, 0, rci, cont);
  182. }
  183. //---------------------------------------------------------------------------
  184. void __fastcall TForm1::GLSceneViewer1BeforeRender(TObject *Sender)
  185. {
  186. // GLMemoryViewer1->Buffer->RenderingContext->ShareLists(GLSceneViewer1->Buffer->RenderingContext);
  187. // GLMemoryViewer1->BeforeRender = NULL;
  188. }
  189. //---------------------------------------------------------------------------