lighting.c

来自「用brew开发3D的例程」· C语言 代码 · 共 1,515 行 · 第 1/3 页

C
1,515
字号
				else if(pMe->lightTypeOp == AEE3D_LIGHT_SPECULAR)
				{

					// if we lose color info by switching lighting modes, 
					// make sure we have a valid option selected. Directions
					// are always valid
					if(isLightingColorOption(pMe->lightDirColOp) &&
						isColorLightingMode(mode, AEE3D_LIGHT_SPECULAR) &&
						!isColorLightingMode(mode, AEE3D_LIGHT_DIFFUSED) )
					{
						pMe->lightDirColOp = LIGHT_X;
					}

					// can always change to diffused lighting 
					pMe->lightTypeOp = AEE3D_LIGHT_DIFFUSED;
				}
										
			}
			break;

			// toggle lighting mode
			case AVK_9:
			{
				
				I3D_GetLightingMode(pMe->m_p3D, &mode);

				if(mode == AEE3D_LIGHT_MODE_DIFFUSED)
				{
					mode = AEE3D_LIGHT_MODE_COLOR_DIFFUSED;
				}
				else if(mode == AEE3D_LIGHT_MODE_COLOR_DIFFUSED)
				{
					// when going from AEE3D_LIGHT_MODE_COLOR_DIFFUSED
					// to AEE3D_LIGHT_MODE_DIFFUSED_COLOR_SPECULAR, we lose
					// diffuse color information, so make sure we're on a valid
					// selection
					if(pMe->lightDirColOp == LIGHT_RED ||
					   pMe->lightDirColOp == LIGHT_GREEN ||
					   pMe->lightDirColOp == LIGHT_BLUE ||
					   pMe->lightDirColOp == LIGHT_ALPHA)
					{
						pMe->lightDirColOp = LIGHT_X;
					}
					mode = AEE3D_LIGHT_MODE_DIFFUSED_COLOR_SPECULAR;
				}
				else if(mode == AEE3D_LIGHT_MODE_DIFFUSED_COLOR_SPECULAR)
				{
					mode = AEE3D_LIGHT_MODE_COLOR_DIFFUSED_COLOR_SPECULAR;
				}
				else if(mode == AEE3D_LIGHT_MODE_COLOR_DIFFUSED_COLOR_SPECULAR)
				{	
					// when switching between these modes make sure we have 
					// a valid option selected
					if(pMe->lightTypeOp == AEE3D_LIGHT_SPECULAR)
						pMe->lightTypeOp = AEE3D_LIGHT_DIFFUSED;

					if(pMe->lightDirColOp == LIGHT_RED ||
					   pMe->lightDirColOp == LIGHT_GREEN ||
					   pMe->lightDirColOp == LIGHT_BLUE ||
					   pMe->lightDirColOp == LIGHT_ALPHA)
					{
						pMe->lightDirColOp = LIGHT_X;
					}

					mode = AEE3D_LIGHT_MODE_DIFFUSED;
				}

				I3D_SetLightingMode(pMe->m_p3D, mode);

			}
			break;
					

			// Show/Hide Commands
			case AVK_POUND:
			{
				if(pMe->showHelp == FALSE)
					pMe->showHelp = TRUE;
				else if(pMe->showHelp == TRUE)
					pMe->showHelp = FALSE;
			}
			break;

			case AVK_CLR:
			{
				/* 
					User request to return to previous state. Set the next state, 
					and a flag, and wait until the current frame finishes rendering 
					before switching states. For information on why we do this, 
					read the comment in the parent event handler for the AVK_CLR event.
				*/
				pMe->nextState = STATE_LIGHTING;
				pMe->changeState = TRUE;
			}
			break;
			
			default:
				return FALSE;
		}
		
	} // end case EVT_KEY	
	
	return TRUE;


	case EVT_COMMAND:
		return TRUE;


	default: 
		return FALSE;

	} // end switch(event)


}


/*===========================================================================

FUNCTION 
	TutorI3D_LightingMaterialHandleEvent

DESCRIPTION
	This is the Event Handler for the material tutorial. 
	All events while in the lighting material state are processed
	by this event handler. 

PROTOTYPE:
	static boolean TutorI3D_LightingMaterialHandleEvent(TutorI3D* pMe, AEEEvent event, 
														uint16 wParam, uint32 dwParam)

PARAMETERS:
	pMe [In] : Pointer to TutorI3D sturcture
	ecode [In]: Specifies the Event sent to the event handler
    wParam, dwParam [In]: Event specific data.

DEPENDENCIES
  none

RETURN VALUE
  TRUE: If the event handler has processed the event
  FALSE: If the event handler did not process the event

SIDE EFFECTS
  none
===========================================================================*/
static boolean TutorI3D_LightingMaterialHandleEvent(TutorI3D* pMe, AEEEvent event, 
													uint16 wParam, uint32 dwParam)
{

	switch(event)
	{
	
	case EVT_KEY_RELEASE:
		pMe->keyIsPressed = FALSE;
		return TRUE;

	case EVT_KEY_PRESS:
	{
		AEE3DMaterial material;
		int matUnit=0;
		
		pMe->lastKeyPresswParam = wParam;
		pMe->lastKeyPressdwParam = dwParam;
		pMe->keyIsPressed = TRUE;
							
		if(wParam == AVK_UP || wParam == AVK_DOWN)
		{

			matUnit = (wParam == AVK_UP ? MATERIAL_UNIT : - MATERIAL_UNIT);
			
										
			I3D_GetMaterial(pMe->m_p3D, &material);
						
			if(pMe->lightMaterialOp == MATERIAL_RED )
				material.color.r += matUnit;
			else if(pMe->lightMaterialOp == MATERIAL_GREEN )
				material.color.g += matUnit;
			else if(pMe->lightMaterialOp == MATERIAL_BLUE )
				material.color.b += matUnit;
			else if(pMe->lightMaterialOp == MATERIAL_ALPHA )
				material.color.a += matUnit;
			
			else if(pMe->lightMaterialOp == MATERIAL_SHININESS )
				material.shininess += matUnit;
			else if(pMe->lightMaterialOp == MATERIAL_EMISSIVE )
				material.emissive += matUnit;
							
			I3D_SetMaterial(pMe->m_p3D, &material);
			
			return TRUE;
		}
		else
			return FALSE; // key wasn't AVK_UP or AVK_DOWN
	
	}	// end case EVT_KEY_PRESS
	

	case EVT_KEY:
	{
			
		switch(wParam)
		{
			
			case AVK_1:
				pMe->lightMaterialOp = MATERIAL_RED;
			break;

			case AVK_2:
				pMe->lightMaterialOp = MATERIAL_GREEN;
			break;

			case AVK_3:
				pMe->lightMaterialOp = MATERIAL_BLUE;
			break;

			case AVK_4:
				pMe->lightMaterialOp = MATERIAL_ALPHA;
			break;

			case AVK_5:
				pMe->lightMaterialOp = MATERIAL_SHININESS;
			break;

			case AVK_6:
				pMe->lightMaterialOp = MATERIAL_EMISSIVE;
			break;
							

			// Show/Hide Commands
			case AVK_POUND:
			{
				if(pMe->showHelp == FALSE)
					pMe->showHelp = TRUE;
				else if(pMe->showHelp == TRUE)
					pMe->showHelp = FALSE;
			}
			break;

			case AVK_CLR:
			{
				/* 
					User request to return to previous state. Set the next state, 
					and a flag, and wait until the current frame finishes rendering 
					before switching states. For information on why we do this, 
					read the comment in the parent event handler for the AVK_CLR event.
				*/
				pMe->nextState = STATE_LIGHTING;
				pMe->changeState = TRUE;
			}
			break;
			
			default:
				return FALSE;
		}
		
	} // end case EVT_KEY	
	
	return TRUE;


	case EVT_COMMAND:
		return TRUE;


	default: 
		return FALSE;

	} // end switch(event)

}

