Transparency.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //---------------------------------------------------------------------------
  2. /*: A basic sample to demonstrate how transparency & Z-buffer work/fight together.
  3. In this sample, only the sphere are transparent. The form has a few options
  4. that allow to adjust in which order objects are rendered, and what kind of
  5. transparency is used.
  6. Transparency in GLScene is activated by setting Material.Blending to either
  7. 'bmTransparency' or 'bmAdditive', AND giving a <1.0 value to the Diffuse
  8. color alpha channel (alpha = 0.0 means absolute transparency, alpha = 1.0
  9. means absolute opacity).
  10. How do Z-Buffer & transparency work ? When point has to be rendered, OpenGL
  11. first checks its distance to the camera, the "Z" axis. If the distance
  12. check is successfull (the new point is closer), it is rendered, and if
  13. the point is part of a "transparent" object, then OpenGL will mix the existing
  14. point's color with our new point's color. If the Z check fails, OpenGL doesn't
  15. even bother about checking transparency.
  16. This is why, if you want to render transparent objects, you must make sure
  17. you render the farthest objects first, to give transparency a chance.
  18. However this effect can be usefull if you want to render mixed, half-transparent
  19. half-opaque objects.
  20. They are two ways to order objects in GLScene :<ul>
  21. <li>ordering : can be done at design-time in the editor or at runtime with
  22. MoveUp/MoveDown methods
  23. <li>sorting : adjust the ObjectSorting property (see help for more details)
  24. </ul>
  25. */
  26. //---------------------------------------------------------------------------
  27. #include <vcl.h>
  28. #pragma hdrstop
  29. USERES("Transparency.res");
  30. USEFORM("Unit1.cpp", Form1);
  31. //---------------------------------------------------------------------------
  32. WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  33. {
  34. try
  35. {
  36. Application->Initialize();
  37. Application->CreateForm(__classid(TForm1), &Form1);
  38. Application->Run();
  39. }
  40. catch (Exception &exception)
  41. {
  42. Application->ShowException(&exception);
  43. }
  44. return 0;
  45. }
  46. //---------------------------------------------------------------------------