Noob - Wheres the vertex colors?

Started by
5 comments, last by Buckeye 14 years, 8 months ago
Hi, just triing to render a simple red triangle, but it are being displaied in black..why ? Im posting the info that I think is enought, if I missing something, tell me.. // typedef struct{ float x, y, z; //vertex position DWORD color; //vertex color }GSPVERTEX_PC; #define D3DFVF_GSPVERTEX_PC ( D3DFVF_XYZ | D3DFVF_DIFFUSE ) //init custom vertex data:--------------------------------------------------- GSPVERTEX_PC tri_one[3] = { { 0.0f, 0.0f, 0.0f, D3DCOLOR_XRGB( 255, 0, 0 ) }, { 50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB( 255, 0, 0 ) }, { 100.0f, 0.0f, 0.0f, D3DCOLOR_XRGB( 255, 0, 0 ) } }; //... //create vertex buffer:----------XXXXXXXXXXXXXXXXXXXXXXX-------------------------- if( gsp_pD3Ddevice9->CreateVertexBuffer( (sizeof( GSPVERTEX_PC )*3), D3DUSAGE_SOFTWAREPROCESSING, D3DFVF_GSPVERTEX_PC, D3DPOOL_SYSTEMMEM, &gsp_vb_one, NULL ) != D3D_OK ) MessageBoxA( NULL, "Error creating vertex buffer gsp_vb_one!", " dx error", MB_OK|MB_ICONEXCLAMATION ); /* Note it creates a blank buffer( with the right vertex size and type ) */ //-------------------------------XXXXXXXXXXXXXXXXXXXXXXX-------------------------- //fill vertex buffer:------------XXXXXXXXXXXXXXXXXXXXXXX-------------------------- //step 1: lock the buffer : /* You can retrieve a pointer to vertex buffer memory by calling the Lock method */ void * p_vb; //pointer to the vertex buffer in memory if( gsp_vb_one->Lock( 0, 0, &p_vb, 0 ) != D3D_OK ) MessageBoxA( NULL, "Coldnt lock the gsp_vb_one vertex buffer interface!", " dx error", MB_OK|MB_ICONEXCLAMATION ); //step 2: copy custom vertex data to that buffer: memcpy( p_vb, tri_one, sizeof( tri_one ) ); //step 3: unlock the buffer : gsp_vb_one->Unlock(); //-------------------------------XXXXXXXXXXXXXXXXXXXXXXX-------------------------- //... if( gsp_pD3Ddevice9->BeginScene() == D3D_OK ){ //rendering promitives: gsp_pD3Ddevice9->SetStreamSource( 0, gsp_vb_one, 0, sizeof( GSPVERTEX_PC ) ); gsp_pD3Ddevice9->SetFVF( D3DFVF_GSPVERTEX_PC ); gsp_pD3Ddevice9->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 ); gsp_pD3Ddevice9->EndScene(); } gsp_pD3Ddevice9->Present( NULL, NULL, NULL, NULL );
Advertisement
1. You should be clearing the buffer to a background color before every BeginScene.

2. I don't know what state you set the device to, but make sure D3DRS_LIGHTING is set to FALSE.

By the way, there should be an example app in the SDK (under C++ Examples/Tutorials, I think) that does (almost) exactly what you're trying.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I am calling Clear, sorry, just didnt post it ( // ... )
gsp_pD3Ddevice9->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 200 ), 1.0, 0 );

I didnt set any state..but it should just render a simple triangle in the simplest way possible..Do I need this?

heres how Im creating the device:
ZeroMemory( & gsp_D3Dpp, sizeof(gsp_D3Dpp) );	gsp_D3Dpp.Windowed			= true;//false;	gsp_D3Dpp.BackBufferCount	= 2;							gsp_D3Dpp.BackBufferFormat	= D3DFMT_A8R8G8B8;			//D3DFMT_UNKNOWN = take current// D3DFMT_A8R8G8B8;	//gsp_D3Dpp.BackBufferWidth	= gspWND_W;					//gsp_D3Dpp.BackBufferHeight= gspWND_H;					//0 indicates backbuffer dimensions match windows size	gsp_D3Dpp.SwapEffect		= D3DSWAPEFFECT_DISCARD;																//to multisampletype.(Only discard supports miltisample )	gsp_D3Dpp.hDeviceWindow			= gsp_mainwnd;				//if set to NULL, the focus window is taken.	//gsp_D3Dpp.PresentationInterval= D3DPRESENT_INTERVAL_DEFAULT;	gsp_D3Dpp.EnableAutoDepthStencil = TRUE;	gsp_D3Dpp.AutoDepthStencilFormat = D3DFMT_D16;	//LPDIRECT3DDEVICE9 gsp_pD3Ddevice9;			if( ( gsp_pD3D9->CreateDevice(	D3DADAPTER_DEFAULT,			//primary adapter									D3DDEVTYPE_HAL,				//can be HAL or REF									gsp_mainwnd,				//the focus window( can be NULL if pp(function) is not NULL )	(hwvertexproc == true)? D3DCREATE_HARDWARE_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING,//controls the device behavior									&gsp_D3Dpp,	&gsp_pD3Ddevice9 )  ) != D3D_OK	 ){		MessageBox( NULL,					TEXT( "Error creating D3D device 9" ),					TEXT( " DX error" ),					MB_OK|MB_ICONEXCLAMATION );		CleanUp();		return E_FAIL;	}
For a simple display, you don't need the stencil buffer stuff or 2 back buffers. Once you ZeroMemory the present parameters, try setting just:

Windowed = TRUE;
SwapEffect = D3DSWAPEFFECT_DISCARD;
BackBufferFormat = D3DFMT_UNKNOWN;

Again, take a look at the example in the SDK. It's nearly identical to what you've posted so far.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Yes, you need set your lighting to false, the defult lighting is true, but you didn't set any light, so the triangle is black.
Checking my code with the one of the SDK, I saw that the only difference is that I was using D3DFVF_XYZ not D3DFVF_XYZRHW..Now its all ok, I guess if I want use just XYZ i have to apply the transformations myself before render it..?
Thanx for the help.
Sorry. The example does use transformed vertices. I had intended to refer only to the simpler device creation code.

For your code, you can simplify just the device creation code (which you posted), add
gsp_pD3Ddevice9->SetRenderState(D3DRS_LIGHTING,FALSE);
after the device is created, and keep the rest of your code as-is.

As mentioned by flyfish_bit, lighting is ON by default. You haven't set any lights so everything is dark. Don't worry about lights until you get the more basic triangle rendering stuff correct.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement