Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to render several objects

Started by robleetin Feb 16, 2007 at 6:28 AM 3 replies 1.2k views
Original Post
robleetin
robleetin
Hi ! I'm trying to render several objects on the same scene. I will to add data to my vertex buffer and to draw it in my scene. But It's doesn't work. My code: #include #define SAFE_RELEASE(p) if (p != NULL) { p->Release(); } else return E_FAIL; struct CUSTOMVERTEX { FLOAT x, y, z, rhw; DWORD color; }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE) class DIRECTXMANAGEMENT { private: LPDIRECT3D9 m_D3DObject; LPDIRECT3DDEVICE9 m_D3DDevice; LPDIRECT3DVERTEXBUFFER9 m_VB; HWND m_MainWindowHandle; public: DIRECTXMANAGEMENT(); ~DIRECTXMANAGEMENT(); void InitWindow(); void GameLoop(); HRESULT LoadVertices(); HRESULT SetupVertexBuffer(); HRESULT InitD3D(); HRESULT RenderScene(); HRESULT DestroyD3DObjects(); }; LRESULT WINAPI MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); exit(0); return 0; } return DefWindowProc(hwnd, msg, wParam, lParam); } HRESULT DIRECTXMANAGEMENT::LoadVertices() { CUSTOMVERTEX vertices[] = { { 50.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,128,128), }, { 75.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,128,128), }, { 25.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,128,128), }, }; VOID* pVertices; if( FAILED( m_VB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, vertices, sizeof(vertices) ); m_VB->Unlock(); CUSTOMVERTEX vertices2[] = { { 150.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(128,0,128), }, { 175.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(128,0,128), }, { 125.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(128,0,128), }, }; pVertices = NULL; if( FAILED( m_VB->Lock( 0, sizeof(vertices2), (void**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, vertices2, sizeof(vertices2) ); m_VB->Unlock(); return S_OK; } HRESULT DIRECTXMANAGEMENT::SetupVertexBuffer() { if( FAILED( m_D3DDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_VB, NULL ) ) ) { return E_FAIL; } if (FAILED(LoadVertices())) return E_FAIL; return S_OK; } DIRECTXMANAGEMENT::DIRECTXMANAGEMENT() { InitWindow(); m_VB = NULL; m_D3DObject = NULL; m_D3DDevice = NULL; if(SUCCEEDED(InitD3D())) { if(SUCCEEDED(SetupVertexBuffer())) { GameLoop(); } } } void DIRECTXMANAGEMENT::GameLoop() { MSG msg; ZeroMemory(&msg, sizeof(msg)); while (msg.message != WM_QUIT) { ShowWindow(m_MainWindowHandle, SW_SHOWDEFAULT); UpdateWindow(m_MainWindowHandle); if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else RenderScene(); } } DIRECTXMANAGEMENT::~DIRECTXMANAGEMENT() { DestroyD3DObjects(); } void DIRECTXMANAGEMENT::InitWindow() { WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "Direct3DA", NULL}; RegisterClassEx(&wc); m_MainWindowHandle = CreateWindow("Direct3DA", "D3DApp", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL); } HRESULT DIRECTXMANAGEMENT::InitD3D() { if ((m_D3DObject = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) return E_FAIL; D3DDISPLAYMODE d3ddm; if(FAILED(m_D3DObject->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))) return FALSE; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = true; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; if (FAILED(m_D3DObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_MainWindowHandle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &m_D3DDevice))) return E_FAIL; return S_OK; } HRESULT DIRECTXMANAGEMENT::RenderScene() { if (m_D3DDevice == NULL) return E_FAIL; m_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 128), 1.0f, 0); if (SUCCEEDED(m_D3DDevice->BeginScene())) { m_D3DDevice->SetStreamSource(0, m_VB, 0, sizeof(CUSTOMVERTEX)); m_D3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX); m_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); m_D3DDevice->EndScene(); } m_D3DDevice->Present(NULL, NULL, NULL, NULL); return S_OK; } HRESULT DIRECTXMANAGEMENT::DestroyD3DObjects() { SAFE_RELEASE(m_D3DObject); SAFE_RELEASE(m_D3DDevice); SAFE_RELEASE(m_VB); return S_OK; } int APIENTRY WinMain (HINSTANCE, HINSTANCE, LPSTR, int) { DIRECTXMANAGEMENT dxm; dxm.GameLoop(); return 0; } Thank you!
mattdev
mattdev
It looks like you're trying to fill the same vertex buffer (m_VB) twice, and then are rendering with m_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);, which will only draw one triangle. Either fill a single vertex buffer with 6 vertices (to make up two triangles), and then render that with m_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); (note the 2, because we want to draw two primitives), or create two separate vertex buffers and use SetStreamSource and DrawPrimitive twice (once for each vertex buffer).

Hope that helps!
robleetin
robleetin
Quote:
Original post by mattdev
It looks like you're trying to fill the same vertex buffer (m_VB) twice, and then are rendering with m_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);, which will only draw one triangle. Either fill a single vertex buffer with 6 vertices (to make up two triangles), and then render that with m_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); (note the 2, because we want to draw two primitives), or create two separate vertex buffers and use SetStreamSource and DrawPrimitive twice (once for each vertex buffer).

Hope that helps!

Thank but I've tried your solution and it's doesn't work.

I've changed the following lines in my code :

if( FAILED( m_D3DDevice->CreateVertexBuffer(3*2*sizeof(CUSTOMVERTEX),		0, D3DFVF_CUSTOMVERTEX,	D3DPOOL_DEFAULT, &m_VB, NULL ) ) )

m_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
mattdev
mattdev
Not sure if you've already done this, but you'd also need to fill that single vertex buffer with both triangles, like so:

CUSTOMVERTEX vertices[] ={{ 50.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,128,128), },{ 75.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,128,128), },{ 25.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,128,128), },{ 150.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(128,0,128), },{ 175.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(128,0,128), },{ 125.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(128,0,128), },};VOID* pVertices;if( FAILED( m_VB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )return E_FAIL;memcpy( pVertices, vertices, sizeof(vertices) );m_VB->Unlock();return S_OK;
robleetin
robleetin
Quote:
Original post by mattdev
Not sure if you've already done this, but you'd also need to fill that single vertex buffer with both triangles, like so:

*** Source Snippet Removed ***

Yeah ! That's work great !

I've separated the vertices in two parts and I've added them in the Vertex Buffer.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.