/*===========================================================================

FUNCTION 
	OnLightingDirectionColor

DESCRIPTION
	 This function is called when the lighting direction and color menu item 
	 is selected. Anything to do when first entering this tutorial should be 
	 done here, ie. tutorial specific initializations. 

PROTOTYPE:
	static boolean OnLightingDirectionColor(TutorI3D* pMe)

PARAMETERS:
	pMe [In] : Pointer to TutorI3D sturcture
	
DEPENDENCIES
  none

RETURN VALUE
  TRUE: If succeded
  FALSE: If there was an error

SIDE EFFECTS
  none
===========================================================================*/
static boolean OnLightingDirectionColor(TutorI3D* pMe)
{
	return TRUE;
}

/*===========================================================================

FUNCTION 
	OnLightingMaterial

DESCRIPTION
	 This function is called when the material menu item 
	 is selected. Anything to do when first entering this tutorial should be 
	 done here, ie. tutorial specific initializations. 

PROTOTYPE:
	static boolean OnLightingMaterial(TutorI3D* pMe)

PARAMETERS:
	pMe [In] : Pointer to TutorI3D sturcture
	
DEPENDENCIES
  none

RETURN VALUE
  TRUE: If succeded
  FALSE: If there was an error

SIDE EFFECTS
  none
===========================================================================*/
static boolean OnLightingMaterial(TutorI3D* pMe)
{
	return TRUE;
}


/*===========================================================================

  FUNCTION: LightingDirectionColorManipulateBuf
  
	DESCRIPTION
	   This function adds text and 2D graphics to the frame buffer after
	   3D frame render is complete for the lighting direction and color
	   tutorial.
	  
	PROTOTYPE:
	  void LightingDirectionColorManipulateBuf(TutorI3D* pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture
	  		  
	DEPENDENCIES
	  none
				  
	RETURN VALUE
	  none
					
	SIDE EFFECTS
	  none
===========================================================================*/

