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

📄 render.cpp

📁 游戏《家园》源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	if(pObj == NULL) return(ERR);

	// Get object mode
	*nMode=pObj->nMode;

	return(OK);
}

__declspec(dllexport) int __stdcall rendSetObjMode(RENDOBJ *pObj,int nMode)
{
	// Check object
	if(pObj == NULL) return(ERR);

	// Check mode for toggle
	if(nMode == REND_OTOG)
	{
		// Toggle object mode
		if(pObj->nMode == REND_OSHOW) pObj->nMode=REND_OSEL;
		else pObj->nMode=REND_OSHOW;
	}
	// Set object mode
	else pObj->nMode=nMode;

	return(OK);
}

__declspec(dllexport) int __stdcall rendSetObjCol(RENDOBJ *pObj,int nCol)
{
	// Check object
	if(pObj == NULL) return(ERR);

	// Set object color
	pObj->nCol=nCol;
	return(OK);
}

__declspec(dllexport) int __stdcall rendNewCont(RENDCONT **pContext,HWND hWnd,int nMode)
{
	// Allocate memory for new context
	*pContext=(RENDCONT *)malloc(sizeof(RENDCONT));
	if(*pContext == NULL) return(ERR);

	// Initialize directdraw
	(*pContext)->pDdc=ddInit(hWnd);
	if((*pContext)->pDdc == NULL)
	{
		free(*pContext);
		return(ERR);
	}

	// Set mode and detail
	(*pContext)->nMode=nMode;
	(*pContext)->nDetail=REND_CHIGH;

	// Reset origin
	(*pContext)->fOffsetx=0.0F;
	(*pContext)->fOffsety=0.0F;

	// Set default colors
	(*pContext)->nBandCol=REND_WHITE;
	(*pContext)->nCamCol=REND_WHITE;
	(*pContext)->nGridCol=REND_WHITE;
	(*pContext)->nRotCol=REND_WHITE;
	(*pContext)->nSelCol=REND_RED;
	(*pContext)->nCursCol=REND_GREEN;

	// Set rotation object
	(*pContext)->pRotObj=NULL;
	
	// Reset viewport width and height
	(*pContext)->fWidth=0.0F;
	(*pContext)->fHeight=0.0F;

	// Set default scale scale
	(*pContext)->fScale=REND_CSCALE;

	// Set default grid size
	(*pContext)->fGridSize=REND_CGSIZE;

	// Reset band box position
	memset((*pContext)->aBandPos,0,sizeof(float)*4);

	// Reset camera
	memset((*pContext)->aEye,0,nSizeOfFloatTimes3);
	memset((*pContext)->aFocus,0,nSizeOfFloatTimes3);

	// Set transformation matrix to identity matrix
	matIdentity((*pContext)->aTransform);
	
	return(OK);
}

__declspec(dllexport) int __stdcall rendDelCont(RENDCONT *pContext)
{
	// Initialize directdraw
	if(ddClean((DDCONT *)pContext->pDdc) < 0) return(ERR);

	// Check context
	if(pContext == NULL) return(ERR);

	// Free context
	free(pContext);

	return(OK);
}

__declspec(dllexport) int __stdcall rendSetContDetail(RENDCONT *pContext,int nDetail)
{
	// Check context
	if(pContext == NULL) return(ERR);

	// Set Level-of-detail
	pContext->nDetail=nDetail;
	return(OK);
}

__declspec(dllexport) int __stdcall rendSetContView(RENDCONT *pContext,float x,float y,float w,float h)
{
	// Check context
	if(pContext == NULL) return(ERR);

	// Set context offsets
	pContext->fOffsetx=x;
	pContext->fOffsety=y;

	// Set context viewpoert width and height
	pContext->fWidth=w;
	pContext->fHeight=h;
	return(OK);
}

