PageRenderTime 298ms CodeModel.GetById 115ms app.highlight 10ms RepoModel.GetById 171ms app.codeStats 0ms

/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// --------------------------------------------------------------------------
 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 (int index = 0; index < inputCount; ++index) {
 55
 56		current = input[index];
 57
 58		if (current->m_ClipCoords.COORDINATE COMPARISON CLIP_VALUE(current)) {
 59
 60			if (previous->m_ClipCoords.COORDINATE COMPARISON CLIP_VALUE(previous)) {
 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				// initialize the new vertex
 70				//EGL_Fixed factor = 
 71				//	InterpolationCoefficient(previous->m_ClipCoords.COORDINATE, CLIP_VALUE(previous), current->m_ClipCoords.COORDINATE);
 72				
 73				EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
 74				EGL_Fixed c_w = CLIP_VALUE(current);
 75				EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
 76				EGL_Fixed p_w = CLIP_VALUE(previous);
 77				
 78				EGL_Fixed factor = EGL_Div(p_w - p_x, (p_w - p_x) - (c_w - c_x)); 
 79				Interpolate(newVertex, *current, *previous, factor);
 80				newVertex.m_ClipCoords.SET_COORDINATE(CLIP_VALUE((&newVertex)));
 81
 82				output[resultCount++] = current;
 83				//previous = current;
 84			}
 85		} else {
 86			if (previous->m_ClipCoords.COORDINATE COMPARISON CLIP_VALUE(previous)) {
 87				// line segment between previous and current is intersected;
 88				// create vertex at intersection and add it
 89				RasterPos & newVertex = *nextTemporary++;
 90				output[resultCount++] = &newVertex;
 91				// initialize the new vertex
 92				//EGL_Fixed factor = 
 93				//	InterpolationCoefficient(current->m_ClipCoords.COORDINATE, CLIP_VALUE(previous), previous->m_ClipCoords.COORDINATE);
 94				
 95				EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
 96				EGL_Fixed c_w = CLIP_VALUE(current);
 97				EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
 98				EGL_Fixed p_w = CLIP_VALUE(previous);
 99				
100				EGL_Fixed factor = EGL_Div(p_w - p_x, (p_w - p_x) - (c_w - c_x)); 
101				Interpolate(newVertex, *current, *previous, factor);
102				newVertex.m_ClipCoords.SET_COORDINATE(CLIP_VALUE((&newVertex)));
103
104				//previous = current;
105			}
106		}
107
108		previous = current;
109	}
110
111	return resultCount;