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

📄 tutori3d.c

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

SERVICES:   I3D interactive tutorial
  
DESCRIPTION: This is the main TutorI3D application file. 
			 This application is an interactive tutorial of the I3D interface. 
			 It is meant to help users of the I3D interface, get up to speed
			 on its features and capabilities. This application allows users
			 to change the I3D parameters and see the results of these changes. 
                        
      
           
         
        Copyright (c) 1999-2003 QUALCOMM Incorporated.
               All Rights Reserved.
            QUALCOMM Proprietary/GTDR
=================================================================================*/

/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */

#include "tutori3d.h"
#include "transform.h"
#include "projection.h"
#include "model.h"
#include "lighting.h"
#include "textures_blending.h"

/*-------------------------------------------------------------------
Core BREW Functions
-------------------------------------------------------------------*/
boolean TutorI3D_HandleEvent(IApplet * pi, AEEEvent eCode, 
                                      uint16 wParam, uint32 dwParam);
boolean TutorI3DInit(TutorI3D* pMe);
void	TutorI3D_FreeAppData(TutorI3D* pMe);


/*----------------------------------------------
Application Functions
-------------------------------------------*/

void TutorI3D_EventNotify(TutorI3D * pMe, AEE3DEventNotify * pcn);

static boolean TutorI3D_LoadResTexture(TutorI3D *pme, I3DModel* model, int16 textureIndex, int16 resID);
static boolean TutorI3D_SetDefaults(TutorI3D *pMe);
static boolean TutorI3D_DrawIntroScreen(TutorI3D* pMe);


static void TutorI3D_DrawLadyBug(TutorI3D * pMe);
static void TutorI3D_DrawSmoothSphere(TutorI3D* pMe);
static void TutorI3D_DrawSphere(TutorI3D* pMe);
static void TutorI3D_DrawCube(TutorI3D* pMe);
static void TutorI3D_DrawField(TutorI3D * pMe);
static void TutorI3D_DrawAxis(TutorI3D * pMe);
static void TutorI3D_DrawAxisLabels(TutorI3D* pMe);


static boolean TutorI3D_InitMainMenu(TutorI3D* pMe);
static boolean TutorI3D_DrawMainMenu(TutorI3D* pMe);
static void TutorI3D_ManipulateBuf(TutorI3D* pMe);

static boolean Get2DPointFrom3DPoint(TutorI3D* pMe, const AEE3DPoint* srcPoint, 
							  const AEE3DTransformMatrix* transformMtx, 
							  const AEE3DPoint* translation, AEE3DPoint* dstPoint);

static boolean TutorI3D_CloseAllMenus(TutorI3D* pMe);
static void TutorI3D_UpdateTransformMtxAndModelData(TutorI3D* pMe);

static PFN_DRAWMENUFUNC GetDrawMenuFunction(TutorI3D_State state);
static PFN_EVENTHANDLER GetEventHandler(TutorI3D_State state);
static PFN_DRAWFUNC GetDrawFunction(DrawModel modelType);

static boolean TutorI3D_SetTransformMatrix(TutorI3D* pMe, int rotate_unitx, 
										   int rotate_unity, int rotate_unitz, 
										   AEE3DPoint* translateVector, 
										   AEE3DTransformMatrix* transformMtx);

static void TutorI3D_DrawBackground(TutorI3D* pMe, uint16 imageID);


static boolean TutorI3D_EnableModelShading(I3DModel* pIModel, boolean enable);
static boolean isSubMenuState(TutorI3D_State state);


/*===============================================================================
FUNCTIONS USED FOR STATIC APPLICATION
=============================================================================== */
#if defined(BREW_STATIC_APP)
int   TutorI3D_CreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj);
static int  TutorI3D_Load(IShell *ps, void * pHelpers, IModule ** pMod);


// Constant Data...
static const AEEAppInfo	gaiTutorI3D = {AEECLSID_TUTOR_I3D_APP, TUTORI3D_RES_FILE, IDS_TUTORI3D,
                                        0, 0,0,0, AFLAG_GAME};                      