__declspec(dllexport) int __stdcall rendSetContScale(RENDCONT *pContext,float fScale)
{
	// Check context
	if(pContext == NULL) return(ERR);

	// Set context scale
	pContext->fScale=fScale;
	return(OK);
}

__declspec(dllexport) int __stdcall rendSetContBand(RENDCONT *pContext,float *aPos,int nCol)
{
	// Check context
	if(pContext == NULL) return(ERR);

	// Set context band box position and color
	memcpy(pContext->aBandPos,aPos,sizeof(float)*4);
	pContext->nBandCol=nCol;
	return(OK);
}

__declspec(dllexport) int __stdcall rendSetContGrid(RENDCONT *pContext,float fSize,int nCol)
{
	// Check context
	if(pContext == NULL) return(ERR);

	// Set context grid
	pContext->fGridSize=fSize;
	pContext->nGridCol=nCol;
	return(OK);
}

__declspec(dllexport) int __stdcall rendSetContRot(RENDCONT *pContext,RENDOBJ *pObj,int nCol)
{
	// Check context
	if(pContext == NULL) return(ERR);

	// Set context rotation
	pContext->pRotObj=pObj;
	pContext->nRotCol=nCol;
	return(OK);
}

__declspec(dllexport) int __stdcall rendSetContCursor(RENDCONT *pContext,float *aCursor,int nCol)
{
	// Check context
	if(pContext == NULL) return(ERR);

	// Set context cursor
	memcpy(pContext->aCursor,aCursor,nSizeOfFloatTimes3);

	// Set context cursor
	pContext->nCursCol=nCol;
	return(OK);
}

__declspec(dllexport) int __stdcall rendSetContSel(RENDCONT *pContext,int nCol)
{
	// Check context
	if(pContext == NULL) return(ERR);

	// Set context selection
	pContext->nSelCol=nCol;
	return(OK);
}

__declspec(dllexport) int __stdcall rendCheckContSel(RENDCONT *pContext,int nMode,int *nKey)
{
	float fSel,fWork,fDot;
	float aOrig[3],aWork[3],aLookAt[3];
	float fMinX,fMaxX,fMinY,fMaxY;

	RENDOBJ *pCur;

	// Check context
	if(pContext == NULL) return(ERR);

	// Check camera
	if(pContext->nMode == REND_CCAM)
	{
		// Calculate look-at vector
		aLookAt[0]=pContext->aFocus[0]-pContext->aEye[0];
		aLookAt[1]=pContext->aFocus[1]-pContext->aEye[1];
		aLookAt[2]=pContext->aFocus[2]-pContext->aEye[2];
	}

	// Reset key
	*nKey=0;

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

		// Check object mode
		if(pCur->nMode != REND_OHIDE)
		{
			// Get object origin
			memcpy(aOrig,pCur->aOrig,nSizeOfFloatTimes3);

			// Object-to-world transformation
			matVectProduct(NULL,aOrig,pCur->aTransform);

			// Scale selection average
			fSel=pCur->fSel*(pCur->aScale[0]+pCur->aScale[1]+pCur->aScale[2])*fOneOver3;

			// Reset dot product
			fDot=1.0F;

			// Check context mode
			switch(pContext->nMode)
			{
				// Camera view
				case REND_CCAM:
					// Calculate camera to origin vector
					aWork[0]=aOrig[0]-pContext->aEye[0];
					aWork[1]=aOrig[1]-pContext->aEye[1];
					aWork[2]=aOrig[2]-pContext->aEye[2];

					// Calc dot product
					fDot=aLookAt[0]*aWork[0]+aLookAt[1]*aWork[1]+aLookAt[2]*aWork[2];

					// Check dot product
					if(fDot > 0.0F)
					{
						// Compose selection vector
						aWork[0]=(float)sqrt(3.0F)*fSel;
						aWork[1]=(float)sqrt(3.0F)*fSel;
						aWork[2]=(float)sqrt(3.0F)*fSel;

						// World-to-camera transformations
						matVectProduct(NULL,aOrig,pContext->aTransform);
						matVectProduct(NULL,aWork,pContext->aTransform);

						// Scale origin
						aOrig[0]/=aOrig[2];
						aOrig[1]/=aOrig[2];
						aOrig[2]=0.0F;

						// Scale selection vector
						aWork[0]/=aWork[2];
						aWork[1]/=aWork[2];
						aWork[2]=0.0F;

						// Decompose selection vector
						fSel=(float)sqrt(aWork[0]*aWork[0]+aWork[1]*aWork[1]);
					}
					break;

					// X (front) view
				case REND_CX:
					// Non-perspective transformation
					aOrig[0]=-aOrig[2];
					aOrig[1]=-aOrig[1];
					aOrig[2]=0.0F;
					break;

				// Y (top) view
				case REND_CY:
					// Non-perspective transformation
					fWork=-aOrig[0];
					aOrig[0]=-aOrig[2];
					aOrig[1]=fWork;
					aOrig[2]=0.0F;
					break;

				// Z (side) view
				case REND_CZ:
					// Non-perspective transformation
					// aOrig[0]=aOrig[0];
					aOrig[1]=-aOrig[1];
					aOrig[2]=0.0F;
					break;
			}

			// Check dot product
			if(fDot > 0.0F)
			{
				// Scale and offset origin
				aOrig[0]=aOrig[0]*pContext->fScale-pContext->fOffsetx;
				aOrig[1]=aOrig[1]*pContext->fScale-pContext->fOffsety;

				// Scale selection average
				fSel*=pContext->fScale;

				// Tune selection average
				fSel*=REND_STUNE;

				// Get min and max X
				if(pContext->aBandPos[0] < pContext->aBandPos[2])
				{
					fMinX=pContext->aBandPos[0];
					fMaxX=pContext->aBandPos[2];
				}
				else
				{
					fMinX=pContext->aBandPos[2];
					fMaxX=pContext->aBandPos[0];
				}

				// Get min and max Y
				if(pContext->aBandPos[1] < pContext->aBandPos[3])
				{
					fMinY=pContext->aBandPos[1];
					fMaxY=pContext->aBandPos[3];
				}
				else
				{
					fMinY=pContext->aBandPos[3];
					fMaxY=pContext->aBandPos[1];
				}
				
				// Check rectangular region (band-box)
				if((aOrig[0]+fSel >= fMinX) && (aOrig[0]-fSel <= fMaxX) &&
					(aOrig[1]+fSel >= fMinY) && (aOrig[1]-fSel <= fMaxY))
				{
					// Check key
					if(*nKey == 0) *nKey=pCur->nKey;

					// Check mode for toggle and set object mode
					if(nMode == REND_OSEL) pCur->nMode=REND_OSEL;
					if(nMode == REND_OTOG)
					{
						// Toggle object mode
						if(pCur->nMode == REND_OSHOW) pCur->nMode=REND_OSEL;
						else pCur->nMode=REND_OSHOW;
					}
				}
				else
				{
					// Check mode for toggle ans set object mode
					if(nMode != REND_OTOG) pCur->nMode=REND_OSHOW;
				}
			}
		}
		
		// Set pointer to next object
		pCur=pCur->pNext;
	}

	// Reset band box position
	memset(pContext->aBandPos,0,sizeof(float)*4);

	return(OK);
}

