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

📄 tutori3d.c

📁 用brew开发3D的例程
💻 C
📖 第 1 页 / 共 5 页
字号:
	*/

	  case EVT_KEY_PRESS:
		  
		  if(!pMe->isSuspended)
		  {
			  if(pMe->state == STATE_MAIN)
			  {
					// this event not handled by main
				  return FALSE;
			  }
			  else
			  {
				  // call corresponding sub menu event handler with EVT_KEY_PRESS   
				  if(eventHandlerFunc)
					 return eventHandlerFunc(pMe, EVT_KEY_PRESS, wParam, dwParam);
				  else 
					return FALSE;
			  }
		  }
		  else	// don't handle key presses when we're suspended
			return FALSE;
				  

	  case EVT_KEY:
		  
		  if(!pMe->isSuspended)
		  {
			  // use default menu handler in main menu state
			  if(pMe->state == STATE_MAIN)
			  {
				 // If the select key is pressed on an IMENU interface object, 
				 // the default menu handler for IMENU interfaces will send
				 // an EVT_COMMAND event back to the application, with the
				 // ID specified for the menu item in the wParam parameter. 
				  
				  return IMENUCTL_HandleEvent(pMe->m_pMainMenu, EVT_KEY, 
											wParam, dwParam);
			  }
			  else
			  {
				  // call corresponding sub menu event handler with EVT_KEY
				  if(eventHandlerFunc)
						return eventHandlerFunc(pMe, EVT_KEY, wParam, dwParam);
					else 
						return FALSE;
			  }
		  }
		  else	// don't handle keys when we're suspended
			  return FALSE;
		  
	 case EVT_KEY_RELEASE:
		
		 // we handle EVT_KEY_RELEASE even if we're suspended, because
		 // when we resume, we don't want the key to still be pressed. 
		   
		   if(pMe->state == STATE_MAIN)
		   {
				// this event not handled by main
			   return FALSE;
		   }
		   else
		   {
			   // call corresponding sub menu event handler with EVT_KEY_RELEASE
			   if(eventHandlerFunc)
				   return eventHandlerFunc(pMe, EVT_KEY_RELEASE, wParam, dwParam);
			   else 
				   return FALSE;
		   }

	   /*  We receive an EVT_COMMAND event here if a menu item off of a menu
		   has been selected. In this case, we need to change states and menus. 
	   */
		  
	   case EVT_COMMAND:
		
		if(!pMe->isSuspended)
		{
			if(pMe->state == STATE_MAIN)	// handling main menu EVT_COMMANDs
			{
			   switch(wParam)
			   {
				
			   case IDS_TRANSFORMATIONS:	// going to transformations menu
					
					// don't allow any more events to be processed by the main 
					// menu, since we're changing menus here. 
					IMENUCTL_SetActive( pMe->m_pMainMenu, FALSE );
					
					// set the state we're going to.
					pMe->state = STATE_TRANSFORM;
					
					// set defaults before going into new state. This essentially
					// resets any I3D states/parameters the user may have changed 
					// in a tutorial.
					TutorI3D_SetDefaults(pMe);

					IDISPLAY_ClearScreen (pMe->a.m_pIDisplay);
					
					// initialize the screen we're going to and the 
					// model data that we're going to use. 
					TutorI3D_InitTransformScreen(pMe);
					TutorI3D_InitLadyBug(pMe);
					TutorI3D_InitAxis(pMe);
					
					// draw the menu for the new state
					TutorI3D_DrawTransformMenu(pMe);
					
					//start the rendering loop
					TutorI3D_DrawLadyBug(pMe);
					break;
					
			   case IDS_PROJECTION:
				   IMENUCTL_SetActive( pMe->m_pMainMenu, FALSE );
				   pMe->state = STATE_PROJECTION;
				   TutorI3D_SetDefaults(pMe);
				   IDISPLAY_ClearScreen (pMe->a.m_pIDisplay);
				   TutorI3D_InitProjectionScreen(pMe);
				   TutorI3D_InitLadyBug(pMe);
				   TutorI3D_DrawProjectionMenu(pMe);
				   TutorI3D_DrawLadyBug(pMe);
				   break;
				   
			   case IDS_LIGHTING:
				   IMENUCTL_SetActive( pMe->m_pMainMenu, FALSE );
				   pMe->state = STATE_LIGHTING;
				   TutorI3D_SetDefaults(pMe);
				   IDISPLAY_ClearScreen(pMe->a.m_pIDisplay);
				   TutorI3D_InitLightingScreen(pMe);
				   TutorI3D_InitSmoothSphere(pMe);
				   TutorI3D_DrawLightingMenu(pMe);
				   TutorI3D_DrawSmoothSphere(pMe);
				   break;

				case IDS_TEXTURES_BLENDING:
				   IMENUCTL_SetActive( pMe->m_pMainMenu, FALSE );
				   pMe->state = STATE_TEXTURES_BLENDING;
				   TutorI3D_SetDefaults(pMe);
				   IDISPLAY_ClearScreen(pMe->a.m_pIDisplay);
				   TutorI3D_InitTexturesBlendingScreen(pMe);
				   TutorI3D_InitCube(pMe);
				   TutorI3D_DrawTexturesBlendingMenu(pMe);
				   TutorI3D_DrawCube(pMe);
				   break;
    
			   default:
				   return FALSE;
			   }
			   return TRUE;
			}
			else
			{
				if(eventHandlerFunc)
					return eventHandlerFunc(pMe, EVT_COMMAND, wParam, dwParam);
				else
					return FALSE;

			}
		}
		else	// don't process commands while we're suspended
			return FALSE;

		
		/* 
			When we receive EVT_APP_STOP we are supposed to close the application.
			Cancel any pending timers, and reset the fonts to their original values. 
			Memory will be freed in the free memory callback function. 
		*/
	   case EVT_APP_STOP:
	  	  
		  // cancel the callback set by ISHELL_SetTimerEx() that may be active 
		  CALLBACK_Cancel(&pMe->cb);
		  
		  if(!IDISPLAY_SetFont(pMe->a.m_pIDisplay, AEE_FONT_NORMAL, pMe->oldFontNormal))
		  {
			DBGPRINTF("Error setting font");
		  }
		  if(!IDISPLAY_SetFont(pMe->a.m_pIDisplay, AEE_FONT_BOLD, pMe->oldFontBold))
		  {
			DBGPRINTF("Error setting font");
		  }
          return TRUE;

	  /*
			Suspend event comes in if the application needs to be suspended 
			(ie. a call comes in on the phone) temporarily. In this case, 
			cancel the rendering callback because we don't want to continue 
			rendering while there is a call. 
			 
	  */

	  case EVT_APP_SUSPEND:
		  
		  pMe->isSuspended = TRUE;

		  // cancel the callback set by ISHELL_SetTimerEx() that may be active 
		  CALLBACK_Cancel(&pMe->cb);
		  
		  // if the menus are allocated, make sure they're deactivated
		  if(pMe->m_pMainMenu)
			IMENUCTL_SetActive( pMe->m_pMainMenu, FALSE );
		  if(pMe->m_pSubMenu)
			IMENUCTL_SetActive( pMe->m_pSubMenu, FALSE );

		  return TRUE;
  	  
	  /*
			When we resume, all the app data is as it was before it was 
			suspended. So if we were in a render state, draw the menu (if necessary),
			and call the appropriate draw function using the current active model 
			to restart the rendering loop. If it wasn't a rendering state, 
			go back to the main menu.

	  */

	  case EVT_APP_RESUME:
			
		  pMe->isSuspended = FALSE;

		  if(isRenderState(pMe->state))		// start rendering loop again
		  {
			 
			 PFN_DRAWFUNC drawFunc = GetDrawFunction(pMe->drawModel);
			 
			 // determine if a menu has to be redrawn to the screen
			 if(isSubMenuState(pMe->state))
			 {
				// draw the corresponding menu for this state
				 PFN_DRAWMENUFUNC drawMenuFunc = GetDrawMenuFunction(pMe->state);			 
				 
				 if(drawMenuFunc)
				 {
					 drawMenuFunc(pMe);	// draw the menu
				 }
				 else	
				 {
					DBGPRINTF("Error: no draw menu function");
					return FALSE;
				 }

			 }
		 
			 // now draw the 3D scene/model
			 if(drawFunc)
			 {
				 drawFunc(pMe);
			 }
			 else
			 {
				DBGPRINTF("Error: unknown model to draw");
				return FALSE;
			 }
		  }
		  else		// if not a render state, go to main menu
		  {
			 if(!TutorI3D_DrawMainMenu(pMe))
				return FALSE;	

		  }
		  return TRUE;

		
	  case EVT_APP_NO_SLEEP:
			return TRUE;

      default:
         break;
   }

   // event not handled by the app. return FALSE
	 return FALSE;
}

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

  FUNCTION: TutorI3D_LoadResTexture
  
	DESCRIPTION
	  This function will load texture for a 3D model from the resource file
	  	  
	PROTOTYPE:
	  static boolean TutorI3D_LoadResTexture(TutorI3D *pme, I3DModel* model, int textureIndex, 
									  int16 resID)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture;
	  model: [in]: The model instance
	  textureIndex: [in]: index into texture table of model to set the texture for
	  resID: [in]: resource ID of texture bitmap in resource file
		  
	DEPENDENCIES
	  none
				  
	RETURN VALUE
	  TRUE:		on success
	  FALSE:	on failure
					
	SIDE EFFECTS
	  none