PFNMODENTRY TutorI3D_GetModInfo(IShell * ps, AEECLSID ** ppClasses, AEEAppInfo ** pApps, 
							   uint16 * pnApps,uint16 * pwMinPriv)
{
   *pApps = (AEEAppInfo *)&gaiTutorI3D;
   *pnApps = 1;
   return((PFNMODENTRY)TutorI3D_Load);
}

static int TutorI3D_Load(IShell *ps, void * pHelpers, IModule ** pMod)
{
   return(AEEStaticMod_New((int16)(sizeof(AEEMod)),ps,pHelpers,pMod, TutorI3D_CreateInstance,NULL));
}

#endif  //BREW_STATIC_APP



/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */

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

FUNCTION: AEEClsCreateInstance

DESCRIPTION
	This function is invoked while the app is being loaded. All Modules must provide this 
	function. Ensure to retain the same name and parameters for this function.
	In here, the module must verify the ClassID and then invoke the AEEApplet_New() function
	that has been provided in AEEAppGen.c. 

   After invoking AEEApplet_New(), this function can do app specific initialization. In this
   example, a generic structure is provided so that app developers need not change app specific
   initialization section every time except for a call to IDisplay_InitAppData(). 
   This is done as follows: InitAppData() is called to initialize AppletData 
   instance. It is app developers responsibility to fill-in app data initialization 
   code of InitAppData(). App developer is also responsible to release memory 
   allocated for data contained in AppletData -- this can be done in 
   IDisplay_FreeAppData().

PROTOTYPE:
   int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)

PARAMETERS:
	clsID: [in]: Specifies the ClassID of the applet which is being loaded

	pIShell: [in]: Contains pointer to the IShell object. 

	pIModule: pin]: Contains pointer to the IModule object to the current module to which
	this app belongs

	ppObj: [out]: On return, *ppObj must point to a valid IApplet structure. Allocation
	of memory for this structure and initializing the base data members is done by AEEApplet_New().

DEPENDENCIES
  none

RETURN VALUE
  AEE_SUCCESS: If the app needs to be loaded and if AEEApplet_New() invocation was
     successful
  EFAILED: If the app does not need to be loaded or if errors occurred in 
     AEEApplet_New(). If this function returns FALSE, the app will not be loaded.

SIDE EFFECTS
  none
===========================================================================*/

