⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mlrprimitiveclipping.hpp

📁 机甲指挥官2源代码
💻 HPP
📖 第 1 页 / 共 4 页
字号:
		theAnd.SetClippingState(0x3f);
		theOr.SetClippingState(0);
		end = j+stride;
		
		for(k=j;k<end;k++)
		{
			theAnd &= (*clipPerVertex)[index[k]];

			theOr |= (*clipPerVertex)[index[k]];
		}

		//
		//-------------------------------------------------------------------
		// If any bit is set for all vertices, then the polygon is completely
		// outside the viewing space and we don't have to draw it.  On the
		// other hand, if no bits at all were ever set, we can do a trivial
		// accept of the polygon
		//-------------------------------------------------------------------
		//
		if (theAnd != 0)
		{
			testList[i] = 0;
#ifdef LAB_ONLY
			Set_Statistic(PolysClippedButOutside, PolysClippedButOutside+1);
#endif
		}
		else if (theOr == 0)
		{
			ret++;
			for(k=j;k<end;k++)
			{
				visibleIndexedVertices[index[k]] = 1;
			}
#ifdef LAB_ONLY
			Set_Statistic(PolysClippedButInside, PolysClippedButInside+1);
#endif
		}

		//
		//-----------------------------------------------------------------
		// It is not a trivial case, so we must now do real clipping on the
		// polygon
		//-----------------------------------------------------------------
		//
		else
		{
			unsigned short numberVerticesPerPolygon = 0;

			//
			//---------------------------------------------------------------
			// Handle the case of a single clipping plane by stepping through
			// the vertices and finding the edge it originates
			//---------------------------------------------------------------
			//
			bool firstIsIn;
			if (theOr.GetNumberOfSetBits() == 1)
			{
#ifdef LAB_ONLY
				Set_Statistic(PolysClippedButOnePlane, PolysClippedButOnePlane+1);
#endif
				for(k=j;k<end;k++)
				{
					k0 = index[k];
					k1 = index[(k+1) < end ? k+1 : j];

					//
					//----------------------------------------------------
					// If this vertex is inside the viewing space, copy it
					// directly to the clipping buffer
					//----------------------------------------------------
					//
					int clipped_index =
						myNumberUsedClipVertex + numberVerticesPerPolygon;
					theTest = (*clipPerVertex)[k0];

					if(theTest == 0)
					{
						firstIsIn = true;
						(*clipExtraCoords)[clipped_index] = (*transformedCoords)[k0];

#ifdef I_SAY_YES_TO_COLOR
#ifdef I_SAY_YES_TO_LIGHTING
						Verify((*actualColors).GetLength() > 0);
						(*clipExtraColors)[clipped_index] = (*actualColors)[k0];
#else
						Verify(colors.GetLength() > 0);
						(*clipExtraColors)[clipped_index] = colors[k0];
#endif
#endif

						Verify(texCoords.GetLength() > 0);
						(*clipExtraTexCoords)[clipped_index] = texCoords[k0];

#ifdef I_SAY_YES_TO_DUAL_TEXTURES
						(*clipExtraTexCoords2)[clipped_index] = texCoords[k0+numVertices];
#endif						

#ifdef I_SAY_YES_TO_MULTI_TEXTURES
						for(m=1;m<currentNrOfPasses;m++)
						{
							(*clipExtraMultiTexCoords)[m][clipped_index] = multiTexCoordsPointers[m][k0];
						}
#endif						
						numberVerticesPerPolygon++;
						clipped_index++;

						//
						//-------------------------------------------------------
						// We don't need to clip this edge if the next vertex is
						// also in the viewing space, so just move on to the next
						// vertex
						//-------------------------------------------------------
						//
						if((*clipPerVertex)[k1] == 0)
						{
							continue;
						}
					}

					//
					//---------------------------------------------------------
					// This vertex is outside the viewing space, so if the next
					// vertex is also outside the viewing space, no clipping is
					// needed and we throw this vertex away.  Since only one
					// clipping plane is involved, it must be in the same space
					// as the first vertex
					//---------------------------------------------------------
					//
					else
					{
						firstIsIn = false;
						if((*clipPerVertex)[k1] != 0)
						{
							Verify((*clipPerVertex)[k1] == (*clipPerVertex)[k0]);
							continue;
						}
					}

					//
					//--------------------------------------------------
					// We now find the distance along the edge where the
					// clipping plane will intersect
					//--------------------------------------------------
					//
					mask = 1;
					theTest |= (*clipPerVertex)[k1];

					//
					//-----------------------------------------------------
					// Find the boundary conditions that match our clipping
					// plane
					//-----------------------------------------------------
					//
					for (l=0; l<MLRClippingState::NextBit; l++)
					{
						if(theTest.IsClipped(mask))
						{
//							GetDoubleBC(l, bc0, bc1, transformedCoords[k0], transformedCoords[k1]);

							//
							//-------------------------------------------
							// Find the clipping interval from bc0 to bc1
							//-------------------------------------------
							//
							if(firstIsIn==true)
							{
								a = GetLerpFactor(l, (*transformedCoords)[k0], (*transformedCoords)[k1]);
							}
							else
							{
								a = GetLerpFactor(l, (*transformedCoords)[k1], (*transformedCoords)[k0]);
							}

							Verify(a >= 0.0f && a <= 1.0f);

							ct = l;

							break;
						}
						mask <<= 1;
					}

					//
					//------------------------------
					// Lerp the homogeneous position
					//------------------------------
					//
					if(firstIsIn==true)
					{
						(*clipExtraCoords)[clipped_index].Lerp(
							(*transformedCoords)[k0],
							(*transformedCoords)[k1],
							a
						);

						DoClipTrick((*clipExtraCoords)[clipped_index], ct);

						//
						//----------------------------------------------------------
						// If there are colors, lerp them in screen space for now as
						// most cards do that anyway
						//----------------------------------------------------------
						//
#ifdef I_SAY_YES_TO_COLOR
	#ifdef I_SAY_YES_TO_LIGHTING
						Verify((*actualColors).GetLength() > 0);
		#if COLOR_AS_DWORD
						(*clipExtraColors)[clipped_index] = Color_DWORD_Lerp (
							(*actualColors)[k0],
							(*actualColors)[k1],
							a
						);
		#else
						(*clipExtraColors)[clipped_index].Lerp(
							(*actualColors)[k0],
							(*actualColors)[k1],
							a
						);
		#endif
	#else
						Verify(colors.GetLength() > 0);
		#if COLOR_AS_DWORD
						(*clipExtraColors)[clipped_index] = Color_DWORD_Lerp (
							colors[k0],
							colors[k1],
							a
						);
		#else
						(*clipExtraColors)[clipped_index].Lerp(
							colors[k0],
							colors[k1],
							a
						);
		#endif
	#endif
#endif
						//
						//-----------------------------------------------------
						// If there are texture uv's, we need to lerp them in a
						// perspective correct manner
						//-----------------------------------------------------
						//
						Verify(texCoords.GetLength() > 0);
						(*clipExtraTexCoords)[clipped_index].Lerp
							(
								texCoords[k0],
								texCoords[k1],
								a
							);

#ifdef I_SAY_YES_TO_DUAL_TEXTURES
						(*clipExtraTexCoords2)[clipped_index].Lerp
							(
								texCoords[k0+numVertices],
								texCoords[k1+numVertices],
								a
							);
#endif

#ifdef I_SAY_YES_TO_MULTI_TEXTURES
						for(m=1;m<currentNrOfPasses;m++)
						{
							(*clipExtraMultiTexCoords)[m][clipped_index] .Lerp
								(
									multiTexCoordsPointers[m][k0],
									multiTexCoordsPointers[m][k1],
									a
								);
						}

#endif
					}
					else
					{
						(*clipExtraCoords)[clipped_index].Lerp(
							(*transformedCoords)[k1],
							(*transformedCoords)[k0],
							a
						);

						DoClipTrick((*clipExtraCoords)[clipped_index], ct);

						//
						//----------------------------------------------------------
						// If there are colors, lerp them in screen space for now as
						// most cards do that anyway
						//----------------------------------------------------------
						//
#ifdef I_SAY_YES_TO_COLOR
	#ifdef I_SAY_YES_TO_LIGHTING
						Verify((*actualColors).GetLength() > 0);
		#if COLOR_AS_DWORD
						(*clipExtraColors)[clipped_index] = Color_DWORD_Lerp (
							(*actualColors)[k1],
							(*actualColors)[k0],
							a
						);
		#else
						(*clipExtraColors)[clipped_index].Lerp(
							(*actualColors)[k1],
							(*actualColors)[k0],
							a
						);
		#endif
	#else
						Verify(colors.GetLength() > 0);
		#if COLOR_AS_DWORD
						(*clipExtraColors)[clipped_index] = Color_DWORD_Lerp (
							colors[k1],
							colors[k0],
							a
						);
		#else
						(*clipExtraColors)[clipped_index].Lerp(
							colors[k1],
							colors[k0],
							a
						);
		#endif
	#endif
#endif
						//
						//-----------------------------------------------------
						// If there are texture uv's, we need to lerp them in a
						// perspective correct manner
						//-----------------------------------------------------
						//
						Verify(texCoords.GetLength() > 0);
						(*clipExtraTexCoords)[clipped_index].Lerp
							(
								texCoords[k1],
								texCoords[k0],
								a
							);

#ifdef I_SAY_YES_TO_DUAL_TEXTURES
						(*clipExtraTexCoords2)[clipped_index].Lerp
							(
								texCoords[k1+numVertices],
								texCoords[k0+numVertices],
								a
							);
#endif

#ifdef I_SAY_YES_TO_MULTI_TEXTURES
						for(m=1;m<currentNrOfPasses;m++)
						{
							(*clipExtraMultiTexCoords)[m][clipped_index] .Lerp
								(
									multiTexCoordsPointers[m][k1],
									multiTexCoordsPointers[m][k0],
									a
								);
						}

#endif
					}

					//
					//--------------------------------
					// Bump the polygon's vertex count
					//--------------------------------
					//
					numberVerticesPerPolygon++;
				}
				(*clipExtraLength)[myNumberUsedClipLength] = numberVerticesPerPolygon;
#ifdef _ARMOR
				(*clipExtraLength)[myNumberUsedClipLength] &= ~0x8000;
#endif
			}

			//
			//---------------------------------------------------------------
			// We have to handle multiple planes.  We do this by creating two
			// buffers and we switch between them as we clip plane by plane
			//---------------------------------------------------------------
			//
			else
			{
#ifdef LAB_ONLY
				Set_Statistic(PolysClippedButGOnePlane, PolysClippedButGOnePlane+1);
#endif
				ClipData2 srcPolygon, dstPolygon;
				int dstBuffer = 1;

				Verify(texCoords.GetLength() > 0);

				srcPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
#ifdef I_SAY_YES_TO_COLOR
	#ifdef I_SAY_YES_TO_LIGHTING
				Verify((*actualColors).GetLength() > 0);
	#else
				Verify(colors.GetLength() > 0);
	#endif
				srcPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
#endif
				srcPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
				srcPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();

				//
				//----------------------------------------------------------
				// unravel and copy the original data into the source buffer
				//----------------------------------------------------------
				//
				for(k=j,l=0;k<end;k++,l++)
				{
					int indexK = index[k];

					srcPolygon.coords[l] = (*transformedCoords)[indexK];

#ifdef I_SAY_YES_TO_COLOR
	#ifdef I_SAY_YES_TO_LIGHTING
					srcPolygon.colors[l] = (*actualColors)[indexK];
	#else
					srcPolygon.colors[l] = colors[indexK];
	#endif
#endif

#ifdef I_SAY_YES_TO_DUAL_TEXTURES
					srcPolygon.texCoords[2*l] = texCoords[indexK];
					srcPolygon.texCoords[2*l+1] = texCoords[indexK + numVertices];
#else
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
					for(m=0;m<currentNrOfPasses;m++)
					{
						srcPolygon.texCoords[currentNrOfPasses*l+m] = multiTexCoordsPointers[m][indexK];
					}
#else
					srcPolygon.texCoords[l] = texCoords[indexK];
#endif
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -