PageRenderTime 396ms CodeModel.GetById 194ms app.highlight 12ms RepoModel.GetById 188ms app.codeStats 0ms

/opengles/src/TriangleClipperHigh.inc

http://ftk.googlecode.com/
Unknown | 99 lines | 85 code | 14 blank | 0 comment | 0 complexity | 12f2e1378610cc8f942200b65f4c92ef MD5 | raw file
 1// ==========================================================================
 2//
 3// TriangleClipperHigh.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			} else {
64				// line segment between previous and current is intersected;
65				// create vertex at intersection, then add current
66				RasterPos & newVertex = *nextTemporary++;
67				output[resultCount++] = &newVertex;
68				
69				EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
70				EGL_Fixed c_w = current->m_ClipCoords.w();
71				EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
72				EGL_Fixed p_w = previous->m_ClipCoords.w();
73				
74				Interpolate(newVertex, *current, *previous, p_w - p_x, (p_w - p_x) - (c_w - c_x)); 
75				newVertex.m_ClipCoords.SET_COORDINATE(newVertex.m_ClipCoords.w());
76
77				output[resultCount++] = current;
78			}
79		} else {
80			if (previous->m_ClipCoords.COORDINATE < previous->m_ClipCoords.w()) {
81				// line segment between previous and current is intersected;
82				// create vertex at intersection and add it
83				RasterPos & newVertex = *nextTemporary++;
84				output[resultCount++] = &newVertex;
85				
86				EGL_Fixed c_x = current->m_ClipCoords.COORDINATE;
87				EGL_Fixed c_w = current->m_ClipCoords.w();
88				EGL_Fixed p_x = previous->m_ClipCoords.COORDINATE;
89				EGL_Fixed p_w = previous->m_ClipCoords.w();
90				
91				Interpolate(newVertex, *current, *previous, p_w - p_x, (p_w - p_x) - (c_w - c_x)); 
92				newVertex.m_ClipCoords.SET_COORDINATE(newVertex.m_ClipCoords.w());
93			}
94		}
95
96		previous = current;
97	}
98
99	return resultCount;