#if defined(BREW_STATIC_APP)
int TutorI3D_CreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
#else
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
#endif 
{
   *ppObj = NULL;
		
   if(ClsId == AEECLSID_TUTOR_I3D_APP){
      if(AEEApplet_New(sizeof(TutorI3D), ClsId, pIShell,po,(IApplet**)ppObj,
         (AEEHANDLER)TutorI3D_HandleEvent,(PFNFREEAPPDATA) TutorI3D_FreeAppData )
         == TRUE)
      {
		 return (AEE_SUCCESS);
      }
   }
	return (EFAILED);
}




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

  FUNCTION: TutorI3DInit
  
	DESCRIPTION
	   Required Brew function. We call this function on application start up to
	   create the objects we'll need throughout the program.
	   Any program variables can be allocated and initialized here.
	  Return: TRUE on success
			   FALSE on failure
	  
	PROTOTYPE:
	  boolean TutorI3DInit(TutorI3D* pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture
		  
	DEPENDENCIES
	  none
				  
	RETURN VALUE
	  TRUE: on success
	  FALSE: any data failed
					
	SIDE EFFECTS
	  none
===========================================================================*/
boolean TutorI3DInit(TutorI3D* pMe)
{
	if(!pMe)
	{
		return FALSE;
	}
		
	ISHELL_GetDeviceInfo (pMe->a.m_pIShell, &pMe->di);

	//Get screen buffer
	if(IDISPLAY_GetDeviceBitmap(pMe->a.m_pIDisplay, &pMe->m_pIBitmapDDB) !=SUCCESS)
	{
		return FALSE;
	}

	/* initialize menus to NULL to indicate they haven't been activated yet */
	pMe->m_pMainMenu = NULL;
	pMe->m_pSubMenu = NULL;
	
	/* create font instances that will be used throughout */
	if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_FONT_BASIC10,	// size 10 font
				  (void **)&pMe->font10) != SUCCESS)
	{
		return FALSE;
	}

	if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_FONT_BASIC11,	// size 11 font
				  (void **)&pMe->font11) != SUCCESS)
	{
		return FALSE;
	}

	if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_FONT_BASIC11B, // size 11 bold font
				  (void **)&pMe->font11b) != SUCCESS)
	{
		return FALSE;
	}

	
	// save the old fonts so we can restore them on application exit
	pMe->oldFontNormal = IDISPLAY_SetFont(pMe->a.m_pIDisplay, AEE_FONT_NORMAL, pMe->font11);
	pMe->oldFontBold   = IDISPLAY_SetFont(pMe->a.m_pIDisplay, AEE_FONT_BOLD, pMe->font11b);
	
	if(!pMe->oldFontNormal || !pMe->oldFontBold )
	{
		return FALSE;
	}


	// Get the font metrics info
	pMe->charHeight = IDISPLAY_GetFontMetrics(pMe->a.m_pIDisplay, AEE_FONT_NORMAL,
											  NULL, NULL); 
	if(pMe->charHeight == EFAILED)
	{
		return FALSE;
	}

	// create an IGRAPHICS instance
	if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_GRAPHICS, 
				  (void **)&pMe->m_pGraphics) != SUCCESS)
	{
		return FALSE;
	}

	//get I3D 
	if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_3D, 
		(void **)&pMe->m_p3D) !=SUCCESS)
	{
		return FALSE;
	}

	//3D Util instance for multiply and so on
	if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_3DUTIL, 
		(void **)&pMe->m_p3DUtil)!=SUCCESS)
	{
		return FALSE;
	}


	// I3DModel instance
	if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_3DMODEL, 
		(void **)&pMe->pLadyBug)!=SUCCESS)
	{
		return FALSE;
	}
	// Load Ladybug model and required textures
	if(I3DModel_Load(pMe->pLadyBug, "ladybug.q3d") != SUCCESS)
	{
		return FALSE;
	}
	

	// I3DModel instance
	if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_3DMODEL, 
		(void **)&pMe->pModelSmoothSphere)!=SUCCESS)
	{
		return FALSE;
	}
	// Load Smooth Sphere model 
	if(I3DModel_Load(pMe->pModelSmoothSphere, "sphere.q3d") != SUCCESS)
	{
		return FALSE;
	}
	
	// I3DModel instance
	if(ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_3DMODEL, 
		(void **)&pMe->pModelField)!=SUCCESS)
	{
		return FALSE;
	}
	// Load field model 
	if(I3DModel_Load(pMe->pModelField, "field2.q3d") != SUCCESS)
	{
		return FALSE;
	}
	if(TutorI3D_LoadResTexture(pMe, pMe->pModelField, 0, IDB_TEXTURE_CHECKERS) != TRUE)
	{
		return FALSE;
	}

	
	// Initialize graphics context to the device bitmap
	if(I3D_SetDestination(pMe->m_p3D,pMe->m_pIBitmapDDB) != SUCCESS)
	{
		return FALSE;
	}
	
	// Build the axis model and calculate normals
	pMe->axisModel = Obj_BuildAxisModel(pMe);
	if(pMe->axisModel == NULL)
	{
		return FALSE;
	}
	if(Obj_GetTriNorm(pMe, pMe->axisModel) != SUCCESS)
	{
		return FALSE;
	}

	// Build the cube model
	pMe->cubeModel = Obj_BuildCubeModel(pMe);
	if(pMe->cubeModel == NULL)
	{
		return FALSE;
	}
	if(Obj_GetTriNorm(pMe, pMe->cubeModel) != SUCCESS)
	{
		return FALSE;
	}

	//Build the sphere model
	pMe->sphereModel = Obj_BuildSphereModel(pMe);
	if(pMe->sphereModel == NULL)
	{
		return FALSE;
	}
	if(Obj_GetTriNorm(pMe, pMe->sphereModel) != SUCCESS)
	{
		return FALSE;
	}
	
	// Register the 3D event notifier
	I3D_RegisterEventNotify(pMe->m_p3D, (PFN3DEVENTNOTIFY)TutorI3D_EventNotify, pMe);
		

	pMe->state = STATE_NONE;		// current state (haven't entered STATE_MAIN yet)
	pMe->nextState = STATE_NONE;	// initialize next state to NONE. 
	pMe->changeState = FALSE;
	pMe->isSuspended = FALSE;		// start in non suspended state

	return TRUE;

}


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

  FUNCTION: TutorI3D_SetDefaults
  
	DESCRIPTION
	  This function will reset data to default values (ie. view depth, 
		focal length, etc.)
	  
	PROTOTYPE:
	  static boolean TutorI3D_SetDefaults(TutorI3D *pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture
		  
	DEPENDENCIES
	  none
				  
	RETURN VALUE
	  TRUE: If all data was init successfully
	  FALSE: any data failed
					
	SIDE EFFECTS
	  none
===========================================================================*/
static boolean TutorI3D_SetDefaults(TutorI3D *pMe)
{	
	
	int x3d, y3d; 
	
	AEE3DLight light_value;
	AEE3DTexture texture;
	AEE3DPoint directionSpecular = {0,0,16384};
	AEE3DColor colorSpecular = {128,128,128,255};
	
	//Diffused
	AEE3DPoint directionDiffused = {0,0,16384};
	AEE3DColor colorDiffused = {128,128,128,255};
	
	
		
	if(!pMe)
	{
		return FALSE;
	}

	//this moves the origin to the center of the screen
	x3d = pMe->di.cxScreen/2;	
	y3d = pMe->di.cyScreen/2;	

	// Set screen mapping, view depth and focal length
	if( I3D_SetScreenMapping(pMe->m_p3D,MODEL_SIZE, MODEL_SIZE, x3d, y3d) != SUCCESS)
	{
		return FALSE;
	}
		
	if( I3D_SetViewDepth(pMe->m_p3D, 1, 3072) != SUCCESS)
	{
		return FALSE;
	}
	
	if( I3D_SetFocalLength(pMe->m_p3D, 256) != SUCCESS)
	{
		return FALSE;
	}

	if( I3D_SetRenderMode(pMe->m_p3D, AEE3D_RENDER_SMOOTH_SHADING) != SUCCESS)
	{
		return FALSE;
	}

	
	// Set Lighting mode
	if(I3D_SetLightingMode(pMe->m_p3D, AEE3D_LIGHT_MODE_DIFFUSED_COLOR_SPECULAR)
							!= SUCCESS)
	{
		return FALSE;
	}

	// set default texture
	pMe->textureID = IDB_TEXTURE_WOOD;
	texture.type = AEE3D_TEXTURE_DIFFUSED;
	texture.SamplingMode = AEE3D_TEXTURE_SAMPLING_NEAREST;
	texture.Wrap_s = AEE3D_TEXTURE_WRAP_REPEAT;
	texture.Wrap_t = AEE3D_TEXTURE_WRAP_REPEAT;
	texture.BorderColorIndex = 255;
	texture.pImage = ISHELL_LoadResBitmap(pMe->a.m_pIShell,TUTORI3D_RES_FILE,
						pMe->textureID);

	if(I3D_SetTexture(pMe->m_p3D, &texture) != SUCCESS)
	{
		return FALSE;
	}
        	
	if(texture.pImage) IBITMAP_Release(texture.pImage);

⌨️ 快捷键说明

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