You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
8.3 KiB

5 years ago
  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include "Raster.h"
  4. #include "CELLTimestamp.hpp"
  5. #include "CELLCamera.hpp"
  6. CELL::CELLCamera g_camera;
  7. CELL::int2 g_rButtonDown;
  8. bool g_rButtonFlag = false;
  9. CELL::int2 g_lButtonDown;
  10. bool g_lButtonFlag = false;
  11. CELL::float3 calcIntersectPoint( CELL::Ray& ray )
  12. {
  13. CELL::float3 pos = ray.getOrigin();
  14. float tm = abs((pos.y) / ray.getDirection().y);
  15. CELL::float3 target = ray.getPoint(tm);
  16. return CELL::float3(target.x,0,target.z);
  17. }
  18. LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  19. {
  20. switch(msg)
  21. {
  22. case WM_SIZE:
  23. break;
  24. case WM_LBUTTONDOWN:
  25. {
  26. g_lButtonDown.x = LOWORD(lParam);
  27. g_lButtonDown.y = HIWORD(lParam);
  28. g_lButtonFlag = true;
  29. }
  30. break;
  31. case WM_LBUTTONUP:
  32. {
  33. g_lButtonFlag = false;
  34. }
  35. break;
  36. case WM_MOUSEMOVE:
  37. {
  38. int x = LOWORD(lParam);
  39. int y = HIWORD(lParam);
  40. if (g_rButtonFlag)
  41. {
  42. int offsetX = x - g_rButtonDown.x;
  43. int offsetY = y - g_rButtonDown.y;
  44. g_camera.rotateViewY(offsetX);
  45. g_camera.rotateViewX(offsetY);
  46. g_rButtonDown.x = x;
  47. g_rButtonDown.y = y;
  48. }
  49. if (g_lButtonFlag)
  50. {
  51. /**
  52. * ȼһغ͵ǰı
  53. */
  54. CELL::Ray ray0 = g_camera.createRayFromScreen(x,y);
  55. CELL::Ray ray1 = g_camera.createRayFromScreen(g_lButtonDown.x,g_lButtonDown.y);
  56. CELL::float3 pos0 = calcIntersectPoint(ray0);
  57. CELL::float3 pos1 = calcIntersectPoint(ray1);
  58. CELL::float3 offset = pos1 - pos0;
  59. g_lButtonDown = CELL::int2(x,y);
  60. CELL::float3 newEye = g_camera.getEye() + offset;
  61. CELL::float3 newTgt = g_camera.getTarget() + offset;
  62. g_camera.setEye(newEye);
  63. g_camera.setTarget(newTgt);
  64. g_camera.update();
  65. }
  66. }
  67. break;
  68. case WM_RBUTTONDOWN:
  69. {
  70. g_rButtonDown.x = LOWORD(lParam);
  71. g_rButtonDown.y = HIWORD(lParam);
  72. g_rButtonFlag = true;
  73. }
  74. break;
  75. case WM_RBUTTONUP:
  76. {
  77. g_rButtonFlag = false;
  78. }
  79. break;
  80. case WM_MOUSEWHEEL:
  81. {
  82. short delta = GET_WHEEL_DELTA_WPARAM(wParam);
  83. if (delta > 0)
  84. {
  85. float len = CELL::length(g_camera._eye-g_camera._target);
  86. len *= 1.2f;
  87. g_camera._eye = g_camera._target - g_camera._dir * len;
  88. }
  89. else
  90. {
  91. float len = CELL::length(g_camera._eye-g_camera._target);
  92. len *= 0.9f;
  93. g_camera._eye = g_camera._target - g_camera._dir * len;
  94. }
  95. g_camera.update();
  96. }
  97. break;
  98. case WM_CLOSE:
  99. case WM_DESTROY:
  100. PostQuitMessage(0);
  101. break;
  102. default:
  103. break;
  104. }
  105. return DefWindowProc( hWnd, msg, wParam, lParam );
  106. }
  107. void getResourcePath(HINSTANCE hInstance,char pPath[1024])
  108. {
  109. char szPathName[1024];
  110. char szDriver[64];
  111. char szPath[1024];
  112. GetModuleFileNameA(hInstance,szPathName,sizeof(szPathName));
  113. _splitpath( szPathName, szDriver, szPath, 0, 0 );
  114. sprintf(pPath,"%s%s",szDriver,szPath);
  115. }
  116. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
  117. {
  118. // 1 ע�ᴰ����
  119. ::WNDCLASSEXA winClass;
  120. winClass.lpszClassName = "Raster";
  121. winClass.cbSize = sizeof(::WNDCLASSEX);
  122. winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
  123. winClass.lpfnWndProc = windowProc;
  124. winClass.hInstance = hInstance;
  125. winClass.hIcon = 0;
  126. winClass.hIconSm = 0;
  127. winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  128. winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
  129. winClass.lpszMenuName = NULL;
  130. winClass.cbClsExtra = 0;
  131. winClass.cbWndExtra = 0;
  132. RegisterClassExA(&winClass);
  133. // 2 ��������
  134. HWND hWnd = CreateWindowExA(
  135. NULL,
  136. "Raster",
  137. "Raster",
  138. WS_OVERLAPPEDWINDOW,
  139. 0,
  140. 0,
  141. 800,
  142. 600,
  143. 0,
  144. 0,
  145. hInstance,
  146. 0
  147. );
  148. UpdateWindow( hWnd );
  149. ShowWindow(hWnd,SW_SHOW);
  150. RECT rt = {0};
  151. GetClientRect(hWnd,&rt);
  152. int width = rt.right - rt.left;
  153. int height = rt.bottom - rt.top;
  154. void* buffer = 0;
  155. HDC hDC = GetDC(hWnd);
  156. HDC hMem = ::CreateCompatibleDC(hDC);
  157. BITMAPINFO bmpInfor;
  158. bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  159. bmpInfor.bmiHeader.biWidth = width;
  160. bmpInfor.bmiHeader.biHeight = -height;
  161. bmpInfor.bmiHeader.biPlanes = 1;
  162. bmpInfor.bmiHeader.biBitCount = 32;
  163. bmpInfor.bmiHeader.biCompression = BI_RGB;
  164. bmpInfor.bmiHeader.biSizeImage = 0;
  165. bmpInfor.bmiHeader.biXPelsPerMeter = 0;
  166. bmpInfor.bmiHeader.biYPelsPerMeter = 0;
  167. bmpInfor.bmiHeader.biClrUsed = 0;
  168. bmpInfor.bmiHeader.biClrImportant = 0;
  169. HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
  170. SelectObject(hMem,hBmp);
  171. char szPath[1024];
  172. getResourcePath(0,szPath);
  173. char szImage[1024];
  174. sprintf(szImage,"%s/image/bg.png",szPath);
  175. CELL::Image* image = CELL::Image::loadFromFile(szImage);
  176. sprintf(szImage,"%s/image/1.jpg",szPath);
  177. CELL::Image* image1 = CELL::Image::loadFromFile(szImage);
  178. CELL::Raster raster(width,height,buffer);
  179. g_camera.setViewSize(width,height);
  180. g_camera.perspective(60,(float)(width)/(float)(height),0.1,10000);
  181. g_camera.update();
  182. raster.setViewPort(0,0,width,height);
  183. raster.setPerspective(60,(float)(width)/(float)(height),0.1,10000);
  184. struct Vertex
  185. {
  186. float x,y,z;
  187. float u,v;
  188. CELL::Rgba color;
  189. };
  190. MSG msg = {0};
  191. while(true)
  192. {
  193. if (msg.message == WM_DESTROY
  194. ||msg.message == WM_CLOSE
  195. ||msg.message == WM_QUIT)
  196. {
  197. break;
  198. }
  199. if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  200. {
  201. TranslateMessage( &msg );
  202. DispatchMessage( &msg );
  203. }
  204. raster.clear();
  205. CELL::CELLTimestamp tms;
  206. tms.update();
  207. double mis = tms.getElapsedTimeInMicroSec();
  208. char szBuf[128];
  209. sprintf(szBuf,"%f ",mis);
  210. raster.setView(g_camera.getView());
  211. Vertex vertexs[] =
  212. {
  213. {-1, 0, 1, 0, 0, CELL::Rgba(255,0,0)},
  214. { 1, 0, 1, 1, 0, CELL::Rgba(0,255,0)},
  215. { 1, 0, -1, 1, 1, CELL::Rgba(0,0,255)},
  216. {-1, 0, 1, 0, 0, CELL::Rgba(255,0,0)},
  217. { 1, 0, -1, 1, 1, CELL::Rgba(0,0,255)},
  218. {-1, 0, -1, 0, 1, CELL::Rgba(255,255)},
  219. };
  220. for (int i = 0 ;i < 6 ; ++ i)
  221. {
  222. vertexs[i].x *= 100;
  223. vertexs[i].z *= 100;
  224. vertexs[i].u *= 10;
  225. vertexs[i].v *= 10;
  226. }
  227. image1->setWrapType(0);
  228. raster.bindTexture(image1);
  229. raster.vertexPointer(2,CELL::DT_FLOAT, sizeof(Vertex),&vertexs[0].x);
  230. raster.textureCoordPointer(2,CELL::DT_FLOAT,sizeof(Vertex),&vertexs[0].u);
  231. raster.colorPointer(4,CELL::DT_BYTE, sizeof(Vertex),&vertexs[0].color);
  232. raster.drawArrays(CELL::DM_TRIANGES,0,6);
  233. //TextOut(hMem,10,10,szBuf,strlen(szBuf));
  234. BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
  235. }
  236. delete image;
  237. delete image1;
  238. return 0;
  239. }