借鉴JGE的HGEDistortionMesh

  1. HGEDistortionMesh例子应用了HGE包中的内容 用来对图片进行扭曲变形处理   
  2. #include <stdio.h>   
  3.   
  4.     
  5. #include <JGE.h>   
  6. #include <JRenderer.h>   
  7. #include <JLBFont.h>   
  8.   
  9. #include <hge/hgedistort.h>   
  10. #include <hge/hgefont.h>   
  11.   
  12. #include "GameApp.h"   
  13.   
  14.   
  15. //-------------------------------------------------------------------------------------   
  16. // Constructor. Variables can be initialized here.   
  17. //   
  18. //-------------------------------------------------------------------------------------   
  19. GameApp::GameApp()   
  20. {   
  21. mTex = NULL;//人物图片Tex   
  22. mDistortionMesh = NULL;//扭曲工具类   
  23. mFont = NULL;//字体   
  24.   
  25.    mRows=8;//扭曲参数   
  26.    mCols=8;//应该是粒度或强度   
  27.    mCellw=(float)(TEXTURE_WIDTH/(mCols-1));//均匀区分图片为N个Cell cell宽度   
  28.    mCellh=(float)(TEXTURE_HEIGHT/(mRows-1));//Cell高度   
  29.    mMeshx=(SCREEN_WIDTH_F-TEXTURE_WIDTH)/2;//扭曲后的图片位置   
  30.    mMeshy=(SCREEN_HEIGHT_F-TEXTURE_HEIGHT)/2;//在屏幕中央   
  31. }   
  32.   
  33.   
  34. //-------------------------------------------------------------------------------------   
  35. // Destructor.   
  36. //   
  37. //-------------------------------------------------------------------------------------   
  38. GameApp::~GameApp()   
  39. {   
  40.   
  41. }   
  42.   
  43.   
  44. //-------------------------------------------------------------------------------------   
  45. // This is the init callback function. You should load and create your in-game    
  46. // resources here.   
  47. //    
  48. //-------------------------------------------------------------------------------------   
  49. void GameApp::Create()   
  50. {   
  51.   
  52. JRenderer* renderer = JRenderer::GetInstance();     
  53. mTex=renderer->LoadTexture("texture.jpg");//加载人物图片   
  54.   
  55. // Create a distortion mesh   
  56. mDistortionMesh=new hgeDistortionMesh(mCols, mRows);//设置扭曲的分块8*8的分块   
  57. mDistortionMesh->SetTexture(mTex);//用图片来扭曲   
  58. mDistortionMesh->SetTextureRect(0,0,TEXTURE_WIDTH,TEXTURE_HEIGHT);//设置要扭曲的大小   
  59. mDistortionMesh->Clear(ARGB(0xFF,0xFF,0xFF,0xFF));//清楚扭曲区   
  60.   
  61. // Load a font   
  62. mFont=new hgeFont("font1.fnt");//加载显示文字的字体   
  63. }   
  64.   
  65.   
  66. //-------------------------------------------------------------------------------------   
  67. // This is the clean up callback function. You should delete all your in-game    
  68. // resources, for example texture and quads, here.   
  69. //    
  70. //-------------------------------------------------------------------------------------   
  71. void GameApp::Destroy()   
  72. {   
  73.   
  74. SAFE_DELETE(mTex);   
  75.   
  76. SAFE_DELETE(mDistortionMesh);   
  77.   
  78. SAFE_DELETE(mFont);   
  79.   
  80. }   
  81.   
  82.   
  83. //-------------------------------------------------------------------------------------   
  84. // This is the update callback function and is called at each update frame   
  85. // before rendering. You should update the game logic here.   
  86. //   
  87. //-------------------------------------------------------------------------------------   
  88. void GameApp::Update()   
  89. {   
  90.   
  91. JGE* engine = JGE::GetInstance();   
  92.   
  93. // do a screen shot when the TRIANGLE button is pressed   
  94. if (engine->GetButtonClick(PSP_CTRL_TRIANGLE))     
  95. {   
  96.    char s[80];   
  97.   
  98.    // save screen shot to root of Memory Stick    
  99.    sprintf(s, "ms0:/screenshot.png");       
  100.    JRenderer::GetInstance()->ScreenShot(s);   
  101. }   
  102.   
  103. // exit when the CROSS button is pressed   
  104. if (engine->GetButtonClick(PSP_CTRL_CROSS))    
  105. {   
  106.    engine->End();   
  107.    return;   
  108. }   
  109.   
  110. float dt = engine->GetDelta();   // Get time elapsed since last update.   
  111.   
  112. static float t=0.0f;//函数中的静态变量 第一次进入的时候初始化 再进入的时候就沿用原来的值了   
  113. static int trans=2;//这点和Java有点不一样   
  114.   
  115. int i, j, col;   
  116. float r, a, dx, dy;   
  117.   
  118. t+=dt;//当前时间 越加越多阿= =????   
  119. //按圆按钮 每次改变一个扭曲效果并清屏   
  120. if (engine->GetButtonClick(PSP_CTRL_CIRCLE))    
  121. {   
  122.    if(++trans > 2)    
  123.     trans=0;   
  124.    mDistortionMesh->Clear(ARGB(0xFF,0xFF,0xFF,0xFF));   
  125.   
  126. }   
  127.   
  128. // Calculate new displacements and coloring for one of the three effects   
  129. //trans3种不同的扭曲效果   
  130. switch(trans)   
  131. {   
  132.    case 0:    
  133.     //将整个图片划分成各个Cell 并单独扭曲   
  134.     for(i=1;i<mRows-1;i++)   
  135.     {   
  136.      for(j=1;j<mCols-1;j++)   
  137.      {   
  138.       //参数分别为(列,行,Cell中的x坐标,Cell中的y坐标,扭曲效果,根据Cell中心扭曲即HGEDISP_NODE)   
  139.       mDistortionMesh->SetDisplacement(j,i,cosf(t*10+(i+j)/2)*5,sinf(t*10+(i+j)/2)*5,HGEDISP_NODE);   
  140.       //cosf(t*10+(i+j)/2)*5算式根据时间t与Cos余弦函数的乘积 动态改变扭曲的X方向粒度 在一个0~5中变化   
  141.       //sinf(t*10+(i+j)/2)*5根据sin函数 改变扭曲y方向的粒度 在0~5之间变化   
  142.       //每个Cell是32*32的大小 整个扭曲的图片时256*256的 分为8*8个Cell   
  143.      }   
  144.     }   
  145.     //mDistortionMesh->SetDisplacement(2,2,cosf(t*10+(2+2)/2)*8,sinf(t*10+(2+2)/2)*8,HGEDISP_NODE);   
  146.     //mDistortionMesh->SetDisplacement(1,1,cosf(t*10+(1+1)/2)*3,sinf(t*10+(1+1)/2)*3,HGEDISP_NODE);   
  147.     break;   
  148.   
  149.    case 1:    
  150.     for(i=0;i<mRows;i++)   
  151.     {   
  152.      for(j=1;j<mCols-1;j++)   
  153.      {   
  154.       //变形只在x方向上扭曲 并且只根据j值变化 y方向上为0 所以x方向上是0~15的扭曲粒度 变形很严重   
  155.       //依然是从左上角的cell到右下角的cell 效果类似波浪 如果再加上i的值 变形会在上下方向上不一致   
  156.       mDistortionMesh->SetDisplacement(j,i,cosf(t*5+(j+i)/2)*15,0,HGEDISP_NODE);   
  157.       col=int((cosf(t*5+(i+j)/2)+1)*35);   
  158.   
  159.       // colour doesn't work the same as the original HGE version so the following line is commented out.   
  160.       // dis->SetColor(j,i,ARGB(0xFF,col,col,col));   
  161.      }   
  162.     }   
  163.     break;   
  164.    //这个变形函数没看明白   
  165.    case 2:    
  166.     for(i=0;i<mRows;i++)   
  167.     {   
  168.      for(j=0;j<mCols;j++)   
  169.      {   
  170.       r=sqrtf(powf(j-(float)mCols/2,2)+powf(i-(float)mRows/2,2));   
  171.       a=r*cosf(t*2)*0.1f;   
  172.       dx=sinf(a)*(i*mCellh-TEXTURE_HEIGHT/2)+cosf(a)*(j*mCellw-TEXTURE_WIDTH/2);   
  173.       dy=cosf(a)*(i*mCellh-TEXTURE_HEIGHT/2)-sinf(a)*(j*mCellw-TEXTURE_WIDTH/2);   
  174.       mDistortionMesh->SetDisplacement(j,i,dx,dy,HGEDISP_CENTER);   
  175.       col=int((cos(r+t*4)+1)*40);   
  176.   
  177.       // colour doesn't work the same as the original HGE version so the following line is commented out.   
  178.       // dis->SetColor(j,i,ARGB(0xFF, col,(col/2), 0));   
  179.      }   
  180.     }   
  181.     break;   
  182. }   
  183.   
  184.   
  185. }   
  186.   
  187.   
  188. //-------------------------------------------------------------------------------------   
  189. // All rendering operations should be done in Render() only.   
  190. //    
  191. //-------------------------------------------------------------------------------------   
  192. void GameApp::Render()   
  193. {   
  194.   
  195. // get JRenderer instance   
  196. JRenderer* renderer = JRenderer::GetInstance();     
  197.   
  198. // clear screen to black   
  199. renderer->ClearScreen(ARGB(0,0,0,0));   
  200.   
  201. // render the mesh   
  202.    mDistortionMesh->Render(mMeshx, mMeshy);//绘制扭曲的图片    
  203.   
  204.    mFont->printf(5, 5, HGETEXT_LEFT, "Press/nCIRCLE!");//显示文字   
  205.   
  206. }   
  207.   
  208.   
  209. //-------------------------------------------------------------------------------------   
  210. // This function is called when the system wants to pause the game. You can set a flag   
  211. // here to stop the update loop and audio playback.   
  212. //   
  213. //-------------------------------------------------------------------------------------   
  214. void GameApp::Pause()   
  215. {   
  216.   
  217. }   
  218.   
  219.   
  220. //-------------------------------------------------------------------------------------   
  221. // This function is called when the game returns from the pause state.   
  222. //   
  223. //-------------------------------------------------------------------------------------   
  224. void GameApp::Resume()   
  225. {   
  226.   
  227. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值