PageRenderTime 243ms CodeModel.GetById 130ms app.highlight 13ms RepoModel.GetById 98ms app.codeStats 0ms

/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// --------------------------------------------------------------------------
 42// Triangle clipping at a specific coordinate against the frustrum boundary
 43// Return number of vertices retained.
 44// --------------------------------------------------------------------------
 45
 46	if (inputCount < 3) {
 47		return 0;
 48	}
 49
 50	RasterPos * previous = input[inputCount - 1];
 51	RasterPos * current;
 52	int resultCount = 0;
 53
 54	for (size_t index = 0; index < inputCount; ++index) {
 55
 56		current = input[index];
 57
 58		if (current->m_ClipCoords.COORDINATE >= -current->m_ClipCoords.w()) {
 59
 60			if (previous->m_ClipCoords.COORDINATE >= -previous->m_ClipCoords.w()) {
 61				// line segment between previous and current is fully contained in cube
 62				output[resultCount++] = current;
 63				//previous = current;
 64			} else {
 65				// line segment between previous and current is intersected;
 66				// create vertex at intersection, then add current
 67				RasterPos & newVertex = *nextTemporary++;
 68				output[resultCount++] = &newVertex;
 69				
 70				EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
 71				EGL_Fixed c_w = current->m_ClipCoords.w();
 72				EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
 73				EGL_Fixed p_w = previous->m_ClipCoords.w();
 74				
 75				Interpolate(newVertex, *current, *previous, p_w + p_x, (p_w + p_x) - (c_w + c_x));
 76				newVertex.m_ClipCoords.SET_COORDINATE(-newVertex.m_ClipCoords.w());
 77
 78				output[resultCount++] = current;
 79				//previous = current;
 80			}
 81		} else {
 82			if (previous->m_ClipCoords.COORDINATE >= -previous->m_ClipCoords.w()) {
 83				// line segment between previous and current is intersected;
 84				// create vertex at intersection and add it
 85				RasterPos & newVertex = *nextTemporary++;
 86				output[resultCount++] = &newVertex;
 87				
 88				EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
 89				EGL_Fixed c_w = current->m_ClipCoords.w();
 90				EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
 91				EGL_Fixed p_w = previous->m_ClipCoords.w();
 92				
 93				Interpolate(newVertex, *current, *previous, p_w + p_x, (p_w + p_x) - (c_w + c_x));
 94				newVertex.m_ClipCoords.SET_COORDINATE(-newVertex.m_ClipCoords.w());
 95
 96				//previous = current;
 97			}
 98		}
 99
100		previous = current;
101	}
102
103	return resultCount;