problem with drawing primitive

Started by
5 comments, last by Armadon 18 years, 9 months ago
hi i have a problem with drawing a trianglelist and i have no idea what to correct to make it work. I'll drop the code for the method im using to setup directX and vertex buffer and the method to render. Please can anybody give me a hint in what direction to look? I have no problem with getting the window to show and i have tried setting render state to both clock wise and conter clockwise. It's probably some idiotic thing i have forgotten. ok here is the code:

// vertex structure
struct sVertex{

	float x, y, z; // The position for the vertex.
	D3DCOLOR color;     // The vertex color.

};

#define VertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)

// init method
// creating interface
	if((m_pd3d9 = Direct3DCreate9(D3D_SDK_VERSION)) == NULL){
		MessageBox(NULL, "Couldn't create interface!!", "ERROR initializing DirectX", MB_ICONERROR);
		return E_FAIL;
	}

	// checking for hardware vertex processing
	m_pd3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		&m_caps
		);

	// is hardware vertex processing possible
	int vp = 0;

	if (m_caps.DevCaps && D3DDEVCAPS_HWTRANSFORMANDLIGHT){

		vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;

	} else {

		vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	}

	// setting present parameters
	m_d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;			// The format of the depth/stencil buffer				
	m_d3dpp.BackBufferCount =	1;							// number of buffers
	m_d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;				// pixel format
	m_d3dpp.BackBufferHeight = height;						// height of back buffer
	m_d3dpp.BackBufferWidth = width;						// width of back buffer
	m_d3dpp.EnableAutoDepthStencil = true;					// Set to true to have Direct3D create and maintain the depth/stencil buffer automatically.
	m_d3dpp.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;	// the depth/stencil buffer will be discarded after back buffer has been presented
	m_d3dpp.FullScreen_RefreshRateInHz = 0;							// the refresh rate is set to default. If windowed set to 0 else D3DPRESENT_RATE_DEFAULT or like
	m_d3dpp.hDeviceWindow = hWnd;									// which window to present in
	m_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;					// no multi sampling
	m_d3dpp.MultiSampleQuality = 0;									// quality set to zero
	m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;	// when to present back buffer
	m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;						// when buffers are swapped front buffer is discarded
	m_d3dpp.Windowed = true;

	// creates the device interface
	if(FAILED(m_pd3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, vp, &m_d3dpp, &m_pd3dDevice))){
		MessageBox(NULL, "Couldn't create device!!", "ERROR initializing DirectX", MB_ICONERROR);
		return E_FAIL;
	}
	
	// creating vertex buffer
	HRESULT hr = m_pd3dDevice->CreateVertexBuffer(
		3*sizeof(sVertex),	// length is equal to the size of three structures of type sVertex  
		D3DUSAGE_WRITEONLY,	// usage - how to treat memory, 0 is default
		VertexFVF,			// which vertex buffer format
		D3DPOOL_DEFAULT,	// where in memory to proces vertices
		&m_pVertexbuffer,	// pointer to pointer to vertex buffer 
		NULL
		);

	if (FAILED(hr)) {
		MessageBox(NULL, "Couldn't create vertex buffer!!", "ERROR initializing DirectX", MB_ICONERROR);
		return E_FAIL;
	}
	
	//filling vertex buffer
	sVertex vertices[] = { 
		
		{ 15.0f,  5.0f, 0.5f,  D3DCOLOR_XRGB(0, 0, 255) }, // x, y, z,  color
		{ 25.0f, 25.0f, 0.5f,  D3DCOLOR_XRGB(0, 0, 255) },
		{  5.0f, 25.0f, 0.5f,  D3DCOLOR_XRGB(0, 0, 255) },

	};

	VOID* ptr;
	if(SUCCEEDED(m_pVertexbuffer->Lock(0, 0,(void **)&ptr, NULL))){
		memcpy(ptr, vertices, sizeof(vertices));
		m_pVertexbuffer->Unlock();
	}

	// position and aim the camera
	D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMATRIX V;
	D3DXMatrixLookAtLH(&V, &position, &target, &up);

	m_pd3dDevice->SetTransform(D3DTS_VIEW, &V);

	// set projection matrix
	D3DXMATRIX proj;

	D3DXMatrixPerspectiveFovLH(
		&proj,
		D3DX_PI * 0.5f, // 90 - degree
		(float)width / (float)height,
		1.0f,
		1000.0f);

	m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);


	D3DXMATRIX matworld;
	D3DXMatrixIdentity(&matworld);
	m_pd3dDevice->SetTransform(D3DTS_WORLD, &matworld);

	m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
	m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	
	

// render method
// clear screen with a certain color
m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(196,225,255), 1.0f, 0 );

if(SUCCEEDED(m_pd3dDevice->BeginScene())){
		
    m_pd3dDevice->SetStreamSource(0, m_pVertexbuffer, 0, sizeof(sVertex));
    m_pd3dDevice->SetFVF(VertexFVF);
    m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
    m_pd3dDevice->EndScene();
}
	
// present content of back buffer 
m_pd3dDevice->Present(NULL,NULL,NULL,NULL);




I hope somebody can help me. This has bothered me for a day now regards thallish EDIT: I have updated my code. added a diffuse color and diabled light. Still no go [Edited by - thallish on July 24, 2005 6:58:17 AM]
regards/thallishI don't care if I'm known, I'd rather people know me
Advertisement
Quote:Original post by thallish
ps: hmm aren't there normally tags to put code between?

Please look at the FAQ, Section
How do I put links/quotes/images/code/smileys in my posts?
to look up the tags to do this:
// vertex structurestruct sVertex{	float	x, y, z;	// 3D - coordinates};#define VertexFVF (D3DFVF_XYZ)// init method// creating interface	if((m_pd3d9 = Direct3DCreate9(D3D_SDK_VERSION)) == NULL){		MessageBox(NULL, "Couldn't create interface!!", "ERROR initializing DirectX", MB_ICONERROR);		return E_FAIL;	}	// checking for hardware vertex processing	m_pd3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,		D3DDEVTYPE_HAL,		&m_caps		);	// is hardware vertex processing possible	int vp = 0;	if (m_caps.DevCaps && D3DDEVCAPS_HWTRANSFORMANDLIGHT){		vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;	} else {		vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;	}	// setting present parameters	m_d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;					// The format of the depth/stencil buffer					m_d3dpp.BackBufferCount =	1;									// number of buffers	m_d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;						// pixel format	m_d3dpp.BackBufferHeight = height;						// height of back buffer	m_d3dpp.BackBufferWidth = width;							// width of back buffer	m_d3dpp.EnableAutoDepthStencil = true;							// Set to true to have Direct3D create and maintain the depth/stencil buffer automatically.	m_d3dpp.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;			// the depth/stencil buffer will be discarded after back buffer has been presented	m_d3dpp.FullScreen_RefreshRateInHz = 0;							// the refresh rate is set to default. If windowed set to 0 else D3DPRESENT_RATE_DEFAULT or like	m_d3dpp.hDeviceWindow = hWnd;									// which window to present in	m_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;					// no multi sampling	m_d3dpp.MultiSampleQuality = 0;									// quality set to zero	m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;	// when to present back buffer	m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;						// when buffers are swapped front buffer is discarded	m_d3dpp.Windowed = true;	// creates the device interface	if(FAILED(m_pd3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, vp, &m_d3dpp, &m_pd3dDevice))){		MessageBox(NULL, "Couldn't create device!!", "ERROR initializing DirectX", MB_ICONERROR);		return E_FAIL;	}		// creating vertex buffer	HRESULT hr = m_pd3dDevice->CreateVertexBuffer(		3*sizeof(sVertex),	// length is equal to the size of four structures of type sTileTextureVertex  		D3DUSAGE_WRITEONLY,	// usage - how to treat memory, 0 is default		VertexFVF,			// which vertex buffer format		D3DPOOL_DEFAULT,	// where in memory to proces vertices		&m_pVertexbuffer,	// pointer to pointer to vertex buffer 		NULL		);	if (FAILED(hr)) {		MessageBox(NULL, "Couldn't create vertex buffer!!", "ERROR initializing DirectX", MB_ICONERROR);		return E_FAIL;	}		//filling vertex buffer	sVertex vertices[3] = { 		{-1.0f, 0.0f, 2.0f},		{0.0f, 1.0f, 2.0f},		{1.0f, 0.0f, 2.0f}	};	BYTE* ptr;	if(SUCCEEDED(m_pVertexbuffer->Lock(0, 0,(void **)&ptr, NULL))){		memcpy(ptr, vertices, sizeof(vertices));		m_pVertexbuffer->Unlock();	}	// position and aim the camera	D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);	D3DXMATRIX V;	D3DXMatrixLookAtLH(&V, &position, &target, &up);		m_pd3dDevice->SetTransform(D3DTS_VIEW, &V);		// set projection matrix	D3DXMATRIX proj;		D3DXMatrixPerspectiveFovLH(		&proj,		D3DX_PI * 0.5f, // 90 - degree		(float)width / (float)height,		1.0f,		1000.0f);		m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);		// set the render states	m_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);	//m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);// render method// clear screen with a certain color	m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(196,225,255), 1.0f, 0L );	if(SUCCEEDED(m_pd3dDevice->BeginScene())){				m_pd3dDevice->SetStreamSource(0, m_pVertexbuffer, 0, sizeof(sVertex));		m_pd3dDevice->SetFVF(VertexFVF);		m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);		m_pd3dDevice->EndScene();	}		// present content of back buffer 	m_pd3dDevice->Present(NULL,NULL,NULL,NULL);	return S_OK;

Regarding your question, I am not sure but you should either
a) disable lighting and add a diffuse colour component to your vertex or
b) enable lighting, set a material and add a normal vector to your vertex

HTH,
Pat
Hi there thallish,
From your code I cannot see where you actually specify the color of each vertex.
It's normally specified in the following manner


