DX problem.

Started by
12 comments, last by Endurion 19 years, 6 months ago
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);
	p_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&display);
	ZeroMemory(&params,sizeof(params));
	params.Windowed = true;
	params.SwapEffect = D3DSWAPEFFECT_DISCARD;
	params.BackBufferFormat = display.Format;
	p_d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&params,&p_device);	
}
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.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "Cory";
	wc.lpszMenuName = NULL;
	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,
-Goten
Advertisement
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.
Nope that wasn't it. I tried it :)
Thanks for trying though :)
-Goten
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....
I thought I didn't have to define them if I was using 2d space.
-Goten
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
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,
-Goten
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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
The default cullmode is counter-clockwise (CCW). RHW should be 1.0f.
http://www.codeflow.fi/

This topic is closed to new replies.

Advertisement