/opengles/src/TriangleClipper.inc

http://ftk.googlecode.com/ · Unknown · 111 lines · 96 code · 15 blank · 0 comment · 0 complexity · 1f89467deaa302ce5f88b2984047ac45 MD5 · raw file

  1. // ==========================================================================
  2. //
  3. // TriangleClipper.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, COMPARISON, CLIP_VALUE
  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 (int index = 0; index < inputCount; ++index) {
  51. current = input[index];
  52. if (current->m_ClipCoords.COORDINATE COMPARISON CLIP_VALUE(current)) {
  53. if (previous->m_ClipCoords.COORDINATE COMPARISON CLIP_VALUE(previous)) {
  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. // initialize the new vertex
  63. //EGL_Fixed factor =
  64. // InterpolationCoefficient(previous->m_ClipCoords.COORDINATE, CLIP_VALUE(previous), current->m_ClipCoords.COORDINATE);
  65. EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
  66. EGL_Fixed c_w = CLIP_VALUE(current);
  67. EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
  68. EGL_Fixed p_w = CLIP_VALUE(previous);
  69. EGL_Fixed factor = EGL_Div(p_w - p_x, (p_w - p_x) - (c_w - c_x));
  70. Interpolate(newVertex, *current, *previous, factor);
  71. newVertex.m_ClipCoords.SET_COORDINATE(CLIP_VALUE((&newVertex)));
  72. output[resultCount++] = current;
  73. //previous = current;
  74. }
  75. } else {
  76. if (previous->m_ClipCoords.COORDINATE COMPARISON CLIP_VALUE(previous)) {
  77. // line segment between previous and current is intersected;
  78. // create vertex at intersection and add it
  79. RasterPos & newVertex = *nextTemporary++;
  80. output[resultCount++] = &newVertex;
  81. // initialize the new vertex
  82. //EGL_Fixed factor =
  83. // InterpolationCoefficient(current->m_ClipCoords.COORDINATE, CLIP_VALUE(previous), previous->m_ClipCoords.COORDINATE);
  84. EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
  85. EGL_Fixed c_w = CLIP_VALUE(current);
  86. EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
  87. EGL_Fixed p_w = CLIP_VALUE(previous);
  88. EGL_Fixed factor = EGL_Div(p_w - p_x, (p_w - p_x) - (c_w - c_x));
  89. Interpolate(newVertex, *current, *previous, factor);
  90. newVertex.m_ClipCoords.SET_COORDINATE(CLIP_VALUE((&newVertex)));
  91. //previous = current;
  92. }
  93. }
  94. previous = current;
  95. }
  96. return resultCount;