__declspec(dllexport) int __stdcall rendCheckContCamera(RENDCONT *pContext,float x,float y,int *nSel)
{
	float fWork;
	float aFocus[3],aEye[3];

	// Set selection
	*nSel=REND_SNONE;
	
	// Check context
	if(pContext == NULL) return(ERR);

	// Check camera
	if(pContext->nCamCol == REND_NONE) return(OK);

	// Copy eye and focus
	memcpy(aEye,pContext->aEye,nSizeOfFloatTimes3);
	memcpy(aFocus,pContext->aFocus,nSizeOfFloatTimes3);

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

			aFocus[0]=-aFocus[2]*pContext->fScale-pContext->fOffsetx;
			aFocus[1]=-aFocus[1]*pContext->fScale-pContext->fOffsety;
			aFocus[2]=0.0F;
			break;

		// Y (top) view
		case REND_CY:
			// Set eye and focus
			fWork=-aEye[0]*pContext->fScale-pContext->fOffsety;
			aEye[0]=-aEye[2]*pContext->fScale-pContext->fOffsetx;
			aEye[1]=fWork;
			aEye[2]=0.0F;

			fWork=-aFocus[0]*pContext->fScale-pContext->fOffsety;
			aFocus[0]=-aFocus[2]*pContext->fScale-pContext->fOffsetx;
			aFocus[1]=fWork;
			aFocus[2]=0.0F;
			break;

		// Z (side) view
		case REND_CZ:
			// Set eye and focus
			aEye[0]=aEye[0]*pContext->fScale-pContext->fOffsetx;
			aEye[1]=-aEye[1]*pContext->fScale-pContext->fOffsety;
			aEye[2]=0.0F;

			aFocus[0]=aFocus[0]*pContext->fScale-pContext->fOffsetx;
			aFocus[1]=-aFocus[1]*pContext->fScale-pContext->fOffsety;
			aFocus[2]=0.0F;
			break;
	}

	// Check eye and set selection
	if((aEye[0] >= x-REND_SCAM) && (aEye[0] <= x+REND_SCAM) &&
		(aEye[1] >= y-REND_SCAM) && (aEye[1] <= y+REND_SCAM))
			*nSel=REND_SEYE;
		
	// Check focus and set selection
	if((aFocus[0] >= x-REND_SCAM) && (aFocus[0] <= x+REND_SCAM) &&
		(aFocus[1] >= y-REND_SCAM) && (aFocus[1] <= y+REND_SCAM))
			*nSel=REND_SFOCUS;
		
	return(OK);
}

__declspec(dllexport) int __stdcall rendGetContCamera(RENDCONT *pContext,float *aEye,float *aFocus)
{
	// Check context
	if(pContext == NULL) return(ERR);

	// Get context camera eye and focus
	memcpy(aEye,pContext->aEye,nSizeOfFloatTimes3);
	memcpy(aFocus,pContext->aFocus,nSizeOfFloatTimes3);
	return(OK);
}

__declspec(dllexport) int __stdcall rendSetContCamera(RENDCONT *pContext,float *aEye,float *aFocus,int nCol)
{
	float aLookAt[3],fDot;

	// Check context
	if(pContext == NULL) return(ERR);

	// Set context camera
	pContext->nCamCol=nCol;

	// Calculate camera look-at vector
	aLookAt[0]=aEye[0]-aFocus[0];
	aLookAt[1]=aEye[1]-aFocus[1];
	aLookAt[2]=aEye[2]-aFocus[2];

	// Check look-at vector
	matNormalize(aLookAt);
	fDot=aLookAt[0]*aAxisy[0]+aLookAt[1]*aAxisy[1]+aLookAt[2]*aAxisy[2];
	if((fDot > -0.99F) && (fDot < 0.99F))
	{
		// Set context camera eye and focus
		memcpy(pContext->aEye,aEye,nSizeOfFloatTimes3);
		memcpy(pContext->aFocus,aFocus,nSizeOfFloatTimes3);
	}

	// Set context world-to-camera transformation matrix
	matIdentity(pContext->aTransform);
	matLookAt(pContext->aTransform,pContext->aEye,pContext->aFocus,aAxisy);
	return(OK);
}