void LightingDirectionColorManipulateBuf(TutorI3D* pMe)
{

	AECHAR szBuf[80];
	char chBuf[80];
	AEE3DLight lightDiffused;
	AEE3DLight lightSpecular;
	int8 colCharStart=-1;	// index of first color character on the line 
	AEE3DLightingMode lightMode;
	IFont* oldFont;
	int oldFontHeight;
	RGBVAL rgb=0;
	RGBVAL rgb1=0;
	int textWidth;
	
	int nLine=0;	// # of lines used to output text
	int maxWidth;
	int xText;
	int yText;

	if(!pMe)
		return;

	TutorI3D_SetTopMenuTextValues(pMe, &xText, &yText, &maxWidth);

	TutorI3D_SetupTutorialWindow(pMe);
			
	rgb = IDISPLAY_SetColor(pMe->a.m_pIDisplay, CLR_USER_TEXT, CLR_API_TEXT);
	
	
	SPRINTF(chBuf, "I3D_SetLight(light)" );
	STR_TO_WSTR (chBuf, szBuf, sizeof(szBuf));
	outputColorText(pMe, szBuf, 13, 5, xText, yText, 
					maxWidth, CLR_API_VAR_TEXT);

	
	// Now draw the tutorial information in the bottom menu. 
	// First set the initial output point for the text and maxWidth. 
	TutorI3D_SetBottomMenuTextValues(pMe,&xText,&yText,&maxWidth);
	
	I3D_GetLightingMode(pMe->m_p3D, &lightMode);
	I3D_GetLight(pMe->m_p3D, AEE3D_LIGHT_DIFFUSED, &lightDiffused);
	I3D_GetLight(pMe->m_p3D, AEE3D_LIGHT_SPECULAR, &lightSpecular);

	IDISPLAY_SetColor(pMe->a.m_pIDisplay, CLR_USER_TEXT, MENU_TEXT_CLR );
	
	if(lightMode == AEE3D_LIGHT_MODE_DIFFUSED)
		SPRINTF(chBuf, "DIFFUSED");
	else if(lightMode == AEE3D_LIGHT_MODE_COLOR_DIFFUSED)
		SPRINTF(chBuf, "COLOR_DIFFUSED");
	else if(lightMode == AEE3D_LIGHT_MODE_DIFFUSED_COLOR_SPECULAR)
		SPRINTF(chBuf, "DIFFUSED_COLOR_SPECULAR");
	else
		SPRINTF(chBuf, "COLOR_DIFFUSED_COLOR_SPECULAR");
	
	STR_TO_WSTR (chBuf, szBuf, sizeof(szBuf));
	
	oldFont = IDISPLAY_SetFont(pMe->a.m_pIDisplay, AEE_FONT_NORMAL, pMe->font10);
	oldFontHeight = pMe->charHeight;
	pMe->charHeight = IDISPLAY_GetFontMetrics(pMe->a.m_pIDisplay, AEE_FONT_NORMAL,
											  NULL, NULL); 
	nLine = outputMultiLineStr(pMe, szBuf, -1, xText, yText, -1, maxWidth,
					&textWidth );
	yText += nLine*pMe->charHeight;
	
	if(pMe->lightTypeOp == AEE3D_LIGHT_DIFFUSED)
	{
		rgb1=IDISPLAY_SetColor(pMe->a.m_pIDisplay, CLR_USER_TEXT, CLR_BLUE);
	}


	/* Only display color diffused info if we have a color diffused light mode*/
	if(lightMode == AEE3D_LIGHT_MODE_COLOR_DIFFUSED ||
		lightMode == AEE3D_LIGHT_MODE_COLOR_DIFFUSED_COLOR_SPECULAR)
	{
			// we highlight the parameter only if we have the right lightType
			// otherwise don't highlight (ie. set colCharStart to -1)
			if(pMe->lightTypeOp == AEE3D_LIGHT_DIFFUSED)
			{
				if(pMe->lightDirColOp == LIGHT_RED)
					colCharStart = 17;
				else if(pMe->lightDirColOp == LIGHT_GREEN)
					colCharStart = 21;
				else if(pMe->lightDirColOp == LIGHT_BLUE)
					colCharStart = 25;
				else if(pMe->lightDirColOp == LIGHT_ALPHA)
					colCharStart = 29;
				else	
					colCharStart = -1;
			}
			else	
				colCharStart = -1;
		

		SPRINTF(chBuf, "light.colorDiff=(%.3d,%.3d,%.3d,%.3d)",
						lightDiffused.color.r, lightDiffused.color.g,
						lightDiffused.color.b, lightDiffused.color.a);
		STR_TO_WSTR (chBuf, szBuf, sizeof(szBuf));
		
		nLine = outputColorText(pMe, szBuf, colCharStart, 3, xText, yText, 
								maxWidth, ITEM_SELECT_COLOR);
		
		yText += nLine*pMe->charHeight;
	}
	
	/* Always display diffused direction info*/

	if(pMe->lightTypeOp == AEE3D_LIGHT_DIFFUSED)
	{
		if(pMe->lightDirColOp == LIGHT_X)
			colCharStart = 15;
		else if(pMe->lightDirColOp == LIGHT_Y)
			colCharStart = 22;
		else if(pMe->lightDirColOp == LIGHT_Z)
			colCharStart = 29;
		else	
			colCharStart = -1;
	}
	else
		colCharStart = -1;

		
	SPRINTF(chBuf, "light.dirDiff=(%+.5d,%+.5d,%+.5d)", lightDiffused.direction.x, 
				lightDiffused.direction.y, lightDiffused.direction.z);
	
		
	STR_TO_WSTR (chBuf, szBuf, sizeof(szBuf));

	// output 6 color characters, with the character at colCharStart as the first 
	// color character.
	nLine = outputColorText(pMe, szBuf, colCharStart, 6, xText, yText, 
							maxWidth, ITEM_SELECT_COLOR);
	yText += nLine*pMe->charHeight;


	if(pMe->lightTypeOp == AEE3D_LIGHT_DIFFUSED)
	{
		IDISPLAY_SetColor(pMe->a.m_pIDisplay, CLR_USER_TEXT, rgb1);
	}
	else if(pMe->lightTypeOp == AEE3D_LIGHT_SPECULAR)
	{
		rgb1 = IDISPLAY_SetColor(pMe->a.m_pIDisplay, CLR_USER_TEXT, CLR_BLUE);
	}

⌨️ 快捷键说明

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