c++ - DirectX Transforms not working -
i have begun directx c++ tutorial , drew triangle, whenever try transform it, matrix, never works although followed tutorial step step , looked in msdn tutorial, didn't figure out problem, here's full code:
main.cpp:
#include <windows.h> #include <d3d9.h> #include <d3dx9.h> #include "define.h" lresult winapi wndproc( hwnd, uint, wparam, lparam ); int winapi winmain( hinstance hinst, hinstance hprevinst, lpstr lpcmdline, int ncmdshow ) { hicon icon; icon = loadicon(hinst, makeintresource(idi_app_icon)); wndclass wc; wc.cbclsextra = 0; wc.cbwndextra = 0; wc.hbrbackground = (hbrush)null; wc.hcursor = loadcursor(hinst, idc_arrow); wc.hicon = icon; wc.hinstance = hinst; wc.lpfnwndproc = wndproc; wc.lpszclassname = "window class"; wc.lpszmenuname = null; wc.style = cs_vredraw | cs_hredraw; registerclass( &wc ); hwnd hwnd = createwindow("window class", "d3d tutorial 01: createdevice", ws_overlappedwindow, 0, 0, 1440, 900, null, null, hinst, null ); showwindow( hwnd, sw_normal); initd3d(hwnd); initgraphics(); render(); msg msg; // check see if messages waiting in queue while(getmessage(&msg, null, 0, 0)) { translatemessage(&msg); dispatchmessage(&msg); } return 0; } lresult winapi wndproc( hwnd hwnd, uint msg, wparam wparam, lparam lparam ) { switch( msg ) { case wm_keydown: { if(wparam == 0x51) postquitmessage(1); return 0; break; } case wm_destroy: { postquitmessage(0); cleard3d(); return 1; break; } default: { return defwindowproc( hwnd, msg, wparam, lparam ); } } } void initd3d(hwnd hwnd) { d3d = direct3dcreate9(d3d_sdk_version); d3dpresent_parameters d3dparams; zeromemory(&d3dparams, sizeof(d3dpresent_parameters)); d3dparams.hdevicewindow = hwnd; d3dparams.windowed = false; d3dparams.backbufferformat = d3dfmt_a8r8g8b8; d3dparams.backbufferheight = 900; d3dparams.backbufferwidth = 1440; d3dparams.swapeffect = d3dswapeffect_flip; d3d->createdevice(d3dadapter_default, d3ddevtype_hal, hwnd, d3dcreate_hardware_vertexprocessing | d3dcreate_nowindowchanges, &d3dparams, &d3ddevice); d3ddevice->setrenderstate(d3drs_lighting, false); } void render() { // clear window deep blue d3ddevice->clear(null, 0, d3dclear_target, d3dcolor_argb(255, 20, 40, 100), 1.0f, 0); d3ddevice->beginscene(); // begins 3d scene // 3d rendering on buffer here d3ddevice->setfvf(fvfcode); d3dxmatrixidentity(&matrix); d3dxmatrixtranslation(&matrix, 900, 0, 0); d3ddevice->settransform(d3dts_world, &matrix); d3dxmatrix out; d3dxvector3 eye(2,3,3); d3dxvector3 lat(0,0,0); d3dxvector3 up(0,1,0); d3dxmatrixlookatlh(&out, &eye, &lat, &up); d3ddevice->settransform(d3dts_view, &out); d3dxmatrix pro; d3dxmatrixperspectivefovlh(&pro, d3dxtoradian(45), // horizontal field of view (float)1440 / (float)900, // aspect ratio 1.0f, // near view-plane 100.0f); // far view-plane d3ddevice->settransform(d3dts_projection, &pro); d3ddevice->setstreamsource(0, v_buffer, 0, sizeof(vertex)); d3ddevice->drawprimitive(d3dpt_trianglelist, 0, 1); d3ddevice->endscene(); // ends 3d scene d3ddevice->present(null, null, null, null); // displays created frame on screen } void initgraphics() { vertex v [] = {{320.0f, 50.0f, 1.0f, 1.0f, d3dcolor_xrgb(0, 0, 255)}, {520.0f, 400.0f, 1.0f, 1.0f, d3dcolor_xrgb(0, 255, 0)}, {120.0f, 400.0f, 1.0f, 1.0f, d3dcolor_xrgb(255, 0, 0)}, }; d3ddevice->createvertexbuffer( 3*sizeof(vertex), 0, fvfcode, d3dpool_default, &v_buffer, null ); void* pvoid; v_buffer->lock(0, 0, (void**)&pvoid, 0); copymemory(pvoid, v, sizeof(v)); v_buffer->unlock(); }
define.h:
#ifndef define_h_included #define define_h_included #define idi_app_icon 1 #define fvfcode (d3dfvf_diffuse | d3dfvf_xyzrhw) //all declarations , prototypes: //dx functions: void initd3d (hwnd); void cleard3d (void); void render(void); void initgraphics(void); //constants: lpdirect3d9 d3d; d3dxmatrix matrix; lpdirect3dvertexbuffer9 v_buffer = null; lpdirect3ddevice9 d3ddevice; //vertices structure: struct vertex { float x, y,z, rhw; dword color; }; void cleard3d () { d3d->release(); d3ddevice->release(); v_buffer->release(); } #endif // define_h_included
and have resource file imported app icon, know code long , take time read, sorry can't find other solution this. have question, have set 3 matrices, world view , projection, able make effect changing triangles place using world matrix, have set other 2 matrices, change or world?? lastly, replier much, fr effort , time.
edit
the relevant parts/ parts related transforms whole code -coz long- are:
d3ddevice->beginscene(); // begins 3d scene // 3d rendering on buffer here d3ddevice->setfvf(fvfcode); d3dxmatrix matrix; d3dxmatrixidentity(&matrix); d3dxmatrixtranslation(&matrix, 900, 0, 0); d3ddevice->settransform(d3dts_world, &matrix); d3dxmatrix out; d3dxvector3 eye(2,3,3); d3dxvector3 lat(0,0,0); d3dxvector3 up(0,1,0); d3dxmatrixlookatlh(&out, &eye, &lat, &up); d3ddevice->settransform(d3dts_view, &out); d3dxmatrix pro; d3dxmatrixperspectivefovlh(&pro, d3dxtoradian(45), // horizontal field of view (float)1440 / (float)900, // aspect ratio 1.0f, // near view-plane 100.0f); // far view-plane d3ddevice->settransform(d3dts_projection, &pro); d3ddevice->setstreamsource(0, v_buffer, 0, sizeof(vertex)); d3ddevice->drawprimitive(d3dpt_trianglelist, 0, 1); d3ddevice->endscene(); // ends 3d scene
so lets start basics:
the world matrix
defined here
d3dxmatrix matrix; d3dxmatrixidentity(&matrix); d3dxmatrixtranslation(&matrix, 900, 0, 0); d3ddevice->settransform(d3dts_world, &matrix);
this matrix defines transformation origin vertices have. in case, translating vertices 900 units positive x axis. i.e, if render position (0,0,0), @ (900,0,0).
this matrix used when need efficiently transform static mesh, don't have mess vertex buffer, single matrix, more efficent. matrix should @ in order translate, rotate , skew.
the view matrix
defined here:
d3dxmatrix out; d3dxvector3 eye(2,3,3); d3dxvector3 lat(0,0,0); d3dxvector3 up(0,1,0); d3dxmatrixlookatlh(&out, &eye, &lat, &up); d3ddevice->settransform(d3dts_view, &out);
this matrix defines transformation take place in order transform vertex world space view space. is, relative position of vertex camera. if want rotate or translate camera, should change.
the projection matrix
d3dxmatrix pro; d3dxmatrixperspectivefovlh(&pro, d3dxtoradian(45), // horizontal field of view (float)1440 / (float)900, // aspect ratio 1.0f, // near view-plane 100.0f); // far view-plane d3ddevice->settransform(d3dts_projection, &pro);
this matrix defines how vertices projected screen space. @ image:
objects closer camera near view plane or farther far view plane not shown. in image, green sphere not shown, since out of bounds.
the fov angle in practice, means "zoom" (not quite, can think that).
this matrix changed when need artifacts, zooming, pip , such.
in case, seems want change world matrix. start using d3dxmatrixtransformation function.
Comments
Post a Comment