/opengles/src/TriangleClipperLow.inc

http://ftk.googlecode.com/ · Unknown · 103 lines · 88 code · 15 blank · 0 comment · 0 complexity · 58cfe0991a44e19840ee91378d7a35a1 MD5 · raw file

  1. // ==========================================================================
  2. //
  3. // TriangleClipperLow.inc Rendering Context Class for 3D Rendering Library
  4. //
  5. // Clipping Code for Clipping of Triangles
  6. // This code is included by "ContextTriangles.cpp" with different
  7. // settings for the COORDINATE and SET_COORDINATE
  8. //
  9. // --------------------------------------------------------------------------
  10. //
  11. // 20-12-2003 Hans-Martin Will initial version
  12. //
  13. // --------------------------------------------------------------------------
  14. //
  15. // Copyright (c) 2004, Hans-Martin Will. All rights reserved.
  16. //
  17. // Redistribution and use in source and binary forms, with or without
  18. // modification, are permitted provided that the following conditions are
  19. // met:
  20. //
  21. // * Redistributions of source code must retain the above copyright
  22. // notice, this list of conditions and the following disclaimer.
  23. // * Redistributions in binary form must reproduce the above copyright
  24. // notice, this list of conditions and the following disclaimer in the
  25. // documentation and/or other materials provided with the distribution.
  26. //
  27. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  32. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  37. // THE POSSIBILITY OF SUCH DAMAGE.
  38. //
  39. // ==========================================================================
  40. // --------------------------------------------------------------------------
  41. // Triangle clipping at a specific coordinate against the frustrum boundary
  42. // Return number of vertices retained.
  43. // --------------------------------------------------------------------------
  44. if (inputCount < 3) {
  45. return 0;
  46. }
  47. RasterPos * previous = input[inputCount - 1];
  48. RasterPos * current;
  49. int resultCount = 0;
  50. for (size_t index = 0; index < inputCount; ++index) {
  51. current = input[index];
  52. if (current->m_ClipCoords.COORDINATE >= -current->m_ClipCoords.w()) {
  53. if (previous->m_ClipCoords.COORDINATE >= -previous->m_ClipCoords.w()) {
  54. // line segment between previous and current is fully contained in cube
  55. output[resultCount++] = current;
  56. //previous = current;
  57. } else {
  58. // line segment between previous and current is intersected;
  59. // create vertex at intersection, then add current
  60. RasterPos & newVertex = *nextTemporary++;
  61. output[resultCount++] = &newVertex;
  62. EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
  63. EGL_Fixed c_w = current->m_ClipCoords.w();
  64. EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
  65. EGL_Fixed p_w = previous->m_ClipCoords.w();
  66. Interpolate(newVertex, *current, *previous, p_w + p_x, (p_w + p_x) - (c_w + c_x));
  67. newVertex.m_ClipCoords.SET_COORDINATE(-newVertex.m_ClipCoords.w());
  68. output[resultCount++] = current;
  69. //previous = current;
  70. }
  71. } else {
  72. if (previous->m_ClipCoords.COORDINATE >= -previous->m_ClipCoords.w()) {
  73. // line segment between previous and current is intersected;
  74. // create vertex at intersection and add it
  75. RasterPos & newVertex = *nextTemporary++;
  76. output[resultCount++] = &newVertex;
  77. EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
  78. EGL_Fixed c_w = current->m_ClipCoords.w();
  79. EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
  80. EGL_Fixed p_w = previous->m_ClipCoords.w();
  81. Interpolate(newVertex, *current, *previous, p_w + p_x, (p_w + p_x) - (c_w + c_x));
  82. newVertex.m_ClipCoords.SET_COORDINATE(-newVertex.m_ClipCoords.w());
  83. //previous = current;
  84. }
  85. }
  86. previous = current;
  87. }
  88. return resultCount;