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

📄 render.cpp

📁 游戏《家园》源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				if((i%10 == 0) && (j%10 == 0))
				{
					// Draw coarse grid
					LineTo(hdc,nPosx-2,nPosy);
					MoveToEx(hdc,nPosx,nPosy,NULL);
					LineTo(hdc,nPosx,nPosy-1);
					LineTo(hdc,nPosx,nPosy+2);
				}
			}
		}

		// Reset to default
		SelectObject(hdc,hdefault);

		// Delete pen
		DeleteObject(hpen);
	}

	// Check band box position
	if((pContext->nMode != REND_CCAM) &&
		(pContext->aBandPos[0] != pContext->aBandPos[2]) &&
		(pContext->aBandPos[1] != pContext->aBandPos[3]))
	{
		// Set band box color
		lb.lbColor=pContext->nBandCol;

		// Create pen
		hpen=ExtCreatePen(PS_COSMETIC|PS_DASH,1,&lb,0,NULL); 

		// Set color and width
		hdefault=(HPEN)SelectObject(hdc,hpen);

		// Draw band box
		MoveToEx(hdc,(int)pContext->aBandPos[0],(int)pContext->aBandPos[1],NULL);
		LineTo(hdc,(int)pContext->aBandPos[2],(int)pContext->aBandPos[1]);
		LineTo(hdc,(int)pContext->aBandPos[2],(int)pContext->aBandPos[3]);
		LineTo(hdc,(int)pContext->aBandPos[0],(int)pContext->aBandPos[3]);
		LineTo(hdc,(int)pContext->aBandPos[0],(int)pContext->aBandPos[1]);

		// Reset to default
		SelectObject(hdc,hdefault);

		// Delete pen
		DeleteObject(hpen);
	}

	// Set pointer to first object
	pCur=pFirst;
	
	while(1)
	{
		// Check pointer
		if(pCur == NULL) break;

		// Check object mode
		if(pCur->nMode != REND_OHIDE)
		{
			// Check link
			if(pCur->pLink != NULL)
			{
				// Set color
				lb.lbColor=pCur->nCol;

				// Create pen
				hpen=ExtCreatePen(PS_COSMETIC|PS_SOLID,1,&lb,0,NULL); 

				// Set color and width
				hdefault=(HPEN)SelectObject(hdc,hpen);

				// Get origins
				memset(aVerts[0],0,nSizeOfFloatTimes3);
				memset(aVerts[1],0,nSizeOfFloatTimes3);
				memset(aVerts[2],0,nSizeOfFloatTimes3);
				memset(aVerts[3],0,nSizeOfFloatTimes3);
				
				// Object-to-world transformation
				matVectProduct(NULL,aVerts[0],pCur->pLink->aTransform);
				matVectProduct(NULL,aVerts[1],pCur->aTransform);

				// Reset dot product
				fDot=1.0F;

				// Check context mode
				switch(pContext->nMode)
				{
					// Camera view
					case REND_CCAM:
						// Loop thru origins
						for(m=0;m<2;m++)
						{
							// Calculate camera to vertex vector
							aWork[0]=aVerts[m][0]-pContext->aEye[0];
							aWork[1]=aVerts[m][1]-pContext->aEye[1];
							aWork[2]=aVerts[m][2]-pContext->aEye[2];

							// Calc dot product
							if(fDot > 0.0F) fDot=aLookAt[0]*aWork[0]+aLookAt[1]*aWork[1]+aLookAt[2]*aWork[2];

							// Check dot product
							if(fDot > 0.0F)
							{
								// World-to-camera transformations
								matVectProduct(NULL,aVerts[m],pContext->aTransform);

								// Scale vertex
								aVerts[m][0]/=aVerts[m][2];
								aVerts[m][1]/=aVerts[m][2];
								aVerts[m][2]=0.0F;
							}
						}
						break;

					// X (front) view
					case REND_CX:
						// Loop thru origins
						for(m=0;m<2;m++)
						{
							// Non-perspective transformation
							aVerts[m][0]=-aVerts[m][2];
							aVerts[m][1]=-aVerts[m][1];
							aVerts[m][2]=0.0F;
						}
						break;

					// Y (top) view
					case REND_CY:
						// Loop thru origins
						for(m=0;m<2;m++)
						{
							// Non-perspective transformation
							fWork=-aVerts[m][0];
							aVerts[m][0]=-aVerts[m][2];
							aVerts[m][1]=fWork;
							aVerts[m][2]=0.0F;
						}
						break;

					// Z (side) view
					case REND_CZ:
						// Loop thru origins
						for(m=0;m<2;m++)
						{
							// Non-perspective transformation
							// aVerts[m][0]=aVerts[m][0];
							aVerts[m][1]=-aVerts[m][1];
							aVerts[m][2]=0.0F;
						}
						break;
				}

				// Check dot product
				if(fDot > 0.0F)
				{
					// Draw line
					MoveToEx(hdc,(int)(aVerts[0][0]*pContext->fScale-pContext->fOffsetx),
						(int)(aVerts[0][1]*pContext->fScale-pContext->fOffsety),NULL);
					LineTo(hdc,(int)(aVerts[1][0]*pContext->fScale-pContext->fOffsetx),
						(int)(aVerts[1][1]*pContext->fScale-pContext->fOffsety));
				}
				
				// Reset to default
				SelectObject(hdc,hdefault);

				// Delete pen
				DeleteObject(hpen);
			}

			// Check level-of-detail
			if(pContext->nDetail == REND_CLOW)
			{
				// Copy object
				memcpy(&rCubeObj,pCur,sizeof(RENDOBJ));

				// Set number of polys and vertices to cube
				rCubeObj.nPolys=REND_DNPOLYS/LWOB_SIZE;
				rCubeObj.nVerts=REND_DNVERTS/3;

				// Calculate min and max
				aMin[0]=-rCubeObj.aSize[0]/2.0F+rCubeObj.aOrig[0];
				aMin[1]=-rCubeObj.aSize[1]/2.0F+rCubeObj.aOrig[1];
				aMin[2]=-rCubeObj.aSize[2]/2.0F+rCubeObj.aOrig[2];

				aMax[0]=rCubeObj.aSize[0]/2.0F+rCubeObj.aOrig[0];
				aMax[1]=rCubeObj.aSize[1]/2.0F+rCubeObj.aOrig[1];
				aMax[2]=rCubeObj.aSize[2]/2.0F+rCubeObj.aOrig[2];

				// Calculate cube vertices
				aCubeVerts[0]=aMin[0];
				aCubeVerts[1]=aMin[1];
				aCubeVerts[2]=aMin[2];

				aCubeVerts[3]=aMin[0];
				aCubeVerts[4]=aMax[1];
				aCubeVerts[5]=aMax[2];

				aCubeVerts[6]=aMin[0];
				aCubeVerts[7]=aMax[1];
				aCubeVerts[8]=aMin[2];

				aCubeVerts[9]=aMin[0];
				aCubeVerts[10]=aMin[1];
				aCubeVerts[11]=aMax[2];

				aCubeVerts[12]=aMax[0];
				aCubeVerts[13]=aMin[1];
				aCubeVerts[14]=aMin[2];

				aCubeVerts[15]=aMax[0];
				aCubeVerts[16]=aMax[1];
				aCubeVerts[17]=aMax[2];

				aCubeVerts[18]=aMax[0];
				aCubeVerts[19]=aMax[1];
				aCubeVerts[20]=aMin[2];

				aCubeVerts[21]=aMax[0];
				aCubeVerts[22]=aMin[1];
				aCubeVerts[23]=aMax[2];

				// Set pointers to polys and vertices to cube
				rCubeObj.pPolys=REND_DAPOLYS;
				rCubeObj.pVerts=aCubeVerts;

				// Set pointer
				pCur=&rCubeObj;
			}
			
			// Check mode and set color
			if(pCur->nMode != REND_OSHOW) lb.lbColor=pContext->nSelCol;
			else lb.lbColor=pCur->nCol;
	
			// Create pen
			hpen=ExtCreatePen(PS_COSMETIC|PS_SOLID,1,&lb,0,NULL); 

			// Set color and width
			hdefault=(HPEN)SelectObject(hdc,hpen);

			// Check context mode
			switch(pContext->nMode)
			{
				// Camera view
				case REND_CCAM:
					// Loop thru polys
					for(n=0;n<pCur->nPolys;n++)
					{
						// Reset dot product
						fDot=1.0F;

						// Reset flag
						nFlag=FALSE;
							
						// Loop thru vertices
						for(m=0;m<=LWOB_SIZE;m++)
						{
							// Get vertex index
							nVert=pCur->pPolys[n*LWOB_SIZE+(m%LWOB_SIZE)];

							// Check vertex index
							if(nVert < pCur->nVerts)
							{
								// Get vertex
								memcpy(aVerts[m],&(pCur->pVerts[nVert*3]),nSizeOfFloatTimes3);

								// Object-to-world transformation
								matVectProduct(NULL,aVerts[m],pCur->aTransform);

								// Calculate camera to vertex vector
								aWork[0]=aVerts[m][0]-pContext->aEye[0];
								aWork[1]=aVerts[m][1]-pContext->aEye[1];
								aWork[2]=aVerts[m][2]-pContext->aEye[2];

								// Calc dot product
								if(fDot > 0.0F) fDot=aLookAt[0]*aWork[0]+aLookAt[1]*aWork[1]+aLookAt[2]*aWork[2];

								// Check dot product
								if(fDot > 0.0F)
								{
									// World-to-camera transformations
									matVectProduct(NULL,aVerts[m],pContext->aTransform);

									// Scale vertex
									aVerts[m][0]/=aVerts[m][2];
									aVerts[m][1]/=aVerts[m][2];
									aVerts[m][2]=0.0F;
								}
								else
								{
									// Check index
									if(m == 0)
									{
										// Set flag
										nFlag=TRUE;
										break;
									}

									// Get last vertex
									memcpy(aVerts[m],aVerts[0],nSizeOfFloatTimes3);
								}
							}
							else
							{
								// Check index
								if(m == 0)
								{
									// Set flag
									nFlag=TRUE;
									break;
								}
									
								// Get last vertex
								memcpy(aVerts[m],aVerts[0],nSizeOfFloatTimes3);
							}
						}

						// Check flag
						if(nFlag == TRUE) continue;

						// Reset area
						fArea=0.0F;

						// Loop thru vertices and calc poly area
						for(m=0;m<LWOB_SIZE;m++)
							fArea+=(aVerts[m][0]-aVerts[(m+1)%LWOB_SIZE][0])*(aVerts[m][1]+aVerts[(m+1)%LWOB_SIZE][1]);

						// Check poly area
						if(fArea*0.5F < 0.0F) continue;

						// Loop thru vertices
						for(m=0;m<=LWOB_SIZE;m++)
						{
							// Draw poly
							if(m == 0) MoveToEx(hdc,(int)(aVerts[m][0]*pContext->fScale-pContext->fOffsetx),
								(int)(aVerts[m][1]*pContext->fScale-pContext->fOffsety),NULL);
							else LineTo(hdc,(int)(aVerts[m][0]*pContext->fScale-pContext->fOffsetx),
								(int)(aVerts[m][1]*pContext->fScale-pContext->fOffsety));
						}
					}
					break;

				// X (front) view
				case REND_CX:
					// Loop thru polys
					for(n=0;n<pCur->nPolys;n++)
					{
						// Reset flag
						nFlag=FALSE;
							
						// Loop thru vertices
						for(m=0;m<=LWOB_SIZE;m++)
						{
							// Get vertex index
							nVert=pCur->pPolys[n*LWOB_SIZE+(m%LWOB_SIZE)];

							// Check vertex index
							if(nVert < pCur->nVerts)
							{
								// Get vertex
								memcpy(aVerts[m],&(pCur->pVerts[nVert*3]),nSizeOfFloatTimes3);

								// Object-to-world transformation
								matVectProduct(NULL,aVerts[m],pCur->aTransform);

								// Non-perspective transformation
								aVerts[m][0]=-aVerts[m][2];
								aVerts[m][1]=-aVerts[m][1];
								aVerts[m][2]=0.0F;
							}
							else
							{
								// Check index
								if(m == 0)
								{
									// Set flag
									nFlag=TRUE;
									break;
								}
									
								// Get last vertex
								memcpy(aVerts[m],aVerts[0],nSizeOfFloatTimes3);
							}
						}

						// Check flag
						if(nFlag == TRUE) continue;

						// Reset area
						fArea=0.0F;

						// Loop thru vertices and calc poly area
						for(m=0;m<LWOB_SIZE;m++)
							fArea+=(aVerts[m][0]-aVerts[(m+1)%LWOB_SIZE][0])*(aVerts[m][1]+aVerts[(m+1)%LWOB_SIZE][1]);

						// Check poly area
						if(fArea*0.5F < 0.0F) continue;

						// Loop thru vertices
						for(m=0;m<=LWOB_SIZE;m++)
						{
							// Draw poly
							if(m == 0) MoveToEx(hdc,(int)(aVerts[m][0]*pContext->fScale-pContext->fOffsetx),
								(int)(aVerts[m][1]*pContext->fScale-pContext->fOffsety),NULL);
							else LineTo(hdc,(int)(aVerts[m][0]*pContext->fScale-pContext->fOffsetx),
								(int)(aVerts[m][1]*pContext->fScale-pContext->fOffsety));
						}
					}
					break;

				// Y (top) view
				case REND_CY:
					// Loop thru polys
					for(n=0;n<pCur->nPolys;n++)
					{
						// Reset flag
						nFlag=FALSE;
							
						// Loop thru vertices
						for(m=0;m<=LWOB_SIZE;m++)
						{
							// Get vertex index
							nVert=pCur->pPolys[n*LWOB_SIZE+(m%LWOB_SIZE)];

							// Check vertex index
							if(nVert < pCur->nVerts)
							{
								// Get vertex
								memcpy(aVerts[m],&(pCur->pVerts[nVert*3]),nSizeOfFloatTimes3);

								// Object-to-world transformation
								matVectProduct(NULL,aVerts[m],pCur->aTransform);

								// Non-perspective transformation
								fWork=-aVerts[m][0];
								aVerts[m][0]=-aVerts[m][2];
								aVerts[m][1]=fWork;
								aVerts[m][2]=0.0F;
							}
							else
							{
								// Check index
								if(m == 0)
								{
									// Set flag
									nFlag=TRUE;
									break;
								}
									
								// Get last vertex
								memcpy(aVerts[m],aVerts[0],nSizeOfFloatTimes3);
							}
						}

						// Check flag
						if(nFlag == TRUE) continue;

						// Reset area
						fArea=0.0F;

						// Loop thru vertices and calc poly area
						for(m=0;m<LWOB_SIZE;m++)
							fArea+=(aVerts[m][0]-aVerts[(m+1)%LWOB_SIZE][0])*(aVerts[m][1]+aVerts[(m+1)%LWOB_SIZE][1]);

						// Check poly area
						if(fArea*0.5F < 0.0F) continue;

						// Loop thru vertices
						for(m=0;m<=LWOB_SIZE;m++)
						{
							// Draw poly
							if(m == 0) MoveToEx(hdc,(int)(aVerts[m][0]*pContext->fScale-pContext->fOffsetx),
								(int)(aVerts[m][1]*pContext->fScale-pContext->fOffsety),NULL);
							else LineTo(hdc,(int)(aVerts[m][0]*pContext->fScale-pContext->fOffsetx),
								(int)(aVerts[m][1]*pContext->fScale-pContext->fOffsety));
						}
					}
					break;

				// Z (side) view
				case REND_CZ:
					// Loop thru polys
					for(n=0;n<pCur->nPolys;n++)
					{
						// Reset flag
						nFlag=FALSE;
							
						// Loop thru vertices
						for(m=0;m<=LWOB_SIZE;m++)
						{
							// Get vertex index
							nVert=pCur->pPolys[n*LWOB_SIZE+(m%LWOB_SIZE)];

							// Check vertex index
							if(nVert < pCur->nVerts)
							{
								// Get vertex
								memcpy(aVerts[m],&(pCur->pVerts[nVert*3]),nSizeOfFloatTimes3);

								// Object-to-world transformation
								matVectProduct(NULL,aVerts[m],pCur->aTransform);

								// Non-perspective transformation
								// aVerts[m][0]=aVerts[m][0];
								aVerts[m][1]=-aVerts[m][1];
								aVerts[m][2]=0.0F;
							}
							else
							{
								// Check index
								if(m == 0)
								{
									// Set flag
									nFlag=TRUE;
									break;
								}
									
								// Get last vertex
								memcpy(aVerts[m],aVerts[0],nSizeOfFloatTimes3);
							}
						}

						// Check flag
						if(nFlag == TRUE) continue;

						// Reset area
						fArea=0.0F;

						// Loop thru vertices and calc poly area
						for(m=0;m<LWOB_SIZE;m++)
							fArea+=(aVerts[m][0]-aVerts[(m+1)%LWOB_SIZE][0])*(aVerts[m][1]+aVerts[(m+1)%LWOB_SIZE][1]);

						// Check poly area
						if(fArea*0.5F < 0.0F) continue;

						// Loop thru vertices
						for(m=0;m<=LWOB_SIZE;m++)
						{
							// Draw poly
							if(m == 0) MoveToEx(hdc,(int)(aVerts[m][0]*pContext->fScale-pContext->fOffsetx),
								(int)(aVerts[m][1]*pContext->fScale-pContext->fOffsety),NULL);
							else LineTo(hdc,(int)(aVerts[m][0]*pContext->fScale-pContext->fOffsetx),
								(int)(aVerts[m][1]*pContext->fScale-pContext->fOffsety));
						}
					}
					break;
			}

			// Reset to default
			SelectObject(hdc,hdefault);

			// Delete pen
			DeleteObject(hpen);
		}
		
		// Set pointer to next object
		pCur=pCur->pNext;
	}

	// Draw cursor
	if(pContext->nMode != REND_CCAM)
	{
		// Copy cursor
		memcpy(aCursor,pContext->aCursor,nSizeOfFloatTimes3);

		// Check context mode
		switch(pContext->nMode)
		{
			// X (front) view
			case REND_CX:
				// Set cursor
				aCursor[0]=-aCursor[2]*pContext->fScale-pContext->fOffsetx;
				aCursor[1]=-aCursor[1]*pContext->fScale-pContext->fOffsety;
				aCursor[2]=0.0F;
				break;

			// Y (top) view
			case REND_CY:
				// Set cursor
				fWork=-aCursor[0]*pContext->fScale-pContext->fOffsety;
				aCursor[0]=-aCursor[2]*pContext->fScale-pContext->fOffsetx;
				aCursor[1]=fWork;
				aCursor[2]=0.0F;
				break;

			// Z (side) view
			case REND_CZ:
				// Set cursor
				aCursor[0]=aCursor[0]*pContext->fScale-pContext->fOffsetx;
				aCursor[1]=-aCursor[1]*pContext->fScale-pContext->fOffsety;
				aCursor[2]=0.0F;
				break;
		}

		// Set band box color
		lb.lbColor=pContext->nCursCol;

		// Create pen
		hpen=ExtCreatePen(PS_COSMETIC|PS_SOLID,1,&lb,0,NULL); 

		// Set color and width
		hdefault=(HPEN)SelectObject(hdc,hpen);

		// Draw cursor
		MoveToEx(hdc,(int)aCursor[0]-REND_SCURS,(int)aCursor[1],NULL);
		LineTo(hdc,(int)aCursor[0]+REND_SCURS,(int)aCursor[1]);
		MoveToEx(hdc,(int)aCursor[0],(int)aCursor[1]-REND_SCURS,NULL);
		LineTo(hdc,(int)aCursor[0],(int)aCursor[1]+REND_SCURS);

		// Reset to default
		SelectObject(hdc,hdefault);

		// Delete pen
		DeleteObject(hpen);
	}

	return(OK);
}

⌨️ 快捷键说明

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