===========================================================================*/
static boolean TutorI3D_LoadResTexture(TutorI3D *pme, I3DModel* model, 
									   int16 textureIndex, int16 resID)
{

	AEE3DTexture myT1;
	boolean ret;

	if (pme == NULL || model == NULL)
		return FALSE;
	
	// setup the texture to set
	myT1.type = AEE3D_TEXTURE_DIFFUSED;
	myT1.SamplingMode = AEE3D_TEXTURE_SAMPLING_NEAREST;
	myT1.Wrap_s = AEE3D_TEXTURE_WRAP_REPEAT;
	myT1.Wrap_t = AEE3D_TEXTURE_WRAP_REPEAT;
	myT1.BorderColorIndex = 255;
		
	// load the image from the resource file. 
	myT1.pImage = ISHELL_LoadResBitmap(pme->a.m_pIShell,TUTORI3D_RES_FILE, resID);
	if(myT1.pImage == NULL)
		return FALSE;
	
	// set the texture for the model 
	ret = I3DModel_SetTextureTbl(model, &myT1, textureIndex);
	
	// we can release the image that we loaded from the resource, because
	// I3DModel_SetTextureTbl() will increase the reference count for the image to 
	// incdicate I3D is using it. 
	if(myT1.pImage) IBITMAP_Release(myT1.pImage);
	
	if (ret == SUCCESS)
		return TRUE;
	else
		return FALSE;
	
}

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

  FUNCTION: TutorI3D_InitSmoothSphere
  
	DESCRIPTION
	  Initialization function for drawing the smooth sphere model. 
	  This function should be called before calling the draw function
	  for this model. 
	  	  
	PROTOTYPE:
	  void TutorI3D_InitSmoothSphere(TutorI3D* pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture;
	  
		  
	DEPENDENCIES
	  none
				  
	RETURN VALUE
	  none
					
	SIDE EFFECTS
	  none
===========================================================================*/

void TutorI3D_InitSmoothSphere(TutorI3D* pMe)
{

	AEE3DColor color = {255,127,100,0};
	AEE3DPoint srcDirection = {2000,3000,65536};
	AEE3DPoint direction;
	AEE3DLight light_value;

	if(!pMe) return;
	
	//set the model to be drawn
	pMe->drawModel = MODEL_SMOOTH_SPHERE;
	   
	//GetUnitVector returns a Q12 unit vector. Multiply this by 2^2=4
	//to get a Q14 unit vector as SetLight expects
	I3DUtil_GetUnitVector(pMe->m_p3DUtil, &srcDirection, &direction);
	direction.x <<= 2;
	direction.y <<= 2;
	direction.z <<= 2;
	
	// set the diffused light for the scene. 
	light_value.color = color;
	light_value.direction = direction;
	light_value.type = AEE3D_LIGHT_DIFFUSED;
	if(I3D_SetLight(pMe->m_p3D, &light_value) != SUCCESS)
	{
		DBGPRINTF("Error: Diffused light not set");
	}
	   	
	// set the specular light for the scence
	// (same parameters as for the diffused light)
	light_value.type = AEE3D_LIGHT_SPECULAR;
	if(I3D_SetLight(pMe->m_p3D, &light_value) != SUCCESS)
	{
		DBGPRINTF("Error: Specular light not set");
	}
    

	{
		AEE3DMaterial myMat;
		
		myMat.color.r = 248;
		myMat.color.g = 191;
		myMat.color.b = 36;
		myMat.shininess = 50;	
		myMat.emissive = 5;
		if(I3D_SetMaterial(pMe->m_p3D, &myMat) != SUCCESS)
		{
			DBGPRINTF("Error: setting material");
		}		
	}

	// set render mode to smooth shading when 
	// drawing the sphere. 
	//if(I3D_SetRenderMode(pMe->m_p3D, AEE3D_RENDER_SMOOTH_TEXTURE_SHADING) != SUCCESS)
	if(I3D_SetRenderMode(pMe->m_p3D, AEE3D_RENDER_SMOOTH_SHADING) != SUCCESS)
	{
		DBGPRINTF("Error: Problem setting render mode");
	}
	

	IDISPLAY_ClearScreen (pMe->a.m_pIDisplay);
    I3D_ResetZBuf(pMe->m_p3D);
	
	if(I3D_SetCullingMode(pMe->m_p3D, AEE3D_CULLING_BACK_FACING) != SUCCESS)
	{
		DBGPRINTF("Error: Problem setting culling mode");
	}
		
	if(I3DUtil_SetIdentityMatrix(pMe->m_p3DUtil,&pMe->transformMtx) != SUCCESS)
	{
		DBGPRINTF("Error: can't set identity");
	}

	pMe->translateVector.x = 0;
	pMe->translateVector.y = -40<<16;
	pMe->translateVector.z = 512<<16;
	if(I3DUtil_SetTranslationMatrix(pMe->m_p3DUtil,&pMe->translateVector,
									&pMe->transformMtx) != SUCCESS)
	{
		DBGPRINTF("Error: can't set translation mtx");
	}

	// scale to 1/6th original size
	pMe->transformMtx.m00 /= 6;
	pMe->transformMtx.m11 /= 6;
	pMe->transformMtx.m22 /= 6;
	
	
	// keep a copy of the initial transform matrix
	MEMCPY(&pMe->initalTransMtx, &pMe->transformMtx, sizeof(AEE3DTransformMatrix));
	
}


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

  FUNCTION: TutorI3D_DrawSmoothSphere
  
	DESCRIPTION
	  Draw function for the smooth sphere. This function should only be called
	  after the initialization function has been callled for this model. 
	  This function will start the rendering process and draw one frame. 
	  I3D sends an AEE3D_EVENT_FRAME_COMPLETED event to the registered 
	  I3D event notifier when the frame is finished rendering.

	  	  
	PROTOTYPE:
	  static void TutorI3D_DrawSmoothSphere(TutorI3D* pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture;
	  
		  
	DEPENDENCIES
		TutorI3D_InitSmoothSphere() needs to be called before this function
				  
	RETURN VALUE
	  none

⌨️ 快捷键说明

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