# DX problem.

## Recommended Posts

GotenRulezU    100
My triangle isn't showing up :-/ I did some message box based error checking and figured that it must be something wrong with my vertex buffer. Or something I did or didn't do to them. Anyone help plz :)
#include<windows.h>
#include<d3d9.h>
#pragma comment(lib,"d3d9.lib")
HWND hwnd;
MSG msg;
bool quit = false;
IDirect3D9 *p_d3d = NULL;
IDirect3DDevice9 *p_device = NULL;
IDirect3DVertexBuffer9 *p_vb = NULL;
struct MYVERTEX{
int x,y,z,rhw;
DWORD color;
};
#define D3DFVF_CUSTOM (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
MYVERTEX tri_vert[] = {
{0,0,1,1,D3DCOLOR_XRGB(100,0,0)},
{200,0,1,1,D3DCOLOR_XRGB(0,100,0)},
{100,300,1,1,D3DCOLOR_XRGB(0,0,100)}
};
void initD3D(HWND hWnd){
D3DDISPLAYMODE display;
D3DPRESENT_PARAMETERS params;
p_d3d = Direct3DCreate9(D3D_SDK_VERSION);
MessageBox(NULL,"D3DINIT OK", "D3D INIT",WS_POPUP);
ZeroMemory(&params,sizeof(params));
params.Windowed = true;
params.BackBufferFormat = display.Format;
}
void initVertices(){
VOID* vpVert;
p_device->CreateVertexBuffer(3*sizeof(MYVERTEX),0,D3DFVF_CUSTOM,D3DPOOL_DEFAULT,&p_vb,NULL);
p_vb->Lock(0,sizeof(tri_vert),(VOID**)&vpVert,0);
memcpy(vpVert,tri_vert,sizeof(tri_vert));
p_vb->Unlock();
}
void killD3D(){
p_d3d->Release();
p_d3d = NULL;
p_device->Release();
p_device = NULL;
p_vb->Release();
p_vb = NULL;
MessageBox(NULL,"D3DKILL OK", "D3D KILL",WS_POPUP);
}
void Render(){
p_device->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(200,0,0),1.0f,0);
p_device->BeginScene();
p_device->SetStreamSource(0,p_vb,sizeof(MYVERTEX),sizeof(tri_vert));
p_device->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
p_device->EndScene();
p_device->Present(NULL,NULL,NULL,NULL);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
switch(iMsg){
case WM_KEYDOWN:
quit = true;
PostQuitMessage(0);
break;
case WM_DESTROY:
quit = true;
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdLine,int iCmd){
WNDCLASS wc;
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "Cory";
wc.style = CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wc);
hwnd = CreateWindow("Cory","My Window",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
initD3D(hwnd);
initVertices();
ShowWindow(hwnd,iCmd);
UpdateWindow(hwnd);
while(quit != true){
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
Render();
}
}
killD3D();
UnregisterClass("Cory",hInstance);
return 0;
}


BTW any other pointers on my code are gladly accepted :) I would have used HRESULTS but I think it just makes the code harder to read. Thanks a lot,

##### Share on other sites
Well I don't see anything wrong except for one little thing, your vector data in your MYVERTEX structure is declared as int, this really should be a float. But I don't know if that would actually do anything, I never experimented, but to be safe I think you should try replacing the ints with floats.

##### Share on other sites
GotenRulezU    100
Nope that wasn't it. I tried it :)
Thanks for trying though :)

##### Share on other sites
Guest Anonymous Poster
I only glanced at your code so i might have missed it, but i didn't see the world/view/proj matrices set anywhere. if you did, make sure your triangle is defined so its acually in the view, not behind, not miles off in the distnace, etc....

##### Share on other sites
GotenRulezU    100
I thought I didn't have to define them if I was using 2d space.

##### Share on other sites
Guest Anonymous Poster
It doesn't know your using 2d space till you define them ;) right now it thinks your using some crazy random-d space. And its not really 2d space, its 3d where you just don't use the z component, so that it looks 2d

##### Share on other sites
GotenRulezU    100
This is where I got hung up on my last attempt at directx. I always get confussed where the transformations go which ones you need ect ect :-/ If anyone could show me how I'd be very happy :)
Thanks and goodnight,

##### Share on other sites
Endurion    5411
You're using RHW vertices, you don't need to set matrices for those.

A) Check ALL the direct3d functions for return values. Yes, all of them.

B) Crank up debug output in the DirectX control panel to the max. This will output a lot of warnings, but also errors. Make sure to check Break on Error. If you got an error the cursor will be set to the location of the problem.

Both of those should be able to exactly pinpoint where a call might fail.

##### Share on other sites
SpreeTree    396
What is the default backface culling mode? I always turn backface culling off when starting a new project. There is nothing worse than spendind ages wondering where your primitives are, when all you are doing is looking at it from the wrong direction!

Try turning off all render states you don't need. Things such as backface cullingl, lighting etc are all things that can throw you off when you start

Also I note when you lock the vertex buffer, you lock it by calling

p_vb->Lock(0,sizeof(tri_vert),(VOID**)&vpVert,0);

change this to:

p_vb->Lock(0,0,(VOID**)&vpVert,0);

By changing the second param to 0, you are now locking the entire vertex buffer (which from the look of the code, you want to do), rather then locking only a small portion of it

Spree

Edit: Also, shouldnt the RHW value default to 0.0f... Im not to sure, but worth a try changing that too

##### Share on other sites
Castaway    122
The default cullmode is counter-clockwise (CCW). RHW should be 1.0f.

##### Share on other sites
eriko    122
1.
struct MYVERTEX{
int x,y,z,rhw;
DWORD color;
};

Should be

struct MYVERTEX{
float x,y,z,rhw;
DWORD color;
};

2.
Before you draw you must set the FVF
p_device->SetFVF(D3DFVF_CUSTOM);

3.
p_device->SetStreamSource(0,p_vb,sizeof(MYVERTEX), sizeof(tri_vert));
Shoule be
p_device->SetStreamSource(0,p_vb,0, sizeof(MYVERTEX));

##### Share on other sites
GotenRulezU    100
Oh so the set fvf is to let it know which vertex format you are using. And when it is setting the streamsource it only streams 1 vertex at a time or what?
EDIT: Forgot to say thank you very very much for figuring out my problem. And my next task will be making it so every single function will return an HRESULT. Again thanks! It works just like I wanted it to.

##### Share on other sites
GotenRulezU    100
Another question what does RHW mean? I Just used it because it was like that in the tutorial I was learning from. I searched msdn and it said Reciprocal Homogeneous W. What the hell does that mean lol :) [smile]

##### Share on other sites
Endurion    5411
It basically means that your vertices are already specified in screen space, pixel coordinates. They will not be affected by matrices or lighting.

Pretty convinient for GUI or mouse cursor stuff.