• 12
• 27
• 9
• 20
• 33

Starting DirectX 9.0

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

Recommended Posts

Alright I've started DirectX 9.0, and I want to just put some pixels on the screen. I've created the device and all that beginning stuff - and I end up with a full screen black screen. So now I'm going to create a Vertex Buffer, lock it, add the vertexes, unlock it. Then set the Stream Source and FVF. And so on.
if(FAILED(g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)* 4, 0, VertexFVF, D3DPOOL_MANAGED, &pD3DVB, NULL)))
return 0;

sVertex Verts[4] =
{
{rand()%800,	rand()%600,	0.0f, D3DCOLOR_RGBA(255,255,255,255)},
{rand()%800,	rand()%600,	0.0f, D3DCOLOR_RGBA(255,255,255,255)},
{rand()%800,	rand()%600,	0.0f, D3DCOLOR_RGBA(255,255,255,255)},
{rand()%800,	rand()%600,	0.0f, D3DCOLOR_RGBA(255,255,255,255)}
};

BYTE *ptr;
if(SUCCEEDED(pD3DVB->Lock(0,0, (void**)&ptr,0)))
{
memcpy(ptr, Verts, sizeof(Verts));

pD3DVB->Unlock();
}

if(FAILED(g_pD3DDevice->SetStreamSource(0,pD3DVB,0,sizeof(sVertex))))
return 0;

if(FAILED(g_pD3DDevice->SetFVF(VertexFVF)))
return 0;

if(FAILED(g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_RGBA(0,0,0,0),1.0f,0)))
return 0;

if(FAILED(g_pD3DDevice->BeginScene()))
return 0;

if(FAILED(g_pD3DDevice->DrawPrimitive(D3DPT_POINTLIST,0,2)))
return 0;

if(FAILED(g_pD3DDevice->EndScene()))
return 0;

if(FAILED(g_pD3DDevice->Present(NULL,NULL,NULL,NULL)))
return 0;


Now the question is, why doesn't it show anything? Am I missing something, the color of the vertices black(Do I have to set the material or that diffuse data in the vertex is enough?)? Thanks. There are no syntax errors, but the full code is here -
[source lang = "cpp"]
#define WIN32_LEAN_AND_MEAN

#include<windows.h>
#include<windowsx.h>
#include<d3d9.h>
#include<d3dx9.h>

#define WINCLASS "winclass"

#define KEYDOWN(VK_CODE) ((GetAsyncKeyState(VK_CODE)& 0x8000) ? 1 : 0)

typedef struct
{
FLOAT x,y,z;
D3DCOLOR diffuse;
} sVertex;

#define VertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)

IDirect3D9 *g_D3D;
IDirect3DDevice9 *g_pD3DDevice;
IDirect3DVertexBuffer9 *pD3DVB = NULL;
HWND windowhandle;
int windowclosed = 0;

int Game_Main();
int Game_Init();
int Game_ShutDown();
LRESULT CALLBACK WinProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}break;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}

int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;

winclass.cbClsExtra = 0;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.cbWndExtra = 0;
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.hInstance = hinstance;
winclass.lpfnWndProc = WinProc;
winclass.lpszClassName = WINCLASS;
winclass.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;

if(!RegisterClassEx(&winclass))
return 0;

if(!(hwnd = CreateWindowEx(NULL,
WINCLASS,
"Paintz",
WS_POPUP | WS_VISIBLE,
0,0,
800,600,
NULL,
NULL,
hinstance,
NULL)))
return 0;

windowhandle = hwnd;

Game_Init();

while(TRUE)
{

if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}

Game_Main();
}

Game_ShutDown();

return msg.lParam;
}

int Game_Main()
{
if(KEYDOWN(VK_ESCAPE))
{
SendMessage(windowhandle, WM_DESTROY, NULL, NULL);
windowclosed = 1;
}
if(windowclosed == 1)
return 0;

if(FAILED(g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)* 4, 0, VertexFVF, D3DPOOL_MANAGED, &pD3DVB, NULL)))
return 0;

sVertex Verts[4] =
{
{rand()%800,	rand()%600,	0.0f, D3DCOLOR_RGBA(255,255,255,255)},
{rand()%800,	rand()%600,	0.0f, D3DCOLOR_RGBA(255,255,255,255)},
{rand()%800,	rand()%600,	0.0f, D3DCOLOR_RGBA(255,255,255,255)},
{rand()%800,	rand()%600,	0.0f, D3DCOLOR_RGBA(255,255,255,255)}
};

BYTE *ptr;
if(SUCCEEDED(pD3DVB->Lock(0,0, (void**)&ptr,0)))
{
memcpy(ptr, Verts, sizeof(Verts));

pD3DVB->Unlock();
}

if(FAILED(g_pD3DDevice->SetStreamSource(0,pD3DVB,0,sizeof(sVertex))))
return 0;

if(FAILED(g_pD3DDevice->SetFVF(VertexFVF)))
return 0;

if(FAILED(g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_RGBA(0,0,0,0),1.0f,0)))
return 0;

if(FAILED(g_pD3DDevice->BeginScene()))
return 0;

if(FAILED(g_pD3DDevice->DrawPrimitive(D3DPT_POINTLIST,0,2)))
return 0;

if(FAILED(g_pD3DDevice->EndScene()))
return 0;

if(FAILED(g_pD3DDevice->Present(NULL,NULL,NULL,NULL)))
return 0;

return 0;
}
int Game_Init()
{
if((g_D3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
{
return 0;
}
D3DDISPLAYMODE d3ddm;
d3ddm.Width = 800;
d3ddm.Height = 600;
d3ddm.RefreshRate = 0;
d3ddm.Format = D3DFMT_R5G6B5;
D3DFORMAT d3df = D3DFMT_R5G6B5;
{
return 0;
}

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));

d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferWidth = 800;

return 0;

return 0;
}
int Game_ShutDown()
{

return 0;
}



Share on other sites
I didn't read the entire code, but it might be that DirectX turns on hardware lighting by default. Therefore, if you do not specify a material it will show up as black. You can solve this by simply disabling hardware lighting as follows:
g_pD3DDevice->SetRenderState ( D3DRS_LIGHTING, FALSE );

Again, I am not sure if this solves it, but it's certainly worth a try ;)

Share on other sites
Could be because your points are getting positioned outside of the field of view. I'm assuming by your choice of random values for the vert positions that you're wanting to specify screen space points. In this case you should change your vertex structure to something like this:

typedef struct {	FLOAT x,y,z,w;	D3DCOLOR diffuse;} sVertex;#define VertexFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

D3DFVF_XYZRHW notifies Direct3D that you are giving the verts in screen space and so not to perform any tranformations on them. You also have to specify the 'w' component in your vert positions which you'll want to set to 1.0f. Also if you're passing in verts in screen space like this you should set the D3DRS_CULLMODE render state to D3DCULL_NONE otherwise Direct3D has a habit of culling these polys.

Other than this I've noticed that you're creating a new vertex buffer each loop which you never release so you'll be leaking memory something rotten. You should create the buffer once during your initialisation step and simply re-use this one ... and remember to release it during shutdown.

Hope that sorts things out.

PS: I've never done point rendering in D3D before so I'm not sure if maybe there's additional things you need to set up to get that to work. You might want to swap to rendering triangles to keep things simple whilst you learn (plus the fact that all the tutorials in the SDK center around tri poly rendering).