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

📄 tutori3d.c

📁 用brew开发3D的例程
💻 C
📖 第 1 页 / 共 5 页
字号:
  FUNCTION: TutorI3D_InitCube
  
	DESCRIPTION
	  Initialization function for drawing the cube model. 
	  This function should be called before calling the draw function
	  for this model. 
	  	  
	PROTOTYPE:
	  void TutorI3D_InitCube(TutorI3D* pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture;
	  
		  
	DEPENDENCIES
	  none
				  
	RETURN VALUE
	  none
					
	SIDE EFFECTS
	  none
===========================================================================*/
void TutorI3D_InitCube(TutorI3D* pMe)
{
	AEE3DColor color = {255,127,100,0};
	AEE3DPoint direction = {0,0,16384};
	AEE3DLight light_value;
	
	
	if(!pMe) return;

	pMe->drawModel = MODEL_CUBE;

	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: setting light");
	}
   	
	light_value.type = AEE3D_LIGHT_SPECULAR;
	if(I3D_SetLight(pMe->m_p3D, &light_value) != SUCCESS)
	{		
		DBGPRINTF("Error: setting light");
	}

	if(I3D_SetRenderMode(pMe->m_p3D, AEE3D_RENDER_TEXTURE_REPLACE) != SUCCESS)
	{
		DBGPRINTF("Error: setting render mode");
	}

	IDISPLAY_ClearScreen (pMe->a.m_pIDisplay);
    I3D_ResetZBuf(pMe->m_p3D);
	
	
	if(I3D_SetCullingMode(pMe->m_p3D, AEE3D_CULLING_FRONT_FACING) != SUCCESS)
	{
		DBGPRINTF("Error: 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 = -10 << 16; 
	pMe->translateVector.z = 200<<16;
	if(I3DUtil_SetTranslationMatrix(pMe->m_p3DUtil,&pMe->translateVector,
									&pMe->transformMtx) != SUCCESS)
	{
		DBGPRINTF("Error: setting translation matrix");
	}

	// keep a copy of the initial transform matrix
	MEMCPY(&pMe->initalTransMtx, &pMe->transformMtx, sizeof(AEE3DTransformMatrix));
		
}

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

  FUNCTION: TutorI3D_DrawCube
  
	DESCRIPTION
	  Draw function for the cube. 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_DrawCube(TutorI3D* pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture;
	  
		  
	DEPENDENCIES
	  TutorI3D_InitCube() needs to be called before this function 
				  
	RETURN VALUE
	  none
					
	SIDE EFFECTS
	  none
===========================================================================*/
static void TutorI3D_DrawCube(TutorI3D* pMe)
{
	if(!pMe) return;

	// Clear the 3D drawing region
	IDISPLAY_EraseRect(pMe->a.m_pIDisplay, &pMe->screen3DRect);
	I3D_ResetZBuf(pMe->m_p3D);
	
	TutorI3D_DrawBackground(pMe, IDB_BACKGROUND_BRICK);

	if(!Obj_RenderObj(pMe, pMe->cubeModel, &pMe->transformMtx, FALSE))
	{
		DBGPRINTF("Error: can't render object");
	}
	if(I3D_StartFrame(pMe->m_p3D) != SUCCESS)
	{
		DBGPRINTF("Error: StartFrame() failed");	
	}
}


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

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

void TutorI3D_InitAxis(TutorI3D* pMe)
{
	
	if(!pMe) return;

	if(I3DUtil_SetIdentityMatrix(pMe->m_p3DUtil, &pMe->axisMtx) != SUCCESS)
	{
		DBGPRINTF("Error: can't set identity");
	}

	
	pMe->axisTranslation.x = 0<<16; 
	pMe->axisTranslation.y = -40<<16;
	pMe->axisTranslation.z = 600<<16;
	
	// make the axis 2 and 1/4 times larger by modifying its transform matrix
	pMe->axisMtx.m00 = 9*pMe->axisMtx.m00/4;
	pMe->axisMtx.m11 = 9*pMe->axisMtx.m11/4;
	pMe->axisMtx.m22 = 12*pMe->axisMtx.m22/4;

	if(I3DUtil_SetTranslationMatrix(pMe->m_p3DUtil, &pMe->axisTranslation, 
									&pMe->axisMtx) != SUCCESS)
	{
		DBGPRINTF("Error: setting translation matrix");
	}

	// keep a copy of the initial transform matrix
	MEMCPY(&pMe->initialAxisTransMtx, &pMe->axisMtx, sizeof(AEE3DTransformMatrix));
}


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

  FUNCTION: TutorI3D_DrawAxis
  
	DESCRIPTION
	  Draw function for the axis model. This function does not
	  call StartFrame() like the other draw functions, so 
	  a call to StartFrame must succeed a call to this for the
	  axis to be drawn. 
	  	  
	PROTOTYPE:
	  static void TutorI3D_DrawAxis(TutorI3D * pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture;
	  
		  
	DEPENDENCIES
	  TutorI3D_InitAxis() needs to be called before this function 
				  
	RETURN VALUE
	  none
					
	SIDE EFFECTS
	  none
===========================================================================*/
static void TutorI3D_DrawAxis(TutorI3D * pMe)
{
	
	AEE3DCullingType	cType;
	AEE3DRenderType		rType;
		
	if(!pMe) return;

	if(I3D_GetCullingMode(pMe->m_p3D, &cType) != SUCCESS)
	{
		DBGPRINTF("Error: Getting Culling Mode");
	}
	if(I3D_GetRenderMode(pMe->m_p3D, &rType) != SUCCESS)
	{
		DBGPRINTF("Error: Getting Render Mode");
	}

	if(I3D_SetCullingMode(pMe->m_p3D, AEE3D_CULLING_BACK_FACING) != SUCCESS)
	{
		DBGPRINTF("Error: Setting Culling Mode");
	}
	if(I3D_SetRenderMode(pMe->m_p3D, AEE3D_RENDER_SMOOTH_SHADING) != SUCCESS)
	{
		DBGPRINTF("Error: Setting Render Mode");
	}
		
	// Render the axis.	
	if(!Obj_RenderObj(pMe, pMe->axisModel, &pMe->axisMtx, FALSE))
	{
		DBGPRINTF("Error: Can't draw axis"); 
	}



	if(I3D_SetCullingMode(pMe->m_p3D, cType) != SUCCESS)
	{
		DBGPRINTF("Error: Setting Culling Mode");
	}
	if(I3D_SetRenderMode(pMe->m_p3D, rType) != SUCCESS)
	{
		DBGPRINTF("Error: Setting Render mode Mode");
	}

}



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

  FUNCTION: TutorI3D_ManipulateBuf
  
	DESCRIPTION
	   This function is called only when the current 3D frame has 
	   finished rendering (ie. once the AEE3D_EVENT_FRAME_UPDATE_DISPLAY
	   event has been received by the I3D event callback). This function 
	   updates the frame buffer. Any additional 2D drawing and text overlays 
	   can be added here. No 2D drawing actually appears on the screen 
	   until the end of this function when the Update functions are 
	   called.  

	  
	PROTOTYPE:
	  static void TutorI3D_ManipulateBuf(TutorI3D* pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture
	  		  
	DEPENDENCIES
	  none
				  
	RETURN VALUE
	  none
					
	SIDE EFFECTS
	  none
===========================================================================*/
static void TutorI3D_ManipulateBuf(TutorI3D* pMe)
{
	if(!pMe)
		return;
	
	// based on the state, call the appropriate function
	// to add the 2D graphics and text to the frame. 
	if (pMe->state == STATE_TRANSFORM_ROTATE)
	{
	
		TransformRotateManipulateBuf(pMe);
		
		// only draw the labels if axis is drawn
		if(!pMe->showHelp)
		{
			TutorI3D_DrawAxisLabels(pMe);
		}
	}
	else if (pMe->state == STATE_TRANSFORM_TRANSLATE)
	{
		TransformTranslateManipulateBuf(pMe);
		
		// only draw the labels if axis is drawn
		if(!pMe->showHelp)
		{
			TutorI3D_DrawAxisLabels(pMe);
		}
	}
	
	else if(pMe->state == STATE_PROJECTION_FOCAL)
	{                                                                                                           
		ProjectionFocalManipulateBuf(pMe);
	}
	else if(pMe->state == STATE_PROJECTION_VIEWDEPTH)
	{
		ProjectionViewDepthManipulateBuf(pMe);
	}
	else if(pMe->state == STATE_PROJECTION_SCREENMAP)
	{
		ProjectionScreenMapManipulateBuf(pMe);
	}
	else if(pMe->state == STATE_PROJECTION_CLIPRECT)
	{
		ProjectionClipRectManipulateBuf(pMe);
	}

	else if(pMe->state == STATE_LIGHTING_DIRECTION_COLOR)
	{
		LightingDirectionColorManipulateBuf(pMe);
	}
	else if(pMe->state == STATE_LIGHTING_MATERIAL)
	{
		LightingMaterialManipulateBuf(pMe);
	}

	else if(pMe->state == STATE_TEXTURES_BLENDING_RENDER)
	{
		TexturesBlendingRenderManipulateBuf(pMe);
	}
	else if(pMe->state == STATE_TEXTURES_BLENDING_ALPHA)
	{
		TexturesBlendingAlphaManipulateBuf(pMe);
	}
	else if(pMe->state == STATE_TEXTURES_BLENDING_PERSPECTIVE)
	{
		TexturesBlendingPerspectiveManipulateBuf(pMe);
	}


	// Propagate the deveice bitmap to the screen hardware.
	// After these function calls, the screen is updated
	// with any changes the user may have made. 
	IDISPLAY_Update(pMe->a.m_pIDisplay);
	IGRAPHICS_Update(pMe->m_pGraphics);
	
}


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

  FUNCTION: TutorI3D_DrawAxisLabels
  
	DESCRIPTION
	  Draws the axis labels 'X', 'Y', and 'Z' for the axis model, 
	  by first computing where the tip of the x,y,z axis currently are and then
	  doing a 2D text draw at those points. 
	  
	PROTOTYPE:
	  static void TutorI3D_DrawAxisLabels(TutorI3D* pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture
	  		  
	DEPENDENCIES
	  none
				  
	RETURN VALUE
	  none
					
	SIDE EFFECTS
	  none
===========================================================================*/

static void TutorI3D_DrawAxisLabels(TutorI3D* pMe)
{

	AECHAR szBuf[5];
	AEERect currentClipRectDisplay;
	AEE3DPoint axisPoint2D;

	if(!pMe)
		return;

	// don't allow labels to be drawn outside of the
	// 3D draw area (ie. don't overwrite the menu at the bottom)
	IDISPLAY_GetClipRect(pMe->a.m_pIDisplay, &currentClipRectDisplay);
	IDISPLAY_SetClipRect(pMe->a.m_pIDisplay, &pMe->screenAPI3DRect);

	// get the new location of the axis arrow tips and 
	// write the axis labels out if successful

	if(Get2DPointFrom3DPoint(pMe,  &pMe->xarrow, &pMe->axisMtx, 
							 &pMe->axisTranslation, &axisPoint2D) == TRUE)
	{
		STR_TO_WSTR ("X", szBuf, sizeof(szBuf));
		IDISPLAY_DrawText(pMe->a.m_pIDisplay, AEE_FONT_BOLD, szBuf,
						  -1, axisPoint2D.x + 2, axisPoint2D.y - 2, 
						  NULL, IDF_TEXT_TRANSPARENT);

	}

	if(Get2DPointFrom3DPoint(pMe,  &pMe->yarrow, &pMe->axisMtx, 
							 &pMe->axisTranslation, &axisPoint2D) == TRUE)
	{
		STR_TO_WSTR ("Y", szBuf, sizeof(szBuf));
		IDISPLAY_DrawText(pMe->a.m_pIDisplay, AEE_FONT_BOLD, szBuf,
						  -1, axisPoint2D.x + 2, axisPoint2D.y - 2,
						  NULL, IDF_TEXT_TRANSPARENT);
	}


	if(Get2DPointFrom3DPoint(pMe,  &pMe->zarrow, &pMe->axisMtx, 
							 &pMe->axisTranslation, &axisPoint2D) == TRUE)
	{
		STR_TO_WSTR ("Z", szBuf, sizeof(szBuf));
		IDISPLAY_DrawText(pMe->a.m_pIDisplay, AEE_FONT_BOLD, szBuf,
						  -1, axisPoint2D.x + 2, axisPoint2D.y - 2, 
						  NULL, IDF_TEXT_TRANSPARENT);
	}

	IDISPLAY_SetClipRect(pMe->a.m_pIDisplay, &currentClipRectDisplay);

}


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

  FUNCTION: TutorI3D_DrawIntroScreen
  
	DESCRIPTION
	   Draws the introduction screen. This is the first
	   screen the user sees. 
	  
	PROTOTYPE:
	  static boolean TutorI3D_DrawIntroScreen(TutorI3D* pMe)
		
	PARAMETERS:
	  pMe: [in]: Pointer to TutorI3D sturcture
	  		  
	DEPENDENCIES
	  none
				  
	RETURN VALUE
	  TRUE:	intro screen drawn
	  FALSE: failed
					
	SIDE EFFECTS
	  none
===========================================================================*/
static boolean TutorI3D_DrawIntroScreen(TutorI3D* pMe)
{
	IBitmap* image;
	uint32 time1;
	
	if(!pMe)
	{
		return FALSE;
	}


	// Draw the Background 
	image = ISHELL_LoadR

⌨️ 快捷键说明

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