__declspec(dllexport) int __stdcall rendTransContCamera(RENDCONT *pContext,float x,float y)
{
	float aEye[3],aFocus[3],aLookAt[3],aRight[3],aUp[3];

	// Check context
	if(pContext == NULL) return(ERR);

	// Check x and y
	if((x == 0.0F) && (y == 0.0F)) return(OK);

	// Set context camera eye and focus
	memcpy(aEye,pContext->aEye,nSizeOfFloatTimes3);
	memcpy(aFocus,pContext->aFocus,nSizeOfFloatTimes3);

	// Calculate camera look-at vector
	aLookAt[0]=aEye[0]-aFocus[0];
	aLookAt[1]=aEye[1]-aFocus[1];
	aLookAt[2]=aEye[2]-aFocus[2];

	// Calculate camera eye right and camera eye up vectors
	matCrossProduct(aRight,aAxisy,aLookAt);
	if(y != 0.0F) matCrossProduct(aUp,aLookAt,aRight);

	// Translate camera eye
	if(x != 0.0F)
	{
		// Normalize camera right vector
		matNormalize(aRight);
		
		// Scale camera right vector
		aRight[0]*=x;
		aRight[1]*=x;
		aRight[2]*=x;

		// Translate camera eye horizontally
		aEye[0]=aEye[0]+aRight[0];
		aEye[1]=aEye[1]+aRight[1];
		aEye[2]=aEye[2]+aRight[2];

		// Translate camera focus horizontally
		aFocus[0]+=aRight[0];
		aFocus[1]+=aRight[1];
		aFocus[2]+=aRight[2];
	}

	// Translate camera focus
	if(y != 0.0F)
	{
		// Normalize camera up vector
		matNormalize(aUp);

		// Scale camera up vector
		aUp[0]*=y;
		aUp[1]*=y;
		aUp[2]*=y;

		// Translate camera eye vertically
		aEye[0]=aEye[0]+aUp[0];
		aEye[1]=aEye[1]+aUp[1];
		aEye[2]=aEye[2]+aUp[2];

		// Translate camera focus vertically
		aFocus[0]+=aUp[0];
		aFocus[1]+=aUp[1];
		aFocus[2]+=aUp[2];
	}

	// Set context camera
	rendSetContCamera(pContext,aEye,aFocus,pContext->nCamCol);
	return(OK);
}

__declspec(dllexport) int __stdcall rendScaleContCamera(RENDCONT *pContext,float nScale)
{
	float aEye[3],aFocus[3],aScale[16];

	// Check context
	if(pContext == NULL) return(ERR);

	// Check scale
	if(nScale == 1.0F) return(OK);

	// Set context camera eye and focus
	memcpy(aEye,pContext->aEye,nSizeOfFloatTimes3);
	memcpy(aFocus,pContext->aFocus,nSizeOfFloatTimes3);

	// Translate camera eye
	aEye[0]-=aFocus[0];
	aEye[1]-=aFocus[1];
	aEye[2]-=aFocus[2];

	// Set scale matrix
	matIdentity(aScale);
	matScale(aScale,nScale,nScale,nScale);

	// Scale look-at vector
	matVectProduct(NULL,aEye,aScale);

	// Translate camera eye
	aEye[0]+=aFocus[0];
	aEye[1]+=aFocus[1];
	aEye[2]+=aFocus[2];

	// Set context camera
	rendSetContCamera(pContext,aEye,aFocus,pContext->nCamCol);
	return(OK);
}

__declspec(dllexport) int __stdcall rendRotContCamera(RENDCONT *pContext,float fAngh,float fAngv)
{
	float aEye[3],aFocus[3],aLookAt[3],aRight[3];
	float aRot[16];

	// Check context
	if(pContext == NULL) return(ERR);

	// Check angles
	if((fAngh == 0.0F) && (fAngv == 0.0F)) return(OK);

	// Set context camera eye and focus
	memcpy(aEye,pContext->aEye,nSizeOfFloatTimes3);
	memcpy(aFocus,pContext->aFocus,nSizeOfFloatTimes3);

	// Calculate camera look-at vector
	aLookAt[0]=aEye[0]-aFocus[0];

⌨️ 快捷键说明

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