Unit1.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //---------------------------------------------------------------------------
  2. /*: Simple TGLShader based multipass demo.
  3. This demo uses a custom TGLShader subclass to implement the classic
  4. multipass hidden lines rendering technique on a torus: first pass renders
  5. model with filled triangles, second pass does the wireframe.
  6. You'll also note the glPolygonOffset call, it displaces fragments depths
  7. value a little "farther away" so that surface fill depth values do not
  8. interact with the rendering of the lines (comment out the call and you'll
  9. see).<br>
  10. The axis and sphere allow you to see the limit of that simple technique:
  11. it actually "paints" between the lines, so you cannot use it to make
  12. transparent wireframed objects with hidden lines - if that thought ever
  13. blossomed in your mind ;)
  14. Additionnal objects around the show a glow/toon edges effect achieved in two
  15. passes too: the 1st pass activate lines and gives them a width, the second
  16. is used to fill the surface (and clear the lines that aren't on edges).
  17. (TOutLineShader thanks to Delauney Jerome, [email protected])
  18. */
  19. #include <vcl.h>
  20. #pragma hdrstop
  21. #include "Unit1.h"
  22. #include <assert.h>
  23. //---------------------------------------------------------------------------
  24. #pragma package(smart_init)
  25. #pragma link "GLGeomObjects"
  26. #pragma link "GLObjects"
  27. #pragma link "GLScene"
  28. #pragma link "GLTexture"
  29. #pragma link "GLWin32Viewer"
  30. #pragma link "GLBaseClasses"
  31. #pragma link "GLCoordinates"
  32. #pragma link "GLCrossPlatform"
  33. #pragma link "GLMaterial"
  34. #include "OpenGLTokens.hpp"
  35. #include "OpenGL1x.hpp"
  36. #pragma resource "*.dfm"
  37. TForm1 *Form1;
  38. //---------------------------------------------------------------------------
  39. __fastcall TForm1::TForm1(TComponent* Owner)
  40. : TForm(Owner)
  41. {
  42. }
  43. //---------------------------------------------------------------------------
  44. __fastcall THiddenLineShader::THiddenLineShader(TComponent* AOwner)
  45. : TGLShader(AOwner)
  46. {
  47. }
  48. //---------------------------------------------------------------------------
  49. __fastcall THiddenLineShader::~THiddenLineShader(void)
  50. {
  51. }
  52. //---------------------------------------------------------------------------
  53. void __fastcall THiddenLineShader::DoApply(TGLRenderContextInfo &rci, System::TObject* Sender)
  54. {
  55. // new object getting rendered, 1st pass
  56. PassCount = 1;
  57. // backup state
  58. glPushAttrib(GL_ENABLE_BIT);
  59. // disable lighting, this is a solid fill
  60. glDisable(GL_LIGHTING);
  61. rci.GLStates->PolygonMode = GL_FRONT_AND_BACK, GL_FILL;
  62. // use background color
  63. glColor3fv(&BackgroundColor.X);
  64. // enable and adjust polygon offset
  65. glEnable(GL_POLYGON_OFFSET_FILL);
  66. glPolygonOffset(1, 2);
  67. }
  68. //---------------------------------------------------------------------------
  69. bool __fastcall THiddenLineShader::DoUnApply(TGLRenderContextInfo &rci)
  70. {
  71. bool result;
  72. switch (PassCount)
  73. {
  74. case 1 : {
  75. // 1st pass completed, we setup for the second
  76. PassCount = 2;
  77. // switch to wireframe and its color
  78. rci.GLStates->PolygonMode = GL_FRONT_AND_BACK, GL_LINE;
  79. glColor3fv(&LineColor.X);
  80. // disable polygon offset
  81. glDisable(GL_POLYGON_OFFSET_LINE);
  82. result = true;
  83. break;
  84. }
  85. case 2 : {
  86. // restore state
  87. glPopAttrib();
  88. // we're done
  89. result = false;
  90. break;
  91. }
  92. default : {
  93. // doesn't hurt to be cautious
  94. assert(false);
  95. result = false;
  96. }
  97. }
  98. return result;
  99. }
  100. //---------------------------------------------------------------------------
  101. __fastcall TOutLineShader::TOutLineShader(TComponent* AOwner)
  102. : TGLShader(AOwner)
  103. {
  104. }
  105. //---------------------------------------------------------------------------
  106. __fastcall TOutLineShader::~TOutLineShader(void)
  107. {
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TOutLineShader::DoApply(TGLRenderContextInfo &rci, System::TObject* Sender)
  111. {
  112. PassCount = 1;
  113. glPushAttrib(GL_ENABLE_BIT);
  114. glDisable(GL_LIGHTING);
  115. if (OutlineSmooth)
  116. {
  117. glEnable(GL_BLEND);
  118. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  119. glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  120. glEnable(GL_LINE_SMOOTH);
  121. }
  122. else glDisable(GL_LINE_SMOOTH);
  123. glGetFloatv(GL_LINE_WIDTH,&OldlineWidth);
  124. glLineWidth(OutlineWidth);
  125. glPolygonMode(GL_BACK, GL_LINE);
  126. glCullFace(GL_FRONT);
  127. glDepthFunc(GL_LEQUAL);
  128. glColor3fv(&LineColor.X);
  129. }
  130. //---------------------------------------------------------------------------
  131. bool __fastcall TOutLineShader::DoUnApply(TGLRenderContextInfo &rci)
  132. {
  133. bool result;
  134. switch (PassCount)
  135. {
  136. case 1 : {
  137. PassCount=2;
  138. if (Lighting)
  139. glEnable(GL_LIGHTING);
  140. else glColor3fv(&BackgroundColor.X);
  141. glDepthFunc(GL_LESS);
  142. glCullFace(GL_BACK);
  143. glPolygonMode(GL_BACK, GL_FILL);
  144. result=true;
  145. break;
  146. }
  147. case 2 : {
  148. glPopAttrib();
  149. glLineWidth(OldlineWidth);
  150. result=false;
  151. break;
  152. }
  153. default : {
  154. assert(false);
  155. result=false;
  156. }
  157. }
  158. return result;
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TForm1::BUBindClick(TObject *Sender)
  162. {
  163. THiddenLineShader *shader1;
  164. TOutLineShader *shader2 ,*shader3;
  165. BUBind->Enabled=False;
  166. // instantiate our shaders
  167. shader1 = new THiddenLineShader(this);
  168. shader1->BackgroundColor=ConvertWinColor(GLSceneViewer1->Buffer->BackgroundColor,0);
  169. shader1->LineColor=clrBlue;
  170. shader2 = new TOutLineShader(this);
  171. shader2->BackgroundColor=ConvertWinColor(GLSceneViewer1->Buffer->BackgroundColor,0);
  172. shader2->OutlineSmooth=true;
  173. shader2->OutlineWidth=2;
  174. shader2->Lighting=false;
  175. shader2->LineColor=clrBlack;
  176. shader3 = new TOutLineShader(this);
  177. shader3->BackgroundColor=ConvertWinColor(GLSceneViewer1->Buffer->BackgroundColor,0);
  178. shader3->OutlineSmooth=false;
  179. shader3->OutlineWidth=4;
  180. shader3->Lighting=true;
  181. shader3->LineColor=clrRed;
  182. // binds the shaders to the materials
  183. GLMaterialLibrary1->Materials->Items[0]->Shader=shader1;
  184. GLMaterialLibrary1->Materials->Items[1]->Shader=shader2;
  185. GLMaterialLibrary1->Materials->Items[2]->Shader=shader3;
  186. }
  187. //---------------------------------------------------------------------------
  188. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender,
  189. TMouseButton Button, TShiftState Shift, int X, int Y)
  190. {
  191. mx=X; my=Y;
  192. }
  193. //---------------------------------------------------------------------------
  194. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender,
  195. TShiftState Shift, int X, int Y)
  196. {
  197. if (Shift.Contains(ssLeft))
  198. GLCamera1->MoveAroundTarget(my-Y, mx-X);
  199. else if (Shift.Contains(ssRight))
  200. GLCamera1->RotateTarget(my-Y, mx-X, 0);
  201. mx=X; my=Y;
  202. }
  203. //---------------------------------------------------------------------------