Jump to content
  • Advertisement
Sign in to follow this  
nosajghoul

DX9 vertex buffer trouble

This topic is 5398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive boiled it down to as simple as I can get it. Im trying to draw a triangle to the screen. All it does is make a grey screen. Heres the source code.
//at this point, the window is created and all set up/
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
	
D3DDISPLAYMODE d3ddm;
ghr = pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);

D3DPRESENT_PARAMETERS d3dpp; 
ZeroMemory(&d3dpp, sizeof(d3dpp));

d3dpp.Windowed = TRUE;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
d3dpp.hDeviceWindow = ghWnd;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

ghr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, ghWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice);

ghr = pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
ghr = pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
ghr = pD3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(255,255,255));	
ghr = pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	
ghr = pD3DDevice->SetFVF(D3D8T_CUSTOMVERTEX);
	//create vertex buffer
void *vertices = 0;
ghr = pD3DDevice->CreateVertexBuffer(sizeof(polyvertices), D3DUSAGE_WRITEONLY, D3D8T_CUSTOMVERTEX, D3DPOOL_MANAGED, &polyVB, NULL);      
ghr = polyVB->Lock(0, 0, &vertices, 0); 
memcpy(vertices, polyvertices, sizeof(polyvertices) );
ghr = polyVB->Unlock();
//enter the message loop

MSG msg;
while(1)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
ghr = pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(128,128,128), 1.0f, 0);

ghr = pD3DDevice->BeginScene();
ghr = pD3DDevice->SetStreamSource(0,polyVB, 0 ,sizeof(vertex));
ghr = pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0, 1);
ghr = pD3DDevice->EndScene();
ghr = pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
}

polyVB->Release();
pD3DDevice->Release();		  
pD3D->Release();  
	
UnregisterClass("WinApp", hInstance);
	
return 0;
}


What am I missing??

Share this post


Link to post
Share on other sites
Advertisement
Oh and I forgot, this is in there too at the top.


#include <d3d9.h>
#include <d3dx9.h>
#include <dxerr9.h>


#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)


HWND ghWnd = 0;
HINSTANCE ghInst = 0;
HRESULT ghr = 0;
HDC gdc = 0;

struct vertex
{
float x, y, z; // The transformed position for the vertex.
DWORD colour; // The vertex colour.
};


vertex polyvertices[] =
{
{-2.0f, 1.0f, 10.0f, 0xffff0000},
{-3.0f, -1.0f, 10.0f, 0xff00ff00},
{-1.0f, -1.0f, 10.0f, 0xff0000ff}
};

IDirect3D9 *pD3D;
IDirect3DDevice9 *pD3DDevice;
IDirect3DVertexBuffer9 *polyVB;



Share this post


Link to post
Share on other sites
Debug info : 0 errors 0 warnings

I tried setting a view and perspective earlier, same result, so I simplified further and that resulted in the above. Ill put em in again and see again to be sure.

-Jason

Share this post


Link to post
Share on other sites
Ok, it has a camera and perspective and a matrix to move it now. Heres the complete code :



#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include <stdio.h> //for sprintf

#include <d3d9.h>
#include <d3dx9.h>
#include <dxerr9.h>


#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)


HWND ghWnd = 0;
HINSTANCE ghInst = 0;
HRESULT ghr = 0;
HDC gdc = 0;

struct vertex
{
float x, y, z; // The transformed position for the vertex.
DWORD colour; // The vertex colour.
};


vertex polyvertices[] =
{
{-2.0f,1.0f,10.0f,0xffff0000},
{-3.0f,-1.0f,10.0f,0xff00ff00},
{-1.0f,-1.0f,10.0f,0xff0000ff}
};

IDirect3D9 *pD3D;
IDirect3DDevice9 *pD3DDevice;
IDirect3DVertexBuffer9 *polyVB;



LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
int result;
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
ValidateRect(ghWnd, NULL);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE: DestroyWindow(ghWnd);break;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
ghInst = hInstance;

WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_CLASSDC, //CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc; //where to pass messages to (defined below)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL, //LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW); //can be NULL
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //color of bkgrnd, can be NULL
wc.lpszMenuName = NULL;
wc.lpszClassName = "WinApp";//classname
wc.hIconSm = NULL;

RegisterClassEx(&wc);

ShowCursor(0);

ghWnd = CreateWindowEx(NULL, "WinApp", NULL, WS_OVERLAPPEDWINDOW, 0, 0, 800, 800, NULL, NULL, hInstance, NULL);

ShowWindow(ghWnd, SW_SHOWDEFAULT);
UpdateWindow(ghWnd);
//////////////////setup
//setup D3D
pD3D = Direct3DCreate9(D3D_SDK_VERSION);

D3DDISPLAYMODE d3ddm;
ghr = pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));

d3dpp.Windowed = TRUE;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = ghWnd;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

ghr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, ghWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pD3DDevice);

ghr = pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
ghr = pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
ghr = pD3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(255,255,255));
ghr = pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

ghr = pD3DDevice->SetFVF(D3D8T_CUSTOMVERTEX);
//create vertex buffer
void *vertices = 0;
ghr = pD3DDevice->CreateVertexBuffer(sizeof(polyvertices), D3DUSAGE_WRITEONLY, D3D8T_CUSTOMVERTEX, D3DPOOL_MANAGED,
&polyVB, NULL);
ghr = polyVB->Lock(0, sizeof(vertices), (void**)&vertices, 0);
memcpy(vertices, polyvertices, sizeof(polyvertices) );
ghr = polyVB->Unlock();
//setup camera

D3DXMATRIX proj, view;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI/4.0f,
6/ 8, //aspect ratio
0.0f,
1000.0f
);

// Create the camera matrix
D3DXMatrixLookAtLH(
&view,
&D3DXVECTOR3(0.0f, 10.0f, -10.0f), //eye
&D3DXVECTOR3(0.0f, 0.0f, 0.0f), //at
&D3DXVECTOR3(0.0f, 1.0f, 0.0f) //up
);

// Set the projection matrix
if( FAILED( pD3DDevice->SetTransform(D3DTS_PROJECTION, &proj ) ) )
MessageBox(0, "settransform 1", "error", MB_OK);

// Set the camera matrix
if( FAILED( pD3DDevice->SetTransform(D3DTS_VIEW, &view ) ) )
MessageBox(0, "settransform 2", "error", MB_OK);


MSG msg;
while(1)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
ghr = pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(128,128,128), 1.0f, 0);
ghr = pD3DDevice->BeginScene();
ghr = pD3DDevice->SetStreamSource(0,polyVB, 0 ,sizeof(vertex));

//move it
D3DXMATRIX world;
D3DXMatrixTranslation(&world, 0, 0, 20);
pD3DDevice->SetTransform(D3DTS_WORLD, &world);

ghr = pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0, 1);
ghr = pD3DDevice->EndScene();
ghr = pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
}

polyVB->Release();
pD3DDevice->Release();
pD3D->Release();

UnregisterClass("WinApp", hInstance);

return 0;
}







Could someone copy / paste the whole thing into theirs and see if it shows a poly? Im pullin hair tryin to finger this one out. Its as if all the pieces are there but it just dosent want to work. :/


Share this post


Link to post
Share on other sites
Try drawing transformed and lit triangle first ( i think it's D3DFVF_XYZRHW or something like that, i'm not shure - haven't sdk with me right now :( ) with screen coordinates. If it renders, than there is something wrong with world transform matrix. Anyway, something’s probably wrong with one of transform matrices...or maybe not?

Share this post


Link to post
Share on other sites
"Can you see anything when you turn off the depth buffer?"

You mean turning it off like this?

pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
//pD3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(255,255,255));
//pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

Ive played around with all the possible combinations, nothing, nada, zilch. (Still a grey screen)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!