الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

لم يعمل البرنامج

بدأه simsimzone في 7 يناير 2009 · 4 رد · 1,302 مشاهدة · في DirectX
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

اخوتي لا أعرف لماذا لم يعمل البرنامج التالي, وهو لرسم اسطوانة مفرغة, فقط كانت ترسم نصف الأسطوانة: علما بأن اعداد الأسطوانة كان في الدالة Setup()

  1.  
  2.  
  3.  
  4.  
  5.  
  6. [size="2"]#include "Utility.h"
  7. IDirect3DDevice9* Device = 0;
  8.  
  9. const int Width = 640;
  10.  
  11. const int Height = 480;
  12.  
  13. const float h = 1.0f;
  14.  
  15. const float r = 1.0f;
  16.  
  17. const int N = 10;
  18.  
  19. IDirect3DVertexBuffer9* cylinder = 0;
  20.  
  21. struct Vertex
  22.  
  23. {
  24.  
  25. Vertex(){}
  26.  
  27. Vertex(float x, float y, float z, float nx, float ny, float nz)
  28.  
  29. {
  30.  
  31. _x = x;
  32.  
  33. _y = y;
  34.  
  35. _z = z;
  36.  
  37. _nx = nx;
  38.  
  39. _ny = ny;
  40.  
  41. _nz = nz;
  42.  
  43. }
  44.  
  45. float _x, _y, _z;
  46.  
  47. float _nx, _ny, _nz;
  48.  
  49. static const DWORD FVF;
  50.  
  51. };
  52.  
  53. const DWORD Vertex::FVF = D3DFVF_XYZ|D3DFVF_NORMAL;
  54.  
  55.  
  56.  
  57. bool Setup()
  58.  
  59. {
  60.  
  61.  
  62.  
  63. Device->CreateVertexBuffer(N*6*sizeof(Vertex) ,D3DUSAGE_WRITEONLY
  64.  
  65. , Vertex::FVF, D3DPOOL_MANAGED, &cylinder,0);
  66.  
  67. Vertex* v;
  68.  
  69. cylinder->Lock(0,0,(void**)&v, 0);
  70.  
  71. int i = 0;
  72.  
  73. float dt = 2.0f*D3DX_PI/N;
  74.  
  75. float eps = 0.001f;
  76.  
  77. for(float t = 0;t+eps<dt*N; t+=dt)
  78.  
  79. {
  80.  
  81. v[i+0] = Vertex(r*cos(t) , 0 , r*sin(t) , cos(t) , 0, sin(t));
  82.  
  83. v[i+1] = Vertex(r*cos(t+dt), h , r*sin(t+dt), cos(t+dt), 0, sin(t+dt));
  84.  
  85. v[i+2] = Vertex(r*cos(t+dt), 0 , r*sin(t+dt), cos(t+dt), 0, sin(t+dt));
  86.  
  87. v[i+3] = Vertex(r*cos(t) , 0 , r*sin(t) , cos(t) , 0, sin(t));
  88.  
  89. v[i+4] = Vertex(r*cos(t) , h , r*sin(t) , cos(t) , 0, sin(t));
  90.  
  91. v[i+5] = Vertex(r*cos(t+dt), h , r*sin(t+dt), cos(t+dt), 0, sin(t+dt));
  92.  
  93. i+=6;
  94.  
  95. }
  96.  
  97. cylinder->Unlock();
  98.  
  99.  
  100.  
  101. D3DXVECTOR3 position(0.0f, 5.0f, 0.0f);
  102.  
  103. D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
  104.  
  105. D3DXVECTOR3 up(0.0f, 0.0f, 1.0f);
  106.  
  107. D3DXMATRIX V;
  108.  
  109. D3DXMatrixLookAtLH(&V, &position, &target, &up);
  110.  
  111. Device->SetTransform(D3DTS_VIEW, &V);
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. D3DXMATRIX proj;
  120.  
  121. D3DXMatrixPerspectiveFovLH(
  122.  
  123. &proj,
  124.  
  125. D3DX_PI * 0.5f, // 90 - degree
  126.  
  127. (float)Width / (float)Height,
  128.  
  129. 1.0f,
  130.  
  131. 1000.0f);
  132.  
  133. Device->SetTransform(D3DTS_PROJECTION, &proj);
  134.  
  135.  
  136.  
  137. Device->SetMaterial(&d3d::WHITE_MTRL);
  138.  
  139. D3DXCOLOR c = d3d::YELLOW;
  140.  
  141. Device->SetLight(0, &d3d::InitDirectionalLight(&D3DXVECTOR3(1,0,0),&c));
  142.  
  143. Device->LightEnable(0, true);
  144.  
  145. Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
  146.  
  147. Device->SetRenderState(D3DRS_SPECULARENABLE, true);
  148.  
  149.  
  150.  
  151. Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
  152.  
  153. return true;
  154.  
  155. }
  156.  
  157. void Cleanup()
  158.  
  159. {
  160.  
  161. d3d::Release<IDirect3DVertexBuffer9*>(cylinder);
  162.  
  163. }
  164.  
  165. bool Display(float timeDelta)
  166.  
  167. {
  168.  
  169. if( Device )
  170.  
  171. {
  172.  
  173. //static float y = 0;
  174.  
  175. //D3DXMATRIX mat;
  176.  
  177. //D3DXMatrixTranslation(&mat,3*cos(y),0.0f, 3*sin(y));
  178.  
  179. //y += timeDelta;
  180.  
  181. //if(y>2*D3DX_PI)
  182.  
  183. // y = 0;
  184.  
  185. //Device->SetTransform(D3DTS_WORLD, &mat);
  186.  
  187.  
  188.  
  189. Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
  190.  
  191. Device->BeginScene();
  192.  
  193. Device->SetStreamSource(0, cylinder, 0, sizeof(Vertex));
  194.  
  195. Device->SetFVF(Vertex::FVF);
  196.  
  197. Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, N*2);
  198.  
  199. Device->EndScene();
  200.  
  201. Device->Present(0, 0, 0, 0);
  202.  
  203. }
  204.  
  205. return true;
  206.  
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  216.  
  217. {
  218.  
  219. switch( msg )
  220.  
  221. {
  222.  
  223. case WM_DESTROY:
  224.  
  225. ::PostQuitMessage(0);
  226.  
  227. break;
  228.  
  229.  
  230.  
  231. case WM_KEYDOWN:
  232.  
  233. if( wParam == VK_ESCAPE )
  234.  
  235. ::DestroyWindow(hwnd);
  236.  
  237. break;
  238.  
  239. }
  240.  
  241. return ::DefWindowProc(hwnd, msg, wParam, lParam);
  242.  
  243. }
  244.  
  245.  
  246.  
  247.  
  248.  
  249. int WINAPI WinMain(HINSTANCE hinstance,
  250.  
  251. HINSTANCE prevInstance,
  252.  
  253. PSTR cmdLine,
  254.  
  255. int showCmd)
  256.  
  257. {
  258.  
  259. if(!d3d::InitD3D(hinstance,
  260.  
  261. Width, Height, true, D3DDEVTYPE_HAL, &Device))
  262.  
  263. {
  264.  
  265. ::MessageBox(0, L"InitD3D() - FAILED", 0, 0);
  266.  
  267. return 0;
  268.  
  269. }
  270.  
  271.  
  272.  
  273. if(!Setup())
  274.  
  275. {
  276.  
  277. ::MessageBox(0, L"Setup() - FAILED", 0, 0);
  278.  
  279. return 0;
  280.  
  281. }
  282.  
  283. d3d::EnterMsgLoop( Display );
  284.  
  285. Cleanup();
  286.  
  287. Device->Release();
  288.  
  289. return 0;
  290.  
  291. }
  292.  
  293. [/size]
  294.  
  295.  
  296.  
  297.  
  298.  

#2

الاوجه الخلفية لاتظهر لان اتجاه الرسم ليس مقابل الكاميرا

llback.jpg

اشهد ان لا إله إلا الله وان محمدا ً رسول الله

#3

وكيف أحل المشكلة؟؟؟

طبعا بالنسبة للأسطوانة كان ممكن انشاؤها بال TRIANGLESTRIP

#5
اقتباس
وكيف أحل المشكلة؟؟؟

قم برسم الاوجه مرة ثانية مع تغيير ترتيب الرسم , اي عمل اوجه خلفية واماميه .

llback.jpg

اشهد ان لا إله إلا الله وان محمدا ً رسول الله

مواضيع مشابهة