[source = cpp]//for a single vertex.{-1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(0, 0, 255) //for a blue vertex.


you are also specifying that your vertices are already transformed. Each of your components are very small then and will not be displayed in the place you would normally want them.

rather play with
[source = cpp]#define VertexFVF (D3DFVF_XYZ|D3DFVF_DIFFUSE)


I hope this helps you out a bit.
Hey

I have been playing around with the suggestions that you have given me but unfortunately there is still no triangle in my window :-( this really sucks.

I have updated the code with my current. So feel free to take a glance at it ;-)

regards
/thallish
regards/thallishI don't care if I'm known, I'd rather people know me
Hi there Buddy,
I am including my source for a simple triangle, You can compare it what you have and please tell us what the problem was. So that you may help others.

[source = cpp]#include "CWindow.h"#include "CDirect3d.h"#include <d3d9.h>#include <d3dx9.h>#include <d3dx9math.h>#include <math.h>#include <windows.h>#pragma comment(lib, "d3dx9.lib")#pragma comment(lib, "d3d9.lib")#pragma comment(lib, "d3dx.lib")#pragma comment(lib, "DxErr9.lib")#pragma comment(lib, "dxguid.lib")#pragma comment(lib, "dinput8.lib")CWindow Window;CDirect3d Direct3d;IDirect3DVertexBuffer9* vb;HRESULT hResult;D3DXMATRIX matWorld;struct VERTEX{	float x, y, z;	DWORD dwColor;	enum		{			FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE		};};bool InitDirect3d(){	Direct3d.Create(&Window);	D3DXMatrixTranslation(&matWorld, 0.0f, 0.0f, 40.0f);	if(FAILED(hResult = Direct3d.GetDevice()->SetTransform(D3DTS_VIEW, &matWorld)))		MessageBox(NULL, "Failed - Set Transform", "ERROR", MB_OK);	if(FAILED(hResult = Direct3d.GetDevice()->SetRenderState(D3DRS_LIGHTING, false)))		MessageBox(NULL, "Set Render State(LIGHTING)", "ERROR", MB_OK);	if(FAILED(hResult = Direct3d.GetDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW)))		MessageBox(NULL, "Set Render State(CULLMODE)", "ERROR", MB_OK);	//Zenable false	Direct3d.GetDevice()->SetRenderState(D3DRS_LIGHTING, FALSE);	Direct3d.GetDevice()->SetRenderState(D3DRS_ZENABLE, FALSE);	VERTEX* pData;	VERTEX triangle[] = {{-1.0f, 0.0f, 0.0f, 0x0000ff},						 { 0.0f, 1.0f, 0.0f, 0x0000ff},						 { 1.0f, 0.0f, 0.0f, 0x0000ff}};	Direct3d.GetDevice()->CreateVertexBuffer(sizeof(VERTEX) * 3, D3DUSAGE_WRITEONLY, VERTEX::FVF, D3DPOOL_MANAGED, &vb, NULL);	vb->Lock(0, 0, (void**)&pData, 0);		memcpy(pData, triangle, sizeof(VERTEX) * 3);	vb->Unlock();	return true;}	void Update(){}void Render(){	Direct3d.BeginRender();	Direct3d.GetDevice()->SetStreamSource(0, vb, 0, sizeof(VERTEX));	Direct3d.GetDevice()->SetFVF(VERTEX::FVF);	Direct3d.GetDevice()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);	Direct3d.FinishRender();}void Release(){	Direct3d.Release();}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow){	Window.Create(hInstance, "::Xilath::", "window", 800, 600);	InitDirect3d();	while(1)	{		if(Window.CheckMessages() == -1)			break;		Update();		Render();	}	Direct3d.Release();	return 0;}
hi

I have found out was is causing the problem after several hours of commenting one line out after another ;-)

In my D3DPRESENT_PARAMETERS structure i set the Flags to D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL (got that idea from some book) and when i comment it out behold a triangle! though a damn ugly triangle.

I then comment out the AutoDepthStencilFormat and EnableAutoDepthStencil and then the triangle appears all smooth and good looking.

Can anybody give an explanation why the former changes works?

/regards
thallish

regards/thallishI don't care if I'm known, I'd rather people know me
To understand why it wasn't working you need to know what the depth and stencil buffers are.

Depth buffer
"A depth buffer, often called a z-buffer or a w-buffer, is a property of the device that stores depth information to be used by Microsoft Direct3D. When Direct3D renders a scene to a target surface, it can use the memory in an associated depth-buffer surface as a workspace to determine how the pixels of rasterized polygons occlude one another. Direct3D uses an off-screen Direct3D surface as the target to which final color values are written. The depth-buffer surface that is associated with the render-target surface is used to store depth information that tells Direct3D how deep each visible pixel is in the scene."

Stencil buffer
"Applications use the stencil buffer to mask pixels in an image. The mask controls whether the pixel is drawn or not. Some of the more common effects are shown below."

So the buffers might have been cleared and the information lost. I really don't know. Maybe someone can highlight why...

This topic is closed to new replies.

Advertisement